bldc/main.c

338 lines
7.8 KiB
C
Raw Normal View History

2014-01-09 06:20:26 -08:00
/*
Copyright 2016 - 2019 Benjamin Vedder benjamin@vedder.se
2014-01-09 06:20:26 -08: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-01-09 06:20:26 -08: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-01-09 06:20:26 -08: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 "ch.h"
#include "hal.h"
#include "stm32f4xx_conf.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
2015-12-08 12:01:23 -08:00
#include "mc_interface.h"
2014-01-09 06:20:26 -08:00
#include "mcpwm.h"
2015-12-08 12:01:23 -08:00
#include "mcpwm_foc.h"
2014-01-09 06:20:26 -08:00
#include "ledpwm.h"
#include "comm_usb.h"
2014-01-09 06:20:26 -08:00
#include "ledpwm.h"
#include "terminal.h"
#include "hw.h"
#include "app.h"
#include "packet.h"
#include "commands.h"
2014-09-20 04:41:18 -07:00
#include "timeout.h"
2014-12-06 19:30:25 -08:00
#include "comm_can.h"
#include "ws2811.h"
#include "led_external.h"
2015-04-11 01:07:36 -07:00
#include "encoder.h"
#include "servo_simple.h"
2015-12-08 12:01:23 -08:00
#include "utils.h"
2016-11-04 07:18:34 -07:00
#include "nrf_driver.h"
#include "rfhelp.h"
#include "spi_sw.h"
#include "timer.h"
#include "imu.h"
#include "flash_helper.h"
#if HAS_BLACKMAGIC
#include "bm_if.h"
#endif
2014-01-09 06:20:26 -08:00
/*
* HW resources used:
*
2014-01-09 06:20:26 -08:00
* TIM1: mcpwm
* TIM5: timer
2014-01-09 06:20:26 -08:00
* TIM8: mcpwm
* TIM3: servo_dec/Encoder (HW_R2)/servo_simple
2015-04-11 01:07:36 -07:00
* TIM4: WS2811/WS2812 LEDs/Encoder (other HW)
2014-12-06 19:30:25 -08:00
*
* DMA/stream Device Function
* 1, 2 I2C1 Nunchuk, temp on rev 4.5
* 1, 7 I2C1 Nunchuk, temp on rev 4.5
* 2, 4 ADC mcpwm
* 1, 0 TIM4 WS2811/WS2812 LEDs CH1 (Ch 1)
* 1, 3 TIM4 WS2811/WS2812 LEDs CH2 (Ch 2)
2014-12-06 19:30:25 -08:00
*
2014-01-09 06:20:26 -08:00
*/
// Private variables
static THD_WORKING_AREA(periodic_thread_wa, 1024);
static THD_WORKING_AREA(timer_thread_wa, 128);
2019-04-18 13:30:01 -07:00
static THD_WORKING_AREA(flash_integrity_check_thread_wa, 256);
static THD_FUNCTION(flash_integrity_check_thread, arg) {
(void)arg;
2019-04-18 13:30:01 -07:00
chRegSetThreadName("Flash check");
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
for(;;) {
if (flash_helper_verify_flash_memory_chunk() == FAULT_CODE_FLASH_CORRUPTION) {
NVIC_SystemReset();
}
2019-04-18 13:30:01 -07:00
chThdSleepMilliseconds(6);
}
}
2014-01-09 06:20:26 -08:00
static THD_FUNCTION(periodic_thread, arg) {
2014-01-09 06:20:26 -08:00
(void)arg;
chRegSetThreadName("Main periodic");
for(;;) {
2015-12-08 12:01:23 -08:00
if (mc_interface_get_state() == MC_STATE_RUNNING) {
2014-01-09 06:20:26 -08:00
ledpwm_set_intensity(LED_GREEN, 1.0);
} else {
ledpwm_set_intensity(LED_GREEN, 0.2);
}
2015-12-08 12:01:23 -08:00
mc_fault_code fault = mc_interface_get_fault();
if (fault != FAULT_CODE_NONE) {
for (int i = 0;i < (int)fault;i++) {
ledpwm_set_intensity(LED_RED, 1.0);
chThdSleepMilliseconds(250);
ledpwm_set_intensity(LED_RED, 0.0);
chThdSleepMilliseconds(250);
}
chThdSleepMilliseconds(500);
2014-01-09 06:20:26 -08:00
} else {
ledpwm_set_intensity(LED_RED, 0.0);
}
2015-12-08 12:01:23 -08:00
if (mc_interface_get_state() == MC_STATE_DETECTING) {
commands_send_rotor_pos(mcpwm_get_detect_pos());
2014-03-13 07:28:56 -07:00
}
disp_pos_mode display_mode = commands_get_disp_pos_mode();
switch (display_mode) {
case DISP_POS_MODE_ENCODER:
commands_send_rotor_pos(encoder_read_deg());
break;
case DISP_POS_MODE_PID_POS:
commands_send_rotor_pos(mc_interface_get_pid_pos_now());
break;
case DISP_POS_MODE_PID_POS_ERROR:
commands_send_rotor_pos(utils_angle_difference(mc_interface_get_pid_pos_set(), mc_interface_get_pid_pos_now()));
break;
default:
break;
}
if (mc_interface_get_configuration()->motor_type == MOTOR_TYPE_FOC) {
switch (display_mode) {
case DISP_POS_MODE_OBSERVER:
commands_send_rotor_pos(mcpwm_foc_get_phase_observer());
break;
case DISP_POS_MODE_ENCODER_OBSERVER_ERROR:
commands_send_rotor_pos(utils_angle_difference(mcpwm_foc_get_phase_observer(), mcpwm_foc_get_phase_encoder()));
break;
default:
break;
}
}
chThdSleepMilliseconds(10);
2014-01-09 06:20:26 -08:00
}
}
static THD_FUNCTION(timer_thread, arg) {
(void)arg;
chRegSetThreadName("msec_timer");
for(;;) {
packet_timerfunc();
timeout_feed_WDT(THREAD_TIMER);
chThdSleepMilliseconds(1);
}
}
// When assertions enabled halve PWM frequency. The control loop ISR runs 40% slower
void assert_failed(uint8_t* file, uint32_t line) {
commands_printf("Wrong parameters value: file %s on line %d\r\n", file, line);
mc_interface_release_motor();
while(1) {
chThdSleepMilliseconds(1);
}
}
2014-01-09 06:20:26 -08:00
int main(void) {
halInit();
chSysInit();
2016-11-04 07:18:34 -07:00
// Initialize the enable pins here and disable them
// to avoid excessive current draw at boot because of
// floating pins.
#ifdef HW_HAS_DRV8313
INIT_BR();
#endif
chThdSleepMilliseconds(1000);
hw_init_gpio();
LED_RED_OFF();
LED_GREEN_OFF();
timer_init();
conf_general_init();
if( flash_helper_verify_flash_memory() == FAULT_CODE_FLASH_CORRUPTION ) {
// Loop here, it is not safe to run any code
while (1) {
chThdSleepMilliseconds(100);
LED_RED_ON();
chThdSleepMilliseconds(75);
LED_RED_OFF();
}
}
2014-01-09 06:20:26 -08:00
ledpwm_init();
mc_configuration mcconf;
conf_general_read_mc_configuration(&mcconf);
2015-12-08 12:01:23 -08:00
mc_interface_init(&mcconf);
2014-09-20 04:41:18 -07:00
commands_init();
comm_usb_init();
2017-11-08 05:07:35 -08:00
#if CAN_ENABLE
comm_can_init();
#endif
app_configuration appconf;
conf_general_read_app_configuration(&appconf);
2016-11-04 07:18:34 -07:00
app_set_configuration(&appconf);
2019-02-18 10:30:19 -08:00
app_uartcomm_start_permanent();
2016-11-04 07:18:34 -07:00
#ifdef HW_HAS_PERMANENT_NRF
conf_general_permanent_nrf_found = nrf_driver_init();
if (conf_general_permanent_nrf_found) {
rfhelp_restart();
} else {
nrf_driver_stop();
// Set the nrf SPI pins to the general SPI interface so that
// an external NRF can be used with the NRF app.
spi_sw_change_pins(
HW_SPI_PORT_NSS, HW_SPI_PIN_NSS,
HW_SPI_PORT_SCK, HW_SPI_PIN_SCK,
HW_SPI_PORT_MOSI, HW_SPI_PIN_MOSI,
HW_SPI_PORT_MISO, HW_SPI_PIN_MISO);
2019-02-18 10:30:19 -08:00
HW_PERMANENT_NRF_FAILED_HOOK();
}
2016-11-04 07:18:34 -07:00
#endif
#if WS2811_ENABLE
ws2811_init();
2016-11-04 07:18:34 -07:00
#if !WS2811_TEST
led_external_init();
#endif
#endif
#if SERVO_OUT_ENABLE
servo_simple_init();
#endif
2014-01-09 06:20:26 -08:00
// Threads
chThdCreateStatic(periodic_thread_wa, sizeof(periodic_thread_wa), NORMALPRIO, periodic_thread, NULL);
chThdCreateStatic(timer_thread_wa, sizeof(timer_thread_wa), NORMALPRIO, timer_thread, NULL);
chThdCreateStatic(flash_integrity_check_thread_wa, sizeof(flash_integrity_check_thread_wa), LOWPRIO, flash_integrity_check_thread, NULL);
2014-01-09 06:20:26 -08:00
#if WS2811_TEST
unsigned int color_ind = 0;
const int num = 4;
const uint32_t colors[] = {COLOR_RED, COLOR_GOLD, COLOR_GRAY, COLOR_MAGENTA, COLOR_BLUE};
const int brightness_set = 100;
for (;;) {
chThdSleepMilliseconds(1000);
for (int i = 0;i < brightness_set;i++) {
ws2811_set_brightness(i);
chThdSleepMilliseconds(10);
}
chThdSleepMilliseconds(1000);
for(int i = -num;i <= WS2811_LED_NUM;i++) {
ws2811_set_led_color(i - 1, COLOR_BLACK);
ws2811_set_led_color(i + num, colors[color_ind]);
ws2811_set_led_color(0, COLOR_RED);
ws2811_set_led_color(WS2811_LED_NUM - 1, COLOR_GREEN);
chThdSleepMilliseconds(50);
}
for (int i = 0;i < brightness_set;i++) {
ws2811_set_brightness(brightness_set - i);
chThdSleepMilliseconds(10);
}
color_ind++;
if (color_ind >= sizeof(colors) / sizeof(uint32_t)) {
color_ind = 0;
}
static int asd = 0;
asd++;
if (asd >= 3) {
asd = 0;
for (unsigned int i = 0;i < sizeof(colors) / sizeof(uint32_t);i++) {
ws2811_set_all(colors[i]);
for (int i = 0;i < brightness_set;i++) {
ws2811_set_brightness(i);
chThdSleepMilliseconds(2);
}
chThdSleepMilliseconds(100);
for (int i = 0;i < brightness_set;i++) {
ws2811_set_brightness(brightness_set - i);
chThdSleepMilliseconds(2);
}
}
}
}
#endif
timeout_init();
timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current);
imu_init();
#if HAS_BLACKMAGIC
bm_init();
#endif
2014-01-09 06:20:26 -08:00
for(;;) {
chThdSleepMilliseconds(10);
2014-01-09 06:20:26 -08:00
}
}