Timing

djh82uk

Member
Hey guys

I want to make something that can measure the speed of model trains. Now if I have 2 sensors a fixed distance apart (like 30cm) then I just need to time how long it takes for the 2nd sensor to be tripped after the first one, then speed = distance / time.

Any ideas? im guessing its gonna have to be some sort routine that increments, then measure how big the increment was to get the time?

Sorry for asking loads of questions all the time

Regards

DJH
 

djh82uk

Member
but the sensor is only going to be triggered once. I was going to have the train go over 2 inputs one after another, is'nt pulsin just going the measure how long the input is on for? I need to measure the time between the 2 inputs being triggered.

Ive come up with this, it probs has loads of errors, line 2 comes up with a syntax error, anyone know why?

MainLoop:
IF input1 = 1 THEN ' If input activated ...
timer1 = 0 ' ... Start timeout timer (0mS)
END IF

loop2:


pause 10
timer1 = timer1+10 ' ... Update timeout timer
If input2 = 1 then calc
goto loop2
if timer1 = 10000 then mainloop
END IF
:

calc:

time1 = timer1 / 10
dist = 30 / 100
speed = dist / time1
b2 = speed
debug b2
:
GOTO MainLoop ' Forever
 

djh82uk

Member
oops, forgot the code :S

symbol timer1 = b0
symbol time1 = b1
symbol dist = b2
symbol speed = b3

MainLoop:


IF pin1 = 1 THEN loop2 ' If input activated ...

goto mainloop


loop2:

b0 = 0
pause 10
b0 = b0+10 ' ... Update timer
If pin2 = 1 then calc
goto loop2
if b0 = 10000 then mainloop



calc:

b1 = b1 / 10
b2 = 30 / 100
b3 = b2 / b1
debug b3

GOTO MainLoop ' Forever
 

hippy

Technical Support
Staff member
The theory is sound but there are a number of factors which could affect how you do it.

How fast and/or slow is the train likely to be moving ? That determines the minimum and maximum times you will be looking for which may determine how you do the timing trigger.

The speed of the train passing over the sensor will also affect how long the sensor is triggered for. If it's only a brief trigger, which lasts for less than the time you are pausing for, you may miss it.

PULSIN will only measure the length of a pulse from one sensor, but you might be able to use a single sensor which activates as the train passes over and the length of that pulse would give a timing measurement. With some simple hardware it may be possible to use two sensors to give a pulse; reed relays and magnets under trains would allow one relay to charge a C, another to discharge it, and the voltage on C could be used as the pulse.

Once you've acquired a time, that then needs to be converted to a speed, s=d/t, however that's not always simple because the PICAXE doesn't do anything except integer maths, but we can worry about that once you've got the time measuring sorted out.
 

Jeremy Leach

Senior Member
Ideally you don't want to deface your train too much with painted stripes, reflective strips etc. So thinking in simple terms, maybe have a magnet stuck under the train. Two reed switches separated apart on the tracks.

Can then sample two inputs like you say. Remembering my trainset, the max speed wasn't that great (and yes it was electric not steam !) so I would have thought that the PICAXE could get a fairly accurate estimate of speed.

Or, you could construct a simple 'set/reset flip-flop' from a (very cheap) quad NOR gate (or NAND gate) chip, where one reed sets the output and the other resets it. The output is fed into the PICAXE input and pulsin used to get an accurate measurement of the pulse.


Edited by - jeremy leach on 20/07/2006 15:57:01
 

djh82uk

Member
hiya

well I figure a train is a good 20 Cm long so it's going to take longer than 10MS for it to pass over the sensor fully,

My only problem with that is if it sensing the train when it is half way over then it will mess up the calculations.

here is what i have so far, but it is the calculations messing me up.


symbol timer1 = w3
symbol time1 = w2
symbol dist = w1
symbol speed = w0

MainLoop:


IF pin1 = 1 THEN loop1 ' If input activated ...

goto mainloop

loop1:

w3 = 0
goto loop2

loop2:


pause 10
w3 = w3+10 ' ... Update timer
If pin2 = 1 then calc
goto loop2
if w3 = 10000 then mainloop



calc:

w2 = w3 / 10
w1 = 30 / 100
w0 = w1 / w2
debug w0
debug w2
debug w1
debug w3

GOTO MainLoop ' Forever

w3 (timer) reads about 3900MS when I counted between upto 4 seconds before triggering the 2nd input, w1 then gave an output of 396 which I take to mean 3.96 seconds?

I guess for more accuracy I could move the timer down to 5ms or even 1ms? but then I guess the timing would then be more dependent on how fast the chip is working (hence my 5ms idea).

