From 24c254dd026361779001f769bcecacb5fa6a75c2 Mon Sep 17 00:00:00 2001 From: Michael Nilsson Date: Mon, 5 Jul 2021 20:26:56 +0200 Subject: [PATCH 1/2] Settings versioning and validation + Setting for cut perimeter ratio + Setting for steering correction --- RazorBoard/Core/Inc/sram.h | 12 ++- RazorBoard/Core/Src/help.c | 209 +++++++++++++------------------------ RazorBoard/Core/Src/main.c | 43 +++++--- RazorBoard/Core/Src/sram.c | 132 ++++++++++++++++++++++- 4 files changed, 241 insertions(+), 155 deletions(-) diff --git a/RazorBoard/Core/Inc/sram.h b/RazorBoard/Core/Inc/sram.h index 1ae7183..6da1eec 100644 --- a/RazorBoard/Core/Inc/sram.h +++ b/RazorBoard/Core/Inc/sram.h @@ -9,6 +9,7 @@ #define INC_SRAM_H_ static const char VERSION[] = "Version 1.0.7"; +static const uint8_t global_settings_version = 45; #define CONFIG_SET_ADDR 0x01 //uint8_t #define GO_GOME_DIRECTION_ADDR 0x02 //uint8_t @@ -21,6 +22,7 @@ static const char VERSION[] = "Version 1.0.7"; #define MOVE_COUNT_ADDR 0x09 //uint8_t #define BUMPER_COUNT_ADDR 0x0A //uint8_t #define UNDOCK_BACKING_SECONDS_ADDR 0x0B //uint8_t +#define CUT_PERIMETER_RATIO_ADDR 0x0C //uint8_t #define HOLDCHARGEDETECTION_ADDR 0x32 //uint16_t #define MAGVALUE_ADDR 0x34 //uint16_t @@ -32,6 +34,7 @@ static const char VERSION[] = "Version 1.0.7"; #define BATTERYCHARGETIME_ADDR 0x40 //uint16_t #define PERIMETERTRACKERSPEED_ADDR 0x42 //uint16_t #define ROLL_TILT_COMP_ADDR 0x44 //uint16_t +#define STEERING_CORRECTION_ADDR 0x46 //uint16_t #define BATTERY_LOW_LIMIT_ADDR 0x64 //uint32_t #define BATTERY_HIGH_LIMIT_ADDR 0x68 //uint32_t @@ -75,16 +78,18 @@ typedef struct SRAM { uint8_t move_count_limit; uint8_t bumper_count_limit; uint8_t undock_backing_seconds; + uint8_t cut_perimeter_ratio; uint16_t HoldChargeDetection; uint16_t magValue; uint16_t magMinValue; uint16_t motorMaxSpeed; - uint16_t motorMinSpeed; + uint16_t motorMinSpeed; uint16_t cutterSpeed; uint16_t adcLevel; uint16_t BatteryChargeTime; uint16_t perimeterTrackerSpeed; uint16_t roll_tilt_comp; + uint16_t steering_correction; float Battery_Low_Limit; float Battery_High_Limit; float Signal_Integrity_IN; @@ -102,7 +107,6 @@ typedef struct SRAM { float roll_comp; float pitch_comp; float highgrass_Limit; - } sram_settings; extern void enable_backup_sram(void); @@ -124,6 +128,10 @@ extern float read_sram_float(uint8_t); extern sram_settings read_all_settings(void); extern void write_all_settings(sram_settings w_settings); extern void save_default_settings(uint8_t revision); +extern uint8_t validate_settings(uint8_t revision); + +#define CONFIG_NOT_FOUND 0 +#define CONFIG_FOUND 1 #endif /* INC_SRAM_H_ */ diff --git a/RazorBoard/Core/Src/help.c b/RazorBoard/Core/Src/help.c index c8dfa0f..4acc8ef 100644 --- a/RazorBoard/Core/Src/help.c +++ b/RazorBoard/Core/Src/help.c @@ -33,11 +33,11 @@ void show_config(sram_settings settings) { Serial_Console(msg); sprintf(msg, "WorkingHourEnd: %d\r\n", settings.WorkingHourEnd); Serial_Console(msg); - sprintf(msg, "Overturn_Limit: %d\r\n", settings.Overturn_Limit); + sprintf(msg, "Overturn limit: %d\r\n", settings.Overturn_Limit); Serial_Console(msg); sprintf(msg, "MotorSpeedUpdateFreq: %d\r\n", settings.MotorSpeedUpdateFreq); Serial_Console(msg); - sprintf(msg, "Outside_Threshold: %d\r\n", settings.Outside_Threshold); + sprintf(msg, "Outside threshold: %d\r\n", settings.Outside_Threshold); Serial_Console(msg); sprintf(msg, "HoldChargeDetection: %d\r\n", settings.HoldChargeDetection); Serial_Console(msg); @@ -63,6 +63,8 @@ void show_config(sram_settings settings) { Serial_Console(msg); sprintf(msg, "Roll/tilt comp: %d\r\n", settings.roll_tilt_comp); Serial_Console(msg); + sprintf(msg, "Steering correction: %d\r\n", settings.steering_correction); + Serial_Console(msg); sprintf(msg, "KP: %.4f\r\n", settings.kp); Serial_Console(msg); sprintf(msg, "KI: %.4f\r\n", settings.ki); @@ -77,7 +79,7 @@ void show_config(sram_settings settings) { Serial_Console(msg); sprintf(msg, "Proximity Speed: %.2f\r\n", settings.proximitySpeed); Serial_Console(msg); - sprintf(msg, "Movement Limit: %.2f\r\n", settings.movement); + sprintf(msg, "Movement limit: %.2f\r\n", settings.movement); Serial_Console(msg); sprintf(msg, "Motor Max Speed: %d\r\n", settings.motorMaxSpeed); Serial_Console(msg); @@ -85,9 +87,11 @@ void show_config(sram_settings settings) { Serial_Console(msg); sprintf(msg, "Perimeter Tracker Speed: %d\r\n", settings.perimeterTrackerSpeed); Serial_Console(msg); + sprintf(msg, "Cut perimeter ratio: %d\r\n", settings.cut_perimeter_ratio); + Serial_Console(msg); sprintf(msg, "Cutter Speed: %d\r\n", settings.cutterSpeed); Serial_Console(msg); - sprintf(msg, "Movement limit: %d\r\n", settings.move_count_limit); + sprintf(msg, "Movement count limit: %d\r\n", settings.move_count_limit); Serial_Console(msg); sprintf(msg, "Bumper limit: %d\r\n", settings.bumper_count_limit); Serial_Console(msg); @@ -102,135 +106,72 @@ void show_config(sram_settings settings) { } void help(void) { - sprintf(msg, "Available commands:\r\n\r\n"); - Serial_Console(msg); - sprintf(msg, "HELLO - Welcome message\r\n"); - Serial_Console(msg); - sprintf(msg, "REBOOT - Reboot Razorboard\r\n"); - Serial_Console(msg); - sprintf(msg, "DISABLE - Disable Razorboard\r\n"); - Serial_Console(msg); - sprintf(msg, "ENABLE - Enable Razorboard\r\n"); - Serial_Console(msg); - sprintf(msg, "VERSION - Show version of board\r\n"); - Serial_Console(msg); - sprintf(msg, "DEBUG ON - Enable debug messages\r\n"); - Serial_Console(msg); - sprintf(msg, "DEBUG OFF - Disable debug messages\r\n"); - Serial_Console(msg); - sprintf(msg, "VOLTAGE - Show current voltage\r\n"); - Serial_Console(msg); - sprintf(msg, "UPGRADE - Enter bootloader\r\n"); - Serial_Console(msg); - sprintf(msg, "SHOW SIG - Show reference BWF signature\r\n"); - Serial_Console(msg); - sprintf(msg, "EXPORT SIG - Export reference BWF signature as an array\r\n"); - Serial_Console(msg); - sprintf(msg, "RECORD SIG - Record a new signature\r\n"); - Serial_Console(msg); - sprintf(msg, "TEST LEFT MOTOR - Test left motor (M1)\r\n"); - Serial_Console(msg); - sprintf(msg, "TEST RIGHT MOTOR - Test right motor (M2)\r\n"); - Serial_Console(msg); - sprintf(msg, "SHOW CURRENT - Show current sensors M1, M2, C1\r\n"); - Serial_Console(msg); - sprintf(msg, "STOP MOTORS - Stop motors\r\n"); - Serial_Console(msg); - sprintf(msg, "RUN MOTORS FORWARD - Run motors forward\r\n"); - Serial_Console(msg); - sprintf(msg, "RUN MOTORS REVERSE - Run motors backward\r\n"); - Serial_Console(msg); - sprintf(msg, "SET PROXIMITY SPEED - Set proximity speed\r\n"); - Serial_Console(msg); - sprintf(msg, "SET VOLTAGE MULTIPLY - Voltage Multiply for calculating voltage\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MOTOR MAX LIMIT - Set Motor Max Limit in amp\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MOTOR MIN LIMIT - Set Motor Min Limit in amp\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BOUNDARY TIMEOUT - How many seconds without INSIDE before HALT\r\n"); - Serial_Console(msg); - sprintf(msg, "SET OVERTURN LIMIT - How many degrees it can tilt before HALT\r\n"); - Serial_Console(msg); - sprintf(msg, "SET OUTSIDE LIMIT - How many seconds OUTSIDE before HALT\r\n"); - Serial_Console(msg); - sprintf(msg, "SET CHARGE DETECTION - How many (ms) from detecting charge to STOP\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BAT LOW - Limit when considering charge needed\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BAT HIGH - Limit when considering battery full\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BAT CHARGER TIME - How many minutes to charge battery\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BWF OUT - Limit for considering BWF OUT\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BWF IN - Limit for considering BWF IN\r\n"); - Serial_Console(msg); - sprintf(msg, "SET CUTTER LIMIT - Set Cutter Motor Limit in Amp\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MOTOR LIMIT - Set Motor Limit, in multiply, default = 3.0\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MOVEMENT LIMIT - Set Movement Limit for detecting movement\r\n"); - Serial_Console(msg); - sprintf(msg, "SET ADC LEVEL - Set the ADC level for BWF\r\n"); - Serial_Console(msg); - sprintf(msg, "SET CUTTER SPEED - Set speed of cutter motor\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MOVEMENT COUNT LIMIT - Set limit for movement detection before HALT\r\n"); - Serial_Console(msg); - sprintf(msg, "SET BUMPER COUNT LIMIT - Set limit for bumper detection before HALT\r\n"); - Serial_Console(msg); - sprintf(msg, "SET UNDOCK BACKING SECONDS - Set number of seconds to move backwards when undocking\r\n"); - Serial_Console(msg); - sprintf(msg, "SET PITCH COMP - Compensate pitch if not perfectly leveled\r\n"); - Serial_Console(msg); - sprintf(msg, "SET ROLL COMP - Compensate roll if not perfectly leveled\r\n"); - Serial_Console(msg); - sprintf(msg, "SET ROLL TILT COMP - Compensate wheel power ratio when turning based on roll\r\n"); - Serial_Console(msg); - sprintf(msg, "SET HIGHGRASS LIMIT - When to trigger High Grass, in Amps\r\n"); - Serial_Console(msg); - sprintf(msg, "LOCK DOCKING - Do NOT allow mower to undock when ready\r\n"); - Serial_Console(msg); - sprintf(msg, "UNLOCK DOCKING - Do allow mower to undock when ready\r\n"); - Serial_Console(msg); - sprintf(msg, "SET TIME - Set current time for RTC\r\n"); - Serial_Console(msg); - sprintf(msg, "SET DATE - Set current date for RTC\r\n"); - Serial_Console(msg); - sprintf(msg, " Date must be set in a special order:\r\n"); - Serial_Console(msg); - sprintf(msg, " Year Month Day Weekday -> 21 3 31 2 (2 = Tuesday)\r\n"); - Serial_Console(msg); - sprintf(msg, "TRACK PERIMETER - Track perimeter next time it crosses\r\n"); - Serial_Console(msg); - sprintf(msg, "SET PERIMETER SPEED - Set track perimeter speed\r\n"); - Serial_Console(msg); - sprintf(msg, "SET KP - PID Controller KP for Perimeter Tracking\r\n"); - Serial_Console(msg); - sprintf(msg, "SET KI - PID Controller KI for Perimeter Tracking\r\n"); - Serial_Console(msg); - sprintf(msg, "SET KD - PID Controller KD for Perimeter Tracking\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MAG VALUE - Set Magnitude value for BWF proximity\r\n"); - Serial_Console(msg); - sprintf(msg, "SET MAGMIN VALUE - Set Magnitude Min value for BWF proximity\r\n"); - Serial_Console(msg); - sprintf(msg, "SET WORKING START - Set Working Hour START\r\n"); - Serial_Console(msg); - sprintf(msg, "SET WORKING END - Set Working Hour END\r\n"); - Serial_Console(msg); + Serial_Console("Available commands:\r\n\r\n"); + Serial_Console("HELLO - Welcome message\r\n"); + Serial_Console("REBOOT - Reboot Razorboard\r\n"); + Serial_Console("DISABLE - Disable Razorboard\r\n"); + Serial_Console("ENABLE - Enable Razorboard\r\n"); + Serial_Console("VERSION - Show version of board\r\n"); + Serial_Console("DEBUG ON - Enable debug messages\r\n"); + Serial_Console("DEBUG OFF - Disable debug messages\r\n"); + Serial_Console("VOLTAGE - Show current voltage\r\n"); + Serial_Console("UPGRADE - Enter bootloader\r\n"); + Serial_Console("SHOW SIG - Show reference BWF signature\r\n"); + Serial_Console("EXPORT SIG - Export reference BWF signature as an array\r\n"); + Serial_Console("RECORD SIG - Record a new signature\r\n"); + Serial_Console("TEST LEFT MOTOR - Test left motor (M1)\r\n"); + Serial_Console("TEST RIGHT MOTOR - Test right motor (M2)\r\n"); + Serial_Console("SHOW CURRENT - Show current sensors M1, M2, C1\r\n"); + Serial_Console("STOP MOTORS - Stop motors\r\n"); + Serial_Console("RUN MOTORS FORWARD - Run motors forward\r\n"); + Serial_Console("RUN MOTORS REVERSE - Run motors backward\r\n"); + Serial_Console("SET PROXIMITY SPEED - Set proximity speed\r\n"); + Serial_Console("SET VOLTAGE MULTIPLY - Voltage Multiply for calculating voltage\r\n"); + Serial_Console("SET MOTOR MAX LIMIT - Set Motor Max Limit in amp\r\n"); + Serial_Console("SET MOTOR MIN LIMIT - Set Motor Min Limit in amp\r\n"); + Serial_Console("SET BOUNDARY TIMEOUT - How many seconds without INSIDE before HALT\r\n"); + Serial_Console("SET OVERTURN LIMIT - How many degrees it can tilt before HALT\r\n"); + Serial_Console("SET OUTSIDE LIMIT - How many seconds OUTSIDE before HALT\r\n"); + Serial_Console("SET CHARGE DETECTION - How many (ms) from detecting charge to STOP\r\n"); + Serial_Console("SET BAT LOW - Limit when considering charge needed\r\n"); + Serial_Console("SET BAT HIGH - Limit when considering battery full\r\n"); + Serial_Console("SET BAT CHARGER TIME - How many minutes to charge battery\r\n"); + Serial_Console("SET BWF OUT - Limit for considering BWF OUT\r\n"); + Serial_Console("SET BWF IN - Limit for considering BWF IN\r\n"); + Serial_Console("SET CUTTER LIMIT - Set Cutter Motor Limit in Amp\r\n"); + Serial_Console("SET MOTOR LIMIT - Set Motor Limit, in multiply, default = 3.0\r\n"); + Serial_Console("SET MOVEMENT LIMIT - Set Movement Limit for detecting movement\r\n"); + Serial_Console("SET ADC LEVEL - Set the ADC level for BWF\r\n"); + Serial_Console("SET CUTTER SPEED - Set speed of cutter motor\r\n"); + Serial_Console("SET MOVEMENT COUNT LIMIT - Set limit for movement detection before HALT\r\n"); + Serial_Console("SET BUMPER COUNT LIMIT - Set limit for bumper detection before HALT\r\n"); + Serial_Console("SET UNDOCK BACKING SECONDS - Set number of seconds to move backwards when undocking\r\n"); + Serial_Console("SET PITCH COMP - Compensate pitch if not perfectly leveled\r\n"); + Serial_Console("SET ROLL COMP - Compensate roll if not perfectly leveled\r\n"); + Serial_Console("SET ROLL TILT COMP - Compensate wheel power ratio when turning based on roll\r\n"); + Serial_Console("SET STEERING CORRECTION - Compensate wheel power ratio when drifting\r\n"); + Serial_Console("SET HIGHGRASS LIMIT - When to trigger High Grass, in Amps\r\n"); + Serial_Console("LOCK DOCKING - Do NOT allow mower to undock when ready\r\n"); + Serial_Console("UNLOCK DOCKING - Do allow mower to undock when ready\r\n"); + Serial_Console("SET TIME - Set current time for RTC\r\n"); + Serial_Console("SET DATE - Set current date for RTC\r\n"); + Serial_Console(" Date must be set in a special order:\r\n"); + Serial_Console(" Year Month Day Weekday -> 21 3 31 2 (2 = Tuesday)\r\n"); + Serial_Console("TRACK PERIMETER - Track perimeter next time it crosses\r\n"); + Serial_Console("SET PERIMETER SPEED - Set track perimeter speed\r\n"); + Serial_Console("SET PERIMETER CUT RATIO - Set ratio for cutting perimeter wire (0-100)\r\n"); + Serial_Console("SET KP - PID Controller KP for Perimeter Tracking\r\n"); + Serial_Console("SET KI - PID Controller KI for Perimeter Tracking\r\n"); + Serial_Console("SET KD - PID Controller KD for Perimeter Tracking\r\n"); + Serial_Console("SET MAG VALUE - Set Magnitude value for BWF proximity\r\n"); + Serial_Console("SET MAGMIN VALUE - Set Magnitude Min value for BWF proximity\r\n"); + Serial_Console("SET WORKING START - Set Working Hour START\r\n"); + Serial_Console("SET WORKING END - Set Working Hour END\r\n"); Serial_Console("\r\n"); - sprintf(msg, "LOAD CONFIG - Load config from SRAM\r\n"); - Serial_Console(msg); - sprintf(msg, "SAVE CONFIG - Save config to SRAM\r\n"); - Serial_Console(msg); - sprintf(msg, "SAVE DEFAULT CONFIG - Save default config to SRAM\r\n"); - Serial_Console(msg); - sprintf(msg, "SHOW CONFIG - Show config from SRAM\r\n"); - Serial_Console(msg); - sprintf(msg, "SHOW ERRORS - Show error log\r\n"); - Serial_Console(msg); - sprintf(msg, "CLEAR ERRORS - Clear error log\r\n"); - Serial_Console(msg); + Serial_Console("LOAD CONFIG - Load config from SRAM\r\n"); + Serial_Console("SAVE CONFIG - Save config to SRAM\r\n"); + Serial_Console("SAVE DEFAULT CONFIG - Save default config to SRAM\r\n"); + Serial_Console("SHOW CONFIG - Show config from SRAM\r\n"); + Serial_Console("SHOW ERRORS - Show error log\r\n"); + Serial_Console("CLEAR ERRORS - Clear error log\r\n"); } diff --git a/RazorBoard/Core/Src/main.c b/RazorBoard/Core/Src/main.c index 942234e..f9be504 100644 --- a/RazorBoard/Core/Src/main.c +++ b/RazorBoard/Core/Src/main.c @@ -553,7 +553,7 @@ uint32_t rnd(uint32_t maxValue) { uint32_t rndnum; rndnum = HAL_RNG_GetRandomNumber(&hrng) % maxValue; - return rndnum; + return rndnum + 1; } void CheckMotorCurrent(int RAW) { @@ -1189,6 +1189,12 @@ void parseCommand_Console(void) { sscanf(Command, "%s %s %s %f", cmd1, cmd2, cmd3, &roll); settings.roll_comp = roll; } + if (strncmp(Command, "SET STEERING CORRECTION", 23) == 0) { + uint16_t correction; + char cmd1[3], cmd2[8], cmd3[10]; + sscanf(Command, "%s %s %s %hd", cmd1, cmd2, cmd3, &correction); + settings.steering_correction = correction; + } if (strncmp(Command, "SET PROXIMITY SPEED", 19) == 0) { float speed; char cmd1[3], cmd2[9], cmd3[5]; @@ -1227,7 +1233,7 @@ void parseCommand_Console(void) { } if (strncmp(Command, "SET ROLL TILT COMP", 18) == 0) { int comp; - char cmd1[3], cmd2[5], cmd3[3], cmd4[5]; + char cmd1[3], cmd2[4], cmd3[4], cmd4[4]; sscanf(Command, "%s %s %s %s %d", cmd1, cmd2, cmd3, cmd4, &comp); settings.roll_tilt_comp = comp; } @@ -1237,6 +1243,12 @@ void parseCommand_Console(void) { sscanf(Command, "%s %s %s %d", cmd1, cmd2, cmd3, &speed); settings.perimeterTrackerSpeed = speed; } + if (strncmp(Command, "CUT PERIMETER RATIO", 19) == 0) { + int ratio; + char cmd1[3], cmd2[9], cmd3[5]; + sscanf(Command, "%s %s %s %d", cmd1, cmd2, cmd3, &ratio); + settings.cut_perimeter_ratio = ratio; + } if (strncmp(Command, "SET ADC LEVEL", 13) == 0) { int adc; char cmd1[3], cmd2[3], cmd3[5]; @@ -1536,8 +1548,8 @@ uint8_t CheckSecurity(void) { void cutterHardBreak() { // Cutter disc hard brake - TIM3->CCR1 = settings.motorMaxSpeed; // Motor will hard brake when both "pins" go HIGH - TIM3->CCR2 = settings.motorMaxSpeed; + TIM3->CCR1 = 3359; // Motor will hard brake when both "pins" go HIGH + TIM3->CCR2 = 3359; HAL_Delay(3000); cutterOFF(); } @@ -1789,7 +1801,7 @@ void UpdateMotorSpeed() { // Calculate the difference in bearing, 0-360 accounted for. (Circular heading) diff = (((((int) mpu.heading - (int) mpu.hold_heading) % 360) + 540) % 360) - 180; - diff *= 60.0; + diff *= settings.steering_correction; if (diff < 0) { dir = -1; @@ -2054,10 +2066,10 @@ void MotorHardBrake(void) { State = HARDBRAKE; // Wheels will do a hard brake when both pins go HIGH. - MOTOR_LEFT_BACKWARD = settings.motorMaxSpeed; - MOTOR_LEFT_FORWARD = settings.motorMaxSpeed; - MOTOR_RIGHT_FORWARD = settings.motorMaxSpeed; - MOTOR_RIGHT_BACKWARD = settings.motorMaxSpeed; + MOTOR_LEFT_BACKWARD = 3359; + MOTOR_LEFT_FORWARD = 3359; + MOTOR_RIGHT_FORWARD = 3359; + MOTOR_RIGHT_BACKWARD = 3359; HAL_Delay(250); @@ -2131,7 +2143,9 @@ void CheckState(void) { highgrass_slowdown = 0; TimeToGoHome(); // Check if within working hours, if not, go home if (perimeterTracking == 1) { - cutterOFF(); + if (rnd(100) > settings.cut_perimeter_ratio) { + cutterOFF(); + } perimeterTrackingActive = 1; delay(500); GoHome_timer_IN = HAL_GetTick(); @@ -2320,15 +2334,15 @@ int main(void) { delay_us(100); - settings = read_all_settings(); - if (settings.Config_Set != 44) { - save_default_settings(board_revision); + + if (validate_settings(board_revision) == CONFIG_NOT_FOUND) { add_error_event("No config found"); Serial_Console("No config found - Saving factory defaults\r\n"); Serial_Console("Masterswitch set to OFF - please configure and save settings, then reboot\r\n"); MasterSwitch = 0; - settings = read_all_settings(); } + + settings = read_all_settings(); Serial_Console("Config loaded from SRAM\r\n"); for (uint8_t x = 0; x < 60; x++) { @@ -2366,6 +2380,7 @@ int main(void) { ChargerConnected(); if (Docked == 1) { + cutterOFF(); unDock(); } diff --git a/RazorBoard/Core/Src/sram.c b/RazorBoard/Core/Src/sram.c index b622cea..cc7b242 100644 --- a/RazorBoard/Core/Src/sram.c +++ b/RazorBoard/Core/Src/sram.c @@ -12,6 +12,7 @@ #include extern RTC_HandleTypeDef hrtc; +sram_settings get_default_settings(uint8_t revision); uint8_t read_sram_errorlog(uint16_t addr) { uint8_t i_retval; @@ -219,6 +220,7 @@ sram_settings read_all_settings(void) { settings.move_count_limit = read_sram_uint8(MOVE_COUNT_ADDR); settings.bumper_count_limit = read_sram_uint8(BUMPER_COUNT_ADDR); settings.undock_backing_seconds = read_sram_uint8(UNDOCK_BACKING_SECONDS_ADDR); + settings.cut_perimeter_ratio = read_sram_uint8(CUT_PERIMETER_RATIO_ADDR); settings.HoldChargeDetection = read_sram_uint16(HOLDCHARGEDETECTION_ADDR); settings.magValue = read_sram_uint16(MAGVALUE_ADDR); @@ -229,6 +231,7 @@ sram_settings read_all_settings(void) { settings.cutterSpeed = read_sram_uint16(CUTTERSPEED_ADDR); settings.adcLevel = read_sram_uint16(ADC_LEVEL_ADDR); settings.roll_tilt_comp = read_sram_uint16(ROLL_TILT_COMP_ADDR); + settings.steering_correction = read_sram_uint16(STEERING_CORRECTION_ADDR); settings.Battery_High_Limit = read_sram_float(BATTERY_HIGH_LIMIT_ADDR); settings.Battery_Low_Limit = read_sram_float(BATTERY_LOW_LIMIT_ADDR); @@ -277,7 +280,7 @@ void write_all_settings(sram_settings settings) { //float = 4 byte // uint8_t & int8_t - settings.Config_Set = 44; + settings.Config_Set = global_settings_version; write_sram_uint8(settings.Config_Set, CONFIG_SET_ADDR); write_sram_uint8(settings.Go_Home_Direction, GO_GOME_DIRECTION_ADDR); write_sram_uint8(settings.Boundary_Timeout, BOUNDARY_TIMEOUT_ADDR); @@ -289,6 +292,7 @@ void write_all_settings(sram_settings settings) { write_sram_uint8(settings.move_count_limit, MOVE_COUNT_ADDR); write_sram_uint8(settings.bumper_count_limit, BUMPER_COUNT_ADDR); write_sram_uint8(settings.undock_backing_seconds, UNDOCK_BACKING_SECONDS_ADDR); + write_sram_uint8(settings.cut_perimeter_ratio, CUT_PERIMETER_RATIO_ADDR); // uint16_t @@ -302,6 +306,7 @@ void write_all_settings(sram_settings settings) { write_sram_uint16(settings.adcLevel, ADC_LEVEL_ADDR); write_sram_uint16(settings.BatteryChargeTime, BATTERYCHARGETIME_ADDR); write_sram_uint16(settings.roll_tilt_comp, ROLL_TILT_COMP_ADDR); + write_sram_uint16(settings.steering_correction, STEERING_CORRECTION_ADDR); // uint32_t & float @@ -327,9 +332,15 @@ void write_all_settings(sram_settings settings) { void save_default_settings(uint8_t revision) { + sram_settings defaultSettings = get_default_settings(revision); + write_all_settings(defaultSettings); +} + +sram_settings get_default_settings(uint8_t revision) { + sram_settings settings; - settings.Config_Set = 44; + settings.Config_Set = global_settings_version; settings.Go_Home_Direction = 0; settings.Battery_Low_Limit = 22.00; settings.Battery_High_Limit = 25.00; @@ -363,9 +374,11 @@ void save_default_settings(uint8_t revision) { settings.pitch_comp = 0.0; settings.roll_comp = 0.0; settings.roll_tilt_comp = 50; + settings.steering_correction = 120; settings.highgrass_Limit = 1.5; - settings.BatteryChargeTime = 60; - settings.perimeterTrackerSpeed = 3360 - 1; + settings.BatteryChargeTime = 60; + settings.perimeterTrackerSpeed = 3360 - 1; + settings.cut_perimeter_ratio = 0; if (revision == 12) { settings.adcLevel = 2050; @@ -374,6 +387,115 @@ void save_default_settings(uint8_t revision) { settings.adcLevel = 1267; } - write_all_settings(settings); + return settings; +} + +uint8_t validate_settings(uint8_t revision) { + sram_settings settings = read_all_settings(); + sram_settings defaultSettings = get_default_settings(revision); + + // Validate new version settings + if (settings.Config_Set < 42 || settings.Config_Set > global_settings_version) { + write_all_settings(defaultSettings); + return CONFIG_NOT_FOUND; + } + + if (settings.Config_Set < 43) { + settings.undock_backing_seconds = defaultSettings.undock_backing_seconds; + } + + if (settings.Config_Set < 44) { + settings.BatteryChargeTime = defaultSettings.BatteryChargeTime; + settings.perimeterTrackerSpeed = defaultSettings.perimeterTrackerSpeed; + settings.roll_tilt_comp = defaultSettings.roll_tilt_comp; + } + + if (settings.Config_Set < 45) { + settings.steering_correction = defaultSettings.steering_correction; + settings.cut_perimeter_ratio = defaultSettings.cut_perimeter_ratio; + } + + // Validate allowed values + if (settings.perimeterTrackerSpeed < 1 || settings.perimeterTrackerSpeed > 3359) { + settings.perimeterTrackerSpeed = defaultSettings.perimeterTrackerSpeed; + } + if (settings.cut_perimeter_ratio < 0 || settings.cut_perimeter_ratio > 100) { + settings.cut_perimeter_ratio = defaultSettings.cut_perimeter_ratio; + } + + if (settings.cutterSpeed < 1 || settings.cutterSpeed > 3359) { + settings.cutterSpeed = defaultSettings.cutterSpeed; + } + + if (settings.motorMinSpeed < 1 || settings.motorMinSpeed > 3359) { + settings.motorMinSpeed = defaultSettings.motorMinSpeed; + } + + if (settings.motorMaxSpeed < 1 || settings.motorMaxSpeed > 3359) { + settings.motorMaxSpeed = defaultSettings.motorMaxSpeed; + } + + if (settings.steering_correction < 0 || settings.steering_correction > 3359) { + settings.steering_correction = defaultSettings.steering_correction; + } + + if (settings.roll_tilt_comp < 0 || settings.roll_tilt_comp > 1000) { + settings.roll_tilt_comp = defaultSettings.roll_tilt_comp; + } + if (settings.BatteryChargeTime < 0 || settings.BatteryChargeTime > 4 * 60) { + settings.BatteryChargeTime = defaultSettings.BatteryChargeTime; + } + + if (settings.undock_backing_seconds < 1 || settings.undock_backing_seconds > 30) { + settings.undock_backing_seconds = defaultSettings.undock_backing_seconds; + } + + if (settings.Signal_Integrity_OUT < -1 || settings.Signal_Integrity_OUT > 1) { + settings.Signal_Integrity_OUT = defaultSettings.Signal_Integrity_OUT; + } + + if (settings.Signal_Integrity_IN < -1 || settings.Signal_Integrity_IN > 1) { + settings.Signal_Integrity_IN = defaultSettings.Signal_Integrity_IN; + } + + if (settings.Battery_High_Limit < 7 || settings.Battery_High_Limit > 30) { + settings.Battery_High_Limit = defaultSettings.Battery_High_Limit; + } + + if (settings.Battery_Low_Limit < 7 || settings.Battery_Low_Limit > 30) { + settings.Battery_Low_Limit = defaultSettings.Battery_Low_Limit; + } + + if (settings.voltageMultiply < 0 || settings.voltageMultiply > 7) { + settings.voltageMultiply = defaultSettings.voltageMultiply; + } + + if (settings.roll_comp < -90 || settings.roll_comp > 90) { + settings.roll_comp = defaultSettings.roll_comp; + } + + if (settings.pitch_comp < -90 || settings.pitch_comp > 90) { + settings.pitch_comp = defaultSettings.pitch_comp; + } + + if (settings.highgrass_Limit < 0.5 || settings.highgrass_Limit > 10) { + settings.highgrass_Limit = defaultSettings.highgrass_Limit; + } + + if (settings.kd < 0 || settings.kd > 5) { + settings.kd = defaultSettings.kd; + } + + if (settings.ki < 0 || settings.ki > 5) { + settings.ki = defaultSettings.ki; + } + + if (settings.kp < 0 || settings.kp > 5) { + settings.kp = defaultSettings.kp; + } + + settings.Config_Set = global_settings_version; + write_all_settings(settings); + return CONFIG_FOUND; } From 47a2d40fbfa384852bd39a1362aa23fb54046d8d Mon Sep 17 00:00:00 2001 From: Michael Nilsson Date: Tue, 6 Jul 2021 09:20:29 +0200 Subject: [PATCH 2/2] Update SET-command for perimeter cutting to match help. Debug-statement for perimeter tracking. --- RazorBoard/Core/Src/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/RazorBoard/Core/Src/main.c b/RazorBoard/Core/Src/main.c index f9be504..ba71fbd 100644 --- a/RazorBoard/Core/Src/main.c +++ b/RazorBoard/Core/Src/main.c @@ -690,6 +690,8 @@ void SendInfo() { Serial_DATA(msg); sprintf(msg, "Movement: %.2f\r\n", mpu.movement); Serial_DATA(msg); + sprintf(msg, "Perimeter tracking: %d\r\n", perimeterTracking); + Serial_DATA(msg); if (mpu.movement < settings.movement) { sprintf(msg, "Movement Verdict: Standing\r\n"); } else { @@ -1243,10 +1245,10 @@ void parseCommand_Console(void) { sscanf(Command, "%s %s %s %d", cmd1, cmd2, cmd3, &speed); settings.perimeterTrackerSpeed = speed; } - if (strncmp(Command, "CUT PERIMETER RATIO", 19) == 0) { + if (strncmp(Command, "SET PERIMETER CUT RATIO", 23) == 0) { int ratio; - char cmd1[3], cmd2[9], cmd3[5]; - sscanf(Command, "%s %s %s %d", cmd1, cmd2, cmd3, &ratio); + char cmd1[3], cmd2[9], cmd3[3], cmd4[5]; + sscanf(Command, "%s %s %s %d", cmd1, cmd2, cmd3, cmd4, &ratio); settings.cut_perimeter_ratio = ratio; } if (strncmp(Command, "SET ADC LEVEL", 13) == 0) {