STM3 ESC dual brushless motor controller. 10-60v, motor power rating tiny to kW. Ganged or independent motor control As used in 'The Brute' locomotive - www.jons-workshop.com
Dependencies: mbed BufferedSerial Servo FastPWM
Diff: brushless_motor.h
- Revision:
- 12:d1d21a2941ef
- Parent:
- 11:bfb73f083009
--- a/brushless_motor.h Sat Jan 19 11:45:01 2019 +0000
+++ b/brushless_motor.h Mon Mar 04 17:51:08 2019 +0000
@@ -7,49 +7,53 @@
#include "mbed.h"
#include "DualBLS.h"
+#include "FastPWM.h"
class brushless_motor
{
int32_t angle_cnt;
- uint32_t Hall_index[2], encoder_error_cnt;
- uint32_t Hall_total, visible_mode, inner_mode, edge_count_table[MAIN_LOOP_ITERATION_Hz]; // to contain one seconds worth
- uint32_t latest_pulses_per_sec, Hall_tab_ptr, direction, ppstmp;
- bool moving_flag;
- const uint16_t * lut;
- uint16_t ttabl[34];
- FastPWM maxV;
- FastPWM maxI;
- InterruptIn H1;
- InterruptIn H2;
- InterruptIn H3;
- PortOut OP;
+ uint32_t Hall_index[2], encoder_error_cnt, motor_poles, current_sense_rs_offset;
+ uint32_t Hall_total, // Incremented on every Hall sensor transition
+ Hall_previous,
+ visible_mode,
+ inner_mode;
+ uint32_t direction;
+ int temp_tick;
+ double RPM_filter, dv_by_dt;
+ double target_speed;
+ bool moving_flag;
+ const uint16_t * lut;
AnalogIn Motor_I;
- void Hall_change () ;
- int read_Halls () ; // Returns 3 bits of latest Hall sensor outputs
+ FastPWM maxV, maxI;
+ InterruptIn H1, H2, H3; // Inputs for motor Hall sensors
+ PortOut OP;
+ void Hall_change () ;
+ int read_Halls () ; // Returns 3 bits of latest Hall sensor outputs
+ uint32_t max_rpm ;
+ double V_clamp ; // Used to limit top speed
+ double numof_current_sense_rs;
public:
+#ifdef USING_DC_MOTORS
bool dc_motor;
- struct currents {
- uint32_t max, min, ave;
- } I;
- uint32_t current_samples[CURRENT_SAMPLES_AVERAGED]; // Circular buffer where latest current readings get stored
- uint32_t cs_ptr;
- uint32_t RPM, PPS, tickleon;
+#endif
+ uint32_t tickleon;
+ double Idbl;
double last_V, last_I;
+ double dRPM, dMPH, s[8];
// brushless_motor () {} ; // can not use this with exotic elements PortOut, FastPWM etc
- brushless_motor (PinName iadc, PinName pwv, PinName pwi, const uint16_t *, PinName h1, PinName h2, PinName h3, PortName, int) ; // Constructor
+ brushless_motor (PinName iadc, PinName pwv, PinName pwi, const uint16_t *, PinName h1, PinName h2, PinName h3, PortName, int, uint32_t) ; // Constructor
+ bool poles (int) ; // Set number of motor poles - 4, 6, or 8
+ void set_speed (double) ; // Sets target_speed
void set_V_limit (double) ; // Sets max motor voltage
void set_I_limit (double) ; // Sets max motor current
void motor_set () ; // Energise Port with data determined by Hall sensors
- void direction_set (int) ; // sets 'direction' with bit pattern to eor with FORWARD or REVERSE in set_mode
- bool set_mode (int); // sets mode to HANDBRAKE, FORWARD, REVERSE or REGENBRAKE
+ void direction_set (int) ; // sets 'direction' with bit pattern to eor with MOTOR_FORWARD or MOTOR_REVERSE in set_mode
+ bool set_mode (int); // sets mode to MOTOR_HANDBRAKE, MOTOR_FORWARD, MOTOR_REVERSE or MOTOR_REGENBRAKE
bool is_moving () ; // Returns true if one or more Hall transitions within last 31.25 milli secs
- void current_calc () ; // Updates 3 uint32_t I.min, I.ave, I.max
- uint32_t pulses_per_sec () ; // call this once per main loop pass to keep count = edges per sec
- bool motor_is_brushless ();
+ void speed_monitor_and_control () ; // call this once per main loop pass (32Hz) to keep count = edges per sec
void high_side_off () ;
- void low_side_on () ;
- void raw_V_pwm (int);
- void sniff_current () ;
+// void low_side_on () ;
+ void sniff_current () ; // Call this every 200us to update Idbl
} ; //MotorA, MotorB, or even Motor[2];
#endif