Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions ACAcontrollerState.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,13 @@ uint16_t ui16_x4_value = 0;
uint16_t ui16_throttle_cal_b = 0;
uint16_t ui16_battery_current_max_value = 0;
uint16_t ui16_regen_current_max_value = 0;
uint8_t ui8_possible_motor_state = 0;
uint8_t ui8_dynamic_motor_state = 0;
uint8_t ui8_BatteryVoltage = 0; //Battery Voltage read from ADC
uint8_t ui8_battery_voltage_nominal =0;
uint16_t ui16_motor_speed_erps = 0;
uint32_t ui32_erps_filtered = 0; //filtered value of erps
uint16_t ui16_virtual_erps_speed = 0;
uint16_t ui16_BatteryCurrent = 0; //Battery Current read from ADC8
uint8_t ui8_position_correction_value = 127; // in 360/256 degrees
uint8_t ui8_correction_at_angle = 127;
uint16_t ui16_ADC_iq_current = 0;
uint16_t ui16_ADC_iq_current_filtered = 0;
uint16_t ui16_control_state = 0;
uint8_t ui8_uptime = 0;

Expand All @@ -107,8 +102,6 @@ uint8_t ui8_variableDebugC = 0;

int8_t i8_motor_temperature = 0;

uint8_t uint8_t_60deg_pwm_cycles[6];
uint8_t uint8_t_hall_case[7];
uint8_t uint8_t_hall_order[6];
int8_t int8_t_hall_counter = 0;
uint8_t ui8_hall_order_counter = 5;
Expand Down
7 changes: 0 additions & 7 deletions ACAcontrollerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,13 @@ extern uint16_t ui16_x4_value;
extern uint16_t ui16_throttle_cal_b;
extern uint16_t ui16_battery_current_max_value;
extern uint16_t ui16_regen_current_max_value;
extern uint8_t ui8_possible_motor_state;
extern uint8_t ui8_dynamic_motor_state;
extern uint8_t ui8_BatteryVoltage;
extern uint8_t ui8_battery_voltage_nominal;
extern uint16_t ui16_motor_speed_erps;
extern uint16_t ui16_virtual_erps_speed;
extern uint32_t ui32_erps_filtered; //filtered value of erps
extern uint16_t ui16_BatteryCurrent;
extern uint8_t ui8_position_correction_value;
extern uint8_t ui8_correction_at_angle;
extern uint16_t ui16_ADC_iq_current;
extern uint16_t ui16_ADC_iq_current_filtered;
extern uint8_t ui8_speedlimit_kph;
extern uint8_t ui8_speedlimit_without_pas_kph;
extern uint8_t ui8_speedlimit_actual_kph;
Expand All @@ -79,8 +74,6 @@ extern uint8_t ui8_variableDebugC;

extern int8_t i8_motor_temperature;

