Skip to content

Commit

Permalink
Corrected motor init
Browse files Browse the repository at this point in the history
  • Loading branch information
casainho committed Mar 25, 2020
1 parent 606fedc commit 6664e21
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/controller/TSDZ2-Flash_Debug.launch
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<launchConfiguration type="com.zylin.embeddedcdt.idleNative">
<booleanAttribute key="com.zylin.embeddedcdt.debugger_app_console" value="false"/>
<stringAttribute key="com.zylin.embeddedcdt.debugger_init_commands" value="target remote localhost:3333"/>
<stringAttribute key="com.zylin.embeddedcdt.debugger_run_commands" value="monitor reset&#10;monitor halt&#10;monitor load_image main.bin 0x8000&#10;monitor reg pc 0x8000"/>
<stringAttribute key="com.zylin.embeddedcdt.debugger_run_commands" value="monitor reset halt&#10;monitor load_image main.bin 0x8000&#10;monitor reg pc 0x8000"/>
<stringAttribute key="org.eclipse.cdt.debug.mi.core.DEBUG_NAME" value="/usr/local/bin/stm8-gdb"/>
<stringAttribute key="org.eclipse.cdt.debug.mi.core.GDB_INIT" value=""/>
<booleanAttribute key="org.eclipse.cdt.debug.mi.core.breakpointsFullPath" value="false"/>
Expand Down
113 changes: 70 additions & 43 deletions src/controller/ebike_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,31 @@ volatile struct_config_vars m_config_vars;
#define ERROR_BRAKE_APPLIED_DURING_POWER_ON (1 << 4)
#define ERROR_THROTTLE_APPLIED_DURING_POWER_ON (1 << 5)
#define ERROR_NO_SPEED_SENSOR_DETECTED (1 << 6)
#define ERROR_NO_LOW_VOLTAGE (1 << 7)
#define ERROR_FATAL (1 << 7)

// Motor init state
#define MOTOR_INIT_STATE_NO_INIT 0
#define MOTOR_INIT_STATE_INIT_START_DELAY 1
#define MOTOR_INIT_STATE_INIT_WAIT_DELAY 2
#define MOTOR_INIT_OK 3
#define MOTOR_INIT_STATE_RESET 0
#define MOTOR_INIT_STATE_NO_INIT 1
#define MOTOR_INIT_STATE_INIT_START_DELAY 2
#define MOTOR_INIT_STATE_INIT_WAIT_DELAY 3
#define MOTOR_INIT_OK 4

// Motor init status
#define MOTOR_INIT_STATUS_RESET 0
#define MOTOR_INIT_STATUS_GOT_CONFIG 1
#define MOTOR_INIT_STATUS_INIT_OK 2

// Communications package frame type
#define COMM_FRAME_TYPE_PERIODIC 0
#define COMM_FRAME_TYPE_CONFIGURATIONS 1
#define COMM_TYPE_FIRMWARE_VERSION 2
#define COMM_FRAME_TYPE_CONFIGURATIONS_REPEAT_ANSWER 3
#define COMM_FRAME_TYPE_ALIVE 0
#define COMM_FRAME_TYPE_STATUS 1
#define COMM_FRAME_TYPE_PERIODIC 2
#define COMM_FRAME_TYPE_CONFIGURATIONS 3
#define COMM_FRAME_TYPE_FIRMWARE_VERSION 4

// variables for various system functions
volatile uint8_t ui8_m_system_state = ERROR_NOT_INIT; // start with system error because configurations are empty at startup
volatile uint8_t ui8_m_motor_init_state = MOTOR_INIT_STATE_NO_INIT;
volatile uint8_t ui8_m_motor_init_state = MOTOR_INIT_STATE_RESET;
volatile uint8_t ui8_m_motor_init_status = MOTOR_INIT_STATUS_RESET;
volatile uint16_t ui16_pas_pwm_cycles_ticks = (uint16_t) PAS_ABSOLUTE_MIN_CADENCE_PWM_CYCLE_TICKS;
volatile uint8_t ui8_g_pedaling_direction = 0;
uint8_t ui8_pas_cadence_rpm = 0;
Expand Down Expand Up @@ -175,6 +183,8 @@ uint16_t ui16_torque_sensor_linearize_left[TORQUE_SENSOR_LINEARIZE_NR_POINTS][2]

