diff --git a/config.h b/config.h index 0e1d0d7..d495c51 100644 --- a/config.h +++ b/config.h @@ -44,13 +44,7 @@ #define BAT_CALIB_REAL_VOLTAGE 3970 // input voltage measured by multimeter (multiplied by 100). For example 43.00 V * 100 = 4300 #define BAT_CALIB_ADC 1492 // adc-value measured by mainboard (value nr 5 on UART debug output) -#define BAT_CELLS 10 // battery number of cells. Normal Hoverboard battery: 10s -#define BAT_LOW_LVL1_ENABLE 0 // to beep or not to beep, 1 or 0 -#define BAT_LOW_LVL2_ENABLE 1 // to beep or not to beep, 1 or 0 -#define BAT_LOW_LVL1 (360 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // gently beeps at this voltage level. [V*100/cell]. In this case 3.60 V/cell -#define BAT_LOW_LVL2 (350 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // your battery is almost empty. Charge now! [V*100/cell]. In this case 3.50 V/cell -#define BAT_LOW_DEAD (337 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // undervoltage poweroff. (while not driving) [V*100/cell]. In this case 3.37 V/cell - +#define BAT_CELLS 12 // battery number of cells. Normal Hoverboard battery: 10s /* Board overheat detection: the sensor is inside the STM/GD chip. * It is very inaccurate without calibration (up to 45°C). So only enable this funcion after calibration! diff --git a/main.cpp b/main.cpp index 096ff1a..0845129 100644 --- a/main.cpp +++ b/main.cpp @@ -1565,36 +1565,43 @@ void applyIncomingCanMessage() switch (header.StdId) { 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::CruiseCtrlEna: left.rtP.b_cruiseCtrlEna = *((bool*)buf); break; - case MotorController::Command::CruiseCtrlEna: right.rtP.b_cruiseCtrlEna = *((bool*)buf); break; - case MotorController::Command::CruiseMotTgt: left.rtP.n_cruiseMotTgt = *((int16_T*)buf); break; - case MotorController::Command::CruiseMotTgt: right.rtP.n_cruiseMotTgt = *((int16_T*)buf); 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; + case MotorController::Command::Enable: left .enable = *((bool *)buf); break; + case MotorController ::Command::Enable: right.enable = *((bool *)buf); break; + case MotorController::Command::InpTgt: left. rtU.r_inpTgt = *((int16_t*)buf); timeoutCntLeft = 0; break; + case MotorController ::Command::InpTgt: right.rtU.r_inpTgt = *((int16_t*)buf); timeoutCntRight = 0; 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::CruiseCtrlEna: left .rtP.b_cruiseCtrlEna = *((bool*)buf); break; + case MotorController ::Command::CruiseCtrlEna: right.rtP.b_cruiseCtrlEna = *((bool*)buf); break; + case MotorController::Command::CruiseMotTgt: left .rtP.n_cruiseMotTgt = *((int16_T*)buf); break; + case MotorController ::Command::CruiseMotTgt: right.rtP.n_cruiseMotTgt = *((int16_T*)buf); break; + case MotorController::Command::BuzzerFreq: + case MotorController ::Command::BuzzerFreq: buzzer.freq = *((uint8_t*)buf); break; + case MotorController::Command::BuzzerPattern: + case MotorController ::Command::BuzzerPattern: buzzer.pattern = *((uint8_t*)buf); break; + case MotorController::Command::Led: + case MotorController ::Command::Led: + HAL_GPIO_WritePin(LED_PORT, LED_PIN, *((bool*)buf) ? GPIO_PIN_SET : GPIO_PIN_RESET); + break; + case MotorController::Command::Poweroff: + case MotorController::Command::Poweroff: +#ifdef FEATURE_BUTTON + if (*((bool*)buf)) + poweroff(); +#endif + break; default: #ifndef CAN_LOG_UNKNOWN_ADDR if constexpr (false) @@ -1609,7 +1616,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', @@ -1662,28 +1669,28 @@ void sendCanFeedback() switch (whichToSend++) { 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 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.exchange(0)); break; - case 15: send(MotorController::Feedback::Chops, right.chops.exchange(0)); break; - case 16: send(MotorController::Feedback::Hall, left.hallBits()); break; - case 17: send(MotorController::Feedback::Hall, right.hallBits()); break; + case 15: send(MotorController:: Feedback::Chops, right.chops.exchange(0)); 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; + 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"); } }