8#include "../time_utils.h"
9#include "../foc_utils.h"
10#include "../defaults.h"
12#include "../lowpass_filter.h"
14#define MOT_ERR "ERR-MOT:"
15#define MOT_WARN "WARN-MOT:"
16#define MOT_DEBUG "MOT:"
18#ifndef SIMPLEFOC_DISABLE_DEBUG
19#define SIMPLEFOC_MOTOR_WARN(msg, ...) \
20 SimpleFOCDebug::print(MOT_WARN); \
21 SIMPLEFOC_DEBUG(msg, ##__VA_ARGS__)
23#define SIMPLEFOC_MOTOR_ERROR(msg, ...) \
24 SimpleFOCDebug::print(MOT_ERR); \
25 SIMPLEFOC_DEBUG(msg, ##__VA_ARGS__)
27#define SIMPLEFOC_MOTOR_DEBUG(msg, ...) \
28 SimpleFOCDebug::print(MOT_DEBUG); \
29 SIMPLEFOC_DEBUG(msg, ##__VA_ARGS__)
32#define SIMPLEFOC_MOTOR_DEBUG(msg, ...)
33#define SIMPLEFOC_MOTOR_ERROR(msg, ...)
34#define SIMPLEFOC_MOTOR_WARN(msg, ...)
38#define _MON_TARGET 0b1000000
39#define _MON_VOLT_Q 0b0100000
40#define _MON_VOLT_D 0b0010000
41#define _MON_CURR_Q 0b0001000
42#define _MON_CURR_D 0b0000100
43#define _MON_VEL 0b0000010
44#define _MON_ANGLE 0b0000001
382 customMotionControlCallback = controlMethod;
414 unsigned int monitor_cnt = 0 ;
418 void updateTime(uint32_t& elapsed_time_filetered, uint32_t& elapsed_time, uint32_t& last_timestamp_us,
float alpha = 0.1f){
420 elapsed_time = now - last_timestamp_us;
421 elapsed_time_filetered = (1-alpha) * elapsed_time_filetered + alpha * elapsed_time;
422 last_timestamp_us = now;
426 uint32_t open_loop_timestamp;
429 float (*customMotionControlCallback)(
FOCMotor* motor) =
nullptr;
@ velocity
Velocity motion control.
@ custom
Custom control method - control method added by user.
@ angle_nocascade
Position/angle motion control without velocity cascade.
@ angle
Position/angle motion control.
@ SpaceVectorPWM
Space vector modulation method.
@ SinePWM
Sinusoidal PWM modulation.
@ motor_ready
Motor is initialized and calibrated (closed loop possible)
@ motor_uncalibrated
Motor is initialized, but not calibrated (open loop possible)
@ motor_initializing
Motor intiialization is in progress.
@ motor_uninitialized
Motor is not yet initialized.
@ motor_calibrating
Motor calibration in progress.
@ motor_init_failed
Motor initialization failed (not recoverable)
@ motor_error
Motor is in error state (recoverable, e.g. overcurrent protection active)
@ motor_calib_failed
Motor calibration failed (possibly recoverable)
@ estimated_current
torque control using estimated current (provided motor parameters)
@ voltage
Torque control using voltage.
@ dc_current
Torque control using DC current (one current magnitude)
@ foc_current
torque control using dq currents
int characteriseMotor(float voltage, float correction_factor)
int8_t enabled
enabled or disabled motor flag
void updateCurrentLimit(float new_current_limit)
void linkSensor(Sensor *sensor)
int8_t modulation_centered
flag (1) centered modulation around driver limit /2 or (0) pulled to 0
DQ_s axis_inductance
motor direct axis phase inductance
void useMonitoring(Print &serial)
char monitor_separator
monitor outputs separation character
uint32_t last_loopfoc_timestamp_us
timestamp of the last loopFOC execution in microseconds
PIDController PID_current_q
parameter determining the q current PID config
virtual void setPhaseVoltage(float Uq, float Ud, float angle_el)=0
void updateVoltageLimit(float new_voltage_limit)
float sensor_offset
user defined sensor zero offset
LowPassFilter LPF_angle
parameter determining the angle low pass filter configuration
DQVoltage_s voltage
current d and q voltage set to the motor
LowPassFilter LPF_velocity
parameter determining the velocity Low pass filter configuration
uint8_t monitor_variables
Bit array holding the map of variables the user wants to monitor.
MotionControlType controller
parameter determining the control loop to be used
float phase_resistance
motor phase resistance
uint32_t loopfoc_time_us
filtered loop times
void updateTorqueControlType(TorqueControlType new_torque_controller)
float velocityOpenloop(float target_velocity)
uint32_t last_move_time_us
last elapsed time of move in microseconds
float angleOpenloop(float target_angle)
PIDController PID_current_d
parameter determining the d current PID config
Sensor * sensor
CurrentSense link.
DQVoltage_s feed_forward_voltage
current d and q voltage set to the motor
CurrentSense * current_sense
void linkCurrentSense(CurrentSense *current_sense)
virtual void move(float target=NOT_SET)
uint32_t last_loopfoc_time_us
last elapsed time of loopFOC in microseconds
float current_sp
target current ( q current )
uint32_t last_move_timestamp_us
timestamp of the last move execution in microseconds
float voltage_limit
Voltage limiting variable - global limit.
float velocity_limit
Velocity limiting variable - global limit.
bool pp_check_result
the result of the PP check, if run during loopFOC
PIDController P_angle
parameter determining the position PID configuration
unsigned int monitor_decimals
monitor outputs decimal places
unsigned int monitor_downsample
show monitor outputs each monitor_downsample calls
virtual float estimateBEMF(float velocity)
LowPassFilter LPF_current_q
parameter determining the current Low pass filter configuration
unsigned int motion_cnt
counting variable for downsampling for move commad
char monitor_end_char
monitor outputs ending character
Print * monitor_port
Serial terminal variable if provided.
FOCMotorStatus motor_status
motor status
float velocity_index_search
target velocity for index search
float feed_forward_velocity
current feed forward velocity
char monitor_start_char
monitor starting character
LowPassFilter LPF_current_d
parameter determining the current Low pass filter configuration
float Ubeta
Phase voltages U alpha and U beta used for inverse Park and Clarke transform.
float shaft_angle_sp
current target angle
float shaft_velocity
current motor velocity
float KV_rating
motor KV rating
float voltage_bemf
estimated backemf voltage (if provided KV constant)
float shaft_angle
current motor angle
int tuneCurrentController(float bandwidth)
float voltage_sensor_align
sensor and motor align voltage parameter
Direction sensor_direction
default is CW. if sensor_direction == Direction::CCW then direction will be flipped compared to CW....
void updateMotionControlTime()
float target
current target value - depends of the controller
float current_limit
Current limiting variable - global limit.
float electrical_angle
current electrical angle
DQCurrent_s feed_forward_current
current d and q current measured
void linkCustomMotionControl(float(*controlMethod)(FOCMotor *motor))
TorqueControlType torque_controller
parameter determining the torque control type
PIDController PID_velocity
parameter determining the velocity PID configuration
float phase_inductance
motor phase inductance q axis - FOR BACKWARDS COMPATIBILITY
float zero_electric_angle
absolute zero electric angle - if available
DQCurrent_s current
current d and q current measured
int pole_pairs
motor pole pairs number
void updateVelocityLimit(float new_velocity_limit)
FOCModulationType foc_modulation
parameter determining modulation algorithm
unsigned int motion_downsample
parameter defining the ratio of downsampling for move commad
void updateMotionControlType(MotionControlType new_motion_controller)
float shaft_velocity_sp
current target velocity
#define DEF_MON_DOWNSMAPLE
default monitor downsample
#define DEF_PID_VEL_RAMP
default PID controller voltage ramp value
#define DEF_PID_CURR_I
default PID controller I value
#define DEF_PID_VEL_D
default PID controller D value
#define DEF_PID_VEL_I
default PID controller I value
#define DEF_VEL_FILTER_Tf
default velocity filter time constant
#define DEF_VEL_LIM
angle velocity limit default
#define DEF_PID_CURR_RAMP
default PID controller voltage ramp value
#define DEF_PID_VEL_LIMIT
default PID controller voltage limit
#define DEF_PID_CURR_P
default PID controller P value
#define DEF_PID_CURR_D
default PID controller D value
#define DEF_MOTION_DOWNSMAPLE
default motion downsample - disable
#define DEF_POWER_SUPPLY
default power supply voltage
#define DEF_PID_VEL_P
default PID controller P value
#define DEF_CURR_FILTER_Tf
default currnet filter time constant
#define DEF_P_ANGLE_P
default P controller P value