static uint8_t m_ui8_got_configurations_timer = 0;

static uint8_t ui8_comm_error_counter = 0;

// Measured on 2020.01.02 by Casainho, the following function takes about 35ms to execute
void ebike_app_controller(void)
{
Expand Down Expand Up @@ -361,8 +371,11 @@ static void ebike_control_motor(void)
m_ui8_got_configurations_timer--;
}
else
{
ui8_m_motor_init_state = MOTOR_INIT_OK;
ui8_m_motor_init_status = MOTOR_INIT_STATUS_INIT_OK;
ui8_m_system_state &= ~ERROR_NOT_INIT;
}
break;
}

Expand Down Expand Up @@ -451,25 +464,35 @@ static void communications_controller(void)
if (((((uint16_t) ui8_rx_buffer[ui8_len + 1]) << 8) +
((uint16_t) ui8_rx_buffer[ui8_len])) == ui16_crc_rx)
{
ui8_comm_error_counter = 0;

if (ui8_m_motor_init_state == MOTOR_INIT_STATE_RESET)
ui8_m_motor_init_state = MOTOR_INIT_STATE_NO_INIT;

ui8_frame_type_to_send = ui8_rx_buffer[2];
communications_process_packages(ui8_frame_type_to_send);
}
else
{
ui8_received_package_flag = 0;
ui8_comm_error_counter++;
}
}

switch (ui8_m_motor_init_state)
else
{
default:
communications_process_packages(ui8_frame_type_to_send);
break;
ui8_comm_error_counter++;
}

case MOTOR_INIT_STATE_INIT_WAIT_DELAY:
// keep repeating the answer configurations answer while the init delay, so the display can for get this answer
communications_process_packages(COMM_FRAME_TYPE_CONFIGURATIONS_REPEAT_ANSWER);
break;
// check for communications fail or display master fail
// can't fail more then 800ms
if (ui8_comm_error_counter > 7) {
motor_disable_pwm();
ui8_m_motor_enabled = 0;
ui8_m_system_state |= ERROR_FATAL;
}

if (ui8_m_motor_init_state == MOTOR_INIT_STATE_RESET)
communications_process_packages(COMM_FRAME_TYPE_ALIVE);
#endif
}

Expand All @@ -489,26 +512,26 @@ static void communications_process_packages(uint8_t ui8_frame_type)
switch (ui8_frame_type) {
// periodic data
case COMM_FRAME_TYPE_PERIODIC:
if (ui8_received_package_flag) // the following RX data is valid only if we received a package
{
// assist level
m_config_vars.ui16_assist_level_factor_x1000 = (((uint16_t) ui8_rx_buffer[4]) << 8) + ((uint16_t) ui8_rx_buffer[3]);
// display will send periodic command after motor init ok, now reset so the state machine will be ready for next time
ui8_m_motor_init_status = MOTOR_INIT_STATUS_RESET;

// lights state
m_config_vars.ui8_lights = (ui8_rx_buffer[5] & (1 << 0)) ? 1: 0;
// assist level
m_config_vars.ui16_assist_level_factor_x1000 = (((uint16_t) ui8_rx_buffer[4]) << 8) + ((uint16_t) ui8_rx_buffer[3]);

// set lights
lights_set_state (m_config_vars.ui8_lights);
// lights state
m_config_vars.ui8_lights = (ui8_rx_buffer[5] & (1 << 0)) ? 1: 0;

// walk assist / cruise function
m_config_vars.ui8_walk_assist = (ui8_rx_buffer[5] & (1 << 1)) ? 1: 0;
// set lights
lights_set_state (m_config_vars.ui8_lights);

// battery max power target
m_config_vars.ui8_target_battery_max_power_div25 = ui8_rx_buffer[6];
// walk assist / cruise function
m_config_vars.ui8_walk_assist = (ui8_rx_buffer[5] & (1 << 1)) ? 1: 0;

// startup motor power boost
m_config_vars.ui16_startup_motor_power_boost_assist_level = (((uint16_t) ui8_rx_buffer[8]) << 8) + ((uint16_t) ui8_rx_buffer[7]);
}
// battery max power target
m_config_vars.ui8_target_battery_max_power_div25 = ui8_rx_buffer[6];

// startup motor power boost
m_config_vars.ui16_startup_motor_power_boost_assist_level = (((uint16_t) ui8_rx_buffer[8]) << 8) + ((uint16_t) ui8_rx_buffer[7]);

// now send data back
// ADC 10 bits battery voltage
Expand Down Expand Up @@ -609,6 +632,7 @@ static void communications_process_packages(uint8_t ui8_frame_type)
ui8_m_motor_enabled = 0;
ui8_m_system_state |= ERROR_NOT_INIT;
ui8_m_motor_init_state = MOTOR_INIT_STATE_INIT_START_DELAY;
ui8_m_motor_init_status = MOTOR_INIT_STATUS_GOT_CONFIG;

// battery low voltage cut-off
m_config_vars.ui16_battery_low_voltage_cut_off_x10 = (((uint16_t) ui8_rx_buffer[4]) << 8) + ((uint16_t) ui8_rx_buffer[3]);
Expand Down Expand Up @@ -707,18 +731,20 @@ static void communications_process_packages(uint8_t ui8_frame_type)
break;

// firmware version
case COMM_TYPE_FIRMWARE_VERSION:
case COMM_FRAME_TYPE_FIRMWARE_VERSION:
ui8_tx_buffer[3] = ui8_m_system_state;
ui8_tx_buffer[4] = 0;
ui8_tx_buffer[5] = 56;
ui8_tx_buffer[6] = 0;
ui8_len += 4;
break;

// do nothing but repeat sending the configurations answer
case COMM_FRAME_TYPE_CONFIGURATIONS_REPEAT_ANSWER:
ui8_tx_buffer[2] = COMM_FRAME_TYPE_CONFIGURATIONS; // change frame type to be configurations
ui8_tx_buffer[3] = ui8_m_system_state;
case COMM_FRAME_TYPE_ALIVE:
// nothing to add
break;

case COMM_FRAME_TYPE_STATUS:
ui8_tx_buffer[3] = ui8_m_motor_init_status;
ui8_len += 1;
break;

Expand All @@ -745,8 +771,6 @@ static void communications_process_packages(uint8_t ui8_frame_type)

// get ready to get next package
ui8_received_package_flag = 0;
// enable UART2 receive interrupt
UART2->CR2 |= (1 << 5);
}

