Go Down

Topic: Speed sensor (Read 5792 times) previous topic - next topic

mowcius

Nice, I will have to try that some time...

Mowcius

Sweeduino

looks great, cant miss that even if i tie the laser to a lamp post. Now I'll just need to find solar panels. (Right after these messages... Implies that I'm taking a break now to go to sleep.)
Thanks a lot for all the help! If, (I'd like to say when), this idea becomes real I'll be sure to post it here.

/Ulf

Everett

You didn't say what type of reflex (reflector) you have across the lane from you.  If it is the usual safety reflector, then it should be forgiving of vibration.  Most safety reflectors use the "corner cube" principle. This sends light almost directly back to the source, regardless of the incoming direction of the light.   I just tried this with my pocket laser and the reflector on my bicycle.  The light does not come back in a pencil beam, it is spread out a few cm, but does come back very close to the laser pointer.  Maybe a better quality reflector would have a tighter return beam?

I think you could make this work with the laser aimed at the reflector across the lane, and something like a 5 cm by 5 cm solar cell directly behind the laser pointer, maybe with both inside a 10 cm dark plastic tube to block stray light.  The solar cell should mostly ignore (integrate) flickering in the return beam.  

Here is another idea, what if the time it takes a car to pass a single sensor is long compared to the time of the flickering?  You could program to ignore the quick on and off events and only record data when the blocking was over a certain minimum time interval.  This would reduce your timing accuracy (jitter) and would set an upper limit on the speed you could measure.

Sweeduino

I've just ordered the solar panels, they're 2,5cm square. So in a week or two they should arrive and by then i hope to have some code to test them with. Stay tuned...

/Ulf

mowcius

Cool, I look forward to hearing about it...

Mowcius

jezuz

Quote
I think you could make this work with the laser aimed at the reflector across the lane, and something like a 5 cm by 5 cm solar cell directly behind the laser pointer, maybe with both inside a 10 cm dark plastic tube to block stray light.  The solar cell should mostly ignore (integrate) flickering in the return beam.  


Don't go with reflectance... it's best you have the "receiving" solar panel on the other side of the street and then just send the data by wireless means...

mowcius

Quote
Don't go with reflectance... it's best you have the "receiving" solar panel on the other side of the street and then just send the data by wireless means...

That still only works if you have access to somewhere at ground level on both sides of the street. Interesting anyway. Unfortunately I live on a dead end road with no traffic or I would try somethig similar!

Mowcius

Sweeduino

OK, christmas, free time, hardware and arduino at hand. Got everything hooked up and my code works after some modifications. I realized the hard way that when iside a interrupt routine millis() isn't ticking along. Well, modified and working in my test setup. Here is the code. One problem to solve is that if something breaks one laser beam and not the other the code assumes a car is moving inside the speedgate and calculates a very low speed when the second beem eventually is broken. Have tried several ways to reset this but no good idea works for me.

Code: [Select]
int upSpeed, downSpeed; //Speed calculated in each direction
int upCarCounter=0, downCarCounter=0, totalCars=0; //Number of cars in each direction and total
int upMaxSpeed=0, downMaxSpeed=0; //Max speed for both directions
int upMinSpeed=999, downMinSpeed=999; //Minimum speed for both directions
long upAverageSpeed=0, downAverageSpeed=0, totalAverageSpeed=0; //Sum of speeds to calculate average from
int upCarCounterHour=0, downCarCounterHour=0, totalCarsHour=0; //Number of cars in each direction and total
int upMaxSpeedHour=0, downMaxSpeedHour=0; //Max speed for both directions
int upMinSpeedHour=999, downMinSpeedHour=999; //Minimum speed for both directions
long upAverageSpeedHour=0, downAverageSpeedHour=0, totalAverageSpeedHour=0; //Sum of speeds to calculate average from
long upGateTime=0, downGateTime=0;  //When interrupt happened, car passing time gate
long lastUpGateTime=0, lastDownGateTime=0;  //Previous time a car passed
long hourTimer=0;  //Timer to control hourly output
int hourCounter=0;  //Count hours
int upTime, downTime; //Time for car to pass gate in each direction
int distance=10000; // Distance betveen upwards and downwards gate in mm
int upGate=2;  //Pin for up gate
int downGate=3;  //Pin for down gate

void setup()
{
 pinMode(upGate, INPUT);  //Setup pins for input
 pinMode(downGate, INPUT);
 Serial.begin(9600);
 attachInterrupt(0, upwardsTimer, FALLING);  //Set up interrupts to call when change happens, car is passing gate
 attachInterrupt(1, downwardsTimer, FALLING);
}


void loop()
{
 if (millis()-hourTimer>10000) //Check if 60 minutes has passed
 {
   hourTimer=millis(); //Reset hour timer for new measurement period
   hourlyPrintout();  //Print the total and this hours stats
   resetHourlyStats();  //Reset hourly stats for new period
   hourCounter++;  //Increase hour counter
 }


 if (upGateTime!=lastUpGateTime && downGateTime!=lastDownGateTime)  // Both gates have changed so a car has passed
 {
   if (upGateTime<downGateTime)  //Did it pass downwards or upwards?
     downwardsRun();
   else
     upwardsRun();

   lastUpGateTime=upGateTime;  //Reset timers
   lastDownGateTime=downGateTime;
 }

}


void upwardsRun()  //Prints stats for car going upwards
{


 upTime=upGateTime-downGateTime;  //How long for the car to pass the gate
 upSpeed=distance/upTime*36/10;  //Calculate speed in Km/h

 if(upSpeed<upMinSpeed)  //update fastest and slowest speed
     upMinSpeed=upSpeed;
 if(upSpeed>upMaxSpeed)
   upMaxSpeed=upSpeed;

 if(upSpeed<upMinSpeedHour)  //update fastest and slowest speed this hour
     upMinSpeedHour=upSpeed;
 if(upSpeed>upMaxSpeedHour)
   upMaxSpeedHour=upSpeed;

 upAverageSpeed=upAverageSpeed+upSpeed;    //Update average speeds and car counters
 totalAverageSpeed=totalAverageSpeed+upSpeed;
 upCarCounter++;
 totalCars++;
 upAverageSpeedHour=upAverageSpeedHour+upSpeed;    //Update average speeds and car counters this hour
 totalAverageSpeedHour=totalAverageSpeedHour+upSpeed;
 upCarCounterHour++;
 totalCarsHour++;


 Serial.print("Car number ");
 Serial.print(totalCars);
 Serial.print(" upwards speed: ");  //Print the speed
 Serial.println(upSpeed);
 Serial.println("");

}


void downwardsRun()  //Prints stats for car going downwards
{


 downTime=downGateTime-upGateTime;  //How long for the car to pass the gate
 downSpeed=distance/downTime*36/10;  //Calculate speed in Km/h

 if(downSpeed<downMinSpeed)  //update fastest and slowest speed
     downMinSpeed=downSpeed;
 if(downSpeed>downMaxSpeed)
   downMaxSpeed=downSpeed;

 if(downSpeed<downMinSpeedHour)  //update fastest and slowest speed this hour
     downMinSpeedHour=downSpeed;
 if(downSpeed>downMaxSpeedHour)
   downMaxSpeedHour=downSpeed;

 downAverageSpeed=downAverageSpeed+downSpeed;    //Update average speeds and car counters
 totalAverageSpeed=totalAverageSpeed+downSpeed;
 downCarCounter++;
 totalCars++;

 downAverageSpeedHour=downAverageSpeedHour+downSpeed;    //Update average speeds and car counters this hour
 totalAverageSpeedHour=totalAverageSpeedHour+downSpeed;
 downCarCounterHour++;
 totalCarsHour++;

 Serial.print("Car number ");
 Serial.print(totalCars);
 Serial.print(" downwards speed: ");  //Print the speed
 Serial.println(downSpeed);
 Serial.println("");

}


void hourlyPrintout()  //Prints hourly stats
{
 Serial.println("----------------------------------------------------------");  //Print all info about the cars
 Serial.println("");
 Serial.print("          Hour interval: ");
 Serial.print(hourCounter);
 Serial.print("-");
 Serial.println(hourCounter+1);

 Serial.print("      Total cars passed: ");
 Serial.print(totalCars);
 Serial.print("\t      Total cars passed this hour: ");
 Serial.println(totalCarsHour);
 Serial.print("    Total Average speed: ");
 Serial.print(totalAverageSpeed/totalCars);
 Serial.print("\t    Total Average speed this hour: ");
 Serial.println(totalAverageSpeedHour/totalCarsHour);
 Serial.println("");

 Serial.print("    Cars passed upwards: ");
 Serial.print(upCarCounter);
 Serial.print("\t    Cars passed upwards this hour: ");
 Serial.println(upCarCounterHour);
 Serial.print("  Upwards Average speed: ");
 Serial.print(upAverageSpeed/upCarCounter);
 Serial.print("\t  Upwards Average speed this hour: ");
 Serial.println(upAverageSpeedHour/upCarCounterHour);
 Serial.print("      Upwards Max speed: ");
 Serial.print(upMaxSpeed);
 Serial.print("\t      Upwards Max speed this hour: ");
 Serial.println(upMaxSpeedHour);
 Serial.print("      Upwards Min speed: ");
 Serial.print(upMinSpeed);
 Serial.print("\t      Upwards Min speed this hour: ");
 Serial.println(upMinSpeedHour);
 Serial.println("");

 Serial.print("  Cars passed downwards: ");
 Serial.print(downCarCounter);
 Serial.print("\t  Cars passed downwards this hour: ");
 Serial.println(downCarCounterHour);
 Serial.print("Downwards Average speed: ");
 Serial.print(downAverageSpeed/downCarCounter);
 Serial.print("\tDownwards Average speed this hour: ");
 Serial.println(downAverageSpeedHour/downCarCounterHour);
 Serial.print("    Downwards Max speed: ");
 Serial.print(downMaxSpeed);
 Serial.print("\t    Downwards Max speed this hour: ");
 Serial.println(downMaxSpeedHour);
 Serial.print("    Downwards Min speed: ");
 Serial.print(downMinSpeed);
 Serial.print("\t    Downwards Min speed this hour: ");
 Serial.println(downMinSpeedHour);
 Serial.println("");
 Serial.println("");


}


void resetHourlyStats()
{
 totalCarsHour=0;  //Reset hourly stats for new measurement period
 upCarCounterHour=0;
 downCarCounterHour=0;
 upMaxSpeedHour=0;
 downMaxSpeedHour=0;
 upMinSpeedHour=999;
 downMinSpeedHour=999;
 upAverageSpeedHour=0;
 downAverageSpeedHour=0;
 totalAverageSpeedHour=0;
}


void upwardsTimer()  //Car has passed upper gate
{
 upGateTime=millis();  //Time when car passed gate
 /*
 Serial.print("Upper gate passed at: ");
  Serial.println(upGateTime);
  Serial.println("");
  */
}


void downwardsTimer()  //Car has passed lower gate
{
 downGateTime=millis();  //Time when car passed gate
 /*
 Serial.print("Lower gate passed at: ");
  Serial.println(downGateTime);
  Serial.println("");
  */
}




/Ulf

mowcius

Looking good!

I can't really think of a good way to stop the errors you are having though.

Becuase there are two lanes of traffic it is not going to be easy. Have you tried it on the road yet then or just as a protype test?

Mowcius

Sweeduino

Still on the test bench. Have tested laser pointer module and solar cell about 6 meter apart so this should work. Now I need lots of cable...
I still want to use a reflector so I can have all electronics on one side of the road and just a reflex, mirror or something on the other side.

Now one laser pointer is more of a laser lamp. The point is about 5x5 cm at 6m distance. May need to change it.

/Ulf

mowcius

If you cannot alter the laser focus then I have a lot of laser modules and I would be happy to sell one or two to you (PM me if you want). They are good to about 600m and have focusing on them (with flying leads, apparently 2.3-3v but I have run them off 9v and no issues for a short time - probably should have a resistor off 5v or 3.3v to get them to 3v or lower)

Go Up