Data Structures | Macros | Functions | Variables
mcpwm.c File Reference
#include "ch.h"
#include "hal.h"
#include "stm32f4xx_conf.h"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "mcpwm.h"
#include "digital_filter.h"
#include "utils.h"
#include "ledpwm.h"
#include "hw.h"
#include "terminal.h"
#include "encoder.h"

Data Structures

struct  mc_timer_struct
 

Macros

#define KV_FIR_TAPS_BITS   7
 
#define KV_FIR_LEN   (1 << KV_FIR_TAPS_BITS)
 
#define KV_FIR_FCUT   0.02
 
#define AMP_FIR_TAPS_BITS   7
 
#define AMP_FIR_LEN   (1 << AMP_FIR_TAPS_BITS)
 
#define AMP_FIR_FCUT   0.02
 
#define CURR_FIR_TAPS_BITS   4
 
#define CURR_FIR_LEN   (1 << CURR_FIR_TAPS_BITS)
 
#define CURR_FIR_FCUT   0.15
 
#define IS_DETECTING()   (state == MC_STATE_DETECTING)
 

Functions

void mcpwm_init (mc_configuration *configuration)
 
const volatile mc_configurationmcpwm_get_configuration (void)
 
void mcpwm_set_configuration (mc_configuration *configuration)
 
void mcpwm_init_hall_table (int8_t *table)
 
void mcpwm_set_duty (float dutyCycle)
 
void mcpwm_set_pid_speed (float rpm)
 
void mcpwm_set_pid_pos (float pos)
 
void mcpwm_set_current (float current)
 
void mcpwm_set_brake_current (float current)
 
void mcpwm_brake_now (void)
 
void mcpwm_release_motor (void)
 
void mcpwm_lock (void)
 
void mcpwm_unlock (void)
 
void mcpwm_lock_override_once (void)
 
int mcpwm_get_comm_step (void)
 
float mcpwm_get_duty_cycle_set (void)
 
float mcpwm_get_duty_cycle_now (void)
 
float mcpwm_get_switching_frequency_now (void)
 
float mcpwm_get_rpm (void)
 
mc_state mcpwm_get_state (void)
 
mc_fault_code mcpwm_get_fault (void)
 
const char * mcpwm_fault_to_string (mc_fault_code fault)
 
float mcpwm_get_kv (void)
 
float mcpwm_get_kv_filtered (void)
 
float mcpwm_get_tot_current (void)
 
float mcpwm_get_tot_current_filtered (void)
 
float mcpwm_get_tot_current_directional (void)
 
float mcpwm_get_tot_current_directional_filtered (void)
 
float mcpwm_get_tot_current_in (void)
 
float mcpwm_get_tot_current_in_filtered (void)
 
int mcpwm_get_tachometer_value (bool reset)
 
int mcpwm_get_tachometer_abs_value (bool reset)
 
float mcpwm_get_amp_hours (bool reset)
 
float mcpwm_get_amp_hours_charged (bool reset)
 
float mcpwm_get_watt_hours (bool reset)
 
float mcpwm_get_watt_hours_charged (bool reset)
 
void mcpwm_adc_inj_int_handler (void)
 
void mcpwm_adc_int_handler (void *p, uint32_t flags)
 
void mcpwm_set_detect (void)
 
float mcpwm_get_detect_pos (void)
 
float mcpwm_read_reset_avg_motor_current (void)
 
float mcpwm_read_reset_avg_input_current (void)
 
float mcpwm_read_reset_avg_cycle_integrator (void)
 
void mcpwm_set_min_rpm (float rpm)
 
float mcpwm_get_min_rpm (void)
 
void mcpwm_set_comm_mode (mc_comm_mode mode)
 
mc_comm_mode mcpwm_get_comm_mode (void)
 
float mcpwm_get_last_adc_isr_duration (void)
 
float mcpwm_get_last_inj_adc_isr_duration (void)
 
mc_rpm_dep_struct mcpwm_get_rpm_dep (void)
 
void mcpwm_reset_hall_detect_table (void)
 
int mcpwm_get_hall_detect_result (int8_t *table)
 
int mcpwm_read_hall_phase (void)
 

Variables

volatile uint16_t ADC_Value [HW_ADC_CHANNELS]
 
volatile int ADC_curr_norm_value [3]
 
