Firmware for an Android accessory electric bicycle. See http://www.danielcasner.org/tag/ebike/ for some more information on my build.

Dependencies:   AndroidAccessory mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers throttle.h Source File

throttle.h

00001 #ifndef _throttle_h_
00002 #define _throttle_h_
00003 
00004 #include "mbed.h"
00005 
00006 /** Enumeration of different throttle operation modes. 
00007  * Determines how the throttle output is calculated from
00008  * all possible inputs.
00009  */
00010 typedef enum ThrottleMode {
00011     off = 0, /// Throttle output is 0
00012     raw = 1, /// Throttle output is throttle input (possibly with calibrated curve)
00013     torque = 2, /// Throttle output runs a PID loop on thorque output
00014     cruise_raw, /// Throttle output is held to grip throttle at time cruise was set
00015     cruise_speed, /// Throttle output runs cruise control PID on speed.
00016     cruise_torque, /// Throttle output runs cruise control PID on torque.
00017     droid, /// Throttle output is controlled by attached Android.
00018     calibrate_speed  = 0x55, /// Throttle to speed calibration mode
00019     calibrate_torque = 0x56, /// Throttle to torque calibration mode
00020     calibrate_input  = 0x57, /// Grip throttle calibration mode
00021 } ThrottleMode;
00022 
00023 /** State machine for running cruise control functions
00024  */
00025 typedef enum CruiseState {
00026     waiting, /// Waiting for a valid precondition to be met to start running cruise control
00027     transition, /// A transition or ramp between cruise off and cruise on is in progress
00028     running, /// Full cruise control operating    
00029     inhibited, /// Something (breaks) cause the cruise control to be inhibited
00030 } CruiseState;
00031 
00032 /** A singleton class representing all aspects of throttle control (input and output)
00033  * as well as regenerative breaking function and break inhibits.
00034  */
00035 class Throttle {
00036 public:
00037     /** Get the singleton instance
00038      * All variables will be used asynchronously so they must be available throughout the program
00039      * @param I Motor current in Amps
00040      * @param v_f Front wheel speed in RPM
00041      * @param v_r Rear  wheel speed in RPM
00042      * @param cadence   cadence in RPM
00043      * @param break_l Left break input
00044      * @param break_r Right break input
00045      * @return A pointer to the singleton Throttle object
00046      */
00047     static Throttle *getThrottle(float *I, float *v_f, float *v_r, float *cadence, AnalogIn* break_l, AnalogIn* break_r);
00048     /// Set the throttle control mode
00049     void setMode(ThrottleMode m);
00050     /// Set a limit on motor current for closed loop regulation
00051     void setILimit(float I);
00052     /** Set a speed limit
00053      * @param v Speed in RPM
00054      * @param enforce if true, regenerative breaking will be used to slow the
00055      *        bike if it's going over the speed limit with the motor off.
00056      */
00057     void setSpeedLimit(float v, bool enforce=false);
00058     /// Set the external input (from Droid)
00059     void input(float target);
00060 
00061 private:
00062     /// Private constructor
00063     Throttle(float *I, float *v_f, float *v_r, float *cadence, AnalogIn* break_l, AnalogIn* break_r);
00064     /// and destructor
00065     ~Throttle();
00066     /// Control loop tick
00067     void onTick();
00068 
00069     // The singleton instance
00070     static Throttle* instance;
00071 
00072     Ticker tick;
00073     ThrottleMode mode;
00074     CruiseState state;
00075     float *I, *v_f, *v_r, *cadence;
00076     AnalogIn *brkl, *brkr;
00077     float target, iLimit, speedLimit;
00078     bool enforceSpeedLimit;
00079     
00080     static const float break_inhibit_threshold = 0.1; /// The thrshold at which the breaks cut off the throttle
00081 };
00082 
00083 
00084 #endif