Improve adddeadband function
- Use MAP function - add additional parameter for middle value - use adddeadband function with ADC input when mid resting pot is used
This commit is contained in:
parent
76a27b086c
commit
0ab8e8e5ac
32
Src/util.c
32
Src/util.c
|
@ -565,20 +565,14 @@ void saveConfig() {
|
||||||
* Add Dead-band to a signal
|
* Add Dead-band to a signal
|
||||||
* This function realizes a dead-band around 0 and scales the input between [out_min, out_max]
|
* This function realizes a dead-band around 0 and scales the input between [out_min, out_max]
|
||||||
*/
|
*/
|
||||||
int addDeadBand(int16_t u, int16_t deadBand, int16_t in_min, int16_t in_max, int16_t out_min, int16_t out_max) {
|
int addDeadBand(int16_t u, int16_t deadBand, int16_t in_min, int16_t in_mid, int16_t in_max, int16_t out_min, int16_t out_max) {
|
||||||
#if defined(CONTROL_PPM_LEFT) || defined(CONTROL_PPM_RIGHT) || defined(CONTROL_PWM_LEFT) || defined(CONTROL_PWM_RIGHT)
|
if(u > in_mid - deadBand && u < in_mid + deadBand) {
|
||||||
int outVal = 0;
|
return 0;
|
||||||
if(u > -deadBand && u < deadBand) {
|
|
||||||
outVal = 0;
|
|
||||||
} else if(u > 0) {
|
} else if(u > 0) {
|
||||||
outVal = (out_max * CLAMP(u - deadBand, 0, in_max - deadBand)) / (in_max - deadBand);
|
return MAP(u, in_mid + deadband, in_max, 0, out_max);
|
||||||
} else {
|
} else {
|
||||||
outVal = (out_min * CLAMP(u + deadBand, in_min + deadBand, 0)) / (in_min + deadBand);
|
return MAP(u, in_mid - deadband, in_min, out_min, 0);
|
||||||
}
|
}
|
||||||
return outVal;
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -757,8 +751,8 @@ void readCommand(void) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CONTROL_PPM_LEFT) || defined(CONTROL_PPM_RIGHT)
|
#if defined(CONTROL_PPM_LEFT) || defined(CONTROL_PPM_RIGHT)
|
||||||
cmd1 = addDeadBand((ppm_captured_value[0] - 500) * 2, PPM_DEADBAND, PPM_CH1_MIN, PPM_CH1_MAX, INPUT_MIN, INPUT_MAX);
|
cmd1 = addDeadBand((ppm_captured_value[0] - 500) * 2, PPM_DEADBAND, PPM_CH1_MIN, 0, PPM_CH1_MAX, INPUT_MIN, INPUT_MAX);
|
||||||
cmd2 = addDeadBand((ppm_captured_value[1] - 500) * 2, PPM_DEADBAND, PPM_CH2_MIN, PPM_CH2_MAX, INPUT_MIN, INPUT_MAX);
|
cmd2 = addDeadBand((ppm_captured_value[1] - 500) * 2, PPM_DEADBAND, PPM_CH2_MIN, 0, PPM_CH2_MAX, INPUT_MIN, INPUT_MAX);
|
||||||
#ifdef SUPPORT_BUTTONS
|
#ifdef SUPPORT_BUTTONS
|
||||||
button1 = ppm_captured_value[5] > 500;
|
button1 = ppm_captured_value[5] > 500;
|
||||||
button2 = 0;
|
button2 = 0;
|
||||||
|
@ -770,11 +764,11 @@ void readCommand(void) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CONTROL_PWM_LEFT) || defined(CONTROL_PWM_RIGHT)
|
#if defined(CONTROL_PWM_LEFT) || defined(CONTROL_PWM_RIGHT)
|
||||||
cmd1 = addDeadBand((pwm_captured_ch1_value - 500) * 2, PWM_DEADBAND, PWM_CH1_MIN, PWM_CH1_MAX, INPUT_MIN, INPUT_MAX);
|
cmd1 = addDeadBand((pwm_captured_ch1_value - 500) * 2, PWM_DEADBAND, PWM_CH1_MIN, 0, PWM_CH1_MAX, INPUT_MIN, INPUT_MAX);
|
||||||
#if !defined(VARIANT_SKATEBOARD)
|
#if !defined(VARIANT_SKATEBOARD)
|
||||||
cmd2 = addDeadBand((pwm_captured_ch2_value - 500) * 2, PWM_DEADBAND, PWM_CH2_MIN, PWM_CH2_MAX, INPUT_MIN, INPUT_MAX);
|
cmd2 = addDeadBand((pwm_captured_ch2_value - 500) * 2, PWM_DEADBAND, PWM_CH2_MIN, 0, PWM_CH2_MAX, INPUT_MIN, INPUT_MAX);
|
||||||
#else
|
#else
|
||||||
cmd2 = addDeadBand((pwm_captured_ch2_value - 500) * 2, PWM_DEADBAND, PWM_CH2_MIN, PWM_CH2_MAX, PWM_CH2_OUT_MIN, INPUT_MAX);
|
cmd2 = addDeadBand((pwm_captured_ch2_value - 500) * 2, PWM_DEADBAND, PWM_CH2_MIN, 0, PWM_CH2_MAX, PWM_CH2_OUT_MIN, INPUT_MAX);
|
||||||
#endif
|
#endif
|
||||||
#if defined(SUPPORT_BUTTONS_LEFT) || defined(SUPPORT_BUTTONS_RIGHT)
|
#if defined(SUPPORT_BUTTONS_LEFT) || defined(SUPPORT_BUTTONS_RIGHT)
|
||||||
button1 = !HAL_GPIO_ReadPin(BUTTON1_PORT, BUTTON1_PIN);
|
button1 = !HAL_GPIO_ReadPin(BUTTON1_PORT, BUTTON1_PIN);
|
||||||
|
@ -785,15 +779,13 @@ void readCommand(void) {
|
||||||
#ifdef CONTROL_ADC
|
#ifdef CONTROL_ADC
|
||||||
// ADC values range: 0-4095, see ADC-calibration in config.h
|
// ADC values range: 0-4095, see ADC-calibration in config.h
|
||||||
#ifdef ADC1_MID_POT
|
#ifdef ADC1_MID_POT
|
||||||
cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MID_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MID_CAL), 0, INPUT_MAX)
|
cmd1 = addDeadBand(adc_buffer.l_tx2, ADC_DEADBAND, ADC1_MIN_CAL, ADC1_MID_CAL, ADC1_MAX_CAL, INPUT_MIN, INPUT_MAX);
|
||||||
+CLAMP((ADC1_MID_CAL - adc_buffer.l_tx2) * INPUT_MIN / (ADC1_MID_CAL - ADC1_MIN_CAL), INPUT_MIN, 0); // ADC1
|
|
||||||
#else
|
#else
|
||||||
cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MIN_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MIN_CAL), 0, INPUT_MAX); // ADC1
|
cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MIN_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MIN_CAL), 0, INPUT_MAX); // ADC1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ADC2_MID_POT
|
#ifdef ADC2_MID_POT
|
||||||
cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MID_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MID_CAL), 0, INPUT_MAX)
|
cmd1 = addDeadBand(adc_buffer.l_rx2, ADC_DEADBAND, ADC1_MIN_CAL, ADC1_MID_CAL, ADC1_MAX_CAL, INPUT_MIN, INPUT_MAX);
|
||||||
+CLAMP((ADC2_MID_CAL - adc_buffer.l_rx2) * INPUT_MIN / (ADC2_MID_CAL - ADC2_MIN_CAL), INPUT_MIN, 0); // ADC2
|
|
||||||
#else
|
#else
|
||||||
cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MIN_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MIN_CAL), 0, INPUT_MAX); // ADC2
|
cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MIN_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MIN_CAL), 0, INPUT_MAX); // ADC2
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue