Make drivers interchangeable, Switch to sabertooth driver

- make motorctl compatible with different drivers
    - pass set function instead of specific motor object
    - add lambda function in config.cpp
- update config to use one new sabertooth driver instead of two single100a
- main test controlled motor
This commit is contained in:
jonny_jr9
2023-09-08 12:03:33 +02:00
parent 98956e2bf8
commit 71b63ebbd3
6 changed files with 194 additions and 175 deletions

View File

@@ -11,11 +11,12 @@ static const char * TAG = "motor-control";
//======== constructor ========
//=============================
//constructor, simultaniously initialize instance of motor driver 'motor' and current sensor 'cSensor' with provided config (see below lines after ':')
controlledMotor::controlledMotor(single100a_config_t config_driver, motorctl_config_t config_control):
motor(config_driver),
controlledMotor::controlledMotor(motorSetCommandFunc_t setCommandFunc, motorctl_config_t config_control):
cSensor(config_control.currentSensor_adc, config_control.currentSensor_ratedCurrent) {
//copy parameters for controlling the motor
config = config_control;
//pointer to update motot dury method
motorSetCommand = setCommandFunc;
//copy configured default fading durations to actually used variables
msFadeAccel = config.msFadeAccel;
msFadeDecel = config.msFadeDecel;
@@ -139,7 +140,7 @@ void controlledMotor::handle(){
//brake immediately, update state, duty and exit this cycle of handle function
if (state == motorstate_t::BRAKE){
ESP_LOGD(TAG, "braking - skip fading");
motor.set(motorstate_t::BRAKE, dutyTarget);
motorSetCommand({motorstate_t::BRAKE, dutyTarget});
//dutyNow = 0;
return; //no need to run the fade algorithm
}
@@ -229,7 +230,7 @@ void controlledMotor::handle(){
//--- apply new target to motor ---
motor.set(state, fabs(dutyNow));
motorSetCommand({state, (float)fabs(dutyNow)});
//note: BRAKE state is handled earlier

View File

@@ -19,7 +19,7 @@ extern "C"
//outsourced to common/types.hpp
#include "types.hpp"
typedef void (*motorSetCommandFunc_t)(motorCommand_t cmd);
//===================================
@@ -28,7 +28,7 @@ extern "C"
class controlledMotor {
public:
//--- functions ---
controlledMotor(single100a_config_t config_driver, motorctl_config_t config_control); //constructor with structs for configuring motordriver and parameters for control TODO: add configuration for currentsensor
controlledMotor(motorSetCommandFunc_t setCommandFunc, motorctl_config_t config_control); //constructor with structs for configuring motordriver and parameters for control TODO: add configuration for currentsensor
void handle(); //controls motor duty with fade and current limiting feature (has to be run frequently by another task)
void setTarget(motorstate_t state_f, float duty_f = 0); //adds target command to queue for handle function
motorCommand_t getStatus(); //get current status of the motor (returns struct with state and duty)
@@ -44,13 +44,14 @@ class controlledMotor {
void init(); //creates currentsensor objects, motordriver objects and queue
//--- objects ---
//motor driver
single100a motor;
//queue for sending commands to the separate task running the handle() function very fast
QueueHandle_t commandQueue = NULL;
//current sensor
currentSensor cSensor;
//function pointer that sets motor duty (driver)
motorSetCommandFunc_t motorSetCommand;
//--- variables ---
//struct for storing control specific parameters
motorctl_config_t config;

View File

@@ -80,6 +80,10 @@ void single100a::init(){
//----------- set -----------
//---------------------------
//function to put the h-bridge module in the desired state and duty cycle
void single100a::set(motorCommand_t cmd){
set(cmd.state, cmd.duty);
}
void single100a::set(motorstate_t state_f, float duty_f){
//scale provided target duty in percent to available resolution for ledc
@@ -190,7 +194,7 @@ sabertooth2x60a::sabertooth2x60a(sabertooth2x60_config_t config_f){
void sabertooth2x60a::init(){
ESP_LOGW(TAG, "initializing uart...");
uart_config_t uart_config = {
.baud_rate = 38400,
.baud_rate = 9600, //dip switches: 101011
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,

View File

@@ -51,6 +51,7 @@ class single100a {
//--- functions ---
void set(motorstate_t state, float duty_f = 0); //set mode and duty of the motor (see motorstate_t above)
void set(motorCommand_t cmd);
//TODO: add functions to get the current state and duty
private:
@@ -79,18 +80,19 @@ class single100a {
//===== sabertooth 2x60A motor driver ======
//==========================================
//struct with all config parameters for single100a motor driver
//struct with all config parameters for sabertooth2x60a driver
typedef struct {
gpio_num_t gpio_TX;
uart_port_t uart_num; //(UART_NUM_1/2)
} sabertooth2x60_config_t;
//controll via simplified serial
//=> set dip switches to 'simplified serial 9600 Baud' according to manual 101011
class sabertooth2x60a {
public:
//--- constructor ---
sabertooth2x60a(sabertooth2x60_config_t config_f); //provide config struct (see above)
//--- functions ---
//set motor speed with float value from -100 to 100
void setLeft(float dutyPerSigned);