bldc/applications/app_sten.c

209 lines
4.7 KiB
C
Raw Normal View History

/*
Copyright 2012-2014 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_sten.c
*
* Created on: 18 apr 2014
* Author: benjamin
*/
#include "app.h"
#ifdef USE_APP_STEN
#include "ch.h"
#include "hal.h"
#include "stm32f4xx_conf.h"
#include "mcpwm.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"
2014-05-01 12:20:53 -07:00
#include <math.h>
// Settings
#define HYST 0.10
// 29000rpm = 20kmh
#define RPM_MAX_1 41000.0 // Start decreasing output here
#define RPM_MAX_2 44000.0 // Completely stop output here
// Private variables
static volatile float out_received = 0.0;
// Threads
static THD_FUNCTION(uart_thread, arg);
static THD_WORKING_AREA(uart_thread_wa, 1024);
// Private functions
2014-06-07 12:11:02 -07:00
static void set_output(float output);
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
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
};
void app_sten_init(void) {
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
}
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);
systime_t time = chVTGetSystemTime();
2014-05-01 12:20:53 -07:00
for(;;) {
time += MS2ST(1);
2014-05-01 12:20:53 -07:00
2014-09-21 08:41:11 -07:00
if (!timeout_has_timeout()) {
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) {
output /= (1.0 - HYST);
if (output > HYST) {
output -= HYST;
} else if (output < -HYST) {
output += HYST;
} else {
output = 0.0;
}
const float rpm = mcpwm_get_rpm();
2014-10-05 07:23:02 -07:00
if (output > 0.0 && rpm > -mcpwm_get_configuration()->l_max_erpm_fbrake) {
float current;
if (output > 0.0) {
current = output * mcpwm_get_configuration()->l_current_max;
} else {
current = output * fabsf(mcpwm_get_configuration()->l_current_min);
}
// Soft RPM limit
if (rpm > RPM_MAX_2) {
current = -mcpwm_get_configuration()->cc_min_current;
} else if (rpm > RPM_MAX_1) {
current = utils_map(rpm, RPM_MAX_1, RPM_MAX_2, current, -mcpwm_get_configuration()->cc_min_current);
}
// 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;
if (fabsf(current) < mcpwm_get_configuration()->cc_min_current) {
current = -mcpwm_get_configuration()->cc_min_current;
}
mcpwm_set_current(current);
} else {
mcpwm_set_brake_current(output * mcpwm_get_configuration()->l_current_min);
}
2014-06-07 12:11:02 -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;
}
#endif