@ -34,10 +34,11 @@ bool controllerRear_connected=false;
bool controllers_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 ;
//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 = 20 ; //if adc value falls below this failsafe is triggered
const uint16_t failsafe_throttle_max = 1000 ; //if adc value goes above this failsafe is triggered
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
# define PIN_BRAKE A8
const uint16_t calib_brake_min = 100 ; //better a bit too high than too low
const uint16_t calib_brake_max = 600 ;
@ -186,6 +187,8 @@ void leds();
void readButtons ( ) ;
uint16_t linearizeThrottle ( uint16_t v ) ;
void SendSerial ( SerialCommand & scom , int16_t uSpeedLeft , int16_t uSpeedRight , HardwareSerial & SerialRef )
{
// Create command
@ -420,7 +423,8 @@ void writeLogComment(HardwareSerial &SerialRef, unsigned long time, String msg)
void readADC ( ) {
throttle_raw = analogRead ( PIN_THROTTLE ) ;
throttle_pos = max ( 0 , min ( 1000 , map ( throttle_raw , calib_throttle_min , calib_throttle_max , 0 , 1000 ) ) ) ; //map and constrain
//maps throttle curve to be linear
throttle_pos = max ( 0 , min ( 1000 , linearizeThrottle ( throttle_raw ) ) ) ; //map and constrain
brake_raw = analogRead ( PIN_BRAKE ) ;
brake_pos = max ( 0 , min ( 1000 , map ( brake_raw , calib_brake_min , calib_brake_max , 0 , 1000 ) ) ) ; //map and constrain
//brake_pos = (int16_t)(pow((brake_pos/1000.0),2)*1000);
@ -583,6 +587,12 @@ void sendCMD() {
log_update = true ;
//Serial.print(cmd_send); Serial.print(", "); Serial.print(throttle_pos); Serial.print(", "); Serial.print(filtered_curFL*1000); Serial.print(", "); Serial.print(filtered_curFR*1000); Serial.print(", "); Serial.print(filtered_currentAll*1000); Serial.println()
} else if ( loopmillis > last_log_send + LOGMININTERVAL ) {
//Serial.print(throttle_raw); Serial.println();
Serial . print ( linearizeThrottle ( throttle_raw ) ) ; Serial . println ( ) ;
last_log_send = loopmillis ;
}
}
@ -651,4 +661,29 @@ void readButtons() {
}
}
}
uint16_t linearizeThrottle ( uint16_t v ) {
//input is raw adc value from hall sensor
//uses throttleCurvePerMM array to find linear approximation of actual throttle travel
//array has to be sorted !
uint8_t _searchpos = 0 ;
uint8_t arraysize = sizeof ( throttleCurvePerMM ) / sizeof ( throttleCurvePerMM [ 0 ] ) ;
while ( _searchpos < arraysize & & v > throttleCurvePerMM [ _searchpos ] ) { //find arraypos with value above input value
_searchpos + + ; //try next value
}
if ( _searchpos < = 0 ) { //lower limit
return 0 ;
}
if ( _searchpos > = arraysize ) { //upper limit
return 1000 ;
}
uint16_t nextLower = throttleCurvePerMM [ _searchpos - 1 ] ;
uint16_t nextHigher = throttleCurvePerMM [ _searchpos ] ;
float _linearThrottle = _searchpos + map ( v * 1.0 , nextLower , nextHigher , 0.0 , 1.0 ) ;
_linearThrottle / = arraysize ; //scale to 0-1
_linearThrottle * = 1000 ; //scale to 0-1000
return ( uint16_t ) _linearThrottle ;
}