volatile float mcpwm_detect_currents [6]
 
volatile float mcpwm_detect_voltages [6]
 
volatile float mcpwm_detect_currents_diff [6]
 
volatile int mcpwm_vzero
 

Macro Definition Documentation

#define AMP_FIR_FCUT   0.02
#define AMP_FIR_LEN   (1 << AMP_FIR_TAPS_BITS)
#define AMP_FIR_TAPS_BITS   7
#define CURR_FIR_FCUT   0.15
#define CURR_FIR_LEN   (1 << CURR_FIR_TAPS_BITS)
#define CURR_FIR_TAPS_BITS   4
#define IS_DETECTING ( )    (state == MC_STATE_DETECTING)
#define KV_FIR_FCUT   0.02
#define KV_FIR_LEN   (1 << KV_FIR_TAPS_BITS)
#define KV_FIR_TAPS_BITS   7

Function Documentation

void mcpwm_adc_inj_int_handler ( void  )
void mcpwm_adc_int_handler ( void *  p,
uint32_t  flags 
)
void mcpwm_brake_now ( void  )

Stop the motor and use braking.

const char* mcpwm_fault_to_string ( mc_fault_code  fault)
float mcpwm_get_amp_hours ( bool  reset)

Get the amount of amp hours drawn from the input source.

Parameters
resetIf true, the counter will be reset after this call.
Returns
The amount of amp hours drawn.
float mcpwm_get_amp_hours_charged ( bool  reset)

Get the amount of amp hours fed back into the input source.

Parameters
resetIf true, the counter will be reset after this call.
Returns
The amount of amp hours fed back.
mc_comm_mode mcpwm_get_comm_mode ( void  )
int mcpwm_get_comm_step ( void  )

Get the electrical position (or commutation step) of the motor.

Returns
The current commutation step. Range [1 6]
const volatile mc_configuration* mcpwm_get_configuration ( void  )
float mcpwm_get_detect_pos ( void  )
float mcpwm_get_duty_cycle_now ( void  )
float mcpwm_get_duty_cycle_set ( void  )
mc_fault_code mcpwm_get_fault ( void  )
int mcpwm_get_hall_detect_result ( int8_t *  table)

Get the current detected hall sensor table

Parameters
tablePointer to a table where the result should be stored
Returns
0: OK -1: Invalid hall sensor output -2: WS2811 enabled -3: Encoder enabled
float mcpwm_get_kv ( void  )

Calculate the KV (RPM per volt) value for the motor. This function has to be used while the motor is moving. Note that the return value has to be divided by half the number of motor poles.

Returns
The KV value.
float mcpwm_get_kv_filtered ( void  )

Calculate the FIR-filtered KV (RPM per volt) value for the motor. This function has to be used while the motor is moving. Note that the return value has to be divided by half the number of motor poles.

Returns
The filtered KV value.
float mcpwm_get_last_adc_isr_duration ( void  )
float mcpwm_get_last_inj_adc_isr_duration ( void  )
float mcpwm_get_min_rpm ( void  )

Get the minimum allowed RPM in sensorless mode.

Returns
The minimum allowed RPM.
float mcpwm_get_rpm ( void  )

Calculate the current RPM of the motor. This is a signed value and the sign depends on the direction the motor is rotating in. Note that this value has to be divided by half the number of motor poles.

Returns
The RPM value.
mc_rpm_dep_struct mcpwm_get_rpm_dep ( void  )
mc_state mcpwm_get_state ( void  )
float mcpwm_get_switching_frequency_now ( void  )

Get the current switching frequency.

Returns
The switching frequency in Hz.
int mcpwm_get_tachometer_abs_value ( bool  reset)

Read the absolute number of steps the motor has rotated.

Parameters
resetIf true, the tachometer counter will be reset after this call.
Returns
The tachometer value in motor steps. The number of motor revolutions will be this number divided by (3 * MOTOR_POLE_NUMBER).
int mcpwm_get_tachometer_value ( bool  reset)

Read the number of steps the motor has rotated. This number is signed and will return a negative number when the motor is rotating backwards.

Parameters
resetIf true, the tachometer counter will be reset after this call.
Returns
The tachometer value in motor steps. The number of motor revolutions will be this number divided by (3 * MOTOR_POLE_NUMBER).
float mcpwm_get_tot_current ( void  )

