Skip to content

Commit

Permalink
rewrote velocity calculation to take into account the degree of the t…
Browse files Browse the repository at this point in the history
…rack, as well as take into account rolling force. Also fixed issue 1,2, and 3
  • Loading branch information
zoo1 committed Apr 19, 2015
1 parent 4bae3fa commit 1082d50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
6 changes: 5 additions & 1 deletion TrainModel/src/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public static Block createblock(double gradient, String beaconData, int authorit
}
public static Block createblock(double gradient, String beaconData, int authority, double speedlimit, int length, boolean tunnel, boolean yard)
{
//TODO add gradient and beaconData checking

if(gradient < -2 || gradient > 2) //Invalid gradient
return null;
if(beaconData.length()>20) //beaconData too large
return null;
if(authority<0) //Invalid authority
return null;
if(speedlimit<=0)//Invalid speedlimit
Expand Down
46 changes: 38 additions & 8 deletions TrainModel/src/Train.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
* To change this license header, choose License Headers in Project Properties.
Expand All @@ -23,6 +25,8 @@ public class Train extends Thread {
private final int MAXIMUMPASSENGERS = 222;
private final double SERVICEBRAKE = 1.2; //m/s²
private final double EMERGENCYBRAKE = 2.73; //m/s²
private final double TRAINFRICTION = .001;
private final double GRAVITY = 9.81; //m/s²
private volatile double totalmass;
private volatile double timemodifier = 1;
private int brakestatus = 0;
Expand Down Expand Up @@ -155,11 +159,14 @@ private void opendoors() {
}

public void UpdatePower(double power) {
if (power > 0 && power<=120000) { //Max Power 120 kilowatss
if(power < 0)
this.power = 0;
else if(power > 120000) //Max Power 120 kilowatts
this.power = 120000;
else
this.power = power;
}
}

public void run() {
boolean running=true;
while (running) {
Expand All @@ -182,18 +189,35 @@ public void run() {
finalvelocity = 0;
finalaccel = 0;
} else if (brakestatus == 0) {
double blockradient = Math.toRadians(Math.toDegrees(Math.atan(currentBlock.getGradient()/100)));
double rollingforce = TRAINFRICTION * (Math.cos(blockradient) * GRAVITY * totalmass);
double gradeforce = Math.sin(blockradient) * totalmass * GRAVITY;
//EngForce = power / v;
double engforce;
double engforce = 0;
if (power == 0) {
engforce = 0;
} else if (finalvelocity == 0) {
engforce = maxacceleration * totalmass;
} else {
engforce = power / finalvelocity;
}

finalaccel = engforce / totalmass;
if (finalvelocity > maxspeed) { //cap speed at speed limit
double finalforce=0;
//3 different force summations
if(Math.abs(engforce-gradeforce) < rollingforce) //Rolling Force should not be the driving force
{
if(velocity > 0)
{
finalforce = engforce-gradeforce-rollingforce;
velocity = 0;
}
else
finalforce=0;
}
else
finalforce=engforce-gradeforce-rollingforce;

finalaccel = finalforce / totalmass;
if (finalvelocity/timemodifier > maxspeed) { //cap speed at speed limit
finalvelocity = maxspeed;
finalaccel = 0; //and stop acceleration (for position calc in next step)
}
Expand All @@ -217,11 +241,17 @@ public void run() {
{
MessageLibrary.sendMessage("localhost", TRAINCONTROLLER, "Train Model : " + UID + " : set, Position=" + position);
}
if (velocity != finalvelocity&&running) {
if (velocity != finalvelocity&&running)
{
MessageLibrary.sendMessage("localhost", TRAINCONTROLLER, "Train Model : " + UID + " : set, Actual Speed=" + finalvelocity);
}
velocity = finalvelocity;
acceleration = finalaccel;
try {
Thread.sleep(1); //millisecond time tic
} catch (InterruptedException ex) {
Logger.getLogger(Train.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Expand Down

0 comments on commit 1082d50

Please sign in to comment.