PDA

View Full Version : Help measuring distance....



sirvo
- 29th July 2007, 23:51
Hi Again..

Need a little help in here... maybe it's a picbasic math question.. it goes: My car has a speed sensor and as I had measured, it pulses 9 times per meter (yes, I pushed the car 2 meters by hands and it counted 18). I would like to calc the distance run, like an odometer. The speed sensor is connected to T0CKI.
-> TMR1 is interrupting every 500ms and the interruption causes TMR0=0. -> This is necessary due to other processes. I would not like to change it. How could I measured the distance in this situation?

Hope you understand my english... :)

Thanks a Lot!

Sylvio

HenrikOlsson
- 30th July 2007, 09:01
Hi,
Every 500mS interrupt you read the count from TMR0 and add it to an accumulator. You also keep track of how many interrupt have passed and when 10 interrupts have gone by you divide the accumulator count by 9. You now have the number of metres traveled in the last 5 seconds.

(Untested)



Acc var WORD 'Accumulator
ISRCount var BYTE 'Keeps track number of interrupts
Km var WORD 'Kilometres
m var WORD 'Metres

'//Place this in your ISR to be executed every 500mS
ISRCount = ISRCount + 1 'Increment ISRCount
Acc=Acc + TMR0 'Add TMR0 value to accumulator, TMR0 gets reset elsewhere.

If ISRCount = 10 then 'If 10 interrupts have passed....
m = m + (Acc / 9) '...calculate how many metres...

If m > 1000 then 'If more than 1km have passed..
Km = Km + (m / 1000) '...add to the Km count...
m = m // 1000 '...and keep reminder.
EndIf

ISRCount = 0 'reset ISR counter
EndIf


You can now use...


LCDOut $FE, 1, #Km, ".", #m

or whatever in your main routine to display the distance traveled.

/Henrik Olsson.

GrandPa
- 30th July 2007, 09:56
Hi,


I think you should consider using PIC2331/2431/4331 or 4431.
Those PICS have a built-in motion feedback module. I think the "velocity feedback module" will do exactly what you want. Look at the datasheet for more details.

BTW: Downside is that the ADC module is a bit of pain in the ... to use. Since it's not completely integrated with PBP.

J-P

sayzer
- 30th July 2007, 11:30
WhyNot use a traditional way;




' 9 Pulses come in in 0ne meter.
' Measure the time it takes for 9 pulses to come in.
' Timer0 is in Counter Mode.
' Timer1 is in Timer Mode.

' Set your prescaler and other TMR settings etc... here.

Start:

TMR1ON = 0 ' Stop TMR1.
TMR1H = 0 ' Clear TMR1.
TMR1L = 0 '....
Speed = 0 ' Speed is a Word variable.
TMR0 = 0 ' Clear TRM0.

WHILE TMR0 = 0 : WEND ' Wait for an incoming Pulse (First pulse does not count).
TMR1ON = 1 ' Start Timer1 Module.
WHILE TMR0 < 9 : WEND ' Count 9 pulses.
TMR1ON = 0 ' Stop Timer1 at 9th Pulse.

Speed.LowByte = TMR1L
Speed.HighByte = TMR1H

' The car took 1Meter in "Speed" mS. Example: 1 Meter in 100mS.
Speed = 60000 / Speed ' Speed = 60,000mS / 100Ms => 600 Meter in One Minute.
Speed = Speed * 60 ' Speed = Speed * 60Mins => 600Meter * 60Mins = 36,000Meter. => 36Km/Hr

LCDOUT $FE,1,"Speed:", dec5 Speed,"Km/Hr"


Goto Start

HenrikOlsson
- 30th July 2007, 12:52
Is it just me or didn't he say he want to measure distance? IE distance, not speed. Yep, I'm sure, he said distance...

I may have misunderstood though....

;-)

/Henrik Olsson.

sirvo
- 30th July 2007, 16:11
Hi,
Every 500mS interrupt you read the count from TMR0 and add it to an accumulator. You also keep track of how many interrupt have passed and when 10 interrupts have gone by you divide the accumulator count by 9. You now have the number of metres traveled in the last 5 seconds.

(Untested)



Acc var WORD 'Accumulator
ISRCount var BYTE 'Keeps track number of interrupts
Km var WORD 'Kilometres
m var WORD 'Metres

'//Place this in your ISR to be executed every 500mS
ISRCount = ISRCount + 1 'Increment ISRCount
Acc=Acc + TMR0 'Add TMR0 value to accumulator, TMR0 gets reset elsewhere.

If ISRCount = 10 then 'If 10 interrupts have passed....
m = m + (Acc / 9) '...calculate how many metres...

If m > 1000 then 'If more than 1km have passed..
Km = Km + (m / 1000) '...add to the Km count...
m = m // 1000 '...and keep reminder.
EndIf

ISRCount = 0 'reset ISR counter
EndIf


You can now use...


LCDOut $FE, 1, #Km, ".", #m

or whatever in your main routine to display the distance traveled.

/Henrik Olsson.

Hello.
Thanks Henrik Olsson. I made a few changes and it has been working very well.
Thanks All..

Sylvio