Dual Brushless Motor ESC, 10-62V, up to 50A per motor. Motors ganged or independent, multiple control input methods, cycle-by-cycle current limit, speed mode and torque mode control. Motors tiny to kW. Speed limit and other parameters easily set in firmware. As used in 'The Brushless Brutalist' locomotive - www.jons-workshop.com. See also Model Engineer magazine June-October 2019.

Dependencies:   mbed BufferedSerial Servo PCT2075 FastPWM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STM3_ESC.h Source File

STM3_ESC.h

00001 /*
00002     STM3_ESC    Electronic Speed Controller board, drives Two Brushless Motors, full Four Quadrant Control.
00003     Jon Freeman  B. Eng Hons
00004     2015 - 2020
00005 */
00006 #include "mbed.h"
00007 
00008 #ifndef MBED_DUALBLS_H
00009 #define MBED_DUALBLS_H
00010 
00011 //#define USING_DC_MOTORS     //  NO LONGER SUPPORTED Uncomment this to play with Dinosaur DC motors - WARNING deprecated feature
00012 
00013 //#define TEMP_SENSOR_ENABLE    //   - WARNING deprecated feature, sensor chosen imposed heavy burden on cpu, future looks to simpler analogue type
00014 
00015 #include "BufferedSerial.h"
00016 const   uint32_t    MOTOR_HANDBRAKE   = 0,
00017                     MOTOR_FORWARD     = 8,
00018                     MOTOR_REVERSE     = 16,
00019                     MOTOR_REGENBRAKE  = 24;
00020 
00021 const   uint32_t    TIMEOUT_SECONDS = 2,
00022 
00023 /*  Please Do Not Alter these */
00024                 PWM_PRESECALER_DEFAULT      = 2,
00025                 VOLTAGE_READ_INTERVAL_US    = 50,       //  Interrupts timed every 50 micro sec, runs around loop performing 1 A-D conversion per pass
00026                 MAIN_LOOP_REPEAT_TIME_US    = 31250,    //  31250 us, with TACHO_TAB_SIZE = 32 means tacho_ticks_per_time is tacho_ticks_per_second
00027                 MAIN_LOOP_ITERATION_Hz      = 1000000 / MAIN_LOOP_REPEAT_TIME_US,
00028                 PWM_HZ              = 15000,    //  chosen to be above cutoff frequency of average human ear
00029 //                PWM_HZ              = 8000,    //  chosen to be above cutoff frequency of average human ear - clearly audible annoying noise
00030                 MAX_PWM_TICKS       = (SystemCoreClock / (PWM_HZ * PWM_PRESECALER_DEFAULT)),
00031 //                TICKLE_TIMES    =   100 ,
00032                 TICKLE_TIMES    =   10 ,    //  Massively reduced May 2020 in connection with handbrake implementation.
00033                 WATCHDOG_RELOAD = (TIMEOUT_SECONDS * 8);    //  WatchDog counter ticked down in 8Hz loop
00034 
00035 const   double      PI      = 4.0 * atan(1.0),
00036                     TWOPI   = 8.0 * atan(1.0);
00037 /*  End of Please Do Not Alter these */
00038 
00039 enum    {COM_SOURCES, COM1, COM2, HAND, RC_IN1, RC_IN2, RC_IN_BOTH, THEEND}  ;   //  RC_IN_BOTH new Dec 2019
00040 
00041     //  List user settable firmware bytes in EEROM
00042 enum    {MOTADIR, MOTBDIR, MOTAPOLES, MOTBPOLES, ISHUNTA, ISHUNTB, POT_REGBRAKE_RANGE, SVO1, SVO2, RCIN1, RCIN2, 
00043             COMM_SRC, BOARD_ID, TOP_SPEED, WHEELDIA, MOTPIN, WHEELGEAR, 
00044             RCI1_TRIM, RCI2_TRIM, RCIN_REGBRAKE_RANGE, RCIN_STICK_ATTACK,     //  RC in trims new Dec 2019
00045             RCIN1REVERSE, RCIN2REVERSE, NOM_SYSTEM_VOLTS, BRAKE_EFFECTIVENESS, BAUD, FUT11,
00046             FUT12, FUT13, FUT14, FUT16,}  ;  //  These represent byte address offsets in 24LC64 rom user settable firmware settings
00047 
00048 enum    {    //  List of fault numbers currently dealt with by error handler
00049     FAULT_0,
00050     FAULT_EEPROM,
00051     FAULT_BOARD_ID,
00052     FAULT_COM_LINE_LEN,
00053     FAULT_COM_LINE_NOMATCH,
00054     FAULT_COM_LINE_LEN_PC,
00055     FAULT_COM_LINE_LEN_TS,
00056     FAULT_COM_LINE_NOMATCH_PC,
00057     FAULT_COM_LINE_NOMATCH_TS,
00058     FAULT_UNRECOGNISED_STATE,
00059     FAULT_MAX,
00060     NUMOF_REPORTABLE_TS_ERRORS
00061     }  ;    //  List of fault numbers currently dealt with by error handler
00062 
00063 class   error_handling_Jan_2019
00064 {
00065     int32_t    ESC_fault[NUMOF_REPORTABLE_TS_ERRORS]    ;   //  Some number of reportable error codes, accessible through set and read members
00066     public:
00067     error_handling_Jan_2019 ()  {   //  default constructor
00068         for (int i = 0; i < (sizeof(ESC_fault) / sizeof(int32_t)); i++)
00069             ESC_fault[i] = 0;
00070     }
00071     void        set   (uint32_t, int32_t)   ;
00072     void        clr     (uint32_t)  ;
00073     uint32_t    read  (uint32_t)   ;
00074     bool        all_good    ()  ;
00075     void        report_any  (bool)  ;   //  retain ? true or false
00076 }   ;
00077 
00078 enum    {SOURCE_PC, SOURCE_TS}  ;
00079 const   int BROADCAST   = '\r';
00080 const   int MAX_CLI_PARAMS = 12;
00081 const   int MAX_CMD_LEN = 220;
00082 
00083 struct  parameters  {   //  Used in serial comms with pc and other controller (e.g. touch-screen)
00084     struct kb_command const * command_list;
00085     BufferedSerial * com;   //  pc or com2
00086     int32_t position_in_list, numof_dbls, target_unit, source, numof_menu_items;
00087     double  dbl[MAX_CLI_PARAMS];
00088     bool    respond, resp_always;
00089 }   ;
00090 
00091 enum    {ZONE_BRAKE, ZONE_COAST, ZONE_DRIVE}  ;
00092 class  RC_stick_info   {   //  info read concerning stick positions
00093 public:
00094     double  raw,            //  range 0.0 to 1.0 after clipping and correction
00095             deflection;     //  how far from centre or min positon
00096     double  brake_effort,   //  braking effort
00097             drive_effort;   //  driving effort
00098     uint32_t    zone;       //  in drive, coast or braking zone (if drive, direction in 'stick_implied_motor_direction')
00099     uint32_t    chan_mode;
00100     int32_t     stick_implied_motor_direction;  //  -1, 0, +1 but could be in drive, coast or brake regions
00101     bool    active;
00102     RC_stick_info   ()  {
00103         active = false;
00104         chan_mode = 0;
00105         zone = ZONE_COAST;
00106         drive_effort = brake_effort = 0.0;
00107     }
00108 }   ;
00109 
00110 class   cli_2019    {   //  cli Command Line Interpreter, two off, 1 for pc comms, other touch-screen controller comms
00111     struct  kb_command const * commandlist ;
00112     uint32_t    clindex;
00113     char        cmdline[MAX_CMD_LEN + 8];
00114     char        * cmdline_ptr;
00115     parameters  a   ;
00116 public:
00117     cli_2019    (BufferedSerial * comport, kb_command const * list, uint32_t    list_len, uint32_t  source)  {
00118         a.com           = comport ;
00119         a.command_list = commandlist  = list  ;
00120         a.numof_menu_items  = list_len  ;
00121         a.source        = source    ;
00122         cmdline_ptr = cmdline;
00123         clindex    = 0;
00124         if  (source == SOURCE_PC)
00125             a.resp_always = true;
00126         else
00127             a.resp_always = false;
00128     }  ;
00129     void    core    ()  ;
00130     void    flush   ()  ;   //  Used to clear startup transient garbage
00131 }   ;
00132 
00133 const uint32_t  MAX_I2C_DEVICES = 6;
00134 
00135 class   eeprom_settings {
00136     I2C i2c;
00137     uint32_t    errors;
00138     uint32_t    i2c_device_count;
00139     uint32_t    i2c_device_list[MAX_I2C_DEVICES+1];    //  max 12 i2c devices
00140     char        settings    [36];
00141     double      rpm2mphdbl, user_brake_rangedbl, Vnomdbl, brake_eff, top_speeddbl;
00142     bool        rd_24LC64  (uint32_t start_addr, char * dest, uint32_t length)    ;
00143     bool        wr_24LC64  (uint32_t start_addr, char * dest, uint32_t length)    ;
00144     bool        set_24LC64_internal_address (int    start_addr) ;
00145     bool        ack_poll    ()  ;
00146     void        update_dbls ()  ;
00147   public:
00148     eeprom_settings (PinName sda, PinName scl); //  Constructor
00149     bool        do_we_have_i2c  (uint32_t x)    ;
00150     char        rd  (uint32_t)  ;           //  Read one setup char value from private buffer 'settings'
00151     bool        wr  (char, uint32_t)  ;     //  Write one setup char value to private buffer 'settings'
00152     bool        save    ()  ;               //  Write 'settings' buffer to EEPROM
00153     bool        set_defaults    ();         //  Put default settings into EEPROM and local buffer
00154     uint32_t    errs    ()  ;               //  Return errors
00155     const char *      t   (uint32_t);
00156     uint32_t    min (uint32_t)   ;
00157     uint32_t    max (uint32_t)   ;
00158     uint32_t    def (uint32_t)   ;
00159     bool        in_range   (char val, uint32_t p)  ;
00160     void        edit   (double * dbl, uint32_t numof_dbls)    ;
00161     double      user_brake_range ()  ;
00162     double      brake_effectiveness ()  ;
00163     double      top_speed ()  ;
00164     double      Vnom ()  ;
00165     double      rpm2mph ()  ;
00166     double      rpm2mph (double)  ;
00167     uint32_t    baud    ();
00168 }   ;
00169 
00170 struct  optpar  {
00171     int32_t min, max, de_fault;  //  min, max, default
00172     const char * txt;     //  description
00173 }   ;
00174 
00175 #endif