bldc/applications/app_sten.c

202 lines
4.2 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 "servo_dec.h"
#include "mcpwm.h"
2014-05-06 09:50:41 -07:00
#include "utils.h"
2014-05-01 12:20:53 -07:00
#include <math.h>
// Threads
2014-06-07 12:11:02 -07:00
static msg_t servo_thread(void *arg);
static msg_t uart_thread(void *arg);
static WORKING_AREA(servo_thread_wa, 1024);
static WORKING_AREA(uart_thread_wa, 1024);
static Thread *servo_tp;
2014-05-04 07:05:10 -07:00
static VirtualTimer vt;
2014-06-07 12:11:02 -07:00
static volatile systime_t last_uart_update_time;
// Private functions
static void servodec_func(void);
2014-05-04 07:05:10 -07:00
static void trig_func(void *p);
2014-06-07 12:11:02 -07:00
static void set_output(float output);
// Settings
#define TIMEOUT 500
#define HYST 0.10
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
float val = ((float)c / 128) - 1.0;
set_output(val);
last_uart_update_time = chTimeNow();
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(servo_thread_wa, sizeof(servo_thread_wa), NORMALPRIO, servo_thread, NULL);
chThdCreateStatic(uart_thread_wa, sizeof(uart_thread_wa), NORMALPRIO - 1, uart_thread, NULL);
2014-05-04 07:05:10 -07:00
}
static void trig_func(void *p) {
(void)p;
chSysLock();
chVTSetI(&vt, MS2ST(10), trig_func, NULL);
chSysUnlock();
2014-06-07 12:11:02 -07:00
chEvtSignalI(servo_tp, (eventmask_t) 1);
}
2014-06-07 12:11:02 -07:00
static msg_t uart_thread(void *arg) {
2014-05-01 12:20:53 -07:00
(void)arg;
chRegSetThreadName("LOGGING");
uartStart(&UARTD6, &uart_cfg);
palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(GPIO_AF_USART6) |
PAL_STM32_OSPEED_HIGHEST |
PAL_STM32_PUDR_PULLUP);
palSetPadMode(GPIOC, 7, PAL_MODE_ALTERNATE(GPIO_AF_USART6) |
PAL_STM32_OSPEED_HIGHEST |
PAL_STM32_PUDR_PULLUP);
systime_t time = chTimeNow();
for(;;) {
time += MS2ST(40);
2014-06-07 12:11:02 -07:00
if ((systime_t) ((float) chTimeElapsedSince(last_uart_update_time)
/ ((float) CH_FREQUENCY / 1000.0)) > (float)TIMEOUT) {
mcpwm_set_current(0.0);
}
2014-05-01 12:20:53 -07:00
chThdSleepUntil(time);
}
return 0;
}
static void servodec_func(void) {
chSysLockFromIsr();
2014-06-07 12:11:02 -07:00
chEvtSignalI(servo_tp, (eventmask_t) 1);
chSysUnlockFromIsr();
}
2014-06-07 12:11:02 -07:00
static msg_t servo_thread(void *arg) {
(void)arg;
chRegSetThreadName("APP_STEN");
2014-06-07 12:11:02 -07:00
servo_tp = chThdSelf();
2014-05-10 01:21:47 -07:00
servodec_init(servodec_func);
chSysLock();
chVTSetI(&vt, MS2ST(10), trig_func, NULL);
chSysUnlock();
for(;;) {
chEvtWaitAny((eventmask_t) 1);
2014-06-07 12:11:02 -07:00
if (servodec_get_time_since_update() < TIMEOUT) {
set_output(servodec_get_servo_as_float(0));
} else {
mcpwm_set_current(0.0);
}
}
return 0;
}
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;
}
mcpwm_set_current(output * MCPWM_CURRENT_MAX);
}
#endif