View Single Post
Old 06-11-2020, 04:15 PM   #33
Second Series
Registered User
 
Second Series's Avatar
 
Join Date: May 2015
Location: Tukwila Washington
Posts: 373
Re: Mechanical speedometer drive solution

I’m still working on the code. The code overall is not too complex, and I’m trying to keep it as simple as possible. I am able to drive the speedometer across the entire range from Zero to 90MPH. I use the motor as a stepper below 25MPH. At 25MPH and above the motor should run with the PID control.
In an attempt to keep the code simple, I am using the MAP command to set the range of Input frequency vs. Microsecond delay(between holes in the optical disk) for the stepper motor code. The line looks like this: val2 = map(val2, 1, 14, 25, 1);// Input Range(Hz) - Output Range(uS)
Where val2 is the delay time, with an input from the 2000 pulse per mile signal of 1Hz, the delay is 25uS between 3 holes in the 64 hole optical disk. With an input of 14Hz, the delay is 1uS. The input range is scaled to the output range, and since the two ranges are not equal, or an even fraction, the output is granular. That is to say there are 14 steps from 1MPH to 25MPH. The limiting factor here is the MAP command as it does not handle floating point integers. If I want finer detail, I could code an array, or something, I’m not sure, but it would be more complex. As it is, the slow speed is working well, and I still need to dial it in.
The speed above 25MPH. I’m thinking about using the MAP command here also. The PWM is a range from 1 to 255. My simple code uses the equation (freqency x 5.1) to get the PWM signal. I found that with the speedometer connected to the motor, the maximum PWM of 255 drives the speedo at 90MPH. The 2000 pulse per mile signal will be 50Hz at 90MPH, so 255/50 = 5.1. Unfortunately when I plug a lower frequency into this equation, say 15Hz x 5.1 = 76.5 for the PWM signal. The motor won’t turn at that PWM signal, it will turn at 20MPH with 120 PWM. So the motor has more torque at higher speeds, DUH! I need a multiplier of about 9 with the input of 15Hz, so the MAP code would be something like this: val3 = map(val3, 15, 50, 9, 5);// Input Range(Hz) - Output Range(multiplier). So for the input frequency range of 15Hz to 50Hz, the variable(val3) would be 9 to 5. That variable could be used in the equation to determine what the PWM signal should be. There is another equation that determines what the optical disk frequency should be, and is used to adjust the PWM signal, that is the PID control. That needs to be fine tuned as well.


Well, the MAP command doesn’t work well here. I’ll focus on getting the PID code solid.
It turns out that I know nothing about PID programming. I did find some useful information about it, I could make a career of learning how to implement it. I struggled with the code that I do have for awhile, and found a simple solution based on a PID concept. PID samples the system parameters, put that data in a buffer for a set period of time, samples again and compares the stored data to the current data. I figured out how to write the code to achieve this, and based on the difference between the previous input signal and the current input signal, I change the multiplier by a set amount. That line of code is: change = (diff * .09). Then I determine if the input frequency has increased, or decreased, and add, or subtract “change” from the multiplier val3. Again, the output PWM is determined by multiplying the input frequency by val3. With this bit of code, I get a multiplier at val3 = 9 when the input frequency is 15Hz, and val3 = 5 @ 50Hz. Now I have the desired output of 25MPH with an input of 15Hz, and 90MPH with input of 50Hz. Unfortunately the multiplier is linear, so the intermediate readings are off. I need to put a curve on the multiplier. I know what the results should be, so I can create a graph. From the graph I’ll have to come up with an equation and put that in the code. I remember a math class that had me creating graphs from equations, so I know it’s possible to draw a curved line and determine an equation equal to the curved line. It’s been a decade or two… After I get this working, I’ll get on with figuring out PID programming. Ultimately I’ll need to have the feedback as I am seeing problems with my setup. I found that the motor can get bogged down if it is not lined up exactly with the speedometer. My linkage is not precision, and the bracket is flimsy, if I bump it the motor shifts slightly, and the code is thrown off. When it is aligned well, the speedometer spins freely. I’ll need to come up with a robust bracket to overcome that. Using PID would also adjust for mechanical variations.
__________________
'47 Panel to '88 K2500 Frame Swap
Mechanical Speedometer Drive Solution
1947.2 1 ton Chevy Panel
1955.2 Chevy 6700 Bus/RV
1990 Chevy K1500
Second Series is offline   Reply With Quote