Counting Pins or Pulse Width on 2 Pins
Jim Fouch
Posts: 395
I have a problem i'm not sure the best way is to solve.
I have a project that I designed last year to measure the Pulse Width on two pins and then based on their ratio decide what gear a motorcycle is in. This project was done on a BasicX-24 that supports floating point math it was quite easy. However, I'm now trying to do the same with an SX-18.
For the tach I'm looking at a pulse width of anywhere from 1ms (~15,000RPM) upto 15ms (~1,000RPM). For the Speed it's anywhere from .2 ms (~157MPH) up to 20ms (~1.5·MPH) Below 1 MPH I·don't care about the gear.
On the BasicX I·just took a·Pulsein·on each pin and used the flowting math to·calc the outcomes.·On the SX I'm torn·between counting pulses and measuring their widths.
In my current design I needed to display both RPM & MPH on a LCD & Video overlay. This device will do nothing but display a·gear number based on·the ratio between the pulses.·I would like to see it recalc at least every 250ms.
My concern is that with only·BYTE values to work with I will not have enough resolution to calc a ratio very well. Any ideas. I'm using SX/B with some ASM where I need it.
Also, I'm willing to HARD CODE in the ratios for the gears on this bike, but does anyone have thoughts on how I could have it AUTO LEARN the ratios? I'm thinking that it somehow tries to lock in on a constant ratio for maybe 1 sec and compare it to a list of learned ratios. As the person would start out in 1ST and drive for a while then shift into 2ND and so on.·The first ratio it would lock into would be 1st, then 2nd and so on.·Not sure if the SX would have the power to do that.
Thanks,
Jim Fouch
This is a clip from my current project on the BasicX. This project does more then the new design will have to do.
TachPulse = Pulsein(TachInputPin,0)
SpeedPulse = Pulsein(SpeedInputPin,0)
if SpeedPulse=0 then
··mph·=·0.0
ELSE
···MPH = (1.0/(CSNG(SpeedPulse)/921659.0)/31.81)
···mph = MPH * 0.932 ' Correction for error from factory
···MPH = MPH * 0.9375 ' Caculated for 1/16 percent drop when Stock Sprocket was changed
End If
Ratio = csng(TachPulse)/csng(SpeedPulse)
Private Function CalcGear(byval Ratio as Single, byval MPH as Single) as String
·' This function will return the Gear Based on the Ratio
·If GetPin(NeutralPin)=0 then
··GearNum = 0
··CalcGear = " N "
··Exit Function
·End If
·If (Ratio>7.4) and (MPH>20.0) then
··' 6th Gear
··GearNum = 6
··CalcGear = "6th"
··Exit Function
·Elseif (Ratio>6.8) and (MPH>17.0) Then
··' 5th Gear
··GearNum = 5
··CalcGear = "5th"
··Exit Function
·Elseif (Ratio>6.1) and (MPH>15.0) Then
··' 4th Gear
··GearNum = 4
··CalcGear = "4th"
··Exit Function
·elseif (Ratio>5.2) and (MPH>13.0) Then
··' 3rd Gear
··GearNum = 3
··CalcGear = "3rd"
··Exit Function
·elseif Ratio>4.2 Then
··' 2nd Gear
··GearNum = 2
··CalcGear = "2nd"
··Exit Function
·elseif Ratio>3.2 Then
··' 1st Gear
··GearNum = 1
··CalcGear = "1st"
··Exit Function
·Else
··GearNum=255
··CalcGear = " ? "
··Exit Function
·end if
End Function
I have a project that I designed last year to measure the Pulse Width on two pins and then based on their ratio decide what gear a motorcycle is in. This project was done on a BasicX-24 that supports floating point math it was quite easy. However, I'm now trying to do the same with an SX-18.
For the tach I'm looking at a pulse width of anywhere from 1ms (~15,000RPM) upto 15ms (~1,000RPM). For the Speed it's anywhere from .2 ms (~157MPH) up to 20ms (~1.5·MPH) Below 1 MPH I·don't care about the gear.
On the BasicX I·just took a·Pulsein·on each pin and used the flowting math to·calc the outcomes.·On the SX I'm torn·between counting pulses and measuring their widths.
In my current design I needed to display both RPM & MPH on a LCD & Video overlay. This device will do nothing but display a·gear number based on·the ratio between the pulses.·I would like to see it recalc at least every 250ms.
My concern is that with only·BYTE values to work with I will not have enough resolution to calc a ratio very well. Any ideas. I'm using SX/B with some ASM where I need it.
Also, I'm willing to HARD CODE in the ratios for the gears on this bike, but does anyone have thoughts on how I could have it AUTO LEARN the ratios? I'm thinking that it somehow tries to lock in on a constant ratio for maybe 1 sec and compare it to a list of learned ratios. As the person would start out in 1ST and drive for a while then shift into 2ND and so on.·The first ratio it would lock into would be 1st, then 2nd and so on.·Not sure if the SX would have the power to do that.
Thanks,
Jim Fouch
This is a clip from my current project on the BasicX. This project does more then the new design will have to do.
TachPulse = Pulsein(TachInputPin,0)
SpeedPulse = Pulsein(SpeedInputPin,0)
if SpeedPulse=0 then
··mph·=·0.0
ELSE
···MPH = (1.0/(CSNG(SpeedPulse)/921659.0)/31.81)
···mph = MPH * 0.932 ' Correction for error from factory
···MPH = MPH * 0.9375 ' Caculated for 1/16 percent drop when Stock Sprocket was changed
End If
Ratio = csng(TachPulse)/csng(SpeedPulse)
Private Function CalcGear(byval Ratio as Single, byval MPH as Single) as String
·' This function will return the Gear Based on the Ratio
·If GetPin(NeutralPin)=0 then
··GearNum = 0
··CalcGear = " N "
··Exit Function
·End If
·If (Ratio>7.4) and (MPH>20.0) then
··' 6th Gear
··GearNum = 6
··CalcGear = "6th"
··Exit Function
·Elseif (Ratio>6.8) and (MPH>17.0) Then
··' 5th Gear
··GearNum = 5
··CalcGear = "5th"
··Exit Function
·Elseif (Ratio>6.1) and (MPH>15.0) Then
··' 4th Gear
··GearNum = 4
··CalcGear = "4th"
··Exit Function
·elseif (Ratio>5.2) and (MPH>13.0) Then
··' 3rd Gear
··GearNum = 3
··CalcGear = "3rd"
··Exit Function
·elseif Ratio>4.2 Then
··' 2nd Gear
··GearNum = 2
··CalcGear = "2nd"
··Exit Function
·elseif Ratio>3.2 Then
··' 1st Gear
··GearNum = 1
··CalcGear = "1st"
··Exit Function
·Else
··GearNum=255
··CalcGear = " ? "
··Exit Function
·end if
End Function
Comments
You very tricky.....I do the same with "competitor" vehicles.....although the torque converter messes things up a bit when the vehicle is coasting...the technique N/V works well. Since you are directly coupled (no torque converter) it will work well.
At sxlist.com·(http://sxlist.com/techref/ubicom/lib/math/index.htm)·you can find code for double and triple byte math written in assembly.
I would suggest measuring pusle width for RPM and Speed.
I have a contest entry at the Parallax sight (door velocity meter & Duty cycle meter) that both measure pulse width.
The duty cycle meter measures the high and low time of the incoming signal, and divides the High/(High+Low) for duty cycle high or Low/(High+Low) for duty cycle low. Running at 50MHz you can get very acurate.
Will this go· on "one" bike and stay there? One bike or many....if you know the gear ratio and tire size you can calculate the N/V ratio before hand and hard code that into the SX. You cannot do it on the fly (auto learn) without re-calibrating it every time power is lost to the SX chip itself.
Ken
Post Edited (KenM) : 6/27/2005 8:07:06 PM GMT
As far as the Auto Learn function, Yes I plan on having it Auto Learn each time power is applied. Once it has locked onto a ratio for about 1/2 second. It will always remember that ratio as a given gear until power is lost.
On the current bike (Honda CBR1000RR) I see that the ratio does some movement while in a single gear. My guess is that that is slack in the chain and spacing in between teeth in the tranny. But it is very close.
It's just going to be tricky doing this with out arrays and alot of RAM. But I'm sure it can be done. Other products out there have the end user TRAIN the device·by the use of a grounding wire, and placing the bike on a stand and running it thru each gear. I'm looking to make it total Auto Learning. Plug-n-Play.
Thanks,
Jim Fouch
What is your idea to have the user perform the Auto Learn function?
I believe the key to success in the Auto Learn mode is a repeatable procedure and going into all gears from a dead stop after the device is powered up.....or if the user screws up the cal process, come to a stop, hit a momentary switch (reset button) then run the calibration again. With some tricky coding, that may not be needed.
In any event, this sounds like a fun project and·I would be·glad to work with you on the project·as I am currently doing something similar on automobiles.
Ken
Post Edited (KenM) : 6/28/2005 12:45:28 AM GMT
My thought is to have an array of sorts that has all locked ratios. On Start up the rider would normally start out in 1st. As soon as it locked into 1st it would display it on an 7-seg. display. Then as they shifted into 2nd it would find a new ratio and display it. This would happen for each new locked in ratio. As far as the user is knows, it just works like magic. Should work on all bikes.
The key is going to be setting a margina·and time limit·for the SX to figure it has locked into a new ratio. 1st - 3rd normally are kind of far apart. But on many bikes the difference in the higher gears is very small. With the limited amount of memory and no floating point math, it's going to be a fun project for the SX.
On the past project I was able to have the ratios sent to a video overlay board. I then recorded this while I went for a ride·and figured the math out by hand later.
If you want to see a demo of the current project check out my site.· http://www.seemeride.com
Jim
·
I would aproach this problem by counting one signal against the other.
In other words, how many speed pulses occur during 40 tach pulses.
Of course you will have to code the counting scheme, but if you choose a "good" number of tach pulses for the count you should be able to get count between 1 to 255.
Can you give some examples of RPM and Speed numbers for: 1st gear @ idle, 1st gear @ max rpms, 2nd gear @ idle, 2nd gear @ max rpms, etc..
Very interesting project, BTW I have a video overlay circuit working using the SX28 ask if want more info.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"What's the difference between ignorance and apathy ?"
"I don't know, and I don't care."
·
Time must be included in the calculation....the frequency or peroid must be measured.
Example: At any one speed the same number of speed pulses will occur, say 20 mph = 20 speed pulses.
40 tach pulses in·2nd gear at·20 mph, or 40 tach pulses in·3rd gear at 20 mph will yield the same ratio.
I think Bean probably meant to indicate how many speed pulses occur in the time it takes to obtain 40 tach pulses.
Bean????? Help me out here.
That's why I asked for some numbers to crunch. But he is trying to measure the ratio of speed pulses to tach pulses to figure out what gear he's in. Time doen't come into the equation.
You cannot get the same number of speed pulses & tach pulses in different gears. If you are going 20mph in 3rd gear the rpm's MUST be lower, than 20mph in 2nd gear. (As long as the clutch isn't pulled in).
In his code he is calculating MPH too, and your right that is simply measuring speed pulses vs time.
Hey Jim... How about some numbers so Ken and I can see who's right ?
I thought I was wrong once....but I was mistaken [noparse];)[/noparse]
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"What's the difference between ignorance and apathy ?"
"I don't know, and I don't care."
Post Edited (Bean (Hitt Consulting)) : 6/28/2005 11:44:29 AM GMT
For the current project, it just has to do with the ratio of the two. Most of my problem is how to figure a good ratio w/o floating point math.
Jim.
Did you guys take a look at the site that shows the sample video?
Bean (Hitt Consulting) said...
Ken,
But he is trying to measure the ratio of speed pulses to tach pulses to figure out what gear he's in. Time doen't come into the equation.
There is no question that you are a·craftier programmer than I, but I respectfully disagree that time does not come into the equation.
I agree·the rpm will be different in 2nd vs 3rd gear at one speed.
However,·if you are counting only the number of tach pulses at 20 mph, you will end up with the same pulse ratio no matter what gear the bike is in.
Traveling at one speed, the time to reach 40 tach pulses at 20 mph will be different in 2nd or 3rd gear, so the period of frequency of the tach must be known.
Again, say 20 mph = 20 speed pulses.
40 tach pulses/20 speed pulses is a ratio of two, no matter what gear you are in, yes/no?
In 2nd gear the time to obtain 40 tach pulses will be less than the time to get 40 tach pulses in 3rd gear at the same speed.
Again, 40 pulses is 40 pulses, and 20 mph is 20 mph, the only variable is the time to reach 40 pusles, which·is a function of rpm, which is a function of the selected gear (discounting the clutch as mentioned).
Ken
Post Edited (KenM) : 6/28/2005 1:56:28 PM GMT
I now see the light.
You originally posted "How many speed pulses occur during" 40 tach pulses....."
During is the key word here. That method will work which is an indirect measurement of time, So I summize neither of us is incorrect.
Post Edited (KenM) : 6/28/2005 2:05:09 PM GMT
You are trying to say that there is only a single RPM speed that will make the bike go 20 MPH no matter what gear it is in.
That is simply not true.
Speed pulses=distance travelled
Tach pulses=Motor revolutions
I'm simply saying to count "How far does the bike travel for X motor revolutions" this will definiately change with gearing and has nothing to do with measuring elasped time.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"What's the difference between ignorance and apathy ?"
"I don't know, and I don't care."
·
RPM:
·· 15ms = 1000 RPM
··· 10ms· = 1500 RPM
···· 5ms· =· 3000 RPM
···· 2ms =·· 5000 RPM
···· 1ms =··15000 RPM
·· RPM Limit 11,750 (Readline)
Formula to calculate RPM = (1/PulseWidth)*15
Pulse width in Seconds
Based on a 4 cyl/4 stroke engine
SPEED:
·· 20ms = 1.2mph
·· 10ms = 3.1mph
··· 3ms = 10.5mph
··· 1ms = ~31mph
·· .5ms = ~62.9mph
·· .2ms = ~157mph
·· Top Speed of bike ~182 MPH
Formula to calculate Speed: 1/pulsewidth/31.81
Note this is calibrated against factory speedometer and stock gearing, which is ~8% off
I did not state that there is only a single RPM speed that will make the bike go 20 mph no matter what gear it is in.
I stated that if you use only the number of tach pulses at 20 mph, the ratio will remain the same. What I failed to see in your original post is that you indicated to count the number or speed pulses that occur "during" 40 tach pusles.
See my previous post.
Speed = Miles/hour
Tacho = revolutions / minute or revolutions * 60 / hour
Speed / Tacho = (Miles/hour) / (revolutions * 60 / hour)
in the last formula, you can cancel down the hours, and the time factor is gone - right ?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
Yes, the units of time cancel, making the formula a ratio, a unitless number.
As previously stated, the method is an indirect measurement of time. (last word from me on this)
Now lets help Jim with his a solution
Ken
What I was hoping for was some numbers for each gear.
1st gear : X tach pulse width = Y speed pulse width
OR
1st gear: X RPM = Y MPH
and so on for each gear...
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"What's the difference between ignorance and apathy ?"
"I don't know, and I don't care."
·
I don't have any hard numbers other then the ones that are in my code sample for the last project.
The ratio is ...
TachPulse/SpeedPulse
If this ratio is Larger than 7.4 then I assume that the bike is in 6th gear,
Elseif it's Larger than 6.8 than I assume it's in 5th gear
and so on....
As I coast down to a stop, I was having a problem that when the clutch is in, the code would WALK down thru the gears as it would find a match between RPM and SPEED. To counter this, I added code to my IF statements to weed out any unlikley Gear/Speed combinations. Hence the code "If (Ratio>7.4) and (MPH>20.0) then" would only indicate 6th gear·ONLY if I was going faster than 20mph.
This hard coded approach could be easily solved on the SX, but I'm still trying to figure out a way to have the SX Auto Learn the Ratios. The three inputs I will have to solve this are, Speed Pulse, RPM Pulse and the Nutral Light from the bike. I will assume that the rider will start out in 1st and use all gears up to the highest one on the bike. Maybe not·1st all the way thru 5th (or maybe 6th) but they should always be in a lower gear before it locks onto a higher gear.
Some things that have to be accepted are.... If the rider has the clutch pulled in, there is no way we can tell what gear they are in, show last gear until we have better data. If they come to a stop and we don't have a speed pulse, we can't tell what gear they are in. It's only designed to give the user feedback when they are moving with the clutch out. Also, the device could be used to test if they have a clutch that is slipping. If they leave the bike in a specific gear and accelerate hard and the gear indicator does not stay in the gear, it would mean that the ratio is not staying constant. Meaning that the clutch is slipping.
My goal is to have a system that works on ALL bikes. So my timing for the pulses are based on ONE specific bike. They will be somewhat different for each bike. That is why an Auto Learn approach will be so cool.
Thanks,
Jim Fouch
Can you share with me your approach to the ratio calculation before you post the final solution?
Do you intend to have two seperate interupts, one that counts rpm edges, the other mph edges?
Then stop accumulating mph edges once you get the desired number rpm edges?
Thanks,
Ken
ok about the time cancel .
On the other hand as Jim mentioned in his last post "if (Ratio>7.4) and (MPH>20.0)", to know the current speed might make implementing the Auto Learn feature easier. Counting the number of tacho pulses within a defined period of time, and eventually converting this into MPH can fairly easy be done with the SX, and I think Ken already wrote code for this purpose.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"What's the difference between ignorance and apathy ?"
"I don't know, and I don't care."
FYI, the ratio in 1st gear will be the highest, so the SpeedCnt IF's need a minor rework, but I get the idea.