bobbycar/controller_teensy/include/definitions.h

179 lines
8.0 KiB
C

#ifndef _DEFINITIONS_H
#define _DEFINITIONS_H
// ########################## DEFINES ##########################
#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
bool log_update=true;
unsigned long last_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
bool log_header_written = false;
//#define FEEDBACKRECEIVETIMEOUT 500
//bool controllerFront_connected=false;
//bool controllerRear_connected=false;
bool controllers_connected=false;
//const uint16_t calib_throttle_min = 420; //better a bit too high than too low
//const uint16_t calib_throttle_max = 790;
/*
const uint16_t failsafe_throttle_min_A = 4900; //if adc value falls below this failsafe is triggered.
const uint16_t failsafe_throttle_max_A = 14500; //if adc value goes above this failsafe is triggered.
const uint16_t failsafe_throttle_min_B = 3900; //if adc value falls below this failsafe is triggered.
const uint16_t failsafe_throttle_max_B = 12500; //if adc value goes above this failsafe is triggered.
*/
const uint16_t failsafe_throttle_min_A = 3000; //if adc value falls below this failsafe is triggered.
const uint16_t failsafe_throttle_max_A = 13000; //if adc value goes above this failsafe is triggered.
const uint16_t failsafe_throttle_min_B = 5000; //if adc value falls below this failsafe is triggered.
const uint16_t failsafe_throttle_max_B = 14500; //if adc value goes above this failsafe is triggered.
const uint16_t failsafe_throttle_maxDiff = 200;//maximum throttle pos value difference between both sensors A and B (after linearization). value range 0-1000. choose value at least 2x higher than maximum difference when moving throttle slowly
//Throttle Calibration: First value pair should be 1-2mm pressed down (start of throttle=1). Last value pair should be a bit before fully pressed. all values in between need to be equidistant (for example every 1mm). Use define CALIBRATION_THROTTLE_CURVE for easy calibration output
const uint16_t throttleCurvePerMM_A[] = {9996,9718,9507,9357,9232,9162,9071,8958,8838,8703,8577,8396,8192,7845,7407,6800,5923}; //adc values for every unit (mm) of linear travel
const uint16_t throttleCurvePerMM_B[] = {7698,7948,8133,8277,8403,8503,8588,8695,8791,8899,9034,9196,9400,9711,10110,10657,1146}; //adc values for every unit (mm) of linear travel
const bool throttleCurvePerMM_A_Descending=true; //set true if corresponding array is descending
const bool throttleCurvePerMM_B_Descending=false; //set true if corresponding array is descending
const uint16_t calib_brake_min = 7000; //better a bit too high than too low
const uint16_t calib_brake_max = 11000;
const uint16_t failsafe_brake_min = 4000; //if adc value falls below this failsafe is triggered
const uint16_t failsafe_brake_max = 14500; //if adc value goes above this failsafe is triggered
const uint16_t calib_control_buttonA=7170; // adc value for button A pressed
const uint16_t calib_control_buttonB=10402; // adc value for button B pressed
const uint16_t calib_control_buttonAB=5595; // adc value for button A and B pressed
const uint16_t calib_control_treshold=100; // from calibration value adc-thres to adc+thres
const uint16_t calib_control_max=13000; //over which adc value button are both released
uint16_t ads_throttle_A_raw=0;
uint16_t ads_throttle_B_raw=0;
uint16_t ads_brake_raw=failsafe_brake_min;
uint16_t ads_control_raw=0;
int16_t throttle_posA; //scaled and clamped throttle position for sensor A
int16_t throttle_posB; //scaled and clamped throttle position for sensor B
int16_t throttle_pos=0; //combined and filtered throttle position
int16_t brake_pos=0; //filtered and constrained throttle position
bool control_buttonA=false;
bool control_buttonB=false;
unsigned long loopmillis;
unsigned long looptime_duration_min;
unsigned long looptime_duration_max;
#define ADSREADPERIOD 3 //set slightly higher as actual read time to avoid unnecessary register query
#define ADCREADPERIOD 10
#define BUTTONREADPERIOD 20
unsigned long last_adsread=0; //needed for failcheck
#define THROTTLE_ADC_FILTER 0.4 //higher value = faster response
int16_t brake_linear=0; //linearized bake position
#define ADC_OUTOFRANGE_TIME 100 //for failsafe_throttle_min_X. how long values need to stay bad to trigger failsafe
#define ADC_DIFFHIGH_TIME 500 //for failsafe_throttle_maxDiff. how long values need to stay bad to trigger failsafe
bool error_throttle_outofrange=false;
bool error_throttle_difftoohigh=false;
bool error_brake_outofrange=false;
bool error_ads_max_read_interval=false;
bool error_sdfile_unavailable=false;
#define REVERSE_ENABLE_TIME 300 //ms. how long standstill to be able to drive backward
#define NORMAL_MAX_ACCELERATION_RATE 10000
#define MEDIUM_MAX_ACCELERATION_RATE 500
#define SLOW_MAX_ACCELERATION_RATE 250
int16_t max_acceleration_rate=NORMAL_MAX_ACCELERATION_RATE; //maximum cmd send increase per second
//Driving parameters
int16_t minimum_constant_cmd_reduce=1; //reduce cmd every loop by this constant amount when freewheeling/braking
int16_t brake_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)
float startbrakecurrent=2.0; //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
float startbrakecurrent_offset=0.13; //offset start point for breaking, because of reading fluctuations around 0A. set this slightly above idle current reading
float freewheel_break_factor=200.0; //speed cmd units per amp per second. 1A over freewheel_current decreases cmd speed by this amount (on average). Was 500 until 20240809
float reverse_speed=0.25; //reverse driving speed //0 to 1
int16_t throttle_max=1000; //maximum allowed set speed. used for scaling and limiting. [0,1000]
bool reverse_enabled=false;
unsigned long last_notidle=0; //not rolling to fast, no pedal pressed
#define PIN_PWRBUTTON 22 //PWRBUTTON
#define PIN_LED_START 2 //Enginge start led
#define PIN_LATCH_ENABLE 21
#define PIN_FAN 6 //Output High=Fans on
//unsigned long last_send = 0;
//unsigned long last_receive = 0;
float filtered_currentAll=0;
//Statistics values
float max_filtered_currentAll;
float min_filtered_currentAll;
float max_filtered_wattAll;
float min_filtered_wattAll;
float max_meanSpeed;
float min_voltage;
float minSpeedms; //speed in m/s of slowest wheel
double overallTrip; //m. trip with read distance from sd card
double trip; //m. trip distance since boot
double currentConsumed;
double overallCurrentConsumed;
double watthoursConsumed;
double overallWatthoursConsumed;
float lastTripVoltage=0;
int16_t cmd_send=0;
int16_t last_cmd_send=0;
uint8_t speedmode=0;
#define SPEEDMODE_SLOW 1
#define SPEEDMODE_NORMAL 0
unsigned long button_start_lastchange=0;
bool button_start_state=false;
#define LONG_PRESS_ARMING_TIME 2000
#define DEBOUNCE_TIME 50
bool armed = false; //cmd output values forced to 0 if false
bool statswritten = true;
#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
#define DISPLAYUPDATEPERIOD 100
#endif