Author Topic: Arduino Scale Speed Calculator  (Read 2571 times)

0 Members and 1 Guest are viewing this topic.

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Arduino Scale Speed Calculator
« on: November 08, 2014, 03:56:05 PM »
0
I figured I'd pull this out from the Detection Sensors/Methods thread since the focus is now what happens once the sensor is tripped.

I set up a test loop of unitrack and have a stripped down program to calculate speed. 


Unfortunately, I have two problems.  First, there's a phantom in the program.  The first 10 calculations or so calculate progressively incorrect speeds (20mph, then 25, then 30, 35, 40...).   Eventually, it settles down and reads speed within about a 10smph range.  On the bright side, that range is thanks to some ugly voltage drop in the test loop. 

Originally, I stripped down the crossing signal program to get the speed calc.  However since I'm getting some weirdness, I'm going to start a new sketch from scratch. 
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

sizemore

  • The Pitt
  • Crew
  • *
  • Posts: 2683
  • Respect: +79
Re: Arduino Scale Speed Calculator
« Reply #1 on: November 09, 2014, 10:04:03 PM »
0
Post your code! And your setup! It looks like you're using photoresistors in a voltage divider setup?

Are you testing with the freight cars? Depending on your sketch that might throw the values off.

The S.
« Last Edit: November 09, 2014, 10:09:47 PM by sizemore »

Thompson Sub: Instagram | Youtube | Website

peteski

  • Crew
  • *
  • Posts: 32958
  • Gender: Male
  • Honorary Resident Curmudgeon
  • Respect: +5343
    • Coming (not so) soon...
Re: Arduino Scale Speed Calculator
« Reply #2 on: November 09, 2014, 10:46:52 PM »
0
I would put the sensors much closer together (and modify the calculation accordingly).  I would say about 1-2 inches between the senors.
. . . 42 . . .

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Scale Speed Calculator
« Reply #3 on: November 09, 2014, 11:41:03 PM »
0
I actually have 8 photocells installed evenly around the loop, but only working with two until the script gets going.  I chose two that are farther apart while writing/testing, which should provide much higher precision in the calculation. 


Yep, photocell voltage dividers.  The idea behind the new sketch I'm writing is to 1st only check if sensor 1 is triggered.  When it is, log the millis, then we check for sensor 2.  Once sensor two is triggered, record millis and calculate speed.  I'm hoping that this is efficient, by not reading sensor 2 unless sensor 1 is already triggered and millis logged we should get the most accurate timing for the reading.  Then we ignore sensor one while we read sensor 2.  This does mean if sensor 1 is cleared before sensor 2 is read, we reset.  When fully operational, the sensors will be about the width of a locomotive apart, so this won't be an issue, but for now we need to have a train longer than the distance between the sensors.

The readings I get when the sensor is vacant is 700, when occupied, between 150-450 (150 under bogies, ~450 mid car).  This is soft top lighting.  Eventually, I'll add a timer to average the reading over a few seconds (after the initial trigger) which will make the "continue warning" and end of train detection more reliable.
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

peteski

  • Crew
  • *
  • Posts: 32958
  • Gender: Male
  • Honorary Resident Curmudgeon
  • Respect: +5343
    • Coming (not so) soon...
Re: Arduino Scale Speed Calculator
« Reply #4 on: November 10, 2014, 02:36:45 AM »
0
I actually have 8 photocells installed evenly around the loop, but only working with two until the script gets going.  I chose two that are farther apart while writing/testing, which should provide much higher precision in the calculation. 


I see.  BTW, what function does the D-O-G sensor perform?  :)
. . . 42 . . .

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Scale Speed Calculator
« Reply #5 on: November 10, 2014, 05:19:59 PM »
0
Dog comes pre-loaded with train detection and bark warning.  Unfortunately, level of precision is all over the board.  Also tends to side-track when the ground smells interesting. 
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

sizemore

  • The Pitt
  • Crew
  • *
  • Posts: 2683
  • Respect: +79
Re: Arduino Scale Speed Calculator
« Reply #6 on: November 11, 2014, 09:52:36 AM »
0
I actually have 8 photocells installed evenly around the loop, but only working with two until the script gets going.  I chose two that are farther apart while writing/testing, which should provide much higher precision in the calculation. 


Yep, photocell voltage dividers.  The idea behind the new sketch I'm writing is to 1st only check if sensor 1 is triggered.  When it is, log the millis, then we check for sensor 2.  Once sensor two is triggered, record millis and calculate speed.  I'm hoping that this is efficient, by not reading sensor 2 unless sensor 1 is already triggered and millis logged we should get the most accurate timing for the reading.  Then we ignore sensor one while we read sensor 2.  This does mean if sensor 1 is cleared before sensor 2 is read, we reset.  When fully operational, the sensors will be about the width of a locomotive apart, so this won't be an issue, but for now we need to have a train longer than the distance between the sensors.

The readings I get when the sensor is vacant is 700, when occupied, between 150-450 (150 under bogies, ~450 mid car).  This is soft top lighting.  Eventually, I'll add a timer to average the reading over a few seconds (after the initial trigger) which will make the "continue warning" and end of train detection more reliable.

I kinda get, but I need to see the code. Which side is your photocell on in the voltage divider?

The S.

Thompson Sub: Instagram | Youtube | Website

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Scale Speed Calculator
« Reply #7 on: November 15, 2014, 12:19:09 AM »
0
Welp, that was a waste of a week.  Finally figured out the issue.

