forked from lucysrausch/hoverboard-firmware-hack
► Warning fixes
- Changed all float constants to use f suffix to prevent double conversions - Removed compile warnings due to HAL_GPIO_* related calls - Added comms.h with prototypes for functions which were used in main() - Casts and externs to fix compile warnings in various files. - Made a bunch of file-local variables static. - Fixed buzzerFreq/buzzerPattern extern declarations with correct type (uint8_t) - Since this is C, void func() { } needs to be void func(void) { }, so fixed that in all instances. These updates follows from @trollcop
This commit is contained in:
38
Src/bldc.c
38
Src/bldc.c
@@ -52,28 +52,28 @@ extern volatile adc_buf_t adc_buffer;
|
||||
|
||||
extern volatile uint32_t timeout;
|
||||
|
||||
uint32_t buzzerFreq = 0;
|
||||
uint32_t buzzerPattern = 0;
|
||||
uint32_t buzzerTimer = 0;
|
||||
uint8_t buzzerFreq = 0;
|
||||
uint8_t buzzerPattern = 0;
|
||||
static uint32_t buzzerTimer = 0;
|
||||
|
||||
uint8_t enable = 0;
|
||||
|
||||
const int pwm_res = 64000000 / 2 / PWM_FREQ; // = 2000
|
||||
static const uint16_t pwm_res = 64000000 / 2 / PWM_FREQ; // = 2000
|
||||
|
||||
int offsetcount = 0;
|
||||
int offsetrl1 = 2000;
|
||||
int offsetrl2 = 2000;
|
||||
int offsetrr1 = 2000;
|
||||
int offsetrr2 = 2000;
|
||||
int offsetdcl = 2000;
|
||||
int offsetdcr = 2000;
|
||||
static int offsetcount = 0;
|
||||
static int offsetrl1 = 2000;
|
||||
static int offsetrl2 = 2000;
|
||||
static int offsetrr1 = 2000;
|
||||
static int offsetrr2 = 2000;
|
||||
static int offsetdcl = 2000;
|
||||
static int offsetdcr = 2000;
|
||||
|
||||
float batteryVoltage = BAT_NUMBER_OF_CELLS * 4.0;
|
||||
|
||||
//scan 8 channels with 2ADCs @ 20 clk cycles per sample
|
||||
//meaning ~80 ADC clock cycles @ 8MHz until new DMA interrupt =~ 100KHz
|
||||
//=640 cpu cycles
|
||||
void DMA1_Channel1_IRQHandler() {
|
||||
void DMA1_Channel1_IRQHandler(void) {
|
||||
|
||||
DMA1->IFCR = DMA_IFCR_CTCIF1;
|
||||
// HAL_GPIO_WritePin(LED_PORT, LED_PIN, 1);
|
||||
@@ -91,7 +91,7 @@ void DMA1_Channel1_IRQHandler() {
|
||||
}
|
||||
|
||||
if (buzzerTimer % 1000 == 0) { // because you get float rounding errors if it would run every time
|
||||
batteryVoltage = batteryVoltage * 0.99 + ((float)adc_buffer.batt1 * ((float)BAT_CALIB_REAL_VOLTAGE / (float)BAT_CALIB_ADC)) * 0.01;
|
||||
batteryVoltage = batteryVoltage * 0.99f + ((float)adc_buffer.batt1 * ((float)BAT_CALIB_REAL_VOLTAGE / (float)BAT_CALIB_ADC)) * 0.01f;
|
||||
}
|
||||
|
||||
//disable PWM when current limit is reached (current chopping)
|
||||
@@ -153,9 +153,9 @@ void DMA1_Channel1_IRQHandler() {
|
||||
// motAngleLeft = rtY_Left.a_elecAngle;
|
||||
|
||||
/* Apply commands */
|
||||
LEFT_TIM->LEFT_TIM_U = CLAMP(ul + pwm_res / 2, 10, pwm_res-10);
|
||||
LEFT_TIM->LEFT_TIM_V = CLAMP(vl + pwm_res / 2, 10, pwm_res-10);
|
||||
LEFT_TIM->LEFT_TIM_W = CLAMP(wl + pwm_res / 2, 10, pwm_res-10);
|
||||
LEFT_TIM->LEFT_TIM_U = (uint16_t)CLAMP(ul + pwm_res / 2, 10, pwm_res-10);
|
||||
LEFT_TIM->LEFT_TIM_V = (uint16_t)CLAMP(vl + pwm_res / 2, 10, pwm_res-10);
|
||||
LEFT_TIM->LEFT_TIM_W = (uint16_t)CLAMP(wl + pwm_res / 2, 10, pwm_res-10);
|
||||
// =================================================================
|
||||
|
||||
|
||||
@@ -182,9 +182,9 @@ void DMA1_Channel1_IRQHandler() {
|
||||
// motAngleRight = rtY_Right.a_elecAngle;
|
||||
|
||||
/* Apply commands */
|
||||
RIGHT_TIM->RIGHT_TIM_U = CLAMP(ur + pwm_res / 2, 10, pwm_res-10);
|
||||
RIGHT_TIM->RIGHT_TIM_V = CLAMP(vr + pwm_res / 2, 10, pwm_res-10);
|
||||
RIGHT_TIM->RIGHT_TIM_W = CLAMP(wr + pwm_res / 2, 10, pwm_res-10);
|
||||
RIGHT_TIM->RIGHT_TIM_U = (uint16_t)CLAMP(ur + pwm_res / 2, 10, pwm_res-10);
|
||||
RIGHT_TIM->RIGHT_TIM_V = (uint16_t)CLAMP(vr + pwm_res / 2, 10, pwm_res-10);
|
||||
RIGHT_TIM->RIGHT_TIM_W = (uint16_t)CLAMP(wr + pwm_res / 2, 10, pwm_res-10);
|
||||
// =================================================================
|
||||
|
||||
/* Indicate task complete */
|
||||
|
19
Src/comms.c
19
Src/comms.c
@@ -1,9 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "defines.h"
|
||||
#include "setup.h"
|
||||
#include "config.h"
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "comms.h"
|
||||
|
||||
UART_HandleTypeDef huart2;
|
||||
|
||||
@@ -16,15 +17,15 @@ UART_HandleTypeDef huart2;
|
||||
#endif
|
||||
|
||||
|
||||
volatile uint8_t uart_buf[100];
|
||||
volatile int16_t ch_buf[8];
|
||||
static volatile uint8_t uart_buf[100];
|
||||
static volatile int16_t ch_buf[8];
|
||||
//volatile char char_buf[300];
|
||||
|
||||
void setScopeChannel(uint8_t ch, int16_t val) {
|
||||
ch_buf[ch] = val;
|
||||
}
|
||||
|
||||
void consoleScope() {
|
||||
void consoleScope(void) {
|
||||
#if defined DEBUG_SERIAL_SERVOTERM && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3)
|
||||
uart_buf[0] = 0xff;
|
||||
uart_buf[1] = CLAMP(ch_buf[0]+127, 0, 255);
|
||||
@@ -46,12 +47,12 @@ void consoleScope() {
|
||||
#endif
|
||||
|
||||
#if defined DEBUG_SERIAL_ASCII && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3)
|
||||
memset(uart_buf, 0, sizeof(uart_buf));
|
||||
sprintf(uart_buf, "1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n", ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]);
|
||||
memset((void *)(uintptr_t)uart_buf, 0, sizeof(uart_buf));
|
||||
sprintf((char *)(uintptr_t)uart_buf, "1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n", ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]);
|
||||
|
||||
if(UART_DMA_CHANNEL->CNDTR == 0) {
|
||||
UART_DMA_CHANNEL->CCR &= ~DMA_CCR_EN;
|
||||
UART_DMA_CHANNEL->CNDTR = strlen(uart_buf);
|
||||
UART_DMA_CHANNEL->CNDTR = strlen((char *)(uintptr_t)uart_buf);
|
||||
UART_DMA_CHANNEL->CMAR = (uint32_t)uart_buf;
|
||||
UART_DMA_CHANNEL->CCR |= DMA_CCR_EN;
|
||||
}
|
||||
@@ -60,5 +61,5 @@ void consoleScope() {
|
||||
|
||||
void consoleLog(char *message)
|
||||
{
|
||||
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)message, strlen(message));
|
||||
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)message, (uint16_t)strlen(message));
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "defines.h"
|
||||
#include "setup.h"
|
||||
#include "config.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
TIM_HandleTypeDef TimHandle;
|
||||
uint8_t ppm_count = 0;
|
||||
@@ -14,8 +14,8 @@ uint8_t nunchuck_data[6] = {0};
|
||||
uint8_t i2cBuffer[2];
|
||||
|
||||
extern I2C_HandleTypeDef hi2c2;
|
||||
DMA_HandleTypeDef hdma_i2c2_rx;
|
||||
DMA_HandleTypeDef hdma_i2c2_tx;
|
||||
extern DMA_HandleTypeDef hdma_i2c2_rx;
|
||||
extern DMA_HandleTypeDef hdma_i2c2_tx;
|
||||
|
||||
#ifdef CONTROL_PPM
|
||||
uint16_t ppm_captured_value[PPM_NUM_CHANNELS + 1] = {500, 500};
|
||||
@@ -26,7 +26,7 @@ bool ppm_valid = true;
|
||||
|
||||
#define IN_RANGE(x, low, up) (((x) >= (low)) && ((x) <= (up)))
|
||||
|
||||
void PPM_ISR_Callback() {
|
||||
void PPM_ISR_Callback(void) {
|
||||
// Dummy loop with 16 bit count wrap around
|
||||
uint16_t rc_delay = TIM2->CNT;
|
||||
TIM2->CNT = 0;
|
||||
@@ -48,7 +48,7 @@ void PPM_ISR_Callback() {
|
||||
}
|
||||
|
||||
// SysTick executes once each ms
|
||||
void PPM_SysTick_Callback() {
|
||||
void PPM_SysTick_Callback(void) {
|
||||
ppm_timeout++;
|
||||
// Stop after 500 ms without PPM signal
|
||||
if(ppm_timeout > 500) {
|
||||
@@ -60,7 +60,7 @@ void PPM_SysTick_Callback() {
|
||||
}
|
||||
}
|
||||
|
||||
void PPM_Init() {
|
||||
void PPM_Init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
/*Configure GPIO pin : PA3 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_3;
|
||||
@@ -84,7 +84,7 @@ void PPM_Init() {
|
||||
}
|
||||
#endif
|
||||
|
||||
void Nunchuck_Init() {
|
||||
void Nunchuck_Init(void) {
|
||||
//-- START -- init WiiNunchuck
|
||||
i2cBuffer[0] = 0xF0;
|
||||
i2cBuffer[1] = 0x55;
|
||||
@@ -99,7 +99,7 @@ void Nunchuck_Init() {
|
||||
HAL_Delay(10);
|
||||
}
|
||||
|
||||
void Nunchuck_Read() {
|
||||
void Nunchuck_Read(void) {
|
||||
i2cBuffer[0] = 0x00;
|
||||
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 1, 100);
|
||||
HAL_Delay(5);
|
||||
|
52
Src/main.c
52
Src/main.c
@@ -19,10 +19,12 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdlib.h> // for abs()
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "defines.h"
|
||||
#include "setup.h"
|
||||
#include "config.h"
|
||||
#include "comms.h"
|
||||
//#include "hd44780.h"
|
||||
|
||||
// Matlab includes and defines - from auto-code generation
|
||||
@@ -47,6 +49,7 @@ ExtY rtY_Right; /* External outputs */
|
||||
// ###############################################################################
|
||||
|
||||
void SystemClock_Config(void);
|
||||
void poweroff(void);
|
||||
|
||||
extern TIM_HandleTypeDef htim_left;
|
||||
extern TIM_HandleTypeDef htim_right;
|
||||
@@ -57,8 +60,8 @@ extern volatile adc_buf_t adc_buffer;
|
||||
extern I2C_HandleTypeDef hi2c2;
|
||||
extern UART_HandleTypeDef huart2;
|
||||
|
||||
int cmd1; // normalized input values. -1000 to 1000
|
||||
int cmd2;
|
||||
static int cmd1; // normalized input values. -1000 to 1000
|
||||
static int cmd2;
|
||||
|
||||
typedef struct{
|
||||
int16_t steer;
|
||||
@@ -66,12 +69,12 @@ typedef struct{
|
||||
//uint32_t crc;
|
||||
} Serialcommand;
|
||||
|
||||
volatile Serialcommand command;
|
||||
static volatile Serialcommand command;
|
||||
|
||||
uint8_t button1, button2;
|
||||
static uint8_t button1, button2;
|
||||
|
||||
int steer; // global variable for steering. -1000 to 1000
|
||||
int speed; // global variable for speed. -1000 to 1000
|
||||
static int steer; // global variable for steering. -1000 to 1000
|
||||
static int speed; // global variable for speed. -1000 to 1000
|
||||
|
||||
extern volatile int pwml; // global variable for pwm left. -1000 to 1000
|
||||
extern volatile int pwmr; // global variable for pwm right. -1000 to 1000
|
||||
@@ -84,22 +87,20 @@ extern uint8_t enable; // global variable for motor enable
|
||||
extern volatile uint32_t timeout; // global variable for timeout
|
||||
extern float batteryVoltage; // global variable for battery voltage
|
||||
|
||||
uint32_t inactivity_timeout_counter;
|
||||
static uint32_t inactivity_timeout_counter;
|
||||
|
||||
extern uint8_t nunchuck_data[6];
|
||||
#ifdef CONTROL_PPM
|
||||
extern volatile uint16_t ppm_captured_value[PPM_NUM_CHANNELS+1];
|
||||
#endif
|
||||
|
||||
int milli_vel_error_sum = 0;
|
||||
|
||||
|
||||
void poweroff() {
|
||||
void poweroff(void) {
|
||||
// if (abs(speed) < 20) { // wait for the speed to drop, then shut down -> this is commented out for SAFETY reasons
|
||||
buzzerPattern = 0;
|
||||
enable = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
buzzerFreq = i;
|
||||
buzzerFreq = (uint8_t)i;
|
||||
HAL_Delay(100);
|
||||
}
|
||||
HAL_GPIO_WritePin(OFF_PORT, OFF_PIN, 0);
|
||||
@@ -172,7 +173,7 @@ int main(void) {
|
||||
// ###############################################################################
|
||||
|
||||
for (int i = 8; i >= 0; i--) {
|
||||
buzzerFreq = i;
|
||||
buzzerFreq = (uint8_t)i;
|
||||
HAL_Delay(100);
|
||||
}
|
||||
buzzerFreq = 0;
|
||||
@@ -267,12 +268,12 @@ int main(void) {
|
||||
// cmd1 = 0;
|
||||
|
||||
// ####### LOW-PASS FILTER #######
|
||||
steer = steer * (1.0 - FILTER) + cmd1 * FILTER;
|
||||
speed = speed * (1.0 - FILTER) + cmd2 * FILTER;
|
||||
steer = (int)(steer * (1.0f - FILTER) + cmd1 * FILTER);
|
||||
speed = (int)(speed * (1.0f - FILTER) + cmd2 * FILTER);
|
||||
|
||||
// ####### MIXER #######
|
||||
speedR = CLAMP(speed * SPEED_COEFFICIENT - steer * STEER_COEFFICIENT, -1000, 1000);
|
||||
speedL = CLAMP(speed * SPEED_COEFFICIENT + steer * STEER_COEFFICIENT, -1000, 1000);
|
||||
speedR = CLAMP((int)(speed * SPEED_COEFFICIENT - steer * STEER_COEFFICIENT), -1000, 1000);
|
||||
speedL = CLAMP((int)(speed * SPEED_COEFFICIENT + steer * STEER_COEFFICIENT), -1000, 1000);
|
||||
|
||||
|
||||
#ifdef ADDITIONAL_CODE
|
||||
@@ -300,7 +301,7 @@ int main(void) {
|
||||
|
||||
if (inactivity_timeout_counter % 25 == 0) {
|
||||
// ####### CALC BOARD TEMPERATURE #######
|
||||
board_temp_adc_filtered = board_temp_adc_filtered * 0.99 + (float)adc_buffer.temp * 0.01;
|
||||
board_temp_adc_filtered = board_temp_adc_filtered * 0.99f + (float)adc_buffer.temp * 0.01f;
|
||||
board_temp_deg_c = ((float)TEMP_CAL_HIGH_DEG_C - (float)TEMP_CAL_LOW_DEG_C) / ((float)TEMP_CAL_HIGH_ADC - (float)TEMP_CAL_LOW_ADC) * (board_temp_adc_filtered - (float)TEMP_CAL_LOW_ADC) + (float)TEMP_CAL_LOW_DEG_C;
|
||||
|
||||
// ####### DEBUG SERIAL OUT #######
|
||||
@@ -308,15 +309,14 @@ int main(void) {
|
||||
// setScopeChannel(0, (int)adc_buffer.l_tx2); // 1: ADC1
|
||||
// setScopeChannel(1, (int)adc_buffer.l_rx2); // 2: ADC2
|
||||
#endif
|
||||
setScopeChannel(0, (int)speedR); // 1: output command: [-1000, 1000]
|
||||
setScopeChannel(1, (int)speedL); // 2: output command: [-1000, 1000]
|
||||
setScopeChannel(2, (int)rtY_Right.n_mot); // 3: Real motor speed [rpm]
|
||||
setScopeChannel(3, (int)rtY_Left.n_mot); // 4: Real motor speed [rpm]
|
||||
setScopeChannel(4, (int)adc_buffer.batt1); // 5: for battery voltage calibration
|
||||
setScopeChannel(5, (int)(batteryVoltage * 100.0f)); // 6: for verifying battery voltage calibration
|
||||
setScopeChannel(6, (int)board_temp_adc_filtered); // 7: for board temperature calibration
|
||||
setScopeChannel(7, (int)board_temp_deg_c); // 8: for verifying board temperature calibration
|
||||
|
||||
setScopeChannel(0, (int16_t)speedR); // 1: output command: [-1000, 1000]
|
||||
setScopeChannel(1, (int16_t)speedL); // 2: output command: [-1000, 1000]
|
||||
setScopeChannel(2, (int16_t)rtY_Right.n_mot); // 3: Real motor speed [rpm]
|
||||
setScopeChannel(3, (int16_t)rtY_Left.n_mot); // 4: Real motor speed [rpm]
|
||||
setScopeChannel(4, (int16_t)adc_buffer.batt1); // 5: for battery voltage calibration
|
||||
setScopeChannel(5, (int16_t)(batteryVoltage * 100.0f)); // 6: for verifying battery voltage calibration
|
||||
setScopeChannel(6, (int16_t)board_temp_adc_filtered); // 7: for board temperature calibration
|
||||
setScopeChannel(7, (int16_t)board_temp_deg_c); // 8: for verifying board temperature calibration
|
||||
consoleScope();
|
||||
}
|
||||
|
||||
|
10
Src/setup.c
10
Src/setup.c
@@ -53,7 +53,7 @@ volatile adc_buf_t adc_buffer;
|
||||
#ifdef CONTROL_SERIAL_USART2
|
||||
|
||||
|
||||
void UART_Control_Init() {
|
||||
void UART_Control_Init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
/* DMA1_Channel6_IRQn interrupt configuration */
|
||||
@@ -123,7 +123,7 @@ HAL_DMA_Init(&hdma_usart2_tx);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_SERIAL_USART3
|
||||
void UART_Init() {
|
||||
void UART_Init(void) {
|
||||
__HAL_RCC_USART3_CLK_ENABLE();
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
@@ -156,7 +156,7 @@ void UART_Init() {
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_SERIAL_USART2
|
||||
void UART_Init() {
|
||||
void UART_Init(void) {
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
@@ -189,7 +189,7 @@ void UART_Init() {
|
||||
#endif
|
||||
|
||||
/*
|
||||
void UART_Init() {
|
||||
void UART_Init(void) {
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
@@ -224,7 +224,7 @@ void UART_Init() {
|
||||
DMA_HandleTypeDef hdma_i2c2_rx;
|
||||
DMA_HandleTypeDef hdma_i2c2_tx;
|
||||
|
||||
void I2C_Init()
|
||||
void I2C_Init(void)
|
||||
{
|
||||
|
||||
__HAL_RCC_I2C2_CLK_ENABLE();
|
||||
|
Reference in New Issue
Block a user