premiere ebauche

Dependencies:   mbed PinDetect

Revision:
3:4da392d2bae8
Parent:
2:06f128641b62
Child:
4:a8c9f6a13633
--- a/speedlimiter.hpp	Thu Oct 18 02:20:14 2018 +0000
+++ b/speedlimiter.hpp	Thu Oct 18 17:31:03 2018 +0000
@@ -2,17 +2,83 @@
 author: Sebastian Pelchat
 date: october 2018
 */
+#ifndef SPEED_LIMITER_HPP
+#define SPEED_LIMITER_HPP
+
 #include "mbed.h"
 
-class SpeedLimiter 
+class SpeedLimiter
 {
+    static const float TRANSFER_FUNCTION_PERIOD = 0.1; // seconds
+    static const float DISABLE_ECO_ALGO_TRIGGER = 999; // TODO: probably defined somewhere else.
+
+    static const float ADC_INPUT_MAX_VALUE = 3.3; // Volts
+    static const float ADC_OUTPUT_MAX_VALUE = 3.3; // Volts
+
+    static const float PEDAL_HI_MIN_VALUE = 0.48; // Volts
+    static const float PEDAL_HI_MAX_VALUE = 2.96; // Volts
+    static const float PEDAL_LO_MIN_VALUE = PEDAL_HI_MIN_VALUE / 2;
+    static const float PEDAL_LO_MAX_VALUE = PEDAL_HI_MAX_VALUE / 2;
+    
+    static const float TORQUE_MAX = 20.0; // Newtons
+    
+    static const float SPEED_MAX = 40.0;
+
 public:
     SpeedLimiter(const PinName& pedalInHi, const PinName& pedalInLo, const PinName& pedalOutHi, const PinName& pedalOutLo);
+    ~SpeedLimiter();
+    void start();
 
-
+    inline float getReferenceSpeed() {
+        _mutex->lock();
+        float retval = _referenceSpeed;
+        _mutex->unlock();
+        return retval;
+    }
+    inline float getMeasuredSpeed() {
+        _mutex->lock();
+        float retval = _measuredSpeed;
+        _mutex->unlock();
+        return retval;
+    }
+    inline void setReferenceSpeed(const float speed) {
+        _mutex->lock();
+        _referenceSpeed = speed;
+        _mutex->unlock();
+    }
+    inline void setMeasuredSpeed(const float speed) {
+        _mutex->lock();
+        _measuredSpeed = speed;
+        _mutex->unlock();
+    }
+    
+    
+    
 private:
-
-
+    float readAdcPedalHi();
+    float readAdcPedalLo();
+    void writeAdcPedalHi(const float voltage);
+    void writeAdcPedalLo(const float voltage);
+    float ecoDisabledAlgorithm();
+    float ecoEnabledAlgorithm();
+    void tickerCallback();
+    void ipControllerTransferFunction();
+    
+    float boundValue(float value, const float lowerBound, const float upperBound);
+    float voltageToDecimal(const float decimal, const float reference);
+    
+    inline void setOutputPedalVoltageHi(const float voltage) {
+        _outputPedalVoltageHi = voltage;
+    }
+    inline void setOutputPedalVoltageLo(const float voltage) {
+        _outputPedalVoltageLo = voltage;
+    }
+    inline float getOutputPedalVoltageHi() {
+        return _outputPedalVoltageHi;
+    }
+    inline float getOutputPedalVoltageLo() {
+        return _outputPedalVoltageLo;
+    }
 
 protected:
     AnalogIn    _pedalInHi;
@@ -20,6 +86,15 @@
     AnalogOut   _pedalOutHi;
     AnalogOut   _pedalOutLo;
 
-    
+    volatile float _referenceSpeed;
+    volatile float _measuredSpeed;
+    volatile float _outputPedalVoltageHi;
+    volatile float _outputPedalVoltageLo;
+
+    Ticker* _ticker;
+    Mutex* _mutex;
     
-};
\ No newline at end of file
+    static Serial* pc; // for communication / debugging
+};
+
+#endif
\ No newline at end of file