Good day!
Today I want to continue series of articles about the secondary tasks of my project, next we have the measurement of tension.
As you may remember, in my program there is a variable “gear”, which, as the name implies, is responsible for the gear selected by the rider. I want to set this variable according to the actual tension on pedals of the exercise bike. There are several ways to solve this problem, I propose to consider each of them separately.
Ways of tension measurement
1. The way that is used on my old bike computer – do nothing and assume that the rider always rides in the 6th gear. I believe that this way of measuring is unacceptable, otherwise the article would not have been published today!
2. Manual switching gears using the exercise bike computer buttons. Strangely enough, but from the very beginning, I wanted to switch gears in this way. The drawbacks of this method are obvious, but it has one undeniable plus – when it is used you do not need to make any changes to the design of the exercise bike, for this reason I do not plan to completely abandon it.
3. Determining the position of the tension knob with the help of the rotary switch. This method suggests itself – the tension knob has 8 positions (gears), why not just add one contact group to each position? The reasons for not using this method are several:
- in the body of the exercise bike there is very little space for the installation of the rotary switch;
- from the rotary switch will go 9 wires, which must be pulled through the entire exercise bike;
- these 9 wires still need to be connected to the computer, so we lose a significant part of the free lines of our microcontroller;
- rotary switches do not allow to take into account "fractional" gears (see below).
4. The drawbacks of the rotary switch method do not impact the method for selecting gears by means of a potentiometer. When this method is implemented, the potentiometer is installed closely to the tension mechanism (rather than to the tension knob!) This achieves the maximum measurement accuracy (since in this case there is practically no backlash of the mechanism). The main drawback of the method is the complexity of its implementation, caused by the need to make changes in the design of the exercise bike.
For my experiments today, I decided to implement the 2-nd and 4-th ways, as the most interesting (in my opinion).
Experiment with buttons
The use of buttons is the easiest way to point to the computer in which gear the rider is riding. As already mentioned above, despite all the obvious drawbacks, this method has one important feature – no need to make changes in the design of the exercise bike. And this means that in the future I will be able to transfer my computer to a new exercise bike without any difficulties.
Below I present the code realizing the possibility of switching gears using the SW2 and SW3 buttons on my FRDM-K64FFRDM-K64F board(in the future I will definitely transfer this program to FRDM-KW41ZFRDM-KW41Z
// editing gear value (G) by buttons SW2 and SW3 float button_to_gear(float value) { if (gpio::read(SW2)) // gear UP button { value += 1.0f; while (gpio::read(SW2)) __ASM("NOP"); } if (gpio::read(SW3)) // gear DOWN button { value -= 1.0f; while (gpio::read(SW3)) __ASM("NOP"); } if (value < 1) value = 1.0f; if (value > 8) value = 8.0f; return value; }
Experiment with potentiometer
To conduct this experiment, I specifically asked my colleagues to pick up for me some good potentiometer. They offered me an USSR slider potentiometer [SP3-23A]. This potentiometer has a fairly large size, which simplifies its installation on exercise bike. I secured it by screw connection through a foil-shaped PCB. The rod between the potentiometer stick and the tension mechanism of my exercise bike was made from two large clerical clips welded together:
To carry out the experiment it is necessary to determine the voltage U_adc(G) at the input of the ADC using the formula:
U_adc (G) = R1 (L) / (R1 (L) + R2) * U2
For this we have the following data:
- resistance of resistor R2 = 20 kOhm;
- voltage at the input of circuit U2 = 3.3 V;
- on the first gear (G = 1), the resistance of potentiometer R1(G1) = 2.72 kOhm;
- on the eight gear (G = 8), the resistance of potentiometer R1 (G8) = 9.96 kOhm.
U_adc(G1) = 2.72 / (2.72+20.00) * 3.3 = 0.32 V
U_adc(G8) = 9.96 / (9.96+20.00) * 3.3 = 1.10 V
We get the voltage difference U_adc(G8) – U_adc(G1) of the order of 0.78 V, which corresponds to ~ 22% of the ADC measurement range at a reference voltage level of 3.3 V or ~ 60% at a reference voltage level of 1.2 V.
Now, knowing that our microcontroller has a 16-bit ADC with a reference voltage of 1.2 V, we can determine gear (G) from the ADC value (V). Without going into the details of calculations, I will give the formula I obtained:
G = 0.002629 * V – 1.871
For tests of the developed system I wrote the following program code:
// convert from ADC value to gear value (G) float adc_to_gear(void) { float value = adc::read(ADC_TENSION); value = value*0.002629.0f-1.871f; if (value < 1) value = 1.0f; if (value > 8) value = 8.0f; return value; } // get analog-data from line 'pin' int adc::read(char pin) { // invalid data if (pin != ADC_TENSION) return -1; // 'pin' onfiguration adc16_channel_config_t hadc_ch; hadc_ch.channelNumber = ADC_TENSION_CH; hadc_ch.enableInterruptOnConversionCompleted = false; hadc_ch.enableDifferentialConversion = false; // starting measurements ADC16_SetChannelConfig(ADC_BASE, ADC_TENSION_GR, &hadc_ch); // waiting for new analog value while (!ADC16_GetChannelStatusFlags(ADC_BASE, ADC_TENSION_GR) & kADC16_ChannelConversionDoneFlag) { __ASM("NOP"); } // return measured analog value return ADC16_GetChannelConversionValue(ADC_BASE, ADC_TENSION_GR); }
Note that the variable “gear” now is float type, which means that I do not just define the speed depending on the digit on the tension knob scale, as I did before, I define "speed with a fractional part", because I know for sure in which point the tension mechanism is located.
Conclusion
Today I managed to execute one more item from my plan Maximum – to determine the “gear” in which the rider is riding. The only thing I would like to work on is the rod of the potentiometer. I made it from clerical paper clips and now their stiffness seems to me insufficient. Perhaps in the future I will make it from a welding electrode.
The program obtained in the course of today's experiments, as always, is available in [the official project repository]. If you, for example, have a question, how in my program is made the choice of one or another way of determining tension be sure to check it out!
Thanks for reading and have a nice day!
Top Comments