diff --git a/controller/controller.ino b/controller/controller.ino index 2c5e814..0325cb6 100644 --- a/controller/controller.ino +++ b/controller/controller.ino @@ -43,8 +43,8 @@ uint8_t modeled_green=0; uint8_t modeled_red=0; long last_ledupdate=0; -#define PIN_RELAISFRONT PB14 //connected to relais which presses the powerbutton of the hoverboard for the front wheels -#define PIN_RELAISREAR PB15 //connected to relais which presses the powerbutton of the hoverboard for the rear wheels +#define PIN_RELAISFRONT PB15 //connected to relais which presses the powerbutton of the hoverboard for the front wheels +#define PIN_RELAISREAR PB14 //connected to relais which presses the powerbutton of the hoverboard for the rear wheels #define DEBOUNCETIME 20 //time to not check for inputs after key press #define BUTTONTIMEHOLD 750 //time for button hold @@ -79,10 +79,10 @@ float adc_throttle=0; //filtered value int adc_brake_raw=0; //raw throttle value from adc float adc_brake=0; //filtered value -uint16_t out_speedFL=0; -uint16_t out_speedFR=0; -uint16_t out_speedRL=0; -uint16_t out_speedRR=0; +int16_t out_speedFL=0; +int16_t out_speedFR=0; +int16_t out_speedRL=0; +int16_t out_speedRR=0; long last_send = 0; @@ -101,8 +101,7 @@ byte incomingBytePrev1; long lastValidDataSerial1_time; long board1lastPoweron=0; //mainly for failcheck long board1lastPoweroff=0; -long board1lastFeedbackMinSpeed; -boolean board1lastFeedbackMinSpeed_above=false; + //Same for Serial2 (Front) uint8_t idx2 = 0; // Index for new data pointer @@ -113,8 +112,6 @@ byte incomingBytePrev2; long lastValidDataSerial2_time; long board2lastPoweron=0; //mainly for failcheck long board2lastPoweroff=0; -long board2lastFeedbackMinSpeed; -boolean board2lastFeedbackMinSpeed_above=false; typedef struct{ uint16_t start; @@ -137,7 +134,7 @@ typedef struct{ int16_t boardTemp; int16_t curL_DC; int16_t curR_DC; - int16_t checksum; + uint16_t checksum; } SerialFeedback; SerialFeedback Feedback1; SerialFeedback NewFeedback1; @@ -352,6 +349,15 @@ void handleModeChange() { //TODO: led show aborted modechange }else{ //everythings fine, turn on/off digitalWrite(PIN_RELAISFRONT,HIGH); //simulate hoverboard power button press + //Front board is Serial2 + if (requestmode==on) { + board2Enabled=true; //assume board is online + board2lastPoweron=loopmillis; //save time at which board was powered on + // ### Request Idle or Off ### + }else if(requestmode==idle || requestmode==off) { + board2Enabled=false; //assume board is offline + board2lastPoweroff=loopmillis; //save time at which board was powered off + } state_modechange++; state_modechange_time=loopmillis; //set to current time Serial.println("PIN_RELAISFRONT,HIGH"); @@ -368,6 +374,15 @@ void handleModeChange() { case 2: if (loopmillis - state_modechange_time > 200) { //wait some time digitalWrite(PIN_RELAISREAR,HIGH); //simulate hoverboard power button press + //Rear board is Serial1 + if (requestmode==on) { + board1Enabled=true; //assume board is online + board1lastPoweron=loopmillis; //save time at which board was powered on + // ### Request Idle or Off ### + }else if(requestmode==idle || requestmode==off) { + board1Enabled=false; //assume board is offline + board1lastPoweroff=loopmillis; //save time at which board was powered off + } state_modechange++; state_modechange_time=loopmillis; //set to current time Serial.println("PIN_RELAISREAR,HIGH"); @@ -388,27 +403,6 @@ void handleModeChange() { Serial.println("Waiting finished"); } break; - case 5: - // ### Request On ### - if (requestmode==on) {//wait for both boards to send feedback - state_modechange++; - board1Enabled=true; //assume board is online - board1lastPoweron=loopmillis; //save time at which board was powered on - board2Enabled=true; //assume board is online - board2lastPoweron=loopmillis; //save time at which board was powered on - // ### Request Idle or Off (both power boards off) ### - }else if(requestmode==idle || requestmode==off) { - state_modechange++; - board1Enabled=false; //assume board is offline - board1lastPoweroff=loopmillis; //save time at which board was powered off - board2Enabled=false; //assume board is offline - board2lastPoweroff=loopmillis; //save time at which board was powered off - - Serial.println("finished"); - }else{ //if changed off from error mode - state_modechange++; - } - break; default: //finished modechange currentmode=requestmode; state_modechange=0; @@ -520,8 +514,8 @@ boolean boardsPowered() void failChecks() { - #define FAILCHECK_WAITCHECK_AFTER_POWEROFF_TIME 3000 //time to start failchecking boardpower after board poweroff - #define FAILCHECK_RECEIVERECENT_TIME 1000 //timeout .should be less than FAILCHECK_WAITCHECK_AFTER_POWEROFF_TIME + #define FAILCHECK_WAITCHECK_AFTER_POWEROFF_TIME 1000 //time to start failchecking boardpower after board poweroff + #define FAILCHECK_RECEIVERECENT_TIME 100 //timeout .should be less than FAILCHECK_WAITCHECK_AFTER_POWEROFF_TIME and greater than send delay from mainboard // ## Check if board is really offline ## if (!board1Enabled) { //board should be offline if (loopmillis-board1lastPoweroff > FAILCHECK_WAITCHECK_AFTER_POWEROFF_TIME){ //wait some time before checking if board did power off @@ -542,29 +536,12 @@ void failChecks() } } - #define MINSPEED_FOR_FEEDBACK 250 //speed at which feedback output should be expected - #define RESETSPEED_FOR_FEEDBACK 50 //should be less than MINSPEED_FOR_FEEDBACK. speed at which board2lastFeedbackMinSpeed will be able to be reset - if (!board2lastFeedbackMinSpeed_above && ( abs(out_speedFL) > MINSPEED_FOR_FEEDBACK || abs(out_speedFR) > MINSPEED_FOR_FEEDBACK ) ){ - board2lastFeedbackMinSpeed=loopmillis; //front is board 2 - board2lastFeedbackMinSpeed_above=true; - } - if (board2lastFeedbackMinSpeed_above && abs(out_speedFL) < RESETSPEED_FOR_FEEDBACK && abs(out_speedFR) < RESETSPEED_FOR_FEEDBACK) { //if speed of both wheels goes below a threshold, board2lastFeedbackMinSpeed will be able to reset - board2lastFeedbackMinSpeed_above=false; - } - if (!board1lastFeedbackMinSpeed_above && ( abs(out_speedRL) > MINSPEED_FOR_FEEDBACK || abs(out_speedRR) > MINSPEED_FOR_FEEDBACK ) ){ - board1lastFeedbackMinSpeed=loopmillis; //rear is board 1 - board1lastFeedbackMinSpeed_above=true; - } - if (board1lastFeedbackMinSpeed_above && abs(out_speedRL) < RESETSPEED_FOR_FEEDBACK && abs(out_speedRR) < RESETSPEED_FOR_FEEDBACK) { //if speed of both wheels goes below a threshold, board2lastFeedbackMinSpeed will be able to reset - board1lastFeedbackMinSpeed_above=false; - } - - #define FAILCHECK_WAITCHECK_AFTER_MINSPEED_TIME 3000 //time to start failchecking boardpower after minimum throttle that should give some feedback + #define FAILCHECK_WAITCHECK_AFTER_POWERON_TIME 2000 //time to start failchecking boardpower after startup // ## Check if board is online (when it should send feedback) ## if (board1Enabled) { //board should be online - if (loopmillis-board1lastFeedbackMinSpeed > FAILCHECK_WAITCHECK_AFTER_MINSPEED_TIME) { //wait some time before checking - if (board1lastFeedbackMinSpeed_above && loopmillis-lastValidDataSerial1_time > FAILCHECK_RECEIVERECENT_TIME) { //speed still high enough but no new messages recently received? + if (loopmillis-board1lastPoweron > FAILCHECK_WAITCHECK_AFTER_POWERON_TIME) { //wait some time before checking + if (loopmillis-lastValidDataSerial1_time > FAILCHECK_RECEIVERECENT_TIME) { //speed still high enough but no new messages recently received? errormessage="Board 1 should be online and give feedback but didnt"; Serial.println(errormessage); requestmode=error; @@ -572,8 +549,8 @@ void failChecks() } } if (board2Enabled) { //board should be online - if (loopmillis-board2lastFeedbackMinSpeed > FAILCHECK_WAITCHECK_AFTER_MINSPEED_TIME) { //wait some time before checking - if (board2lastFeedbackMinSpeed_above && loopmillis-lastValidDataSerial2_time > FAILCHECK_RECEIVERECENT_TIME) { //speed still high enough but no new messages recently received? + if (loopmillis-board2lastPoweron > FAILCHECK_WAITCHECK_AFTER_POWERON_TIME) { //wait some time before checking + if (loopmillis-lastValidDataSerial2_time > FAILCHECK_RECEIVERECENT_TIME) { //no new messages recently received? errormessage="Board 2 should be online and give feedback but didnt"; Serial.println(errormessage); requestmode=error; diff --git a/hoverboard-firmware-hack-foc-serial-esc b/hoverboard-firmware-hack-foc-serial-esc index fc5f5a2..b9bf849 160000 --- a/hoverboard-firmware-hack-foc-serial-esc +++ b/hoverboard-firmware-hack-foc-serial-esc @@ -1 +1 @@ -Subproject commit fc5f5a26ca648aa30aec3fc4904a3a7560c11baa +Subproject commit b9bf849330f8e7355d0881300ebdc59607845e2d