diff --git a/src/bobbycar-protocol b/src/bobbycar-protocol index 39f76cb..994d0c9 160000 --- a/src/bobbycar-protocol +++ b/src/bobbycar-protocol @@ -1 +1 @@ -Subproject commit 39f76cb62b32da2ea8d7a7154e1b2854dbf792ab +Subproject commit 994d0c95a72ed8e42a6875f771c778481073d470 diff --git a/src/can.h b/src/can.h index d453b87..2c38126 100644 --- a/src/can.h +++ b/src/can.h @@ -6,6 +6,8 @@ #include +#include "bobbycar-protocol/bobbycar-can.h" + #include "types.h" #include "globals.h" @@ -14,37 +16,43 @@ millis_t m_lastCanFeedback{}; void initCan() { - ESP_LOGI("CAN", "hello world"); + Serial.printf("CAN info hello world\r\n"); can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(GPIO_NUM_21, GPIO_NUM_22, CAN_MODE_NORMAL); can_timing_config_t t_config CAN_TIMING_CONFIG_250KBITS(); - can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL(); + can_filter_config_t f_config CAN_FILTER_CONFIG_ACCEPT_ALL(); +// { +// // +// .acceptance_code = 0b00000000000, +// .acceptance_mask = 0b00001111111, +// .single_filter = true +// }; if (const auto result = can_driver_install(&g_config, &t_config, &f_config); result == ESP_OK) { - ESP_LOGI("CAN", "can_driver_install() succeeded"); + Serial.printf("CAN info can_driver_install() succeeded\r\n"); } else { - ESP_LOGE("CAN", "can_driver_install() failed with %s", esp_err_to_name(result)); + Serial.printf("CAN err can_driver_install() failed with %s\r\n", esp_err_to_name(result)); return; } if (const auto result = can_start(); result == ESP_OK) { - ESP_LOGI("CAN", "can_start() succeeded"); + Serial.printf("CAN info can_start() succeeded\r\n"); } else { - ESP_LOGE("CAN", "can_start() failed with %s", esp_err_to_name(result)); + Serial.printf("CAN err can_start() failed with %s\r\n", esp_err_to_name(result)); if (const auto result = can_driver_uninstall(); result == ESP_OK) { - ESP_LOGI("CAN", "can_driver_uninstall() succeeded"); + Serial.printf("CAN info can_driver_uninstall() succeeded\r\n"); } else { - ESP_LOGE("CAN", "can_driver_uninstall() failed with %s", esp_err_to_name(result)); + Serial.printf("CAN err can_driver_uninstall() failed with %s\r\n", esp_err_to_name(result)); } return; @@ -59,105 +67,10 @@ void parseCanInput() controllers.front.feedbackValid = millis() - m_lastCanFeedback <= 100; if (result != ESP_ERR_TIMEOUT) - ESP_LOGE("CAN", "can_receive() failed with %s", esp_err_to_name(result)); + Serial.printf("CAN err can_receive() failed with %s\r\n", esp_err_to_name(result)); return; } - enum { // vv - DeviceTypeMotorController = 0b00000000000 - }; - - enum { // ..vv - MotorControllerRec = 0b00000000000, - MotorControllerSend = 0b00010000000, - }; - - enum { // ....vvvvv - MotorControllerDcLink = 0b00000000000, - MotorControllerSpeed = 0b00000000100, - MotorControllerError = 0b00000001000, - MotorControllerAngle = 0b00000001100, - MotorControllerDcPhaA = 0b00000010000, - MotorControllerDcPhaB = 0b00000010100, - MotorControllerDcPhaC = 0b00000011000, - MotorControllerChops = 0b00000011100, - MotorControllerHall = 0b00000100000, - MotorControllerVoltage = 0b00000100100, - MotorControllerTemp = 0b00000101000 - }; - - enum { // .........v - MotorControllerFront = 0b00000000000, - MotorControllerBack = 0b00000000010, - }; - - enum { // ..........v - MotorControllerLeft = 0b00000000000, - MotorControllerRight = 0b00000000001, - }; - - enum { - MotorControllerFrontLeftDcLink = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcLink | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightDcLink = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcLink | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftDcLink = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcLink | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightDcLink = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcLink | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftSpeed = DeviceTypeMotorController | MotorControllerSend | MotorControllerSpeed | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightSpeed = DeviceTypeMotorController | MotorControllerSend | MotorControllerSpeed | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftSpeed = DeviceTypeMotorController | MotorControllerSend | MotorControllerSpeed | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightSpeed = DeviceTypeMotorController | MotorControllerSend | MotorControllerSpeed | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftError = DeviceTypeMotorController | MotorControllerSend | MotorControllerError | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightError = DeviceTypeMotorController | MotorControllerSend | MotorControllerError | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftError = DeviceTypeMotorController | MotorControllerSend | MotorControllerError | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightError = DeviceTypeMotorController | MotorControllerSend | MotorControllerError | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftAngle = DeviceTypeMotorController | MotorControllerSend | MotorControllerAngle | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightAngle = DeviceTypeMotorController | MotorControllerSend | MotorControllerAngle | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftAngle = DeviceTypeMotorController | MotorControllerSend | MotorControllerAngle | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightAngle = DeviceTypeMotorController | MotorControllerSend | MotorControllerAngle | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftDcPhaA = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaA | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightDcPhaA = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaA | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftDcPhaA = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaA | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightDcPhaA = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaA | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftDcPhaB = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaB | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightDcPhaB = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaB | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftDcPhaB = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaB | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightDcPhaB = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaB | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftDcPhaC = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaC | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightDcPhaC = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaC | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftDcPhaC = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaC | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightDcPhaC = DeviceTypeMotorController | MotorControllerSend | MotorControllerDcPhaC | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftChops = DeviceTypeMotorController | MotorControllerSend | MotorControllerChops | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightChops = DeviceTypeMotorController | MotorControllerSend | MotorControllerChops | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftChops = DeviceTypeMotorController | MotorControllerSend | MotorControllerChops | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightChops = DeviceTypeMotorController | MotorControllerSend | MotorControllerChops | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftHall = DeviceTypeMotorController | MotorControllerSend | MotorControllerHall | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightHall = DeviceTypeMotorController | MotorControllerSend | MotorControllerHall | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftHall = DeviceTypeMotorController | MotorControllerSend | MotorControllerHall | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightHall = DeviceTypeMotorController | MotorControllerSend | MotorControllerHall | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftVoltage = DeviceTypeMotorController | MotorControllerSend | MotorControllerVoltage | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightVoltage = DeviceTypeMotorController | MotorControllerSend | MotorControllerVoltage | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftVoltage = DeviceTypeMotorController | MotorControllerSend | MotorControllerVoltage | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightVoltage = DeviceTypeMotorController | MotorControllerSend | MotorControllerVoltage | MotorControllerBack | MotorControllerRight, - - MotorControllerFrontLeftTemp = DeviceTypeMotorController | MotorControllerSend | MotorControllerTemp | MotorControllerFront | MotorControllerLeft, - MotorControllerFrontRightTemp = DeviceTypeMotorController | MotorControllerSend | MotorControllerTemp | MotorControllerFront | MotorControllerRight, - MotorControllerBackLeftTemp = DeviceTypeMotorController | MotorControllerSend | MotorControllerTemp | MotorControllerBack | MotorControllerLeft, - MotorControllerBackRightTemp = DeviceTypeMotorController | MotorControllerSend | MotorControllerTemp | MotorControllerBack | MotorControllerRight, - }; - - ESP_LOGI("CAN", "CAN MESSAGE RECEIVED OMG!!!!eins"); - ESP_LOGI("CAN", ".identifier = %u", message.identifier); - ESP_LOGI("CAN", ".flags = %u", message.flags); - ESP_LOGI("CAN", ".length = %hhu", message.data_length_code); - switch (message.identifier) { case MotorControllerFrontLeftDcLink: @@ -210,6 +123,24 @@ void parseCanInput() controllers.front.feedbackValid = true; controllers.front.feedback.right.chops = *((uint16_t*)message.data); break; + case MotorControllerFrontLeftHall: + { + m_lastCanFeedback = millis(); + controllers.front.feedbackValid = true; + controllers.front.feedback.left.hallA = *((uint8_t*)message.data) & 1; + controllers.front.feedback.left.hallB = *((uint8_t*)message.data) & 2; + controllers.front.feedback.left.hallC = *((uint8_t*)message.data) & 4; + break; + } + case MotorControllerFrontRightHall: + { + m_lastCanFeedback = millis(); + controllers.front.feedbackValid = true; + controllers.front.feedback.right.hallA = *((uint8_t*)message.data) & 1; + controllers.front.feedback.right.hallB = *((uint8_t*)message.data) & 2; + controllers.front.feedback.right.hallC = *((uint8_t*)message.data) & 4; + break; + } case MotorControllerFrontLeftVoltage: m_lastCanFeedback = millis(); controllers.front.feedbackValid = true; @@ -220,6 +151,8 @@ void parseCanInput() controllers.front.feedbackValid = true; controllers.front.feedback.boardTemp = *((int16_t*)message.data); break; + default: + Serial.printf("WARNING Unknown CAN info received .identifier = %u\r\n", message.identifier); } } } diff --git a/src/controller.h b/src/controller.h index 9d10d64..8eb5486 100644 --- a/src/controller.h +++ b/src/controller.h @@ -2,7 +2,8 @@ #include -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" +#include "bobbycar-protocol/bobbycar-serial.h" #ifndef FEATURE_CAN #include "feedbackparser.h" diff --git a/src/feedbackparser.h b/src/feedbackparser.h index 037958e..afd4603 100644 --- a/src/feedbackparser.h +++ b/src/feedbackparser.h @@ -5,7 +5,7 @@ #include -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" #include "types.h" diff --git a/src/main.cpp b/src/main.cpp index 444639d..9c754e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,8 @@ #include #include -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" +#include "bobbycar-protocol/bobbycar-serial.h" #include "globals.h" #include "modes/defaultmode.h" diff --git a/src/modes/gametrakmode.h b/src/modes/gametrakmode.h index 959619b..a96b0b3 100644 --- a/src/modes/gametrakmode.h +++ b/src/modes/gametrakmode.h @@ -14,7 +14,7 @@ constexpr const T& clamp( const T& v, const T& lo, const T& hi ) #include "utils.h" #include "defaultmode.h" -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" namespace { #ifdef FEATURE_GAMETRAK diff --git a/src/modes/ignoreinputmode.h b/src/modes/ignoreinputmode.h index ca5343b..17a5075 100644 --- a/src/modes/ignoreinputmode.h +++ b/src/modes/ignoreinputmode.h @@ -4,7 +4,7 @@ #include "globals.h" #include "utils.h" -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" namespace { class IgnoreInputMode : public ModeInterface diff --git a/src/modes/larsmmode.h b/src/modes/larsmmode.h index 269f62e..ef775eb 100644 --- a/src/modes/larsmmode.h +++ b/src/modes/larsmmode.h @@ -4,7 +4,7 @@ #include "globals.h" #include "utils.h" -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" namespace { class LarsmMode : public ModeInterface diff --git a/src/modes/tempomatmode.h b/src/modes/tempomatmode.h index e8ef9c9..42682f4 100644 --- a/src/modes/tempomatmode.h +++ b/src/modes/tempomatmode.h @@ -2,7 +2,7 @@ #include -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" #include "modeinterface.h" #include "globals.h" diff --git a/src/settings.h b/src/settings.h index daee213..0d6e967 100644 --- a/src/settings.h +++ b/src/settings.h @@ -4,7 +4,7 @@ #include -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" #ifdef FEATURE_BLUETOOTH #include "bluetoothmode.h" diff --git a/src/unifiedmodelmode.h b/src/unifiedmodelmode.h index 8aae672..0a66074 100644 --- a/src/unifiedmodelmode.h +++ b/src/unifiedmodelmode.h @@ -4,7 +4,7 @@ #include -#include "bobbycar-protocol/protocol.h" +#include "bobbycar-protocol/bobbycar-common.h" namespace { enum class UnifiedModelMode : uint8_t diff --git a/src/utils.h b/src/utils.h index c1d70b8..a346ab4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -2,11 +2,16 @@ #include #include +#include + +#include #include #include #include +#include "bobbycar-protocol/bobbycar-can.h" + #include "display.h" #include "globals.h" @@ -247,14 +252,116 @@ void fixCommonParams() void sendCommands() { +#ifndef FEATURE_CAN for (Controller &controller : controllers) { controller.command.start = Command::VALID_HEADER; controller.command.checksum = calculateChecksum(controller.command); -#ifndef FEATURE_CAN controller.serial.get().write((uint8_t *) &controller.command, sizeof(controller.command)); -#endif } +#endif + +#ifdef FEATURE_CAN + constexpr auto send = [](uint32_t addr, auto value){ + can_message_t message; + message.identifier = addr; + if (addr == MotorControllerFrontLeftInpTgt || addr == MotorControllerFrontRightInpTgt) + message.flags = CAN_MSG_FLAG_SS; + else + message.flags = CAN_MSG_FLAG_NONE; + message.data_length_code = sizeof(value); + std::fill(std::begin(message.data), std::end(message.data), 0); + std::memcpy(message.data, &value, sizeof(value)); + + const auto result = can_transmit(&message, pdMS_TO_TICKS(200)); + if (result != ESP_OK && result != ESP_ERR_TIMEOUT) + Serial.printf("ERROR: can_transmit() failed with %s\r\n", esp_err_to_name(result)); + return result; + }; + + send(MotorControllerFrontLeftInpTgt, controllers.front.command.left.pwm); + send(MotorControllerFrontRightInpTgt, controllers.front.command.right.pwm); +// send(MotorControllerBackLeftInpTgt, controllers.back.command.left.pwm); +// send(MotorControllerBackRightInpTgt, controllers.back.command.right.pwm); + + static int i = 0; + switch (i++) + { + case 0: + send(MotorControllerFrontLeftEnable, controllers.front.command.left.enable); + send(MotorControllerFrontRightEnable, controllers.front.command.right.enable); +// send(MotorControllerBackLeftEnable, controllers.back.command.left.enable); +// send(MotorControllerBackRightEnable, controllers.back.command.right.enable); + break; + case 1: + send(MotorControllerFrontLeftCtrlTyp, controllers.front.command.left.ctrlTyp); + send(MotorControllerFrontRightCtrlTyp, controllers.front.command.right.ctrlTyp); +// send(MotorControllerBackLeftCtrlTyp, controllers.back.command.left.ctrlTyp); +// send(MotorControllerBackRightCtrlTyp, controllers.back.command.right.ctrlTyp); + break; + case 2: + send(MotorControllerFrontLeftCtrlMod, controllers.front.command.left.ctrlMod); + send(MotorControllerFrontRightCtrlMod, controllers.front.command.right.ctrlMod); +// send(MotorControllerBackLeftCtrlMod, controllers.back.command.left.ctrlMod); +// send(MotorControllerBackRightCtrlMod, controllers.back.command.right.ctrlMod); + break; + case 3: + send(MotorControllerFrontLeftIMotMax, controllers.front.command.left.iMotMax); + send(MotorControllerFrontRightIMotMax, controllers.front.command.right.iMotMax); +// send(MotorControllerBackLeftIMotMax, controllers.back.command.left.iMotMax); +// send(MotorControllerBackRightIMotMax, controllers.back.command.right.iMotMax); + break; + case 4: + send(MotorControllerFrontLeftIDcMax, controllers.front.command.left.iDcMax); + send(MotorControllerFrontRightIDcMax, controllers.front.command.right.iDcMax); +// send(MotorControllerBackLeftIDcMax, controllers.back.command.left.iDcMax); +// send(MotorControllerBackRightIDcMax, controllers.back.command.right.iDcMax); + break; + case 5: + send(MotorControllerFrontLeftNMotMax, controllers.front.command.left.nMotMax); + send(MotorControllerFrontRightNMotMax, controllers.front.command.right.nMotMax); +// send(MotorControllerBackLeftNMotMax, controllers.back.command.left.nMotMax); +// send(MotorControllerBackRightNMotMax, controllers.back.command.right.nMotMax); + break; + case 6: + send(MotorControllerFrontLeftFieldWeakMax, controllers.front.command.left.fieldWeakMax); + send(MotorControllerFrontRightFieldWeakMax, controllers.front.command.right.fieldWeakMax); +// send(MotorControllerBackLeftFieldWeakMax, controllers.back.command.left.fieldWeakMax); +// send(MotorControllerBackRightFieldWeakMax, controllers.back.command.right.fieldWeakMax); + break; + case 7: + send(MotorControllerFrontLeftPhaseAdvMax, controllers.front.command.left.phaseAdvMax); + send(MotorControllerFrontRightPhaseAdvMax, controllers.front.command.right.phaseAdvMax); +// send(MotorControllerBackLeftPhaseAdvMax, controllers.back.command.left.phaseAdvMax); +// send(MotorControllerBackRightPhaseAdvMax, controllers.back.command.right.phaseAdvMax); + break; + case 8: + send(MotorControllerFrontLeftBuzzerFreq, controllers.front.command.buzzer.freq); + send(MotorControllerFrontRightBuzzerFreq, controllers.front.command.buzzer.freq); +// send(MotorControllerBackLeftBuzzerFreq, controllers.back.command.buzzer.freq); +// send(MotorControllerBackRightBuzzerFreq, controllers.back.command.buzzer.freq); + break; + case 9: + send(MotorControllerFrontLeftBuzzerPattern, controllers.front.command.buzzer.pattern); + send(MotorControllerFrontRightBuzzerPattern, controllers.front.command.buzzer.pattern); +// send(MotorControllerBackLeftBuzzerPattern, controllers.back.command.buzzer.pattern); +// send(MotorControllerBackRightBuzzerPattern, controllers.back.command.buzzer.pattern); + break; + case 10: + send(MotorControllerFrontLeftLed, controllers.front.command.led); + send(MotorControllerFrontRightLed, controllers.front.command.led); +// send(MotorControllerBackLeftLed, controllers.back.command.led); +// send(MotorControllerBackRightLed, controllers.back.command.led); + break; + case 11: + send(MotorControllerFrontLeftPoweroff, controllers.front.command.poweroff); + send(MotorControllerFrontRightPoweroff, controllers.front.command.poweroff); +// send(MotorControllerBackLeftPoweroff, controllers.back.command.poweroff); +// send(MotorControllerBackRightPoweroff, controllers.back.command.poweroff); + i=0; + break; + } +#endif } template