I was going to use 2 ldr's or photodiodes as the sensors 30cm apart on the track.

I then eventually want to display the actual speed somehow on an lcd, and then maybe scale speed (1:26 I think for OO scale)

I guess one way would be to calculate it in cm/cs (centi seconds, did I make that up?)

so instead of 0.3m / 3.9s = 0.076 m/s
it would be 30cm / 39 cs = 0.76 cm/cs

but then I still have the problem of the answer being 0. something, so I thought how about ms and mm

so 300mm / 3900ms = 0.076 which is just messed up, my head hurts now

 

djh82uk

Member
Sorry jeremy, I was typing my reply when you posted, I do like your idea of using a quad nor gate tho.

so I have a few different ways of measuring it, but how do i do the calcualtions?
 

Jeremy Leach

Senior Member
If you did use the pulsin method, then assuming a distance of 20cm between the reeds:

Distance travelled in miles = 0.2/1609
(1 mile = 1609m)
Time to travel this distance in hours = P / (60 * 60 * 1000) where P is the word variable read by Pulsin command.

So speed in mph = Dist / Time = (0.2 * 60 * 60 * 1000) / (1609 * P)
Therefore <b> Speed = 447/P </b>

However I think I'd display the scaled up speed: Speed = (26 * 447)/P

I think these calcs are right ;-)
 

Jeremy Leach

Senior Member
Oh sugar ....yes it does. I was thinking it was 65 seconds for some reason !!

But that doesn't matter. Just need the reeds closer together if necessary. My calcs would need altering too. (Speed in mph = 44700/P ...I think)

Edited by - Jeremy Leach on 20/07/2006 16:52:16
 

djh82uk

Member
but if you get the reed switches too close to beat the 0.6s timeout isn;t the magnet going to effect both of them? as it needs to cater for the train moving at slow speeds too.

Is there not anyway of doing the calculations with just timing the difference? I could still use the reed switches but lose the pulsein and the quad nor gate.

but for the life of me I cannot figure out the calculations.

But doing it this way the train can be moving so slow that it can take upto 16 seconds to reach the 2nd input and still be accurate.

DJH
 

Jeremy Leach

Senior Member
Ok, so let's say the reeds were 2cm apart - I think a small magnet would work and wouldn't trigger both at the same time. 2cm in 0.6 seconds is pretty slow (9 seconds to move a ruler-length!)

Choice is yours on the solution ;-)
 

djh82uk

Member
ok so that makes 0.2m into 0.02m?


which makes it 44/p for the speed or (26 * 44) / P for the scale speed?

But if the pulsein command loads a variable of 12000 (0 - 65k) then (26 * 44) / 12000 gives 0.095

or have I messed it up?
 

djh82uk

Member
ok I think I deffo messed that up

but even if we went with the (26 * 447) / p

that (26 * 447) / 15000 = 0.7 isn't it?

DJH

 

Jeremy Leach

Senior Member
Assuming a distance of 2cm between the reeds (!) ..... and getting the Pulsin command right this time (!!):

Distance travelled in miles = 0.02/1609
(1 mile = 1609m)
Time to travel this distance in hours = P / (60 * 60 * 100000) where P is the word variable read by Pulsin command.

So speed in mph = Dist / Time = (0.02 * 60 * 60 * 100000) / (1609 * P)
Therefore Speed = 4475/P


I think ;-)

So now we have to think about PICAXE integer maths. If P is anything over 4475 then the Speed calc will = 0. So it makes sense to calculate speed to one decimal place - ie multiply by 10 first :

Speed10 = 44750 / P

Can then use division to get Whole and fractional parts, and display a decimal point between them.

Edited by - Jeremy Leach on 20/07/2006 17:23:29
 

djh82uk

Member
ok so assuming P is 15000 that means the train is doing 7.75 MPH Scale speed, but thats going to show up as 7mph?

sorry, Im just trying to figure out how all this works

DJH
 

Jeremy Leach

Senior Member
See edited last post. The trick with integer maths is to scale the calculation up as close to 65535 max word limit as you can, before any final divisions.

Also, you need to think here what is a realistic maximum speed then use this to work out the best way of doing the maths.

Edited by - Jeremy Leach on 20/07/2006 17:29:46
 

xstamp

Senior Member
I&#8217;ve done this with &#8216;real&#8217; cars by feeding negative going pulses from the opto detectors into the inputs of cross-coupled (RS flip-flop) Schmitt NAND gates to produce a single output pulse that&#8217;s duration is the time between pulses. When this is measured by the PICAXE it can reset the RS before the next pass.

 

djh82uk

