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

@@ -3,6 +3,7 @@
//===================================
//======= motor configuration =======
//===================================
/* ==> currently using other driver
//--- configure left motor (hardware) ---
single100a_config_t configDriverLeft = {
.gpio_pwm = GPIO_NUM_26,
@@ -27,6 +28,13 @@ single100a_config_t configDriverRight = {
.bEnabledPinState = true, //-> not inverted (direct)
.resolution = LEDC_TIMER_11_BIT,
.pwmFreq = 10000
};
*/
//--- configure sabertooth driver --- (controls both motors in one instance)
sabertooth2x60_config_t sabertoothConfig = {
.gpio_TX = GPIO_NUM_23,
.uart_num = UART_NUM_2
};
@@ -125,10 +133,24 @@ fan_config_t configCooling = {
//===== create global objects =====
//=================================
//TODO outsource global variables to e.g. global.cpp and only config options here?
//create sabertooth motor driver instance
sabertooth2x60a sabertoothDriver(sabertoothConfig);
//--- controlledMotor ---
//functions for updating the duty via certain/current driver that can then be passed to controlledMotor
//-> makes it possible to easily use different motor drivers
//note: ignoring warning "capture of variable 'sabertoothDriver' with non-automatic storage duration", since sabertoothDriver object does not get destroyed anywhere - no lifetime issue
motorSetCommandFunc_t setLeftFunc = [&sabertoothDriver](motorCommand_t cmd) {
sabertoothDriver.setLeft(cmd);
};
motorSetCommandFunc_t setRightFunc = [&sabertoothDriver](motorCommand_t cmd) {
sabertoothDriver.setRight(cmd);
};
//create controlled motor instances (motorctl.hpp)
controlledMotor motorLeft(configDriverLeft, configMotorControlLeft);
controlledMotor motorRight(configDriverRight, configMotorControlRight);
controlledMotor motorLeft(setLeftFunc, configMotorControlLeft);
controlledMotor motorRight(setRightFunc, configMotorControlRight);
//create global joystic instance (joystick.hpp)
evaluatedJoystick joystick(configJoystick);
@@ -149,3 +171,4 @@ controlledArmchair control(configControl, &buzzer, &motorLeft, &motorRight, &joy
automatedArmchair armchair;

View File

@@ -164,174 +164,162 @@ void sendByte(char data){
//=========== app_main ============
//=================================
extern "C" void app_main(void) {
//TEST SABERTOOTH driver:
//config
sabertooth2x60_config_t sabertoothConfig = {
.gpio_TX = GPIO_NUM_23,
.uart_num = UART_NUM_2
};
//enable 5V volate regulator
gpio_pad_select_gpio(GPIO_NUM_17);
gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_17, 1);
//create instance
sabertooth2x60a motors(sabertoothConfig);
//---- define log levels ----
setLoglevels();
//----------------------------------------------
//--- create task for controlling the motors ---
//----------------------------------------------
//task that receives commands, handles ramp and current limit and executes commands using the motordriver function
xTaskCreate(&task_motorctl, "task_motor-control", 2048, NULL, 6, NULL);
// //------------------------------
// //--- create task for buzzer ---
// //------------------------------
// xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
//
// //-------------------------------
// //--- create task for control ---
// //-------------------------------
// //task that generates motor commands depending on the current mode and sends those to motorctl task
// xTaskCreate(&task_control, "task_control", 4096, NULL, 5, NULL);
//
// //------------------------------
// //--- create task for button ---
// //------------------------------
// //task that evaluates and processes the button input and runs the configured commands
// xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL);
//
// //-----------------------------------
// //--- create task for fan control ---
// //-----------------------------------
// //task that evaluates and processes the button input and runs the configured commands
// xTaskCreate(&task_fans, "task_fans", 2048, NULL, 1, NULL);
//
//
// //beep at startup
// buzzer.beep(3, 70, 50);
//
// //--- initialize nvs-flash and netif (needed for wifi) ---
// wifi_initNvs_initNetif();
//
// //--- initialize spiffs ---
// init_spiffs();
//--- initialize and start wifi ---
//FIXME: run wifi_init_client or wifi_init_ap as intended from control.cpp when switching state
//currently commented out because of error "assert failed: xQueueSemaphoreTake queue.c:1549 (pxQueue->uxItemSize == 0)" when calling control->changeMode from button.cpp
//when calling control.changeMode(http) from main.cpp it worked without error for some reason?
ESP_LOGI(TAG,"starting wifi...");
//wifi_init_client(); //connect to existing wifi
wifi_init_ap(); //start access point
ESP_LOGI(TAG,"done starting wifi");
//run test commands
//--- testing http server ---
// wifi_init_client(); //connect to existing wifi
// vTaskDelay(2000 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "initializing http server");
// http_init_server();
//--- testing force http mode after startup ---
//control.changeMode(controlMode_t::HTTP);
//--- main loop ---
//does nothing except for testing things
while(1){
motors.setLeft({motorstate_t::FWD, 70});
//test sabertooth driver
// motors.setLeft({motorstate_t::FWD, 70});
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// motors.setLeft({motorstate_t::IDLE, 0});
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// motors.setLeft({motorstate_t::REV, 50});
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// motors.setLeft(-90);
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// motors.setLeft(90);
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// motors.setLeft(0);
// vTaskDelay(1000 / portTICK_PERIOD_MS);
//--- test controlledMotor --- (ramp)
// //brake for 1 s
//motorLeft.setTarget(motorstate_t::BRAKE);
//vTaskDelay(1000 / portTICK_PERIOD_MS);
//command 90% - reverse
motorLeft.setTarget(motorstate_t::REV, 90);
vTaskDelay(5000 / portTICK_PERIOD_MS);
motorLeft.setTarget(motorstate_t::FWD, 80);
vTaskDelay(1000 / portTICK_PERIOD_MS);
motors.setLeft({motorstate_t::IDLE, 0});
vTaskDelay(1000 / portTICK_PERIOD_MS);
motors.setLeft({motorstate_t::REV, 50});
vTaskDelay(1000 / portTICK_PERIOD_MS);
motors.setLeft(-90);
vTaskDelay(1000 / portTICK_PERIOD_MS);
motors.setLeft(90);
vTaskDelay(1000 / portTICK_PERIOD_MS);
motors.setLeft(0);
motorLeft.setTarget(motorstate_t::IDLE, 90);
vTaskDelay(1000 / portTICK_PERIOD_MS);
//---------------------------------
//-------- TESTING section --------
//---------------------------------
// //--- test functions at mode change HTTP ---
// control.changeMode(controlMode_t::HTTP);
// vTaskDelay(10000 / portTICK_PERIOD_MS);
// control.changeMode(controlMode_t::IDLE);
// vTaskDelay(10000 / portTICK_PERIOD_MS);
//--- test wifi functions ---
// ESP_LOGI(TAG, "creating AP");
// wifi_init_ap(); //start accesspoint
// vTaskDelay(15000 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "stopping wifi");
// wifi_deinit_ap(); //stop wifi access point
// vTaskDelay(5000 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "connecting to wifi");
// wifi_init_client(); //connect to existing wifi
// vTaskDelay(10000 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "stopping wifi");
// wifi_deinit_client(); //stop wifi client
// vTaskDelay(5000 / portTICK_PERIOD_MS);
//--- test button ---
//buttonJoystick.handle();
// if (buttonJoystick.risingEdge){
// ESP_LOGI(TAG, "button pressed, was released for %d ms", buttonJoystick.msReleased);
// buzzer.beep(2, 100, 50);
// }else if (buttonJoystick.fallingEdge){
// ESP_LOGI(TAG, "button released, was pressed for %d ms", buttonJoystick.msPressed);
// buzzer.beep(1, 200, 0);
// }
//--- test joystick commands ---
// motorCommands_t commands = joystick_generateCommandsDriving(joystick);
// motorRight.setTarget(commands.right.state, commands.right.duty); //TODO make motorctl.setTarget also accept motorcommand struct directly
// motorLeft.setTarget(commands.left.state, commands.left.duty); //TODO make motorctl.setTarget also accept motorcommand struct directly
// //motorRight.setTarget(commands.right.state, commands.right.duty);
//--- test joystick class ---
//joystickData_t data = joystick.getData();
//ESP_LOGI(TAG, "position=%s, x=%.1f%%, y=%.1f%%, radius=%.1f%%, angle=%.2f",
// joystickPosStr[(int)data.position], data.x*100, data.y*100, data.radius*100, data.angle);
//--- test the motor driver ---
//fade up duty - forward
// for (int duty=0; duty<=100; duty+=5) {
// motorLeft.setTarget(motorstate_t::FWD, duty);
// vTaskDelay(100 / portTICK_PERIOD_MS);
// }
}
// //enable 5V volate regulator
// gpio_pad_select_gpio(GPIO_NUM_17);
// gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
// gpio_set_level(GPIO_NUM_17, 1);
//
// //---- define log levels ----
// setLoglevels();
//
// //----------------------------------------------
// //--- create task for controlling the motors ---
// //----------------------------------------------
// //task that receives commands, handles ramp and current limit and executes commands using the motordriver function
// xTaskCreate(&task_motorctl, "task_motor-control", 2048, NULL, 6, NULL);
//
// //------------------------------
// //--- create task for buzzer ---
// //------------------------------
// xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
//
// //-------------------------------
// //--- create task for control ---
// //-------------------------------
// //task that generates motor commands depending on the current mode and sends those to motorctl task
// xTaskCreate(&task_control, "task_control", 4096, NULL, 5, NULL);
//
// //------------------------------
// //--- create task for button ---
// //------------------------------
// //task that evaluates and processes the button input and runs the configured commands
// xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL);
//
// //-----------------------------------
// //--- create task for fan control ---
// //-----------------------------------
// //task that evaluates and processes the button input and runs the configured commands
// xTaskCreate(&task_fans, "task_fans", 2048, NULL, 1, NULL);
//
//
// //beep at startup
// buzzer.beep(3, 70, 50);
//
// //--- initialize nvs-flash and netif (needed for wifi) ---
// wifi_initNvs_initNetif();
//
// //--- initialize spiffs ---
// init_spiffs();
//
// //--- initialize and start wifi ---
// //FIXME: run wifi_init_client or wifi_init_ap as intended from control.cpp when switching state
// //currently commented out because of error "assert failed: xQueueSemaphoreTake queue.c:1549 (pxQueue->uxItemSize == 0)" when calling control->changeMode from button.cpp
// //when calling control.changeMode(http) from main.cpp it worked without error for some reason?
// ESP_LOGI(TAG,"starting wifi...");
// //wifi_init_client(); //connect to existing wifi
// wifi_init_ap(); //start access point
// ESP_LOGI(TAG,"done starting wifi");
//
//
// //--- testing http server ---
// // wifi_init_client(); //connect to existing wifi
// // vTaskDelay(2000 / portTICK_PERIOD_MS);
// // ESP_LOGI(TAG, "initializing http server");
// // http_init_server();
//
//
// //--- testing force http mode after startup ---
// //control.changeMode(controlMode_t::HTTP);
//
//
// //--- main loop ---
// //does nothing except for testing things
// while(1){
// vTaskDelay(1000 / portTICK_PERIOD_MS);
//
// //---------------------------------
// //-------- TESTING section --------
// //---------------------------------
// // //--- test functions at mode change HTTP ---
// // control.changeMode(controlMode_t::HTTP);
// // vTaskDelay(10000 / portTICK_PERIOD_MS);
// // control.changeMode(controlMode_t::IDLE);
// // vTaskDelay(10000 / portTICK_PERIOD_MS);
//
//
// //--- test wifi functions ---
// // ESP_LOGI(TAG, "creating AP");
// // wifi_init_ap(); //start accesspoint
// // vTaskDelay(15000 / portTICK_PERIOD_MS);
// // ESP_LOGI(TAG, "stopping wifi");
// // wifi_deinit_ap(); //stop wifi access point
// // vTaskDelay(5000 / portTICK_PERIOD_MS);
// // ESP_LOGI(TAG, "connecting to wifi");
// // wifi_init_client(); //connect to existing wifi
// // vTaskDelay(10000 / portTICK_PERIOD_MS);
// // ESP_LOGI(TAG, "stopping wifi");
// // wifi_deinit_client(); //stop wifi client
// // vTaskDelay(5000 / portTICK_PERIOD_MS);
//
//
// //--- test button ---
// //buttonJoystick.handle();
// // if (buttonJoystick.risingEdge){
// // ESP_LOGI(TAG, "button pressed, was released for %d ms", buttonJoystick.msReleased);
// // buzzer.beep(2, 100, 50);
//
// // }else if (buttonJoystick.fallingEdge){
// // ESP_LOGI(TAG, "button released, was pressed for %d ms", buttonJoystick.msPressed);
// // buzzer.beep(1, 200, 0);
// // }
//
//
// //--- test joystick commands ---
// // motorCommands_t commands = joystick_generateCommandsDriving(joystick);
// // motorRight.setTarget(commands.right.state, commands.right.duty); //TODO make motorctl.setTarget also accept motorcommand struct directly
// // motorLeft.setTarget(commands.left.state, commands.left.duty); //TODO make motorctl.setTarget also accept motorcommand struct directly
// // //motorRight.setTarget(commands.right.state, commands.right.duty);
//
//
// //--- test joystick class ---
// //joystickData_t data = joystick.getData();
// //ESP_LOGI(TAG, "position=%s, x=%.1f%%, y=%.1f%%, radius=%.1f%%, angle=%.2f",
// // joystickPosStr[(int)data.position], data.x*100, data.y*100, data.radius*100, data.angle);
//
// //--- test the motor driver ---
// //fade up duty - forward
// // for (int duty=0; duty<=100; duty+=5) {
// // motorLeft.setTarget(motorstate_t::FWD, duty);
// // vTaskDelay(100 / portTICK_PERIOD_MS);
// // }
//
//
// //--- test controlledMotor --- (ramp)
// // //brake for 1 s
// // motorLeft.setTarget(motorstate_t::BRAKE);
// // vTaskDelay(1000 / portTICK_PERIOD_MS);
// // //command 90% - reverse
// // motorLeft.setTarget(motorstate_t::REV, 90);
// // vTaskDelay(5000 / portTICK_PERIOD_MS);
// // //command 100% - forward
// // motorLeft.setTarget(motorstate_t::FWD, 100);
// // vTaskDelay(1000 / portTICK_PERIOD_MS);
//
// }
}