|
|
@@ -4,6 +4,9 @@
|
|
|
#include "foc/foc_config.h"
|
|
|
#include "foc/samples.h"
|
|
|
#include "math/fast_math.h"
|
|
|
+#include "bsp/timer_count32.h"
|
|
|
+#include "libs/time_measure.h"
|
|
|
+#include "os/os_task.h"
|
|
|
#include "bsp/delay.h"
|
|
|
#include "bsp/bsp.h"
|
|
|
#include "bsp/adc.h"
|
|
|
@@ -11,6 +14,12 @@
|
|
|
|
|
|
static bool _motor_started = false;
|
|
|
static float _motor_throttle = 0;
|
|
|
+static u32 mc_main_task_handler(void *param);
|
|
|
+
|
|
|
+void mc_init(void) {
|
|
|
+ PMSM_FOC_CoreInit();
|
|
|
+ shark_task_create(mc_main_task_handler, NULL);
|
|
|
+}
|
|
|
|
|
|
bool mc_start(u8 mode) {
|
|
|
if (_motor_started) {
|
|
|
@@ -102,17 +111,50 @@ static float _get_idq_from_throttle(float throttle) {
|
|
|
return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
|
|
|
}
|
|
|
|
|
|
-static void _brake_io_process(void) {
|
|
|
- bool brake = gpio_get_brake();
|
|
|
- for (int i = 0; i < 10; i++) {
|
|
|
- if (brake != gpio_get_brake()) {
|
|
|
- return;
|
|
|
- }
|
|
|
+/*do 50 times filter*/
|
|
|
+static void brake_timer_handler(shark_timer_t *t) {
|
|
|
+ int count = 50;
|
|
|
+ int settimes = 0;
|
|
|
+ while(count-- >= 0) {
|
|
|
+ bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
|
|
|
+ bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
|
|
|
+ if (b1 && b2) {
|
|
|
+ settimes++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (settimes == 0) {
|
|
|
+ PMSM_FOC_Brake(true);
|
|
|
+ }else if (settimes == 50) {
|
|
|
+ PMSM_FOC_Brake(false);
|
|
|
+ }else {
|
|
|
+ //有干扰,do nothing
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
|
|
|
+
|
|
|
+void mc_brake_handler(void){
|
|
|
+ shark_timer_post(&_brake_timer, 0);
|
|
|
+}
|
|
|
+void mc_pwm_up_handler(void){
|
|
|
+ phase_current_adc_triger();
|
|
|
+}
|
|
|
+
|
|
|
+measure_time_t g_meas_foc = {.exec_max_time = 15,};
|
|
|
+#define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
|
|
|
+#define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
|
|
|
+
|
|
|
+/*ADC 电流采集中断,调用FOC的核心处理函数*/
|
|
|
+void mc_phase_current_handler(void) {
|
|
|
+ if (phase_current_offset()) {//check if is adc offset checked
|
|
|
+ return;
|
|
|
}
|
|
|
- PMSM_FOC_Brake(brake);
|
|
|
+ TIME_MEATURE_START();
|
|
|
+ PMSM_FOC_Schedule();
|
|
|
+ TIME_MEATURE_END();
|
|
|
}
|
|
|
|
|
|
-u32 mc_main_task(void *param) {
|
|
|
+static u32 mc_main_task_handler(void *param) {
|
|
|
if (_motor_started) {
|
|
|
if (get_throttle_float() != _motor_throttle) {
|
|
|
_motor_throttle = get_throttle_float();
|
|
|
@@ -121,7 +163,6 @@ u32 mc_main_task(void *param) {
|
|
|
float torque_idq = _get_idq_from_throttle(_motor_throttle);
|
|
|
PMSM_FOC_Set_Current(torque_idq);
|
|
|
}
|
|
|
- _brake_io_process();
|
|
|
}
|
|
|
return 0;
|
|
|
}
|