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
Update 17th August 2020 Radio control inputs completed
brushless_motor.h@10:e40d8724268a, 2019-01-15 (annotated)
- Committer:
- JonFreeman
- Date:
- Tue Jan 15 09:03:57 2019 +0000
- Revision:
- 10:e40d8724268a
- Child:
- 11:bfb73f083009
Buggered serial comms to TS controller
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JonFreeman | 10:e40d8724268a | 1 | /* mbed brushless_motor library |
| JonFreeman | 10:e40d8724268a | 2 | Jon Freeman |
| JonFreeman | 10:e40d8724268a | 3 | December 2018 |
| JonFreeman | 10:e40d8724268a | 4 | */ |
| JonFreeman | 10:e40d8724268a | 5 | #ifndef MBED_BRUSHLESSMOTOR_H |
| JonFreeman | 10:e40d8724268a | 6 | #define MBED_BRUSHLESSMOTOR_H |
| JonFreeman | 10:e40d8724268a | 7 | |
| JonFreeman | 10:e40d8724268a | 8 | #include "mbed.h" |
| JonFreeman | 10:e40d8724268a | 9 | |
| JonFreeman | 10:e40d8724268a | 10 | class brushless_motor |
| JonFreeman | 10:e40d8724268a | 11 | { |
| JonFreeman | 10:e40d8724268a | 12 | uint32_t Hall_total, visible_mode, inner_mode, edge_count_table[MAIN_LOOP_ITERATION_Hz]; // to contain one seconds worth |
| JonFreeman | 10:e40d8724268a | 13 | uint32_t latest_pulses_per_sec, Hall_tab_ptr, direction, ppstmp; |
| JonFreeman | 10:e40d8724268a | 14 | bool moving_flag; |
| JonFreeman | 10:e40d8724268a | 15 | const uint16_t * lut; |
| JonFreeman | 10:e40d8724268a | 16 | uint16_t ttabl[34]; |
| JonFreeman | 10:e40d8724268a | 17 | FastPWM * maxV, * maxI; |
| JonFreeman | 10:e40d8724268a | 18 | PortOut * Motor_Port; |
| JonFreeman | 10:e40d8724268a | 19 | InterruptIn * Hall1, * Hall2, * Hall3; |
| JonFreeman | 10:e40d8724268a | 20 | public: |
| JonFreeman | 10:e40d8724268a | 21 | bool dc_motor; |
| JonFreeman | 10:e40d8724268a | 22 | struct currents { |
| JonFreeman | 10:e40d8724268a | 23 | uint32_t max, min, ave; |
| JonFreeman | 10:e40d8724268a | 24 | } I; |
| JonFreeman | 10:e40d8724268a | 25 | int32_t angle_cnt; |
| JonFreeman | 10:e40d8724268a | 26 | uint32_t current_samples[CURRENT_SAMPLES_AVERAGED]; // Circular buffer where latest current readings get stored |
| JonFreeman | 10:e40d8724268a | 27 | uint32_t Hindex[2], tickleon, encoder_error_cnt; |
| JonFreeman | 10:e40d8724268a | 28 | uint32_t RPM, PPS; |
| JonFreeman | 10:e40d8724268a | 29 | double last_V, last_I; |
| JonFreeman | 10:e40d8724268a | 30 | brushless_motor () {} ; // Default constructor |
| JonFreeman | 10:e40d8724268a | 31 | /** |
| JonFreeman | 10:e40d8724268a | 32 | brushless_motor (PortOut * , FastPWM * , FastPWM * , const uint16_t *, InterruptIn **) ; |
| JonFreeman | 10:e40d8724268a | 33 | |
| JonFreeman | 10:e40d8724268a | 34 | PortOut * is &MotA where MotA has been declared as below where PORT_A_MASK identifies which 6 bits of 16 bit port are used to energise motor |
| JonFreeman | 10:e40d8724268a | 35 | PortOut MotA (PortA, PORT_A_MASK); // Activate output ports to motor drivers |
| JonFreeman | 10:e40d8724268a | 36 | |
| JonFreeman | 10:e40d8724268a | 37 | FastPWM * are e.g. &A_MAX_V_PWM, &A_MAX_I_PWM |
| JonFreeman | 10:e40d8724268a | 38 | |
| JonFreeman | 10:e40d8724268a | 39 | const uint16_t * is e.g. A_tabl defining which port bits set to which level |
| JonFreeman | 10:e40d8724268a | 40 | const uint16_t A_tabl[] = { // Origial table |
| JonFreeman | 10:e40d8724268a | 41 | 0, 0, 0, 0, 0, 0, 0, 0, // Handbrake |
| JonFreeman | 10:e40d8724268a | 42 | 0, AWV,AVU,AWU,AUW,AUV,AVW,AUW, // Forward 0, WV1, VU1, WU1, UW1, UV1, VW1, 0, // JP, FR, SG, PWM = 1 0 1 1 Forward1 |
| JonFreeman | 10:e40d8724268a | 43 | 0, AVW,AUV,AUW,AWU,AVU,AWV,AWU, // Reverse 0, VW1, UV1, UW1, WU1, VU1, WV1, 0, // JP, FR, SG, PWM = 1 1 0 1 Reverse1 |
| JonFreeman | 10:e40d8724268a | 44 | 0, BRA,BRA,BRA,BRA,BRA,BRA,BRA, // Regenerative Braking |
| JonFreeman | 10:e40d8724268a | 45 | KEEP_L_MASK_A, KEEP_H_MASK_A // [32 and 33] |
| JonFreeman | 10:e40d8724268a | 46 | } ; |
| JonFreeman | 10:e40d8724268a | 47 | |
| JonFreeman | 10:e40d8724268a | 48 | InterruptIn ** is e.g. AHarr pointer to array of addresses of InterruptIns connected to Hall sensors |
| JonFreeman | 10:e40d8724268a | 49 | InterruptIn * AHarr[] = { |
| JonFreeman | 10:e40d8724268a | 50 | &MAH1, |
| JonFreeman | 10:e40d8724268a | 51 | &MAH2, |
| JonFreeman | 10:e40d8724268a | 52 | &MAH3 |
| JonFreeman | 10:e40d8724268a | 53 | } ; |
| JonFreeman | 10:e40d8724268a | 54 | |
| JonFreeman | 10:e40d8724268a | 55 | */ |
| JonFreeman | 10:e40d8724268a | 56 | brushless_motor (PortOut * , FastPWM * , FastPWM * , const uint16_t *, InterruptIn **) ; |
| JonFreeman | 10:e40d8724268a | 57 | void set_V_limit (double) ; // Sets max motor voltage |
| JonFreeman | 10:e40d8724268a | 58 | void set_I_limit (double) ; // Sets max motor current |
| JonFreeman | 10:e40d8724268a | 59 | void Hall_change () ; // Called in response to edge on Hall input pin |
| JonFreeman | 10:e40d8724268a | 60 | void motor_set () ; // Energise Port with data determined by Hall sensors |
| JonFreeman | 10:e40d8724268a | 61 | void direction_set (int) ; // sets 'direction' with bit pattern to eor with FORWARD or REVERSE in set_mode |
| JonFreeman | 10:e40d8724268a | 62 | bool set_mode (int); // sets mode to HANDBRAKE, FORWARD, REVERSE or REGENBRAKE |
| JonFreeman | 10:e40d8724268a | 63 | bool is_moving () ; // Returns true if one or more Hall transitions within last 31.25 milli secs |
| JonFreeman | 10:e40d8724268a | 64 | void current_calc () ; // Updates 3 uint32_t I.min, I.ave, I.max |
| JonFreeman | 10:e40d8724268a | 65 | uint32_t pulses_per_sec () ; // call this once per main loop pass to keep count = edges per sec |
| JonFreeman | 10:e40d8724268a | 66 | int read_Halls () ; // Returns 3 bits of latest Hall sensor outputs |
| JonFreeman | 10:e40d8724268a | 67 | bool motor_is_brushless (); |
| JonFreeman | 10:e40d8724268a | 68 | void high_side_off () ; |
| JonFreeman | 10:e40d8724268a | 69 | void low_side_on () ; |
| JonFreeman | 10:e40d8724268a | 70 | void raw_V_pwm (int); |
| JonFreeman | 10:e40d8724268a | 71 | } ; //MotorA, MotorB, or even Motor[2]; |
| JonFreeman | 10:e40d8724268a | 72 | |
| JonFreeman | 10:e40d8724268a | 73 | #endif |