#define SERIAL_CONTROL_BAUD 115200 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard)
#define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor)
#define START_FRAME 0xABCD // [-] Start frme definition for reliable serial communication
#define SERIAL_LOG_BAUD 115200 // baud rate for logging output
boollog_update=true;
unsignedlonglast_log_send=0;
#define SENDPERIOD 20 //ms. delay for sending speed and steer data to motor controller via serial
#define LOGMININTERVAL 20 //minimum interval (ms) to send logs
#define LOGMAXINTERVAL 10000 //maximum time (ms) after which data is send
#define WRITE_HEADER_TIME 400 //just before FEEDBACKRECEIVETIMEOUT, so header gets written before error comments
boollog_header_written=false;
#define FEEDBACKRECEIVETIMEOUT 500
boolcontrollerFront_connected=false;
boolcontrollerRear_connected=false;
boolcontrollers_connected=false;
#define PIN_THROTTLE A7
//const uint16_t calib_throttle_min = 420; //better a bit too high than too low
//const uint16_t calib_throttle_max = 790;
constuint16_tfailsafe_throttle_min=4900;//if adc value falls below this failsafe is triggered. old 20
constuint16_tfailsafe_throttle_max=14000;//if adc value goes above this failsafe is triggered. old 1000
//const uint16_t throttleCurvePerMM[] = {414,460,490,511,527,539,548,555,561,567,573,578,584,590,599,611,630,657,697,754,789,795}; //adc values for every unit (mm) of linear travel
constuint16_tthrottleCurvePerMM[]={8485,8904,9177,9368,9513,9623,9705,9768,9823,9877,9932,9978,10032,10087,10169,10278,10451,10697,11061,11579,11898,11952};//adc values for every unit (mm) of linear travel
#define PIN_BRAKE A8
constuint16_tcalib_brake_min=2000;//better a bit too high than too low
constuint16_tcalib_brake_max=11000;
constuint16_tfailsafe_brake_min=700;//if adc value falls below this failsafe is triggered
constuint16_tfailsafe_brake_max=13000;//if adc value goes above this failsafe is triggered
uint16_tads_throttle_A_raw=0;
uint16_tads_throttle_B_raw=0;
uint16_tads_brake_raw=failsafe_brake_min;
uint16_tads_control_raw=0;
int16_tthrottle_pos=0;
int16_tbrake_pos=0;
#define ADSREADPERIOD 3 //set slightly higher as actual read time to avoid unnecessary register query
#define ADCREADPERIOD 10
#define BUTTONREADPERIOD 20
unsignedlonglast_adsread=0;//needed for failcheck
uint16_tthrottle_raw=failsafe_throttle_min;//start at min so that failsafe is not triggered
#define THROTTLE_ADC_FILTER 0.15 //higher value = faster response
uint16_tbrake_raw=failsafe_brake_min;//start at min so that failsafe is not triggered
#define ADC_OUTOFRANGE_TIME 100
unsignedlongthrottle_ok_time=0;
unsignedlongbrake_ok_time=0;
boolerror_throttle_outofrange=false;
boolerror_brake_outofrange=false;
boolerror_ads_max_read_interval=false;
#define REVERSE_ENABLE_TIME 1000 //ms. how long standstill to be able to drive backward
#define REVERSE_SPEED 0.25 //reverse driving speed //0 to 1
#define NORMAL_MAX_ACCELERATION_RATE 10000
#define SLOW_MAX_ACCELERATION_RATE 500
int16_tmax_acceleration_rate=NORMAL_MAX_ACCELERATION_RATE;//maximum cmd send increase per second
floatmeanSpeedms=0;
floattrip=0;//trip distance in meters
floatwheelcircumference=0.5278;//wheel diameter in m. 8.4cm radius -> 0.084m*2*Pi
floatcurrentConsumed=0;//Ah
//Driving parameters
int16_tminimum_constant_cmd_reduce=1;//reduce cmd every loop by this constant amount when freewheeling/braking
int16_tbrake_cmdreduce_proportional=500;//cmd gets reduced by an amount proportional to brake position (ignores freewheeling). cmd_new-=brake_cmdreduce_proportional / second @ full brake. with BREAK_CMDREDUCE_CONSTANT=1000 car would stop with full brake at least after a second (ignoring influence of brake current control/freewheeling)
floatstartbrakecurrent=3;//Ampere. "targeted brake current @full brake". at what point to start apply brake proportional to brake_pos. for everything above that cmd is reduced by freewheel_break_factor
floatstartbrakecurrent_offset=0.1;//offset start point for breaking, because of reading fluctuations around 0A. set this slightly above idle current reading
boolreverse_enabled=false;
unsignedlonglast_notidle=0;//not rolling to fast, no pedal pressed
#define PIN_START A9
#define PIN_LED_START 2 //Enginge start led
#define PIN_LATCH_ENABLE A6
#define PIN_MODE_SWITCH 3
#define PIN_MODE_LEDG 4
#define PIN_MODE_LEDR 5
unsignedlonglast_send=0;
unsignedlonglast_receive=0;
floatfiltered_currentAll=0;
int16_tcmd_send=0;
int16_tlast_cmd_send=0;
uint8_tspeedmode=0;
#define SPEEDMODE_SLOW 1
#define SPEEDMODE_NORMAL 0
unsignedlongbutton_start_lastchange=0;
boolbutton_start_state=false;
#define LONG_PRESS_ARMING_TIME 2000
#define DEBOUNCE_TIME 50
boolarmed=false;//cmd output values forced to 0 if false
#define CURRENT_FILTER_SIZE 60 //latency is about CURRENT_FILTER_SIZE/2*MEASURE_INTERVAL (measure interval is defined by hoverboard controller)
#define CURRENT_MEANVALUECOUNT 20 //0<= meanvaluecount < CURRENT_FILTER_SIZE/2. how many values will be used from sorted weight array from the center region. abour double this values reading are used