diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e319e8..79d1267 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ add_definitions(-DHUARN2) add_definitions(-DLOG_TO_SERIAL) add_definitions(-DFEATURE_CAN) #add_definitions(-DCAN_LOG_UNKNOWN_ADDR) +add_definitions(-DIS_BACK) add_executable(firmware.elf STM32CubeF1/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c diff --git a/bobbycar-protocol b/bobbycar-protocol index 49fa47c..fee1175 160000 --- a/bobbycar-protocol +++ b/bobbycar-protocol @@ -1 +1 @@ -Subproject commit 49fa47c90ac24de50a8f2b1eef040ac9738dd00f +Subproject commit fee1175bf819826222086aa1e3c7fbb66d37a33c diff --git a/main.cpp b/main.cpp index d8f942d..5e00566 100644 --- a/main.cpp +++ b/main.cpp @@ -98,6 +98,13 @@ CAN_HandleTypeDef CanHandle; char logBuffer[512]; #endif +constexpr bool isBackBoard = +#ifdef IS_BACK + true +#else + false +#endif + ; template void myPrintf(const char (&format)[formatLength], Targs ... args) @@ -1584,32 +1591,33 @@ void applyIncomingCanMessage() switch (header.StdId) { - case MotorControllerFrontLeftEnable: /* myPrintf("MotorControllerFrontLeftEnable"); */ left.enable = *((bool *)buf); break; - case MotorControllerFrontRightEnable: /* myPrintf("MotorControllerFrontRightEnable"); */ right.enable = *((bool *)buf); break; - case MotorControllerFrontLeftInpTgt: /* myPrintf("MotorControllerFrontLeftInpTgt"); */ timeoutCntLeft = 0; left.rtU.r_inpTgt = *((int16_t*)buf); break; - case MotorControllerFrontRightInpTgt: /* myPrintf("MotorControllerFrontRightInpTgt"); */ timeoutCntRight = 0; right.rtU.r_inpTgt = *((int16_t*)buf); break; - case MotorControllerFrontLeftCtrlTyp: /* myPrintf("MotorControllerFrontLeftCtrlTyp"); */ left.rtP.z_ctrlTypSel = *((uint8_t*)buf); break; - case MotorControllerFrontRightCtrlTyp: /* myPrintf("MotorControllerFrontRightCtrlTyp"); */ right.rtP.z_ctrlTypSel = *((uint8_t*)buf); break; - case MotorControllerFrontLeftCtrlMod: /* myPrintf("MotorControllerFrontLeftCtrlMod"); */ left.rtU.z_ctrlModReq = *((uint8_t*)buf); break; - case MotorControllerFrontRightCtrlMod: /* myPrintf("MotorControllerFrontRightCtrlMod"); */ right.rtU.z_ctrlModReq = *((uint8_t*)buf); break; - case MotorControllerFrontLeftIMotMax: /* myPrintf("MotorControllerFrontLeftIMotMax"); */ left.rtP.i_max = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; - case MotorControllerFrontRightIMotMax: /* myPrintf("MotorControllerFrontRightIMotMax"); */ right.rtP.i_max = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; - case MotorControllerFrontLeftIDcMax: /* myPrintf("MotorControllerFrontLeftIDcMax"); */ left.iDcMax = *((uint8_t*)buf); break; - case MotorControllerFrontRightIDcMax: /* myPrintf("MotorControllerFrontRightIDcMax"); */ right.iDcMax = *((uint8_t*)buf); break; - case MotorControllerFrontLeftNMotMax: /* myPrintf("MotorControllerFrontLeftNMotMax"); */ left.rtP.n_max = *((uint16_t*)buf) << 4; break; - case MotorControllerFrontRightNMotMax: /* myPrintf("MotorControllerFrontRightNMotMax"); */ right.rtP.n_max = *((uint16_t*)buf) << 4; break; - case MotorControllerFrontLeftFieldWeakMax: /* myPrintf("MotorControllerFrontLeftFieldWeakMax"); */ left.rtP.id_fieldWeakMax = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; - case MotorControllerFrontRightFieldWeakMax: /* myPrintf("MotorControllerFrontRightFieldWeakMax"); */ right.rtP.id_fieldWeakMax = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; - case MotorControllerFrontLeftPhaseAdvMax: /* myPrintf("MotorControllerFrontLeftPhaseAdvMax"); */ left.rtP.a_phaAdvMax = *((uint16_t*)buf) << 4; break; - case MotorControllerFrontRightPhaseAdvMax: /* myPrintf("MotorControllerFrontRightPhaseAdvMax"); */ right.rtP.a_phaAdvMax = *((uint16_t*)buf) << 4; break; - case MotorControllerFrontLeftBuzzerFreq: /* myPrintf("MotorControllerFrontLeftBuzzerFreq"); */ buzzer.freq = *((uint8_t*)buf); break; - case MotorControllerFrontRightBuzzerFreq: /* myPrintf("MotorControllerFrontRightBuzzerFreq"); */ buzzer.freq = *((uint8_t*)buf); break; - case MotorControllerFrontLeftBuzzerPattern: /* myPrintf("MotorControllerFrontLeftBuzzerPattern"); */ buzzer.pattern = *((uint8_t*)buf); break; - case MotorControllerFrontRightBuzzerPattern: /* myPrintf("MotorControllerFrontRightBuzzerPattern"); */ buzzer.pattern = *((uint8_t*)buf); break; - case MotorControllerFrontLeftLed: /* myPrintf("MotorControllerFrontLeftLed"); */ break; - case MotorControllerFrontRightLed: /* myPrintf("MotorControllerFrontRightLed"); */ break; - case MotorControllerFrontLeftPoweroff: /* myPrintf("MotorControllerFrontLeftPoweroff"); */ break; - case MotorControllerFrontRightPoweroff: /* myPrintf("MotorControllerFrontRightPoweroff"); */ break; + using namespace bobbycar::can; + case MotorController::Command::Enable: left.enable = *((bool *)buf); break; + case MotorController::Command::Enable: right.enable = *((bool *)buf); break; + case MotorController::Command::InpTgt: timeoutCntLeft = 0; left.rtU.r_inpTgt = *((int16_t*)buf); break; + case MotorController::Command::InpTgt: timeoutCntRight = 0; right.rtU.r_inpTgt = *((int16_t*)buf); break; + case MotorController::Command::CtrlTyp: left.rtP.z_ctrlTypSel = *((uint8_t*)buf); break; + case MotorController::Command::CtrlTyp: right.rtP.z_ctrlTypSel = *((uint8_t*)buf); break; + case MotorController::Command::CtrlMod: left.rtU.z_ctrlModReq = *((uint8_t*)buf); break; + case MotorController::Command::CtrlMod: right.rtU.z_ctrlModReq = *((uint8_t*)buf); break; + case MotorController::Command::IMotMax: left.rtP.i_max = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; + case MotorController::Command::IMotMax: right.rtP.i_max = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; + case MotorController::Command::IDcMax: left.iDcMax = *((uint8_t*)buf); break; + case MotorController::Command::IDcMax: right.iDcMax = *((uint8_t*)buf); break; + case MotorController::Command::NMotMax: left.rtP.n_max = *((uint16_t*)buf) << 4; break; + case MotorController::Command::NMotMax: right.rtP.n_max = *((uint16_t*)buf) << 4; break; + case MotorController::Command::FieldWeakMax: left.rtP.id_fieldWeakMax = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; + case MotorController::Command::FieldWeakMax: right.rtP.id_fieldWeakMax = (*((uint8_t*)buf) * A2BIT_CONV) << 4; break; + case MotorController::Command::PhaseAdvMax: left.rtP.a_phaAdvMax = *((uint16_t*)buf) << 4; break; + case MotorController::Command::PhaseAdvMax: right.rtP.a_phaAdvMax = *((uint16_t*)buf) << 4; break; + case MotorController::Command::BuzzerFreq: buzzer.freq = *((uint8_t*)buf); break; + case MotorController::Command::BuzzerFreq: buzzer.freq = *((uint8_t*)buf); break; + case MotorController::Command::BuzzerPattern: buzzer.pattern = *((uint8_t*)buf); break; + case MotorController::Command::BuzzerPattern: buzzer.pattern = *((uint8_t*)buf); break; + case MotorController::Command::Led: break; + case MotorController::Command::Led: break; + case MotorController::Command::Poweroff: break; + case MotorController::Command::Poweroff: break; default: #ifndef CAN_LOG_UNKNOWN_ADDR if constexpr (false) @@ -1624,7 +1632,7 @@ void applyIncomingCanMessage() header.StdId&64?'1':'0', header.StdId&32?'1':'0', header.StdId&16?'1':'0', - header.StdId&8?'1':'0', + header.StdId&8?' 1':'0', header.StdId&4?'1':'0', header.StdId&2?'1':'0', @@ -1676,28 +1684,29 @@ void sendCanFeedback() switch (whichToSend++) { - case 0: /* myPrintf("LeftCurrent free=%u", free); */ send(MotorControllerFrontLeftDcLink, left. rtU.i_DCLink); break; - case 1: /* myPrintf("RightCurrent free=%u", free); */ send(MotorControllerFrontRightDcLink, right.rtU.i_DCLink); break; - case 2: /* myPrintf("LeftSpeed free=%u", free); */ send(MotorControllerFrontLeftSpeed, left. rtY.n_mot); break; - case 3: /* myPrintf("RightSpeed free=%u", free); */ send(MotorControllerFrontRightSpeed, right.rtY.n_mot); break; - case 4: /* myPrintf("LeftError free=%u", free); */ send(MotorControllerFrontLeftError, left. rtY.z_errCode); break; - case 5: /* myPrintf("RightError free=%u", free); */ send(MotorControllerFrontRightError, right.rtY.z_errCode); break; - case 6: /* myPrintf("LeftAngle free=%u", free); */ send(MotorControllerFrontLeftAngle, left. rtY.a_elecAngle); break; - case 7: /* myPrintf("RightAngle free=%u", free); */ send(MotorControllerFrontRightAngle, right.rtY.a_elecAngle); break; - case 8: /* myPrintf("LeftDcPhaA free=%u", free); */ send(MotorControllerFrontLeftDcPhaA, left. rtY.DC_phaA); break; - case 9: /* myPrintf("RightDcPhaA free=%u", free); */ send(MotorControllerFrontRightDcPhaA, right.rtY.DC_phaA); break; - case 10: /* myPrintf("LeftDcPhaB free=%u", free); */ send(MotorControllerFrontLeftDcPhaB, left. rtY.DC_phaB); break; - case 11: /* myPrintf("RightDcPhaB free=%u", free); */ send(MotorControllerFrontRightDcPhaB, right.rtY.DC_phaB); break; - case 12: /* myPrintf("LeftDcPhaC free=%u", free); */ send(MotorControllerFrontLeftDcPhaC, left. rtY.DC_phaC); break; - case 13: /* myPrintf("RightDcPhaC free=%u", free); */ send(MotorControllerFrontRightDcPhaC, right.rtY.DC_phaC); break; - case 14: /* myPrintf("LeftChops free=%u", free); */ send(MotorControllerFrontLeftChops, left. chops); break; - case 15: /* myPrintf("RightChops free=%u", free); */ send(MotorControllerFrontRightChops, right.chops); break; - case 16: /* myPrintf("LeftHall free=%u", free); */ send(MotorControllerFrontLeftHall, left.hallBits()); break; - case 17: /* myPrintf("RightHall free=%u", free); */ send(MotorControllerFrontRightHall, right.hallBits()); break; - case 18: /* myPrintf("LeftVoltage free=%u", free); */ send(MotorControllerFrontLeftVoltage, batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC); break; - case 19: /* myPrintf("RightVoltage free=%u", free); */ send(MotorControllerFrontRightVoltage, batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC); break; - case 20: /* myPrintf("LeftTemp free=%u", free); */ send(MotorControllerFrontLeftTemp, board_temp_deg_c); break; - case 21: /* myPrintf("RightTemp free=%u", free); */ send(MotorControllerFrontRightTemp, board_temp_deg_c); whichToSend = 0; break; + using namespace bobbycar::can; + case 0: send(MotorController::Feedback::DcLink, left. rtU.i_DCLink); break; + case 1: send(MotorController::Feedback::DcLink, right.rtU.i_DCLink); break; + case 2: send(MotorController::Feedback::Speed, left. rtY.n_mot); break; + case 3: send(MotorController::Feedback::Speed, right.rtY.n_mot); break; + case 4: send(MotorController::Feedback::Error, left. rtY.z_errCode); break; + case 5: send(MotorController::Feedback::Error, right.rtY.z_errCode); break; + case 6: send(MotorController::Feedback::Angle, left. rtY.a_elecAngle); break; + case 7: send(MotorController::Feedback::Angle, right.rtY.a_elecAngle); break; + case 8: send(MotorController::Feedback::DcPhaA, left. rtY.DC_phaA); break; + case 9: send(MotorController::Feedback::DcPhaA, right.rtY.DC_phaA); break; + case 10: send(MotorController::Feedback::DcPhaB, left. rtY.DC_phaB); break; + case 11: send(MotorController::Feedback::DcPhaB, right.rtY.DC_phaB); break; + case 12: send(MotorController::Feedback::DcPhaC, left. rtY.DC_phaC); break; + case 13: send(MotorController::Feedback::DcPhaC, right.rtY.DC_phaC); break; + case 14: send(MotorController::Feedback::Chops, left. chops); break; + case 15: send(MotorController::Feedback::Chops, right.chops); break; + case 16: send(MotorController::Feedback::Hall, left.hallBits()); break; + case 17: send(MotorController::Feedback::Hall, right.hallBits()); break; + case 18: send(MotorController::Feedback::Voltage, batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC); break; + case 19: send(MotorController::Feedback::Voltage, batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC); break; + case 20: send(MotorController::Feedback::Temp, board_temp_deg_c); break; + case 21: send(MotorController::Feedback::Temp, board_temp_deg_c); whichToSend = 0; break; default: myPrintf("unreachable"); } }