2014-04-18 15:09:46 -07:00
|
|
|
/*
|
2016-11-04 07:18:34 -07:00
|
|
|
Copyright 2016 Benjamin Vedder benjamin@vedder.se
|
2014-04-18 15:09:46 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
2014-04-18 15:09:46 -07:00
|
|
|
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.
|
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
2014-04-18 15:09:46 -07:00
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#ifdef USE_APP_STEN
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "stm32f4xx_conf.h"
|
2015-12-08 12:01:23 -08:00
|
|
|
#include "mc_interface.h"
|
2014-05-06 09:50:41 -07:00
|
|
|
#include "utils.h"
|
2014-08-16 07:12:14 -07:00
|
|
|
#include "hw.h"
|
2014-09-21 08:41:11 -07:00
|
|
|
#include "timeout.h"
|
2016-04-27 06:32:32 -07:00
|
|
|
#include "comm_can.h"
|
|
|
|
|
2014-05-01 12:20:53 -07:00
|
|
|
#include <math.h>
|
2014-04-18 15:09:46 -07:00
|
|
|
|
2014-07-30 03:48:40 -07:00
|
|
|
// Settings
|
|
|
|
#define HYST 0.10
|
|
|
|
// 29000rpm = 20kmh
|
2014-08-01 06:43:36 -07:00
|
|
|
#define RPM_MAX_1 41000.0 // Start decreasing output here
|
|
|
|
#define RPM_MAX_2 44000.0 // Completely stop output here
|
2014-07-30 03:48:40 -07:00
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static volatile float out_received = 0.0;
|
2016-11-04 07:18:34 -07:00
|
|
|
static volatile bool stop_now = true;
|
|
|
|
static volatile bool is_running = false;
|
2014-07-30 03:48:40 -07:00
|
|
|
|
2014-04-18 15:09:46 -07:00
|
|
|
// Threads
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(uart_thread, arg);
|
|
|
|
static THD_WORKING_AREA(uart_thread_wa, 1024);
|
2014-04-18 15:09:46 -07:00
|
|
|
|
|
|
|
// Private functions
|
2014-06-07 12:11:02 -07:00
|
|
|
static void set_output(float output);
|
2014-08-29 17:02:32 -07:00
|
|
|
static uint16_t middle_of_3(uint16_t a, uint16_t b, uint16_t c);
|
2014-06-07 12:11:02 -07:00
|
|
|
|
2014-05-01 12:20:53 -07:00
|
|
|
/*
|
|
|
|
* This callback is invoked when a transmission buffer has been completely
|
|
|
|
* read by the driver.
|
|
|
|
*/
|
|
|
|
static void txend1(UARTDriver *uartp) {
|
|
|
|
(void)uartp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback is invoked when a transmission has physically completed.
|
|
|
|
*/
|
|
|
|
static void txend2(UARTDriver *uartp) {
|
|
|
|
(void)uartp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback is invoked on a receive error, the errors mask is passed
|
|
|
|
* as parameter.
|
|
|
|
*/
|
|
|
|
static void rxerr(UARTDriver *uartp, uartflags_t e) {
|
|
|
|
(void)uartp;
|
|
|
|
(void)e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback is invoked when a character is received but the application
|
|
|
|
* was not ready to receive it, the character is passed as parameter.
|
|
|
|
*/
|
|
|
|
static void rxchar(UARTDriver *uartp, uint16_t c) {
|
|
|
|
(void)uartp;
|
2014-06-07 12:11:02 -07:00
|
|
|
|
2014-08-29 17:02:32 -07:00
|
|
|
static uint16_t c1 = 128;
|
|
|
|
static uint16_t c2 = 128;
|
|
|
|
|
|
|
|
uint16_t med = middle_of_3(c, c1, c2);
|
|
|
|
|
|
|
|
c2 = c1;
|
|
|
|
c1 = c;
|
|
|
|
|
|
|
|
out_received = ((float)med / 128) - 1.0;
|
2014-09-21 08:41:11 -07:00
|
|
|
timeout_reset();
|
2014-05-01 12:20:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback is invoked when a receive buffer has been completely written.
|
|
|
|
*/
|
|
|
|
static void rxend(UARTDriver *uartp) {
|
|
|
|
(void)uartp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UART driver configuration structure.
|
|
|
|
*/
|
|
|
|
static UARTConfig uart_cfg = {
|
|
|
|
txend1,
|
|
|
|
txend2,
|
|
|
|
rxend,
|
|
|
|
rxchar,
|
|
|
|
rxerr,
|
2014-06-07 12:11:02 -07:00
|
|
|
9600,
|
2014-05-01 12:20:53 -07:00
|
|
|
0,
|
|
|
|
USART_CR2_LINEN,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
void app_sten_start(void) {
|
|
|
|
stop_now = false;
|
2014-06-07 12:11:02 -07:00
|
|
|
chThdCreateStatic(uart_thread_wa, sizeof(uart_thread_wa), NORMALPRIO - 1, uart_thread, NULL);
|
2014-05-04 07:05:10 -07:00
|
|
|
}
|
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
void app_sten_stop(void) {
|
|
|
|
stop_now = true;
|
|
|
|
|
|
|
|
if (is_running) {
|
|
|
|
uartStop(&HW_UART_DEV);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (is_running) {
|
|
|
|
chThdSleepMilliseconds(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(uart_thread, arg) {
|
2014-05-01 12:20:53 -07:00
|
|
|
(void)arg;
|
|
|
|
|
2014-08-16 07:12:14 -07:00
|
|
|
chRegSetThreadName("UART");
|
2014-05-01 12:20:53 -07:00
|
|
|
|
2014-08-16 07:12:14 -07:00
|
|
|
uartStart(&HW_UART_DEV, &uart_cfg);
|
|
|
|
palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_ALTERNATE(HW_UART_GPIO_AF) |
|
2014-05-01 12:20:53 -07:00
|
|
|
PAL_STM32_OSPEED_HIGHEST |
|
|
|
|
PAL_STM32_PUDR_PULLUP);
|
2014-08-16 07:12:14 -07:00
|
|
|
palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_ALTERNATE(HW_UART_GPIO_AF) |
|
2014-05-01 12:20:53 -07:00
|
|
|
PAL_STM32_OSPEED_HIGHEST |
|
|
|
|
PAL_STM32_PUDR_PULLUP);
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
systime_t time = chVTGetSystemTime();
|
2014-05-01 12:20:53 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
is_running = true;
|
|
|
|
|
2014-05-01 12:20:53 -07:00
|
|
|
for(;;) {
|
2014-07-30 03:48:40 -07:00
|
|
|
time += MS2ST(1);
|
2014-05-01 12:20:53 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
if (stop_now) {
|
|
|
|
is_running = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-21 08:41:11 -07:00
|
|
|
if (!timeout_has_timeout()) {
|
2014-07-30 03:48:40 -07:00
|
|
|
set_output(out_received);
|
2014-06-07 12:11:02 -07:00
|
|
|
}
|
2014-05-01 12:20:53 -07:00
|
|
|
|
|
|
|
chThdSleepUntil(time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:11:02 -07:00
|
|
|
static void set_output(float output) {
|
2016-04-27 06:32:32 -07:00
|
|
|
utils_deadband(&output, HYST, 1.0);
|
2014-06-07 12:11:02 -07:00
|
|
|
|
2015-12-08 12:01:23 -08:00
|
|
|
const float rpm = mc_interface_get_rpm();
|
2014-07-30 03:48:40 -07:00
|
|
|
|
2015-12-08 12:01:23 -08:00
|
|
|
if (output > 0.0 && rpm > -mc_interface_get_configuration()->l_max_erpm_fbrake) {
|
2014-09-18 14:00:14 -07:00
|
|
|
float current;
|
|
|
|
|
|
|
|
if (output > 0.0) {
|
2015-12-08 12:01:23 -08:00
|
|
|
current = output * mc_interface_get_configuration()->l_current_max;
|
2014-09-18 14:00:14 -07:00
|
|
|
} else {
|
2015-12-08 12:01:23 -08:00
|
|
|
current = output * fabsf(mc_interface_get_configuration()->l_current_min);
|
2014-09-18 14:00:14 -07:00
|
|
|
}
|
2014-07-30 03:48:40 -07:00
|
|
|
|
|
|
|
// Soft RPM limit
|
|
|
|
if (rpm > RPM_MAX_2) {
|
2015-12-08 12:01:23 -08:00
|
|
|
current = -mc_interface_get_configuration()->cc_min_current;
|
2014-07-30 03:48:40 -07:00
|
|
|
} else if (rpm > RPM_MAX_1) {
|
2015-12-08 12:01:23 -08:00
|
|
|
current = utils_map(rpm, RPM_MAX_1, RPM_MAX_2, current, -mc_interface_get_configuration()->cc_min_current);
|
2014-08-01 06:43:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some low-pass filtering
|
|
|
|
static float current_p1 = 0.0;
|
|
|
|
static float current_p2 = 0.0;
|
|
|
|
current = (current + current_p1 + current_p2) / 3;
|
|
|
|
current_p2 = current_p1;
|
|
|
|
current_p1 = current;
|
|
|
|
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_current(current);
|
2016-04-27 06:32:32 -07:00
|
|
|
comm_can_set_current(255, current);
|
2014-07-27 10:40:38 -07:00
|
|
|
} else {
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_brake_current(output * mc_interface_get_configuration()->l_current_min);
|
2016-04-27 06:32:32 -07:00
|
|
|
comm_can_set_current_brake(255, output * mc_interface_get_configuration()->l_current_min);
|
2014-07-27 10:40:38 -07:00
|
|
|
}
|
2014-06-07 12:11:02 -07:00
|
|
|
}
|
|
|
|
|
2014-08-29 17:02:32 -07:00
|
|
|
static uint16_t middle_of_3(uint16_t a, uint16_t b, uint16_t c) {
|
|
|
|
uint16_t middle;
|
|
|
|
|
|
|
|
if ((a <= b) && (a <= c)) {
|
|
|
|
middle = (b <= c) ? b : c;
|
|
|
|
} else if ((b <= a) && (b <= c)) {
|
|
|
|
middle = (a <= c) ? a : c;
|
|
|
|
} else {
|
|
|
|
middle = (a <= b) ? a : b;
|
|
|
|
}
|
|
|
|
return middle;
|
|
|
|
}
|
|
|
|
|
2014-04-18 15:09:46 -07:00
|
|
|
#endif
|