Quote
if (E1Occupied = true){

Programmers will know how dumb I am.   :facepalm:


For non-programmers, this if statement checks to see if variable "E1Occupied" is equal to "true"... except the syntax is wrong. 
E1Occupied = true is a command to SET E1Occupied to true.
it needs to be E1Occupied == true to CHECK if it is already true. 
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

peteski

  • Crew
  • *
  • Posts: 32958
  • Gender: Male
  • Honorary Resident Curmudgeon
  • Respect: +5343
    • Coming (not so) soon...
Re: Arduino Scale Speed Calculator
« Reply #8 on: November 15, 2014, 12:35:21 AM »
0
Well, the double equal sign is not intuitive.  Back in the BASIC programming days a single equal would have been correct in either case.  Like in:

LET E1Occupied = "true"
IF E1Occupied = "true" THEN ......

At least that was the syntax of the BASIC I used to mess around with  (on a Sinclair ZX81 computer).
. . . 42 . . .

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Scale Speed Calculator
« Reply #9 on: November 15, 2014, 12:35:27 AM »
0

Here's the code.  Nothing fancy, but it works!  Now, to test how accurate I can get it working with closely paired sensors...   

Quote
//Sensor Pins
int senseE1 = 0;
int senseE2 = 1;
int sensorDistance = 63; // distance in Centimeters.

//Sensor Readings
int readE1, readE2;

//Timers
long timeE1;
long timeE2;
long currentTime;
long printTimer;

long printInterval = 1000;

//Status
boolean E1Occupied = false;
boolean E2Occupied = false;
int Speed = 0;

void setup(){
 
 Serial.begin(9600);
 
}


void loop(){
 

 
readE1 = (analogRead(senseE1));
currentTime = millis();



// If E1 IS triggered
if (readE1 < 600){
 
  // If E1 IS already occupied, read E2
  if (E1Occupied == true){
   
   
    readE2 = (analogRead(senseE2));
   
    // If E2 IS triggered
    if(readE2 < 600){
     
      // If E2 IS already occupied, calculate speed
      if (E2Occupied == true){
       
        // Calculate speed between E1 and E2
       if(Speed == 0){
               
        Serial.println("Calculate Speed.");
        Speed = ((sensorDistance *160/100*.00062137)/((timeE2 - timeE1)/1000.00000000*.00027777));
        Serial.print("Speed = ");
        Serial.println(Speed);
       
       }
       
      }
     
        // IF E2 is NOT already occupied, set time of trigger and mark as occupied
        else{
          timeE2 = millis();
          E2Occupied = true;
           
          Serial.println("E2 is Occupied");
          Serial.print("timeE2 = ");
          Serial.println(timeE2);
       }
   
      }
     
  }
    else{
    // If E1 is NOT already occupied, set time of trigger and mark as occupied
     timeE1 = millis();
     E1Occupied = true;
     
     Serial.println("E1 is Occupied");
     Serial.print("timeE1 = ");
     Serial.println(timeE1);
     
   }
   
  }
 
  // If E1 is NOT triggered
  else{
   
    // Reset E1 and E2 to Vacant
    timeE1 = 0;
    E1Occupied = false;
    timeE2 = 0;
    readE2 = 0;
    E2Occupied = false;
    Speed = 0;
   
  }

}


« Last Edit: November 15, 2014, 12:38:08 AM by Mark W »
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

wcfn100

  • Crew
  • *
  • Posts: 8841
  • Respect: +1221
    • Chicago Great Western Modeler
Re: Arduino Scale Speed Calculator
« Reply #10 on: November 15, 2014, 03:29:08 AM »
0
Well, the double equal sign is not intuitive.

But it does make sense to differentiate comparative and assignment statements.

I just got my kit today. Hoping to check it out this weekend.


Jason

sizemore

  • The Pitt
  • Crew
  • *
  • Posts: 2683
  • Respect: +79
Re: Arduino Scale Speed Calculator
« Reply #11 on: November 15, 2014, 09:43:39 PM »
0
Welp, that was a waste of a week.  Finally figured out the issue.

Programmers will know how dumb I am.   :facepalm:


For non-programmers, this if statement checks to see if variable "E1Occupied" is equal to "true"... except the syntax is wrong. 
E1Occupied = true is a command to SET E1Occupied to true.
it needs to be E1Occupied == true to CHECK if it is already true.

Its the simple things like this one that bite me in the toukis.

The S.

Thompson Sub: Instagram | Youtube | Website

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Scale Speed Calculator
« Reply #12 on: November 15, 2014, 11:34:52 PM »
0
Check it out!  Hooked up the warning lights and buzzer and added a sensor to detect when the crossing is all clear.

Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.

peteski

  • Crew
  • *
  • Posts: 32958
  • Gender: Male
  • Honorary Resident Curmudgeon
  • Respect: +5343
    • Coming (not so) soon...
Re: Arduino Scale Speed Calculator
« Reply #13 on: November 15, 2014, 11:42:53 PM »
0
Looks good!  I like to see projects like this taking shape.  How will this circuit behave with just the locomotive running around?
. . . 42 . . .

Mark W

  • Crew
  • *
  • Posts: 1988
  • Respect: +2125
    • Free-moNebraska
Re: Arduino Scale Speed Calculator
« Reply #14 on: November 16, 2014, 12:08:46 AM »
0
Won't work with just a loco right now, sensor 1 and sensor 2 are too far apart.  Loco + 1 car will work though.  I currently have a 'warn for 10 seconds' thing built in to allow the train to reach the exit sensor.  Then after 10 seconds the warning continues until the exit sensor is clear.  Gotta figure out how to handle this part better though.

When installed on the module, sensor 1 and sensor 2 will be just closer than the length of a short loco (GP-38 or F7 or something). 
Contact me about custom model building.
Learn more about Free-moNebraska.
Learn more about HOn3-mo.