// each 1 unit = 0.156 amps
Expand Down Expand Up @@ -1374,13 +1398,11 @@ void UART2_IRQHandler(void) __interrupt(UART2_IRQHANDLER)
ui8_rx_buffer[ui8_rx_cnt + 2] = ui8_byte_received;
++ui8_rx_cnt;

// reset if it is the last byte of the package and index is out of bounds
if (ui8_rx_cnt >= ui8_rx_len)
{
ui8_rx_cnt = 0;
ui8_state_machine = 0;
ui8_received_package_flag = 1; // signal that we have a full package to be processed
UART2->CR2 &= ~(1 << 5); // disable UART2 receive interrupt
}
break;

Expand All @@ -1389,6 +1411,11 @@ void UART2_IRQHandler(void) __interrupt(UART2_IRQHANDLER)
}
}
}
else // if there was any error, restart our state machine
{
ui8_rx_cnt = 0;
ui8_state_machine = 0;
}
}


Expand Down
24 changes: 1 addition & 23 deletions src/controller/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ int main (void)
{
uint16_t ui16_TIM3_counter = 0;
uint16_t ui16_ebike_app_controller_counter = 0;
uint16_t ui16_motor_controller_counter = 0;
uint16_t ui16_debug_uart_counter = 0;

uint16_t ui16_temp = 0, ui16_throttle_value_filtered = 0;
uint16_t ui16_motor_controller_counter = 0;;

//set clock at the max 16MHz
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
Expand Down Expand Up @@ -100,25 +97,6 @@ int main (void)
ebike_app_controller();
continue;
}

#ifdef DEBUG_UART

ui16_TIM3_counter = TIM3_GetCounter();

if((ui16_TIM3_counter - ui16_debug_uart_counter) > 50)
{
ui16_debug_uart_counter = ui16_TIM3_counter;

// sugestion: no more than 6 variables printed (takes about 3ms to printf 6 variables)
printf "%d,%d,%d,%d\n",
ui16_motor_get_motor_speed_erps(),
ui8_duty_cycle,
ui8_adc_battery_current,
ui8_foc_angle
);
}

#endif
}

return 0;
Expand Down

0 comments on commit 6664e21

Please sign in to comment.