extern uint8_t uint8_t_60deg_pwm_cycles[6];
extern uint8_t uint8_t_hall_case[7];
extern uint8_t uint8_t_hall_order[6];
extern int8_t int8_t_hall_counter;
extern uint8_t ui8_hall_debug_counter;
Expand Down
9 changes: 2 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "uart.h"
#include "adc.h"
#include "brake.h"
#include "cruise_control.h"
#include "timers.h"
#include "pwm.h"
#include "PAS.h"
Expand Down Expand Up @@ -158,19 +157,15 @@ int main(void) {
ui8_slowloop_flag = 0; //reset flag for slow loop
ui8_veryslowloop_counter++; // increase counter for very slow loop

motor_slow_update_pre();
checkPasInActivity();
updateRequestedTorque(); //now calculates tq for sensor as well
updateSlowLoopStates();
updateX4();
updateLight();
ui16_setpoint = (uint16_t) aca_setpoint(ui16_time_ticks_between_pas_interrupt, ui16_setpoint); //update setpoint

//#define DO_CRUISE_CONTROL 1
#if DO_CRUISE_CONTROL == 1
ui16_setpoint = cruise_control(ui16_setpoint);
#endif

pwm_set_duty_cycle((uint8_t) ui16_setpoint);
motor_slow_update_post();

//pwm_set_duty_cycle ((uint8_t)ui16_sum_throttle);

Expand Down
99 changes: 70 additions & 29 deletions motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,44 @@
#include "pwm.h"
#include "config.h"
#include "adc.h"

#include "ACAcontrollerState.h"
#include "ACAcommons.h"

uint8_t ui8_counter = 0;
uint8_t ui8_half_rotation_flag = 0;
uint8_t ui8_foc_enable_flag = 0;

/* Local only */
static uint8_t ui8_half_rotation_flag = 0;
static uint8_t ui8_foc_enable_flag = 0;
static uint8_t ui8_assumed_motor_position = 0;
static uint8_t ui8_motor_rotor_hall_position = 0; // in 360/256 degrees
static uint8_t ui8_sinetable_precalc = 0;
static uint8_t ui8_interpolation_start_position = 0;
static uint8_t ui8_interpolation_angle = 0;
static int8_t hall_sensors_last = 0;
static uint16_t ui16_ADC_iq_current_accumulated = 4096;

// Local except for diagnostics (main)
uint16_t ui16_PWM_cycles_counter = 0;
uint16_t ui16_PWM_cycles_counter_6 = 0;
uint16_t ui16_PWM_cycles_counter_total = 0;
uint16_t ui16_ADC_iq_current = 0; // main plus BO
int8_t hall_sensors; // main only; plus it's an int8, should be fine.
uint8_t ui8_possible_motor_state = 0;
uint8_t ui8_dynamic_motor_state = 0;
uint8_t ui8_position_correction_value = 127; // in 360/256 degrees
// Only for diagnostics
uint8_t uint8_t_60deg_pwm_cycles[6];
uint8_t uint8_t_hall_case[7];

uint8_t ui8_assumed_motor_position = 0;
uint8_t ui8_sinetable_position = 0; // in 360/256 degrees
uint8_t ui8_motor_rotor_hall_position = 0; // in 360/256 degrees
uint8_t ui8_sinetable_precalc = 0;
uint8_t ui8_interpolation_start_position = 0;

uint8_t ui8_interpolation_angle = 0;

uint16_t ui16_adc_current_phase_B = 0;
uint16_t ui16_adc_current_phase_B_accumulated = 0;
uint16_t ui16_adc_current_phase_B_filtered = 0;
// Motor->PWM (we call pwm; same context.)
uint8_t ui8_sinetable_position = 0; // in 360/256 degrees

int8_t hall_sensors;
int8_t hall_sensors_last = 0;
// Slow loop -> motor (by the motor_slow_update_post)
static uint16_t BatteryCurrent;

uint16_t ui16_ADC_iq_current_accumulated = 4096;
uint16_t ui16_iq_current_ma = 0;
// Motor-> slow loop (_pre)
static uint16_t motor_speed_erps;

void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void) __interrupt(TIM1_UPD_OVF_TRG_BRK_IRQHANDLER) {
adc_trigger();
Expand All @@ -70,9 +80,8 @@ void hall_sensors_read_and_action(void) {
if (hall_sensors_last >0 && hall_sensors_last < 7) {
uint8_t_60deg_pwm_cycles[hall_sensors_last-1] = ui16_PWM_cycles_counter_6;
}
updateHallOrder(hall_sensors);
updateHallOrder(hall_sensors); // this stores the hall order elsewhere, only for debug

//printf("hall change! %d, %d \n", hall_sensors, hall_sensors_last );
hall_sensors_last = hall_sensors;

if (ui8_possible_motor_state == MOTOR_STATE_COAST) {
Expand All @@ -91,17 +100,17 @@ void hall_sensors_read_and_action(void) {
if (ui16_PWM_cycles_counter > 20) ui16_PWM_cycles_counter_total = ui16_PWM_cycles_counter;

ui16_PWM_cycles_counter = 0;
ui16_motor_speed_erps = ((uint16_t) ui16_pwm_cycles_second) / ui16_PWM_cycles_counter_total; // this division takes ~4.2us
motor_speed_erps = ((uint16_t) ui16_pwm_cycles_second) / ui16_PWM_cycles_counter_total; // this division takes ~4.2us

}

if (ui16_motor_speed_erps == -1) {
ui16_motor_speed_erps = 0;
if (motor_speed_erps == -1) {
motor_speed_erps = 0;
}
// update motor state based on motor speed
if (ui16_motor_speed_erps > 75) {
if (motor_speed_erps > 75) {
ui8_possible_motor_state = MOTOR_STATE_RUNNING_INTERPOLATION_360;
}else if (ui16_motor_speed_erps > 3) {
}else if (motor_speed_erps > 3) {
ui8_possible_motor_state = MOTOR_STATE_RUNNING_INTERPOLATION_60;
} else {
ui8_possible_motor_state = MOTOR_STATE_RUNNING_NO_INTERPOLATION;
Expand Down Expand Up @@ -162,15 +171,18 @@ void updateCorrection() {
return;
}

if (ui16_motor_speed_erps > 3 && ui16_BatteryCurrent > ui16_current_cal_b + 3) { //normal riding,
if (ui16_ADC_iq_current >> 2 > 128 && ui8_position_correction_value < 143) {
const uint8_t curr_target = 126;
const uint8_t max_angle = 143;

if (motor_speed_erps > 3 && BatteryCurrent > ui16_current_cal_b + 3) { //normal riding,
if (ui16_ADC_iq_current >> 2 > (curr_target+2) && ui8_position_correction_value < max_angle) {
ui8_position_correction_value++;
} else if (ui16_ADC_iq_current >> 2 < 126 && ui8_position_correction_value > 111) {
ui8_position_correction_value--;
}
} else if (ui16_motor_speed_erps > 3 && ui16_BatteryCurrent < ui16_current_cal_b - 3) {//regen
} else if (motor_speed_erps > 3 && BatteryCurrent < ui16_current_cal_b - 3) {//regen
ui8_position_correction_value = 127; //set advance angle to neutral value
} else if (ui16_motor_speed_erps < 3) {
} else if (motor_speed_erps < 3) {
ui8_position_correction_value = 127; //reset advance angle at very low speed)
}

Expand All @@ -179,6 +191,8 @@ void updateCorrection() {
// runs every 64us (PWM frequency)

void motor_fast_loop(void) {

/* FIXME: These counters are... not well implemented. */
if (ui16_time_ticks_for_uart_timeout < 65530) {
ui16_time_ticks_for_uart_timeout++;
}
Expand All @@ -200,7 +214,7 @@ void motor_fast_loop(void) {
ui16_PWM_cycles_counter_total = 0xffff; //(SVM_TABLE_LEN_x1024) / PWM_CYCLES_COUNTER_MAX;
ui8_position_correction_value = 127;
hall_sensors_last = 0;
ui16_motor_speed_erps = 0;
motor_speed_erps = 0;


// next code is need for motor startup correctly
Expand Down Expand Up @@ -305,3 +319,30 @@ void watchdog_init(void) {
IWDG_SetReload(2); // 187.5us; for some reason, a value of 1 don't work, only 2
IWDG_ReloadCounter();
}


// Move these includes here if you want to see the identifiers that the ISR code uses

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major downside to code readability?

// without much regard for safety (most of whats left (except those timers) are effectively constants (except if adjusted during debug).
//#include "ACAcontrollerState.h"
//#include "ACAcommons.h"


// The concept here is loaned from linux; except that SDCC doesnt do typeof(x), so we need one per width.
#define READ_ONCE_U16(x) (*(const volatile uint16_t *)&(x))
#define WRITE_ONCE_U16(x, val) \
do { \
*(volatile uint16_t *)&(x) = (val); \
} while (0)

/* Communicate between "fast loop" (ISR) and rest of the system. */
void motor_slow_update_pre(void) {
disableInterrupts();
ui16_motor_speed_erps = READ_ONCE_U16(motor_speed_erps);
enableInterrupts();
}

void motor_slow_update_post(void) {
disableInterrupts();
WRITE_ONCE_U16(BatteryCurrent, ui16_BatteryCurrent);
enableInterrupts();
}
17 changes: 14 additions & 3 deletions motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,27 @@
#define MOTOR_STATE_RUNNING_INTERPOLATION_60 2
#define MOTOR_STATE_RUNNING_INTERPOLATION_360 3

// external for PWM (TIM1 isr context)
extern uint8_t ui8_sinetable_position;
extern uint16_t ui16_speed_inverse;

// external for debug only (and the BOdisplay... now there's an acronym.)
extern uint16_t ui16_PWM_cycles_counter_total;
extern uint16_t ui16_iq_current_ma;
extern uint16_t ui16_log;
extern uint16_t ui16_ADC_iq_current;
extern uint8_t ui8_possible_motor_state;
extern uint8_t ui8_dynamic_motor_state;
extern uint8_t ui8_position_correction_value;
extern uint8_t uint8_t_60deg_pwm_cycles[6];
extern uint8_t uint8_t_hall_case[7];


void hall_sensor_init (void);
void hall_sensors_read_and_action (void);
void motor_fast_loop (void);
void watchdog_init (void);

// slow loop before setpoint
void motor_slow_update_pre(void);
// slow loop after setpoint
void motor_slow_update_post(void);

#endif /* _MOTOR_H_ */