Firmware for an Android accessory electric bicycle. See http://www.danielcasner.org/tag/ebike/ for some more information on my build.
Dependencies: AndroidAccessory mbed
throttle.h@3:dc564aaf8a81, 2012-08-25 (annotated)
- Committer:
- DanielC
- Date:
- Sat Aug 25 20:09:35 2012 +0000
- Revision:
- 3:dc564aaf8a81
- Parent:
- 2:e2c3c7340fb3
A first public commit. This isn't working code yet but I think it's enough to share to show the structure of what I am developing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DanielC | 2:e2c3c7340fb3 | 1 | #ifndef _throttle_h_ |
DanielC | 2:e2c3c7340fb3 | 2 | #define _throttle_h_ |
DanielC | 2:e2c3c7340fb3 | 3 | |
DanielC | 2:e2c3c7340fb3 | 4 | #include "mbed.h" |
DanielC | 2:e2c3c7340fb3 | 5 | |
DanielC | 2:e2c3c7340fb3 | 6 | /** Enumeration of different throttle operation modes. |
DanielC | 2:e2c3c7340fb3 | 7 | * Determines how the throttle output is calculated from |
DanielC | 2:e2c3c7340fb3 | 8 | * all possible inputs. |
DanielC | 2:e2c3c7340fb3 | 9 | */ |
DanielC | 2:e2c3c7340fb3 | 10 | typedef enum ThrottleMode { |
DanielC | 2:e2c3c7340fb3 | 11 | off = 0, /// Throttle output is 0 |
DanielC | 2:e2c3c7340fb3 | 12 | raw = 1, /// Throttle output is throttle input (possibly with calibrated curve) |
DanielC | 2:e2c3c7340fb3 | 13 | torque = 2, /// Throttle output runs a PID loop on thorque output |
DanielC | 2:e2c3c7340fb3 | 14 | cruise_raw, /// Throttle output is held to grip throttle at time cruise was set |
DanielC | 2:e2c3c7340fb3 | 15 | cruise_speed, /// Throttle output runs cruise control PID on speed. |
DanielC | 2:e2c3c7340fb3 | 16 | cruise_torque, /// Throttle output runs cruise control PID on torque. |
DanielC | 2:e2c3c7340fb3 | 17 | droid, /// Throttle output is controlled by attached Android. |
DanielC | 2:e2c3c7340fb3 | 18 | calibrate_speed = 0x55, /// Throttle to speed calibration mode |
DanielC | 2:e2c3c7340fb3 | 19 | calibrate_torque = 0x56, /// Throttle to torque calibration mode |
DanielC | 2:e2c3c7340fb3 | 20 | calibrate_input = 0x57, /// Grip throttle calibration mode |
DanielC | 2:e2c3c7340fb3 | 21 | } ThrottleMode; |
DanielC | 2:e2c3c7340fb3 | 22 | |
DanielC | 2:e2c3c7340fb3 | 23 | /** State machine for running cruise control functions |
DanielC | 2:e2c3c7340fb3 | 24 | */ |
DanielC | 2:e2c3c7340fb3 | 25 | typedef enum CruiseState { |
DanielC | 2:e2c3c7340fb3 | 26 | waiting, /// Waiting for a valid precondition to be met to start running cruise control |
DanielC | 2:e2c3c7340fb3 | 27 | transition, /// A transition or ramp between cruise off and cruise on is in progress |
DanielC | 2:e2c3c7340fb3 | 28 | running, /// Full cruise control operating |
DanielC | 2:e2c3c7340fb3 | 29 | inhibited, /// Something (breaks) cause the cruise control to be inhibited |
DanielC | 2:e2c3c7340fb3 | 30 | } CruiseState; |
DanielC | 2:e2c3c7340fb3 | 31 | |
DanielC | 2:e2c3c7340fb3 | 32 | /** A singleton class representing all aspects of throttle control (input and output) |
DanielC | 2:e2c3c7340fb3 | 33 | * as well as regenerative breaking function and break inhibits. |
DanielC | 2:e2c3c7340fb3 | 34 | */ |
DanielC | 2:e2c3c7340fb3 | 35 | class Throttle { |
DanielC | 2:e2c3c7340fb3 | 36 | public: |
DanielC | 2:e2c3c7340fb3 | 37 | /** Get the singleton instance |
DanielC | 2:e2c3c7340fb3 | 38 | * All variables will be used asynchronously so they must be available throughout the program |
DanielC | 2:e2c3c7340fb3 | 39 | * @param I Motor current in Amps |
DanielC | 2:e2c3c7340fb3 | 40 | * @param v_f Front wheel speed in RPM |
DanielC | 2:e2c3c7340fb3 | 41 | * @param v_r Rear wheel speed in RPM |
DanielC | 2:e2c3c7340fb3 | 42 | * @param cadence cadence in RPM |
DanielC | 2:e2c3c7340fb3 | 43 | * @param break_l Left break input |
DanielC | 2:e2c3c7340fb3 | 44 | * @param break_r Right break input |
DanielC | 2:e2c3c7340fb3 | 45 | * @return A pointer to the singleton Throttle object |
DanielC | 2:e2c3c7340fb3 | 46 | */ |
DanielC | 2:e2c3c7340fb3 | 47 | static Throttle *getThrottle(float *I, float *v_f, float *v_r, float *cadence, AnalogIn* break_l, AnalogIn* break_r); |
DanielC | 2:e2c3c7340fb3 | 48 | /// Set the throttle control mode |
DanielC | 2:e2c3c7340fb3 | 49 | void setMode(ThrottleMode m); |
DanielC | 2:e2c3c7340fb3 | 50 | /// Set a limit on motor current for closed loop regulation |
DanielC | 2:e2c3c7340fb3 | 51 | void setILimit(float I); |
DanielC | 2:e2c3c7340fb3 | 52 | /** Set a speed limit |
DanielC | 2:e2c3c7340fb3 | 53 | * @param v Speed in RPM |
DanielC | 2:e2c3c7340fb3 | 54 | * @param enforce if true, regenerative breaking will be used to slow the |
DanielC | 2:e2c3c7340fb3 | 55 | * bike if it's going over the speed limit with the motor off. |
DanielC | 2:e2c3c7340fb3 | 56 | */ |
DanielC | 2:e2c3c7340fb3 | 57 | void setSpeedLimit(float v, bool enforce=false); |
DanielC | 2:e2c3c7340fb3 | 58 | /// Set the external input (from Droid) |
DanielC | 2:e2c3c7340fb3 | 59 | void input(float target); |
DanielC | 2:e2c3c7340fb3 | 60 | |
DanielC | 2:e2c3c7340fb3 | 61 | private: |
DanielC | 2:e2c3c7340fb3 | 62 | /// Private constructor |
DanielC | 2:e2c3c7340fb3 | 63 | Throttle(float *I, float *v_f, float *v_r, float *cadence, AnalogIn* break_l, AnalogIn* break_r); |
DanielC | 2:e2c3c7340fb3 | 64 | /// and destructor |
DanielC | 2:e2c3c7340fb3 | 65 | ~Throttle(); |
DanielC | 2:e2c3c7340fb3 | 66 | /// Control loop tick |
DanielC | 2:e2c3c7340fb3 | 67 | void onTick(); |
DanielC | 2:e2c3c7340fb3 | 68 | |
DanielC | 2:e2c3c7340fb3 | 69 | // The singleton instance |
DanielC | 2:e2c3c7340fb3 | 70 | static Throttle* instance; |
DanielC | 2:e2c3c7340fb3 | 71 | |
DanielC | 2:e2c3c7340fb3 | 72 | Ticker tick; |
DanielC | 2:e2c3c7340fb3 | 73 | ThrottleMode mode; |
DanielC | 2:e2c3c7340fb3 | 74 | CruiseState state; |
DanielC | 2:e2c3c7340fb3 | 75 | float *I, *v_f, *v_r, *cadence; |
DanielC | 2:e2c3c7340fb3 | 76 | AnalogIn *brkl, *brkr; |
DanielC | 2:e2c3c7340fb3 | 77 | float target, iLimit, speedLimit; |
DanielC | 2:e2c3c7340fb3 | 78 | bool enforceSpeedLimit; |
DanielC | 2:e2c3c7340fb3 | 79 | |
DanielC | 2:e2c3c7340fb3 | 80 | static const float break_inhibit_threshold = 0.1; /// The thrshold at which the breaks cut off the throttle |
DanielC | 2:e2c3c7340fb3 | 81 | }; |
DanielC | 2:e2c3c7340fb3 | 82 | |
DanielC | 2:e2c3c7340fb3 | 83 | |
DanielC | 2:e2c3c7340fb3 | 84 | #endif |