Member
The reason I was hoping to be able to use photodiodes/ldr's or reed switches is just because I have a load here, and was hoping to not have to meet the minimum order with farnell of &#163;20 for a few components.

Is there anyway the maths can be done this way?

DJH
 

djh82uk

Member
Ok thanks for all your help guys, esspecially jeremy.

I finally got it working with 2 normal sensors., it outputs scale speed in mph, no decimal places but it gives out between 1mph and 17000mph.

Not stupidly accurate but it is a start. I also took the timings, distance and multiplied by 26 (1:26 scale) and calculated it on a calculator and i get the same readings. (just with decimal places). So Thats my next task, (more accuracy with decimal places, even just 1dp.

Heres my code:

symbol timer1 = w3
symbol time1 = w2
symbol dist = w1
symbol speed = w0

MainLoop:


IF pin1 = 1 THEN loop1 ' If input activated ...

goto mainloop

loop1:

w3 = 0
goto loop2

loop2:


pause 5
w3 = w3+5 ' ... Update timer
If pin2 = 1 then calc
goto loop2
if w3 = 10000 then mainloop



calc:

w2 = w3
w1 = 17446
w0 = w1 / w2
debug w0
debug w2
debug w1
debug w3

GOTO MainLoop ' Forever
 

djh82uk

Member
damn, just found out that the scale is actually 1:76, changed my figures a bit as the number was over 65k, but all seems ok now, could do with some help on the decimal point tho

I am using word variables but b0 only holds the final number as it is less than 255, if I multiply it by 10, I get funny results in that b1 will be 1 and b0 will be a meaningless number

DJH
 

Jeremy Leach

Senior Member
Here's something I worked out recently in a thread with Beaniebots that might help. It's to calculate Word1 * Word2 / Word3 without getting overflow (but with a few restrictions). I think it works ...

MSW = Word1 ** Word2
LSW = Word1 * Word2
ResultWord = LSW/Word3
ResultWord = 65535/Word3 * MSW + ResultWord
ResultWord = MSW/Word3 + ResultWord


To display the decimal point you need to work out the Whole and Fractional parts. In general terms, if you had a value Speed100 that's 100 * the speed, then:

Whole = Speed100 / 100
Remainder = Speed100 // 100
Where // means Mod, ie the remainder.

Then display: Whole , &quot;.&quot;, Remainder
to show Speed to two decimal places.


Edited by - jeremy leach on 21/07/2006 08:55:17
 

alimauro

New Member
have you given thought to using a hall effect transistor and a series of magnets close together alternating north and south poles (6 works well) and producing a shaded pole effect? close together on one car shoutd get you in under the .6 sec time. Just a thought cause its better to count multiple pulses than to time reed switches.

Edited by - alima on 22/07/2006 07:39:10
 

Jeremy Leach

Senior Member
Hi Alima ... I'm not sure I get this idea. Ok, a series of magnets under the loco/carriage running over a hall effect sensor. So you'll get a series of pulses from the sensor.

But the <i>number </i> of pulses will always be the same. The pulse <i>width </i> will change with speed but the number of pulses won't.

I think a hall effect switch will be more accurate than a reed because it's not mechanical, however I still think a reed feeding a flip-flop will still be pretty good because it will consistently trigger in the same way and there's no worry over switch bounce because it's always triggering as soon as the reed first closes.

There are lots of variations and other ideas. One idea, to get round the timeout issues of Pulsin, would be:

Use a quad NAND gate chip for the flip-flop (using only 2 of the 4 NANDs).
Have the PICAXE generate a PWM signal.
Feed this signal into another NAND gate, with the other input being fed from the output of the flip-flop.

The result would be a gated high frequency signal which is turned on and off by the flip flop.

Have the first reed encountered on the track generate an interrupt that immediately starts the PICAXE running the Count command. The counts received will depend on the train speed. I think this could give a speed range right down to zero, but with good accuracy.

The only issue here though is that there will be a slight delay between the interrupt and starting the Count command. But this could be got round by having a THIRD reed slightly ahead of the other two, with a sole purpose of firing the interrupt and starting the Count command !

It's all good fun, and it's a nice project - particularly scaling the speed of the train up to real-life speeds. It must give the whole railway a more realistic feel.

Edited by - jeremy leach on 22/07/2006 10:42:06
 

djh82uk

Member
I will try the nand gate idea, I just wanted to get it working first and then make it better afterwards.

It seems reasonably accurate, i used a stopwatch to make it 4 seconds between the inputs, and the pic counted a time of 3978MS, which I am quite happy with at the moment. I then got it to report the speedm but i also calculated it manually and they were very close.

Regards

DJH
 
Top