![]() |
#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) |
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 |
#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 |
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.
reset | If true, the counter will be reset after this call. |
float mcpwm_get_amp_hours_charged | ( | bool | reset | ) |
Get the amount of amp hours fed back into the input source.
reset | If true, the counter will be reset after this call. |
mc_comm_mode mcpwm_get_comm_mode | ( | void | ) |
int mcpwm_get_comm_step | ( | void | ) |
Get the electrical position (or commutation step) of the motor.
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
table | Pointer to a table where the result should be stored |
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.
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.
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.
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.
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.
int mcpwm_get_tachometer_abs_value | ( | bool | reset | ) |
Read the absolute number of steps the motor has rotated.
reset | If true, the tachometer counter will be reset after this call. |
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.
reset | If true, the tachometer counter will be reset after this call. |
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.
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.
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.
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.
float mcpwm_get_tot_current_in | ( | void | ) |
Get the input current to the motor controller.
float mcpwm_get_tot_current_in_filtered | ( | void | ) |
Get the FIR-filtered input current to the motor controller.
float mcpwm_get_watt_hours | ( | bool | reset | ) |
Get the amount of watt hours drawn from the input source.
reset | If true, the counter will be reset after this call. |
float mcpwm_get_watt_hours_charged | ( | bool | reset | ) |
Get the amount of watt hours fed back into the input source.
reset | If true, the counter will be reset after this call. |
void mcpwm_init | ( | mc_configuration * | configuration | ) |
void mcpwm_init_hall_table | ( | int8_t * | table | ) |
Initialize the hall sensor lookup table
table | The 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
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.
current | The 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.
mode | COMM_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.
current | The 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.
dutyCycle | The 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.
rpm | The minimum allowed RPM. |
void mcpwm_set_pid_pos | ( | float | pos | ) |
Use PID position control. Note that this only works when encoder support is enabled.
pos | The 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.
rpm | The electrical RPM goal value to use. |
void mcpwm_unlock | ( | void | ) |
Unlock all control commands.
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 |