Decimal points

mas11

Member
Hello everyone. Thank you for your past help in solving my problems with programming.

I am trying to build a wireless speedometer for my G scale train that will display real time speed with scale speed. believe I have the math worked out correctly, but when trying to display the real time speed, I don't know how to place the decimal point in the readout to get 0.000 real time speed. I get 0000.0 MPH instead. The code so far displays the readings on a serial display hardwired to a breadboard for test purposes. I still need to figure out the Tx and Rx code but for now I just need to figure out the decimal point. Can you please help? If you need any more information, the scale is 1/29th and the wheel travels 3.5 inches per one revolution.

Right now my display reads as follows:2x16
speed = 00000MPH
Real =0000.0MPH

Only need three places for scale speed and 3 decimal for real speed.
Scale = 000MPH
Real = 0.000MPH


Thank you,

mas11

#terminal 4800
symbol speed_sensor_pin=C.3
symbol tenthous=b4
symbol thous=b5
symbol hunds=b6
symbol tens=b7
symbol units=b8
symbol tones=b9
symbol ttenths=b10
symbol thundredths =b11
symbol tthousandths =b12
symbol units2=b13




init:
pause 500
serout C.4,N2400, (254,1)
main:
COUNT speed_sensor_pin, 1000, w1 'count number of pulses in 1 sec
'3.5inches=1RPS. 1RPM=3.5*60min*1/63360i

w1=w1*96/100 'convert RPS to scale mph 1/29, divide by 6 pulses/sec
w7=w1*335/1000 'real speed in mph

BINTOASCII w1, tenthous,thous,hunds,tens,units 'convert value to ASCII characters ready to be sent to PC
BINTOASCII w7, tones,ttenths,thundredths,tthousandths,units2
SERTXD ("speed = ", tenthous,thous,hunds,tens,units, "MPH", 13,10) 'send RPM value to PC
SERTXD ("Real =",tones,".",ttenths,thundredths,tthousandths,units2, "MPH",13,10)
serout C.4,N2400, (254,128)
serout C.4,N2400, ("speed = ", tenthous,thous,hunds,tens,units, "MPH", 13,10)
serout C.4, N2400, (254,192)
serout C.4, N2400, ("Real =",tones,ttenths,thundredths,tthousandths,".",units2, "MPH",13,10)
debug
goto main
 

westaust55

Moderator
What is the maximum rpm that can be expected?

Is there only one pulse per wheel revolution?

For 3 decimal points for the real/actual speed plus 1 digit of whole/integer part we need to have a 4 digit number minimum.
at 50 revolutions per second and 1 pulse per revolution that equates to 9.943 mph actual - at 51 pulses per sec there will be overflow with a word variable.

Without getting into the compexities of 32 bit maths, you are going to struggle with better than 2 decimal places of accuracy due to 16 bit limitation on the internal maths.
The best you may achieve is:
actual physical speed = pulses/sec * 199​
Then, on average the error will be -0.003 but can be as high as -0.007.

Scale Speed = pulses/sec * 576 / 100​


Also please ensure that you place your program code within [code] and [/code] tags as requested in the forum stuiicky note
Read Me First found at the top of the active forum here: http://www.picaxeforum.co.uk/showthread.php?7679-Read-Me-First!
 

westaust55

Moderator
@mas,
Are your maths neglecting the mins to hours factor.

Presuming 1 pulse per rotation, then
at say 50 pulses per second from the COUNT function

The total distance is 3.5 * 50 inches in 1 second.
The total distance in 1 hour would be 3.5 * 50 * 60 * 60 = 630,000 inches per hour.

630000 / 63360 = 9.943 actual miles

Based on the 1/29 scale the scale speed is 9.943 * 29 = 288 scale mph.


Try this code:

Code:
#terminal 4800
symbol speed_sensor_pin=C.3 
symbol tenthous=b4
symbol thous=b5
symbol hunds=b6
symbol tens=b7 
symbol units=b8
symbol tones=b9
symbol ttenths=b10
symbol thundredths =b11
symbol tthousandths =b12
symbol units2=b13


init:
	pause 500
	serout C.4,N2400, (254,1) 
main:
	COUNT speed_sensor_pin, 1000, w1 'count number of pulses in 1 sec

	w7=w1* 576 / 100 ; calculate scale speed
	w8=w1*199 ; calculate Actual speed

	BINTOASCII w7, tenthous,thous,hunds,tens,units 'convert value to ASCII characters ready to be sent to PC
	BINTOASCII w8, tones,ttenths,thundredths,tthousandths,units2
	SERTXD ("Scale =",tenthous,thous,hunds,tens,units, "MPH", 13,10) 'send RPM value to PC
	SERTXD ("Actual=",tones,ttenths,".",thundredths,tthousandths,units2, "MPH",13,10)
	serout C.4, N2400, (254,128)
	serout C.4, N2400, ("Scale =", tenthous,thous,hunds,tens,units, "MPH", 13,10)
	serout C.4, N2400, (254,192)
	serout C.4, N2400, ("Actual=",tones,ttenths,".",thundredths,tthousandths,units2, "MPH",13,10) 
	debug
	goto main
Note that I have
1. indented the program so that the position of the labels ( init: and main: ) are clearly identified.
2. used the above mentioned code markers/tags so the program listing appears in a sub window and formatting plus intenting is preserved
3. moved the "." part for the sctual speed as sent to the LCD
 

mas11

Member
Thank you for the reply. I will follow your advice. I neglected to say that there are 6 pulses per revolution. I know that will make a difference in the math. So if I use your example, I would divide the sum of 630,000 by 6 and divide that answer by 63,360 to get my actual speed which would be aproximately 1.66 mph times 29 would give 48 scale MPH, it that correct? Please let me know.

Thank you again,

Mas11
 

mas11

Member
westaust55,

Thank you for the advice and help. The code works great. If I only needed 3 places for the scale MPH, would I remove the tenthous and thous or the tens and units. Or should I just play around with them on my own to see what happens? I am still trying to understand the use of byte and word variables and when and how to use them. Any help is appreciated.

Thanks again,
mas11
 
Top