Get the motor current. The sign of this value will represent whether the motor is drawing (positive) or generating (negative) current.

Returns
The motor current.
float mcpwm_get_tot_current_directional ( void  )

Get the motor current. The sign of this value represents the direction in which the motor generates torque.

Returns
The motor current.
float mcpwm_get_tot_current_directional_filtered ( void  )

Get the filtered motor current. The sign of this value represents the direction in which the motor generates torque.

Returns
The filtered motor current.
float mcpwm_get_tot_current_filtered ( void  )

Get the FIR-filtered motor current. The sign of this value will represent whether the motor is drawing (positive) or generating (negative) current.

Returns
The filtered motor current.
float mcpwm_get_tot_current_in ( void  )

Get the input current to the motor controller.

Returns
The input current.
float mcpwm_get_tot_current_in_filtered ( void  )

Get the FIR-filtered input current to the motor controller.

Returns
The filtered input current.
float mcpwm_get_watt_hours ( bool  reset)

Get the amount of watt hours drawn from the input source.

Parameters
resetIf true, the counter will be reset after this call.
Returns
The amount of watt hours drawn.
float mcpwm_get_watt_hours_charged ( bool  reset)

Get the amount of watt hours fed back into the input source.

Parameters
resetIf true, the counter will be reset after this call.
Returns
The amount of watt hours fed back.
void mcpwm_init ( mc_configuration configuration)
void mcpwm_init_hall_table ( int8_t *  table)

Initialize the hall sensor lookup table

Parameters
tableThe commutations corresponding to the hall sensor states in the forward direction-
void mcpwm_lock ( void  )

Lock the control by disabling all control commands.

void mcpwm_lock_override_once ( void  )

Allow just one motor control command in the locked state.

int mcpwm_read_hall_phase ( void  )

Read the current phase of the motor using hall effect sensors

Returns
The phase read.
float mcpwm_read_reset_avg_cycle_integrator ( void  )
float mcpwm_read_reset_avg_input_current ( void  )
float mcpwm_read_reset_avg_motor_current ( void  )
void mcpwm_release_motor ( void  )

Disconnect the motor and let it turn freely.

void mcpwm_reset_hall_detect_table ( void  )

Reset the hall sensor detection table

void mcpwm_set_brake_current ( float  current)

Brake the motor with a desired current. Absolute values less than conf.cc_min_current will release the motor.

Parameters
currentThe current to use. Positive and negative values give the same effect.
void mcpwm_set_comm_mode ( mc_comm_mode  mode)

Set the commutation mode for sensorless commutation.

Parameters
modeCOMM_MODE_INTEGRATE: More robust, but requires many parameters. COMM_MODE_DELAY: Like most hobby ESCs. Requires less parameters, but has worse startup and is less robust.
void mcpwm_set_configuration ( mc_configuration configuration)
void mcpwm_set_current ( float  current)

Use current control and specify a goal current to use. The sign determines the direction of the torque. Absolute values less than conf.cc_min_current will release the motor.

Parameters
currentThe current to use.
void mcpwm_set_detect ( void  )
void mcpwm_set_duty ( float  dutyCycle)

Use duty cycle control. Absolute values less than MCPWM_MIN_DUTY_CYCLE will stop the motor.

Parameters
dutyCycleThe duty cycle to use.
void mcpwm_set_min_rpm ( float  rpm)

Set the minimum allowed RPM in sensorless mode. This will affect startup performance. WARNING: Setting this too high can break stuff.

Parameters
rpmThe minimum allowed RPM.
void mcpwm_set_pid_pos ( float  pos)

Use PID position control. Note that this only works when encoder support is enabled.

Parameters
posThe desired position of the motor in degrees.
void mcpwm_set_pid_speed ( float  rpm)

Use PID rpm control. Note that this value has to be multiplied by half of the number of motor poles.

Parameters
rpmThe electrical RPM goal value to use.
void mcpwm_unlock ( void  )

Unlock all control commands.

Variable Documentation

volatile int ADC_curr_norm_value[3]
volatile uint16_t ADC_Value[HW_ADC_CHANNELS]
volatile float mcpwm_detect_currents[6]
volatile float mcpwm_detect_currents_diff[6]
volatile float mcpwm_detect_voltages[6]
volatile int mcpwm_vzero