Files
bobbycar-foc-model/Src/control.c
T

345 lines
9.5 KiB
C
Raw Normal View History

2019-10-06 15:09:15 +02:00
#include <stdbool.h>
#include <string.h>
#include "stm32f1xx_hal.h"
#include "defines.h"
#include "setup.h"
#include "config.h"
#define NUNCHUK_I2C_ADDRESS 0xA4
2019-10-06 15:09:15 +02:00
TIM_HandleTypeDef TimHandle;
2020-03-21 19:24:29 +01:00
TIM_HandleTypeDef TimHandle2;
2020-12-20 10:16:31 +01:00
uint8_t ppm_count = 0;
uint8_t pwm_count = 0;
2020-12-20 22:21:01 +01:00
uint32_t timeoutCntGen = TIMEOUT;
2020-12-20 10:16:31 +01:00
uint8_t timeoutFlgGen = 0;
uint8_t nunchuk_data[6] = {0};
2019-10-06 15:09:15 +02:00
uint8_t i2cBuffer[2];
nunchuk_state nunchukState = NUNCHUK_CONNECTING;
2019-10-06 15:09:15 +02:00
extern I2C_HandleTypeDef hi2c2;
extern DMA_HandleTypeDef hdma_i2c2_rx;
extern DMA_HandleTypeDef hdma_i2c2_tx;
2020-06-25 20:17:55 +02:00
#if defined(CONTROL_PPM_LEFT) || defined(CONTROL_PPM_RIGHT)
2019-10-06 15:09:15 +02:00
uint16_t ppm_captured_value[PPM_NUM_CHANNELS + 1] = {500, 500};
uint16_t ppm_captured_value_buffer[PPM_NUM_CHANNELS+1] = {500, 500};
uint32_t ppm_timeout = 0;
bool ppm_valid = true;
void PPM_ISR_Callback(void) {
// Dummy loop with 16 bit count wrap around
uint16_t rc_delay = TIM2->CNT;
TIM2->CNT = 0;
if (rc_delay > 3000) {
if (ppm_valid && ppm_count == PPM_NUM_CHANNELS) {
ppm_timeout = 0;
2020-12-20 10:16:31 +01:00
timeoutCntGen = 0;
timeoutFlgGen = 0;
2019-10-06 15:09:15 +02:00
memcpy(ppm_captured_value, ppm_captured_value_buffer, sizeof(ppm_captured_value));
}
ppm_valid = true;
ppm_count = 0;
}
else if (ppm_count < PPM_NUM_CHANNELS && IN_RANGE(rc_delay, 900, 2100)){
ppm_captured_value_buffer[ppm_count++] = CLAMP(rc_delay, 1000, 2000) - 1000;
} else {
ppm_valid = false;
}
}
// SysTick executes once each ms
void PPM_SysTick_Callback(void) {
ppm_timeout++;
// Stop after 500 ms without PPM signal
if(ppm_timeout > 500) {
int i;
for(i = 0; i < PPM_NUM_CHANNELS; i++) {
ppm_captured_value[i] = 500;
}
ppm_timeout = 0;
}
}
void PPM_Init(void) {
2020-06-25 20:17:55 +02:00
GPIO_InitTypeDef GPIO_InitStruct = {0};
2019-10-06 15:09:15 +02:00
/*Configure GPIO pin : PA3 */
2020-06-25 20:17:55 +02:00
GPIO_InitStruct.Pin = PPM_PIN;
2019-10-06 15:09:15 +02:00
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
2020-06-25 20:17:55 +02:00
HAL_GPIO_Init(PPM_PORT, &GPIO_InitStruct);
2019-10-06 15:09:15 +02:00
__HAL_RCC_TIM2_CLK_ENABLE();
TimHandle.Instance = TIM2;
TimHandle.Init.Period = UINT16_MAX;
TimHandle.Init.Prescaler = (SystemCoreClock/DELAY_TIM_FREQUENCY_US)-1;;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_Base_Init(&TimHandle);
2020-11-15 20:04:40 +01:00
#if defined(CONTROL_PPM_LEFT)
2019-10-06 15:09:15 +02:00
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
2020-11-15 20:04:40 +01:00
#endif
#if defined(CONTROL_PPM_RIGHT)
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
#endif
2019-10-06 15:09:15 +02:00
HAL_TIM_Base_Start(&TimHandle);
}
#endif
2020-03-21 19:24:29 +01:00
2020-06-25 20:17:55 +02:00
#if defined(CONTROL_PWM_LEFT) || defined(CONTROL_PWM_RIGHT)
2020-06-13 10:57:54 +02:00
/*
* Illustration of the PWM functionality
* CH1 ________|‾‾‾‾‾‾‾‾‾‾|________
* CH2 ______________|‾‾‾‾‾‾‾‾‾‾‾|________
* ↑ ↑ ↑ ↑
* TIM2 RST SAVE RC_CH1 RC_CH1
*/
2020-03-24 19:57:08 +01:00
uint16_t pwm_captured_ch1_value = 500;
2020-03-21 19:24:29 +01:00
uint16_t pwm_captured_ch2_value = 500;
2020-06-13 10:57:54 +02:00
uint16_t pwm_CNT_prev_ch1 = 0;
uint16_t pwm_CNT_prev_ch2 = 0;
2020-06-13 00:08:34 +02:00
uint32_t pwm_timeout_ch1 = 0;
uint32_t pwm_timeout_ch2 = 0;
2020-03-21 19:24:29 +01:00
2020-06-02 13:04:10 +02:00
void PWM_ISR_CH1_Callback(void) {
2020-03-21 19:24:29 +01:00
// Dummy loop with 16 bit count wrap around
2020-06-25 20:17:55 +02:00
if(HAL_GPIO_ReadPin(PWM_PORT_CH1, PWM_PIN_CH1)) { // Rising Edge interrupt -> save timer value OR reset timer
if (HAL_GPIO_ReadPin(PWM_PORT_CH2, PWM_PIN_CH2)) {
2020-06-13 10:57:54 +02:00
pwm_CNT_prev_ch1 = TIM2->CNT;
} else {
TIM2->CNT = 0;
pwm_CNT_prev_ch1 = 0;
}
2020-06-13 00:08:34 +02:00
} else { // Falling Edge interrupt -> measure pulse duration
2020-06-13 10:57:54 +02:00
uint16_t rc_signal = TIM2->CNT - pwm_CNT_prev_ch1;
2020-06-13 00:08:34 +02:00
if (IN_RANGE(rc_signal, 900, 2100)){
2020-12-20 10:16:31 +01:00
timeoutCntGen = 0;
timeoutFlgGen = 0;
2020-06-13 00:08:34 +02:00
pwm_timeout_ch1 = 0;
pwm_captured_ch1_value = CLAMP(rc_signal, 1000, 2000) - 1000;
}
2020-06-02 11:53:59 +02:00
}
}
2020-06-02 13:04:10 +02:00
void PWM_ISR_CH2_Callback(void) {
2020-06-02 11:53:59 +02:00
// Dummy loop with 16 bit count wrap around
2020-06-25 20:17:55 +02:00
if(HAL_GPIO_ReadPin(PWM_PORT_CH2, PWM_PIN_CH2)) { // Rising Edge interrupt -> save timer value OR reset timer
if (HAL_GPIO_ReadPin(PWM_PORT_CH1, PWM_PIN_CH1)) {
2020-06-13 10:57:54 +02:00
pwm_CNT_prev_ch2 = TIM2->CNT;
} else {
TIM2->CNT = 0;
pwm_CNT_prev_ch2 = 0;
}
2020-06-13 00:08:34 +02:00
} else { // Falling Edge interrupt -> measure pulse duration
2020-06-13 10:57:54 +02:00
uint16_t rc_signal = TIM2->CNT - pwm_CNT_prev_ch2;
2020-06-13 00:08:34 +02:00
if (IN_RANGE(rc_signal, 900, 2100)){
2020-12-20 10:16:31 +01:00
timeoutCntGen = 0;
timeoutFlgGen = 0;
2020-06-13 00:08:34 +02:00
pwm_timeout_ch2 = 0;
pwm_captured_ch2_value = CLAMP(rc_signal, 1000, 2000) - 1000;
}
2020-03-21 19:24:29 +01:00
}
}
// SysTick executes once each ms
2020-06-02 13:04:10 +02:00
void PWM_SysTick_Callback(void) {
2020-06-13 00:08:34 +02:00
pwm_timeout_ch1++;
pwm_timeout_ch2++;
2020-07-01 19:50:32 +02:00
// Stop after 500 ms without PWM signal
2020-06-13 00:08:34 +02:00
if(pwm_timeout_ch1 > 500) {
2020-06-02 14:42:14 +02:00
pwm_captured_ch1_value = 500;
2020-06-13 00:08:34 +02:00
pwm_timeout_ch1 = 0;
}
if(pwm_timeout_ch2 > 500) {
2020-03-21 19:24:29 +01:00
pwm_captured_ch2_value = 500;
2020-06-13 00:08:34 +02:00
pwm_timeout_ch2 = 0;
2020-03-21 19:24:29 +01:00
}
}
2020-06-02 13:04:10 +02:00
void PWM_Init(void) {
2020-06-02 11:53:59 +02:00
// PWM Timer (TIM2)
__HAL_RCC_TIM2_CLK_ENABLE();
2020-06-02 13:04:10 +02:00
TimHandle.Instance = TIM2;
TimHandle.Init.Period = UINT16_MAX;
TimHandle.Init.Prescaler = (SystemCoreClock/DELAY_TIM_FREQUENCY_US)-1;;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
2020-06-25 20:17:55 +02:00
HAL_TIM_Base_Init(&TimHandle);
2020-06-02 11:53:59 +02:00
2020-03-21 19:24:29 +01:00
// Channel 1 (steering)
2020-06-25 20:17:55 +02:00
GPIO_InitTypeDef GPIO_InitStruct1 = {0};
// Configure GPIO pin : PA2 (Left) or PB10 (Right)
GPIO_InitStruct1.Pin = PWM_PIN_CH1;
GPIO_InitStruct1.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct1.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct1.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(PWM_PORT_CH1, &GPIO_InitStruct1);
// Channel 2 (speed)
GPIO_InitTypeDef GPIO_InitStruct2 = {0};
/*Configure GPIO pin : PA3 (Left) or PB11 (Right) */
GPIO_InitStruct2.Pin = PWM_PIN_CH2;
2020-06-02 13:04:10 +02:00
GPIO_InitStruct2.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct2.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct2.Pull = GPIO_PULLDOWN;
2020-06-25 20:17:55 +02:00
HAL_GPIO_Init(PWM_PORT_CH2, &GPIO_InitStruct2);
2020-03-21 19:24:29 +01:00
2020-06-25 20:17:55 +02:00
#ifdef CONTROL_PWM_LEFT
/* EXTI interrupt init*/
2020-03-21 19:24:29 +01:00
HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
2020-06-25 20:17:55 +02:00
#endif
#ifdef CONTROL_PWM_RIGHT
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
#endif
2020-06-02 11:53:59 +02:00
// Start timer
2020-03-21 19:24:29 +01:00
HAL_TIM_Base_Start(&TimHandle);
}
#endif
uint8_t Nunchuk_tx(uint8_t i2cBuffer[], uint8_t i2cBufferLength) {
if(HAL_I2C_Master_Transmit(&hi2c2,NUNCHUK_I2C_ADDRESS,(uint8_t*)i2cBuffer, i2cBufferLength, 100) == HAL_OK) {
return true;
2019-11-26 23:58:20 +01:00
}
return false;
2019-11-26 23:58:20 +01:00
}
uint8_t Nunchuk_rx(uint8_t i2cBuffer[], uint8_t i2cBufferLength) {
if(HAL_I2C_Master_Receive(&hi2c2,NUNCHUK_I2C_ADDRESS,(uint8_t*)i2cBuffer, i2cBufferLength, 100) == HAL_OK) {
return true;
}
return false;
}
uint8_t Nunchuk_Init(void) {
//-- START -- init WiiNunchuk
2019-10-06 15:09:15 +02:00
i2cBuffer[0] = 0xF0;
i2cBuffer[1] = 0x55;
if(Nunchuk_tx(i2cBuffer, 2) == false) {
return false;
}
2019-10-06 15:09:15 +02:00
HAL_Delay(10);
i2cBuffer[0] = 0xFB;
i2cBuffer[1] = 0x00;
if(Nunchuk_tx(i2cBuffer, 2) == false) {
return false;
}
2019-10-06 15:09:15 +02:00
HAL_Delay(10);
return true;
2019-10-06 15:09:15 +02:00
}
uint8_t Nunchuk_Connect() {
/* Initialise / re-initialise I2C peripheral */
I2C_Init();
/* Initialise / re-initialise nunchuk */
if(Nunchuk_Init() == true) {
nunchukState = NUNCHUK_CONNECTED;
return true;
} else {
return false;
}
}
nunchuk_state Nunchuk_Read(void) {
static uint8_t delay_counter = 0;
uint16_t checksum = 0;
uint8_t success = true;
uint8_t i = 0;
switch(nunchukState) {
case NUNCHUK_DISCONNECTED:
success = false;
/* Delay a bit before reconnecting */
if(delay_counter++ > 100) {
success = Nunchuk_Connect();
delay_counter = 0;
}
break;
case NUNCHUK_CONNECTING:
case NUNCHUK_RECONNECTING:
/* Try to reconnect once, if fails again fall back to disconnected state */
success = Nunchuk_Connect();
if(!success) {
nunchukState = NUNCHUK_DISCONNECTED;
}
break;
case NUNCHUK_CONNECTED:
/* Send read address of 0x00 to the Nunchuk */
i2cBuffer[0] = 0x00;
if(!Nunchuk_tx(i2cBuffer, 1)) {
success = false;
}
HAL_Delay(3);
/* Clear the receive data buffer */
for(i = 0; i<6; i++) {
nunchuk_data[i] = 0;
}
/* Read back 6 bytes from the Nunchuk */
if(!Nunchuk_rx(nunchuk_data, 6)) {
success = false;
}
HAL_Delay(3);
/* Checksum the receive buffer to ensure it is not in an error condition, i.e. all 0x00 or 0xFF */
for(i = 0; i<6; i++) {
checksum += nunchuk_data[i];
}
if(checksum == 0 || checksum == 0x5FA) {
success = false;
}
/* Comms failure or timeout counter reached timeout limit */
if(success == false || timeoutCntGen > 3) {
/* Clear the receive data buffer */
for(i = 0; i<6; i++) {
nunchuk_data[i] = 0;
}
/* Brings motors to safe stop */
/* Expected values from nunchuk for stopped (mid) position */
nunchuk_data[0] = 127;
nunchuk_data[1] = 128;
timeoutFlgGen = 1;
nunchukState = NUNCHUK_RECONNECTING;
}
break;
}
/* Reset the timeout flag and counter if successful communication */
if(success == true) {
2020-12-20 10:16:31 +01:00
timeoutCntGen = 0;
timeoutFlgGen = 0;
2019-10-06 15:09:15 +02:00
}
return nunchukState;
2020-01-18 01:05:06 +01:00
//setScopeChannel(0, (int)nunchuk_data[0]);
//setScopeChannel(1, (int)nunchuk_data[1]);
//setScopeChannel(2, (int)nunchuk_data[5] & 1);
//setScopeChannel(3, ((int)nunchuk_data[5] >> 1) & 1);
2019-10-06 15:09:15 +02:00
}