2015-05-04 14:25:43 -07:00
|
|
|
/*
|
|
|
|
Copyright 2012-2015 Benjamin Vedder benjamin@vedder.se
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* app_adc.c
|
|
|
|
*
|
|
|
|
* Created on: 1 may 2015
|
|
|
|
* Author: benjamin
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "stm32f4xx_conf.h"
|
2015-12-08 12:01:23 -08:00
|
|
|
#include "mc_interface.h"
|
2015-05-04 14:25:43 -07:00
|
|
|
#include "timeout.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "comm_can.h"
|
|
|
|
#include "hw.h"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
// Settings
|
|
|
|
#define MAX_CAN_AGE 0.1
|
|
|
|
#define MIN_MS_WITHOUT_POWER 500
|
|
|
|
#define FILTER_SAMPLES 5
|
2015-08-23 09:26:05 -07:00
|
|
|
#define RPM_FILTER_SAMPLES 8
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
// Threads
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(adc_thread, arg);
|
|
|
|
static THD_WORKING_AREA(adc_thread_wa, 1024);
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static volatile adc_config config;
|
|
|
|
static volatile float ms_without_power = 0;
|
|
|
|
static volatile float decoded_level = 0.0;
|
|
|
|
static volatile float read_voltage = 0.0;
|
2016-02-02 07:28:55 -08:00
|
|
|
static volatile float decoded_level2 = 0.0;
|
|
|
|
static volatile float read_voltage2 = 0.0;
|
2015-08-23 09:26:05 -07:00
|
|
|
static volatile bool use_rx_tx_as_buttons = false;
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
void app_adc_configure(adc_config *conf) {
|
|
|
|
config = *conf;
|
|
|
|
ms_without_power = 0.0;
|
|
|
|
}
|
|
|
|
|
2015-08-23 09:26:05 -07:00
|
|
|
void app_adc_start(bool use_rx_tx) {
|
|
|
|
use_rx_tx_as_buttons = use_rx_tx;
|
2015-05-04 14:25:43 -07:00
|
|
|
chThdCreateStatic(adc_thread_wa, sizeof(adc_thread_wa), NORMALPRIO, adc_thread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
float app_adc_get_decoded_level(void) {
|
|
|
|
return decoded_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
float app_adc_get_voltage(void) {
|
|
|
|
return read_voltage;
|
|
|
|
}
|
|
|
|
|
2016-02-02 07:28:55 -08:00
|
|
|
float app_adc_get_decoded_level2(void) {
|
|
|
|
return decoded_level2;
|
|
|
|
}
|
|
|
|
|
|
|
|
float app_adc_get_voltage2(void) {
|
|
|
|
return read_voltage2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(adc_thread, arg) {
|
2015-05-04 14:25:43 -07:00
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
chRegSetThreadName("APP_ADC");
|
|
|
|
|
|
|
|
// Set servo pin as an input with pullup
|
2015-08-23 09:26:05 -07:00
|
|
|
if (use_rx_tx_as_buttons) {
|
|
|
|
palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_INPUT_PULLUP);
|
|
|
|
palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_INPUT_PULLUP);
|
|
|
|
} else {
|
|
|
|
palSetPadMode(HW_ICU_GPIO, HW_ICU_PIN, PAL_MODE_INPUT_PULLUP);
|
|
|
|
}
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
// Sleep for a time according to the specified rate
|
2015-10-08 14:09:39 -07:00
|
|
|
systime_t sleep_time = CH_CFG_ST_FREQUENCY / config.update_rate_hz;
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
// At least one tick should be slept to not block the other threads
|
|
|
|
if (sleep_time == 0) {
|
|
|
|
sleep_time = 1;
|
|
|
|
}
|
|
|
|
chThdSleep(sleep_time);
|
|
|
|
|
2015-08-23 09:26:05 -07:00
|
|
|
// For safe start when fault codes occur
|
2015-12-08 12:01:23 -08:00
|
|
|
if (mc_interface_get_fault() != FAULT_CODE_NONE) {
|
2015-08-23 09:26:05 -07:00
|
|
|
ms_without_power = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
// Read the external ADC pin and convert the value to a voltage.
|
|
|
|
float pwr = (float)ADC_Value[ADC_IND_EXT];
|
|
|
|
pwr /= 4095;
|
|
|
|
pwr *= V_REG;
|
|
|
|
|
|
|
|
read_voltage = pwr;
|
|
|
|
|
|
|
|
// Optionally apply a mean value filter
|
|
|
|
if (config.use_filter) {
|
|
|
|
static float filter_buffer[FILTER_SAMPLES];
|
|
|
|
static int filter_ptr = 0;
|
|
|
|
|
|
|
|
filter_buffer[filter_ptr++] = pwr;
|
|
|
|
if (filter_ptr >= FILTER_SAMPLES) {
|
|
|
|
filter_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwr = 0.0;
|
|
|
|
for (int i = 0;i < FILTER_SAMPLES;i++) {
|
|
|
|
pwr += filter_buffer[i];
|
|
|
|
}
|
|
|
|
pwr /= FILTER_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map and truncate the read voltage
|
|
|
|
pwr = utils_map(pwr, config.voltage_start, config.voltage_end, 0.0, 1.0);
|
|
|
|
utils_truncate_number(&pwr, 0.0, 1.0);
|
|
|
|
|
|
|
|
// Optionally invert the read voltage
|
|
|
|
if (config.voltage_inverted) {
|
|
|
|
pwr = 1.0 - pwr;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded_level = pwr;
|
|
|
|
|
2016-02-02 07:28:55 -08:00
|
|
|
// Read the external ADC pin and convert the value to a voltage.
|
2016-02-02 08:19:37 -08:00
|
|
|
#ifdef ADC_IND_EXT2
|
2016-02-02 07:28:55 -08:00
|
|
|
float brake = (float)ADC_Value[ADC_IND_EXT2];
|
|
|
|
brake /= 4095;
|
|
|
|
brake *= V_REG;
|
2016-02-02 08:19:37 -08:00
|
|
|
#else
|
|
|
|
float brake = 0.0;
|
|
|
|
#endif
|
2016-02-02 07:28:55 -08:00
|
|
|
|
|
|
|
read_voltage2 = brake;
|
|
|
|
|
|
|
|
// Optionally apply a mean value filter
|
|
|
|
if (config.use_filter) {
|
|
|
|
static float filter_buffer2[FILTER_SAMPLES];
|
|
|
|
static int filter_ptr2 = 0;
|
|
|
|
|
|
|
|
filter_buffer2[filter_ptr2++] = brake;
|
|
|
|
if (filter_ptr2 >= FILTER_SAMPLES) {
|
|
|
|
filter_ptr2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
brake = 0.0;
|
|
|
|
for (int i = 0;i < FILTER_SAMPLES;i++) {
|
|
|
|
brake += filter_buffer2[i];
|
|
|
|
}
|
|
|
|
brake /= FILTER_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map and truncate the read voltage
|
|
|
|
brake = utils_map(brake, config.voltage_start, config.voltage_end, 0.0, 1.0);
|
|
|
|
utils_truncate_number(&brake, 0.0, 1.0);
|
|
|
|
|
|
|
|
// Optionally invert the read voltage
|
|
|
|
if (config.voltage_inverted) {
|
|
|
|
brake = 1.0 - brake;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded_level2 = brake;
|
|
|
|
|
|
|
|
|
2015-08-23 09:26:05 -07:00
|
|
|
// Read the button pins
|
|
|
|
bool cc_button = false;
|
|
|
|
bool rev_button = false;
|
|
|
|
if (use_rx_tx_as_buttons) {
|
|
|
|
cc_button = !palReadPad(HW_UART_TX_PORT, HW_UART_TX_PIN);
|
|
|
|
if (config.cc_button_inverted) {
|
|
|
|
cc_button = !cc_button;
|
|
|
|
}
|
|
|
|
rev_button = !palReadPad(HW_UART_RX_PORT, HW_UART_RX_PIN);
|
|
|
|
if (config.rev_button_inverted) {
|
|
|
|
rev_button = !rev_button;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When only one button input is available, use it differently depending on the control mode
|
|
|
|
if (config.ctrl_type == ADC_CTRL_TYPE_CURRENT_REV_BUTTON ||
|
|
|
|
config.ctrl_type == ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON ||
|
|
|
|
config.ctrl_type == ADC_CTRL_TYPE_DUTY_REV_BUTTON) {
|
|
|
|
rev_button = !palReadPad(HW_ICU_GPIO, HW_ICU_PIN);
|
|
|
|
if (config.rev_button_inverted) {
|
|
|
|
rev_button = !rev_button;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cc_button = !palReadPad(HW_ICU_GPIO, HW_ICU_PIN);
|
|
|
|
if (config.cc_button_inverted) {
|
|
|
|
cc_button = !cc_button;
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 14:25:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (config.ctrl_type) {
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_REV_CENTER:
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER:
|
|
|
|
case ADC_CTRL_TYPE_DUTY_REV_CENTER:
|
|
|
|
// Scale the voltage and set 0 at the center
|
|
|
|
pwr *= 2.0;
|
|
|
|
pwr -= 1.0;
|
|
|
|
break;
|
|
|
|
|
2016-02-02 07:28:55 -08:00
|
|
|
case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_ADC:
|
|
|
|
if( brake > 0.01 ) {
|
|
|
|
pwr = -brake;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
case ADC_CTRL_TYPE_CURRENT_REV_BUTTON:
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON:
|
|
|
|
case ADC_CTRL_TYPE_DUTY_REV_BUTTON:
|
|
|
|
// Invert the voltage if the button is pressed
|
2015-08-23 09:26:05 -07:00
|
|
|
if (rev_button) {
|
2015-05-04 14:25:43 -07:00
|
|
|
pwr = -pwr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-23 09:26:05 -07:00
|
|
|
// Apply deadband
|
2015-05-04 14:25:43 -07:00
|
|
|
utils_deadband(&pwr, config.hyst, 1.0);
|
|
|
|
|
2015-08-23 09:26:05 -07:00
|
|
|
float current = 0.0;
|
2015-05-04 14:25:43 -07:00
|
|
|
bool current_mode = false;
|
|
|
|
bool current_mode_brake = false;
|
2015-12-08 12:01:23 -08:00
|
|
|
const volatile mc_configuration *mcconf = mc_interface_get_configuration();
|
2015-05-04 14:25:43 -07:00
|
|
|
bool send_duty = false;
|
|
|
|
|
|
|
|
// Use the filtered and mapped voltage for control according to the configuration.
|
|
|
|
switch (config.ctrl_type) {
|
|
|
|
case ADC_CTRL_TYPE_CURRENT:
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_REV_CENTER:
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_REV_BUTTON:
|
|
|
|
current_mode = true;
|
|
|
|
if (pwr >= 0.0) {
|
|
|
|
current = pwr * mcconf->l_current_max;
|
|
|
|
} else {
|
|
|
|
current = pwr * fabsf(mcconf->l_current_min);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fabsf(pwr) < 0.001) {
|
2015-10-08 14:09:39 -07:00
|
|
|
ms_without_power += (1000.0 * (float)sleep_time) / (float)CH_CFG_ST_FREQUENCY;
|
2015-05-04 14:25:43 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER:
|
|
|
|
case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON:
|
2016-02-02 07:28:55 -08:00
|
|
|
case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_ADC:
|
2015-05-04 14:25:43 -07:00
|
|
|
current_mode = true;
|
|
|
|
if (pwr >= 0.0) {
|
|
|
|
current = pwr * mcconf->l_current_max;
|
|
|
|
} else {
|
|
|
|
current = fabsf(pwr * mcconf->l_current_min);
|
|
|
|
current_mode_brake = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwr < 0.001) {
|
2015-10-08 14:09:39 -07:00
|
|
|
ms_without_power += (1000.0 * (float)sleep_time) / (float)CH_CFG_ST_FREQUENCY;
|
2015-05-04 14:25:43 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADC_CTRL_TYPE_DUTY:
|
|
|
|
case ADC_CTRL_TYPE_DUTY_REV_CENTER:
|
|
|
|
case ADC_CTRL_TYPE_DUTY_REV_BUTTON:
|
|
|
|
if (fabsf(pwr) < 0.001) {
|
2015-10-08 14:09:39 -07:00
|
|
|
ms_without_power += (1000.0 * (float)sleep_time) / (float)CH_CFG_ST_FREQUENCY;
|
2015-05-04 14:25:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ms_without_power < MIN_MS_WITHOUT_POWER && config.safe_start)) {
|
2016-02-09 03:23:58 -08:00
|
|
|
mc_interface_set_duty(utils_map(pwr, -1.0, 1.0, -mcconf->l_max_duty, mcconf->l_max_duty));
|
2015-05-04 14:25:43 -07:00
|
|
|
send_duty = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If safe start is enabled and the output has not been zero for long enough
|
|
|
|
if (ms_without_power < MIN_MS_WITHOUT_POWER && config.safe_start) {
|
|
|
|
static int pulses_without_power_before = 0;
|
|
|
|
if (ms_without_power == pulses_without_power_before) {
|
|
|
|
ms_without_power = 0;
|
|
|
|
}
|
|
|
|
pulses_without_power_before = ms_without_power;
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_brake_current(timeout_get_brake_current());
|
2015-05-04 14:25:43 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset timeout
|
|
|
|
timeout_reset();
|
|
|
|
|
2015-08-23 09:26:05 -07:00
|
|
|
// If c is pressed and no throttle is used, maintain the current speed with PID control
|
|
|
|
static bool was_pid = false;
|
|
|
|
|
|
|
|
// Filter RPM to avoid glitches
|
|
|
|
static float filter_buffer[RPM_FILTER_SAMPLES];
|
|
|
|
static int filter_ptr = 0;
|
2015-12-08 12:01:23 -08:00
|
|
|
filter_buffer[filter_ptr++] = mc_interface_get_rpm();
|
2015-08-23 09:26:05 -07:00
|
|
|
if (filter_ptr >= RPM_FILTER_SAMPLES) {
|
|
|
|
filter_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float rpm_filtered = 0.0;
|
|
|
|
for (int i = 0;i < RPM_FILTER_SAMPLES;i++) {
|
|
|
|
rpm_filtered += filter_buffer[i];
|
|
|
|
}
|
|
|
|
rpm_filtered /= RPM_FILTER_SAMPLES;
|
|
|
|
|
|
|
|
if (current_mode && cc_button && fabsf(pwr) < 0.001) {
|
|
|
|
static float pid_rpm = 0.0;
|
|
|
|
|
|
|
|
if (!was_pid) {
|
|
|
|
was_pid = true;
|
|
|
|
pid_rpm = rpm_filtered;
|
|
|
|
}
|
|
|
|
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_pid_speed(pid_rpm);
|
2015-08-23 09:26:05 -07:00
|
|
|
|
|
|
|
// Send the same duty cycle to the other controllers
|
|
|
|
if (config.multi_esc) {
|
2015-12-08 12:01:23 -08:00
|
|
|
float duty = mc_interface_get_duty_cycle_now();
|
2015-08-23 09:26:05 -07:00
|
|
|
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
can_status_msg *msg = comm_can_get_status_msg_index(i);
|
|
|
|
|
|
|
|
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
|
|
|
|
comm_can_set_duty(msg->id, duty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
was_pid = false;
|
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
// Find lowest RPM (for traction control)
|
2015-12-08 12:01:23 -08:00
|
|
|
float rpm_local = mc_interface_get_rpm();
|
2015-05-04 14:25:43 -07:00
|
|
|
float rpm_lowest = rpm_local;
|
|
|
|
if (config.multi_esc) {
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
can_status_msg *msg = comm_can_get_status_msg_index(i);
|
|
|
|
|
|
|
|
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
|
|
|
|
float rpm_tmp = msg->rpm;
|
|
|
|
|
|
|
|
if (fabsf(rpm_tmp) < fabsf(rpm_lowest)) {
|
|
|
|
rpm_lowest = rpm_tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally send the duty cycles to the other ESCs seen on the CAN-bus
|
|
|
|
if (send_duty && config.multi_esc) {
|
2015-12-08 12:01:23 -08:00
|
|
|
float duty = mc_interface_get_duty_cycle_now();
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
can_status_msg *msg = comm_can_get_status_msg_index(i);
|
|
|
|
|
|
|
|
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
|
|
|
|
comm_can_set_duty(msg->id, duty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_mode) {
|
|
|
|
if (current_mode_brake) {
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_brake_current(current);
|
2015-05-04 14:25:43 -07:00
|
|
|
|
|
|
|
// Send brake command to all ESCs seen recently on the CAN bus
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
can_status_msg *msg = comm_can_get_status_msg_index(i);
|
|
|
|
|
|
|
|
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
|
|
|
|
comm_can_set_current_brake(msg->id, current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Apply soft RPM limit
|
|
|
|
if (rpm_lowest > config.rpm_lim_end && current > 0.0) {
|
|
|
|
current = mcconf->cc_min_current;
|
|
|
|
} else if (rpm_lowest > config.rpm_lim_start && current > 0.0) {
|
|
|
|
current = utils_map(rpm_lowest, config.rpm_lim_start, config.rpm_lim_end, current, mcconf->cc_min_current);
|
|
|
|
} else if (rpm_lowest < -config.rpm_lim_end && current < 0.0) {
|
|
|
|
current = mcconf->cc_min_current;
|
|
|
|
} else if (rpm_lowest < -config.rpm_lim_start && current < 0.0) {
|
|
|
|
rpm_lowest = -rpm_lowest;
|
|
|
|
current = -current;
|
|
|
|
current = utils_map(rpm_lowest, config.rpm_lim_start, config.rpm_lim_end, current, mcconf->cc_min_current);
|
|
|
|
current = -current;
|
|
|
|
rpm_lowest = -rpm_lowest;
|
|
|
|
}
|
|
|
|
|
|
|
|
float current_out = current;
|
|
|
|
bool is_reverse = false;
|
|
|
|
if (current_out < 0.0) {
|
|
|
|
is_reverse = true;
|
|
|
|
current_out = -current_out;
|
|
|
|
current = -current;
|
|
|
|
rpm_local = -rpm_local;
|
|
|
|
rpm_lowest = -rpm_lowest;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traction control
|
|
|
|
if (config.multi_esc) {
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
can_status_msg *msg = comm_can_get_status_msg_index(i);
|
|
|
|
|
|
|
|
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
|
|
|
|
if (config.tc) {
|
|
|
|
float rpm_tmp = msg->rpm;
|
|
|
|
if (is_reverse) {
|
|
|
|
rpm_tmp = -rpm_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
float diff = rpm_tmp - rpm_lowest;
|
|
|
|
current_out = utils_map(diff, 0.0, config.tc_max_diff, current, 0.0);
|
|
|
|
if (current_out < mcconf->cc_min_current) {
|
|
|
|
current_out = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_reverse) {
|
|
|
|
comm_can_set_current(msg->id, -current_out);
|
|
|
|
} else {
|
|
|
|
comm_can_set_current(msg->id, current_out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.tc) {
|
|
|
|
float diff = rpm_local - rpm_lowest;
|
|
|
|
current_out = utils_map(diff, 0.0, config.tc_max_diff, current, 0.0);
|
|
|
|
if (current_out < mcconf->cc_min_current) {
|
|
|
|
current_out = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_reverse) {
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_current(-current_out);
|
2015-05-04 14:25:43 -07:00
|
|
|
} else {
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_current(current_out);
|
2015-05-04 14:25:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|