Hesu-eco
/
limiteur_vitesse
premiere ebauche
main.cpp
- Committer:
- shovelcat
- Date:
- 2018-10-11
- Revision:
- 0:011f69c1276c
- Child:
- 1:c6b4ccebd483
File content as of revision 0:011f69c1276c:
#include "mbed.h" /* ############################## Constants ############################## */ const float ADC_INPUT_MAX_VALUE = 5; const float ADC_OUTPUT_MAX_VALUE = 5; const float PEDAL_HI_MIN_VALUE = 0.48; const float PEDAL_HI_MAX_VALUE = 2.96; const float PEDAL_LO_MIN_VALUE = 0.25; const float PEDAL_LO_MAX_VALUE = 1.48; /* ############################## Static variables and functions ############################## */ static float __reference_speed = 0; static float __measured_speed = 0; static void setReferenceSpeed(const float speed) { // TODO insert mutex here __reference_speed = speed; } static void setMeasuredSpeed(const float speed) { // TODO insert mutex here __measured_speed = speed; } static float getReferenceSpeed() { // TODO insert mutex here return __reference_speed; } static float getMeasuredSpeed() { // TODO insert mutex here return __measured_speed; } /* ############################## uC I/O ############################## */ AnalogIn pedal_in_hi(A0); AnalogIn pedal_in_lo(A1); AnalogOut pedal_out_hi(A2); AnalogOut pedal_out_lo(A3); DigitalOut led(LED1); /* ############################## Utility functions ############################## */ // Returns 'value' bounded between 'lowerBound' and 'upperBound' float boundValue(float value, const float lowerBound, const float upperBound) { if(value < lowerBound) { value = lowerBound; } else if(value > upperBound) { value = upperBound; } return value; } // Returns "value/reference" as a percentage in decimal form (0.5 for 50%) float toDecimalPercent(const float value, const float reference) { return value/reference; } // Returns voltage read on analog input port chosen for pedal input 1 float readAdcPedalHi() { const float decPcValue = pedal_in_hi.read(); const float voltage = decPcValue * ADC_INPUT_MAX_VALUE; return voltage; } // Returns voltage read on analog input port chosen for pedal input 2 float readAdcPedalLo() { const float decPcValue = pedal_in_lo.read(); const float voltage = decPcValue * ADC_INPUT_MAX_VALUE; return voltage; } // Accepts a value in volts, converts to % and sets ADC for pedal output 1 void writeAdcPedalHi(const float voltage) { const float boundedValue = boundValue(voltage, PEDAL_HI_MIN_VALUE, PEDAL_HI_MAX_VALUE); const float decPcValue = toDecimalPercent(boundedValue, ADC_OUTPUT_MAX_VALUE); pedal_out_hi.write(decPcValue); } // Accepts a value in volts, converts to % and sets ADC for pedal output 2 void writeAdcPedalLo(const float voltage) { const float boundedValue = boundValue(voltage, PEDAL_LO_MIN_VALUE, PEDAL_LO_MAX_VALUE); const float decPcValue = toDecimalPercent(boundedValue, ADC_OUTPUT_MAX_VALUE); pedal_out_lo.write(decPcValue); } // main transfer function void controlFunctionCallback() { // const float Ki = 1.0; // const float Kp = 1.0; // const float Kc = 1.0; // // const float Vref = getReferenceSpeed(); // const float Vmes = getMeasuredSpeed(); // const float err = Vref - Vmes; // const float errKi = err * Ki; // const float err2 = errKi - Vmes; // const float Ud = err2 * Kp * Kc; const float udHi = readAdcPedalHi(); const float udLo = readAdcPedalLo(); writeAdcPedalHi(udHi); writeAdcPedalLo(udLo); } int main() { printf("\nAnalogIn example\n"); setReferenceSpeed(0); setMeasuredSpeed(0); printf("%f", getReferenceSpeed()); printf("%f", getMeasuredSpeed()); while(1) { controlFunctionCallback(); led.write(!led.read()); wait(0.2); // 200 ms } }