![]() |
SimpleFOClibrary 2.4.0
|
#include <FOCMotor.h>
Public Member Functions | |
| FOCMotor () | |
| virtual int | init ()=0 |
| virtual void | disable ()=0 |
| virtual void | enable ()=0 |
| virtual void | setPhaseVoltage (float Uq, float Ud, float angle_el)=0 |
| virtual float | estimateBEMF (float velocity) |
| virtual int | initFOC () |
| virtual void | loopFOC () |
| virtual void | move (float target=NOT_SET) |
| void | linkSensor (Sensor *sensor) |
| void | linkCurrentSense (CurrentSense *current_sense) |
| float | shaftAngle () |
| float | shaftVelocity () |
| float | electricalAngle () |
| int | characteriseMotor (float voltage, float correction_factor) |
| int | tuneCurrentController (float bandwidth) |
| void | useMonitoring (Print &serial) |
| void | monitor () |
| void | updateVelocityLimit (float new_velocity_limit) |
| void | updateCurrentLimit (float new_current_limit) |
| void | updateVoltageLimit (float new_voltage_limit) |
| void | updateTorqueControlType (TorqueControlType new_torque_controller) |
| void | updateMotionControlType (MotionControlType new_motion_controller) |
| float | velocityOpenloop (float target_velocity) |
| float | angleOpenloop (float target_angle) |
| void | linkCustomMotionControl (float(*controlMethod)(FOCMotor *motor)) |
Public Attributes | |
| float | target |
| current target value - depends of the controller | |
| float | feed_forward_velocity = 0.0f |
| current feed forward velocity | |
| float | shaft_angle |
| current motor angle | |
| float | electrical_angle |
| current electrical angle | |
| float | shaft_velocity |
| current motor velocity | |
| float | current_sp |
| target current ( q current ) | |
| float | shaft_velocity_sp |
| current target velocity | |
| float | shaft_angle_sp |
| current target angle | |
| DQVoltage_s | voltage |
| current d and q voltage set to the motor | |
| DQCurrent_s | current |
| current d and q current measured | |
| float | voltage_bemf |
| estimated backemf voltage (if provided KV constant) | |
| float | Ualpha |
| float | Ubeta |
| Phase voltages U alpha and U beta used for inverse Park and Clarke transform. | |
| DQCurrent_s | feed_forward_current |
| current d and q current measured | |
| DQVoltage_s | feed_forward_voltage |
| current d and q voltage set to the motor | |
| float | voltage_sensor_align |
| sensor and motor align voltage parameter | |
| float | velocity_index_search |
| target velocity for index search | |
| float | phase_resistance |
| motor phase resistance | |
| int | pole_pairs |
| motor pole pairs number | |
| float | KV_rating |
| motor KV rating | |
| float | phase_inductance |
| motor phase inductance q axis - FOR BACKWARDS COMPATIBILITY | |
| DQ_s | axis_inductance {NOT_SET, NOT_SET} |
| motor direct axis phase inductance | |
| float | voltage_limit |
| Voltage limiting variable - global limit. | |
| float | current_limit |
| Current limiting variable - global limit. | |
| float | velocity_limit |
| Velocity limiting variable - global limit. | |
| int8_t | enabled = 0 |
| enabled or disabled motor flag | |
| FOCMotorStatus | motor_status = FOCMotorStatus::motor_uninitialized |
| motor status | |
| FOCModulationType | foc_modulation |
| parameter determining modulation algorithm | |
| int8_t | modulation_centered = 1 |
| flag (1) centered modulation around driver limit /2 or (0) pulled to 0 | |
| TorqueControlType | torque_controller |
| parameter determining the torque control type | |
| MotionControlType | controller |
| parameter determining the control loop to be used | |
| PIDController | PID_current_q {DEF_PID_CURR_P,DEF_PID_CURR_I,DEF_PID_CURR_D,DEF_PID_CURR_RAMP, DEF_POWER_SUPPLY} |
| parameter determining the q current PID config | |
| PIDController | PID_current_d {DEF_PID_CURR_P,DEF_PID_CURR_I,DEF_PID_CURR_D,DEF_PID_CURR_RAMP, DEF_POWER_SUPPLY} |
| parameter determining the d current PID config | |
| LowPassFilter | LPF_current_q {DEF_CURR_FILTER_Tf} |
| parameter determining the current Low pass filter configuration | |
| LowPassFilter | LPF_current_d {DEF_CURR_FILTER_Tf} |
| parameter determining the current Low pass filter configuration | |
| PIDController | PID_velocity {DEF_PID_VEL_P,DEF_PID_VEL_I,DEF_PID_VEL_D,DEF_PID_VEL_RAMP,DEF_PID_VEL_LIMIT} |
| parameter determining the velocity PID configuration | |
| PIDController | P_angle {DEF_P_ANGLE_P,0,0,0,DEF_VEL_LIM} |
| parameter determining the position PID configuration | |
| LowPassFilter | LPF_velocity {DEF_VEL_FILTER_Tf} |
| parameter determining the velocity Low pass filter configuration | |
| LowPassFilter | LPF_angle {0.0} |
| parameter determining the angle low pass filter configuration | |
| unsigned int | motion_downsample = DEF_MOTION_DOWNSMAPLE |
| parameter defining the ratio of downsampling for move commad | |
| unsigned int | motion_cnt = 0 |
| counting variable for downsampling for move commad | |
| float | sensor_offset |
| user defined sensor zero offset | |
| float | zero_electric_angle = NOT_SET |
| absolute zero electric angle - if available | |
| Direction | sensor_direction = Direction::UNKNOWN |
| default is CW. if sensor_direction == Direction::CCW then direction will be flipped compared to CW. Set to UNKNOWN to set by calibration | |
| bool | pp_check_result = false |
| the result of the PP check, if run during loopFOC | |
| unsigned int | monitor_downsample = DEF_MON_DOWNSMAPLE |
| show monitor outputs each monitor_downsample calls | |
| char | monitor_start_char = '\0' |
| monitor starting character | |
| char | monitor_end_char = '\0' |
| monitor outputs ending character | |
| char | monitor_separator = '\t' |
| monitor outputs separation character | |
| unsigned int | monitor_decimals = 4 |
| monitor outputs decimal places | |
| uint8_t | monitor_variables = _MON_TARGET | _MON_VOLT_Q | _MON_VEL | _MON_ANGLE |
| Bit array holding the map of variables the user wants to monitor. | |
| Sensor * | sensor |
| CurrentSense link. | |
| CurrentSense * | current_sense |
| Print * | monitor_port |
| Serial terminal variable if provided. | |
| uint32_t | loopfoc_time_us = 0 |
| filtered loop times | |
| uint32_t | move_time_us = 0 |
Protected Member Functions | |
| void | updateLoopFOCTime () |
| void | updateMotionControlTime () |
| int | alignSensor () |
| int | alignCurrentSense () |
| int | absoluteZeroSearch () |
Protected Attributes | |
| uint32_t | last_loopfoc_timestamp_us = 0 |
| timestamp of the last loopFOC execution in microseconds | |
| uint32_t | last_loopfoc_time_us = 0 |
| last elapsed time of loopFOC in microseconds | |
| uint32_t | last_move_timestamp_us = 0 |
| timestamp of the last move execution in microseconds | |
| uint32_t | last_move_time_us = 0 |
| last elapsed time of move in microseconds | |
Generic motor class
Definition at line 97 of file FOCMotor.h.
| FOCMotor::FOCMotor | ( | ) |
Default constructor - setting all variabels to default values
Definition at line 7 of file FOCMotor.cpp.
|
protected |
Motor and sensor alignment to the sensors absolute 0 angle
Definition at line 930 of file FOCMotor.cpp.
|
protected |
Current sense and motor phase alignment
Definition at line 827 of file FOCMotor.cpp.
|
protected |
Sensor alignment to electrical 0 angle of the motor
Definition at line 847 of file FOCMotor.cpp.
| float FOCMotor::angleOpenloop | ( | float | target_angle | ) |
Function (iterative) generating open loop movement towards the target angle it uses voltage_limit and velocity_limit variables
| target_angle | - rad |
Definition at line 414 of file FOCMotor.cpp.
| int FOCMotor::characteriseMotor | ( | float | voltage, |
| float | correction_factor = 1.0f |
||
| ) |
Measure resistance and inductance of a motor and print results to debug. If a sensor is available, an estimate of zero electric angle will be reported too.
| voltage | The voltage applied to the motor |
| correction_factor | Is 1.5 for 3 phase motors, because we measure over a series-parallel connection. TODO: what about 2 phase motors? |
How this code works: If we apply a current spike in the d´-axis, there will be cross coupling to the q´-axis current, if we didn´t use the actual d-axis (ie. d´ != d). This has to do with saliency (Ld != Lq). The amount of cross coupled current is somewhat proportional to the angle error, which means that if we iteratively change the angle to min/maximise this current, we get the correct d-axis (and q-axis).
The d_electrical_angle should now be aligned to the d axis or the -d axis. We can therefore calculate two possible electrical zero angles. We then report the one closest to the actual value. This could be useful if the zero search method is not reliable enough (eg. high pole count).
Definition at line 96 of file FOCMotor.cpp.
|
pure virtual |
Motor disable function
Implemented in BLDCMotor, HybridStepperMotor, and StepperMotor.
| float FOCMotor::electricalAngle | ( | ) |
Electrical angle calculation
Definition at line 77 of file FOCMotor.cpp.
|
pure virtual |
Motor enable function
Implemented in BLDCMotor, HybridStepperMotor, and StepperMotor.
|
inlinevirtual |
Estimation of the Back EMF voltage
| velocity | - current shaft velocity |
Reimplemented in BLDCMotor, HybridStepperMotor, and StepperMotor.
Definition at line 130 of file FOCMotor.h.
|
pure virtual |
Motor hardware init function
Implemented in BLDCMotor, HybridStepperMotor, and StepperMotor.
|
virtual |
Function initializing FOC algorithm and aligning sensor's and motors' zero position
Definition at line 774 of file FOCMotor.cpp.
| void FOCMotor::linkCurrentSense | ( | CurrentSense * | _current_sense | ) |
Function linking a motor and current sensing
| current_sense | CurrentSense class wrapper for the FOC algorihtm to read the motor current measurements |
CurrentSense linking method
Definition at line 60 of file FOCMotor.cpp.
|
inline |
Function setting a custom motion control method defined by the user
| controlMethod | - pointer to the custom control method function defined by the user |
Definition at line 381 of file FOCMotor.h.
| void FOCMotor::linkSensor | ( | Sensor * | _sensor | ) |
Function linking a motor and a sensor
| sensor | Sensor class wrapper for the FOC algorihtm to read the motor angle and velocity |
Sensor linking method
Definition at line 53 of file FOCMotor.cpp.
|
virtual |
Function running FOC algorithm in real-time it calculates the gets motor angle and sets the appropriate voltages to the phase pwm signals
Definition at line 580 of file FOCMotor.cpp.
| void FOCMotor::monitor | ( | ) |
Utility function intended to be used with serial plotter to monitor motor variables significantly slowing the execution down!!!!
Definition at line 321 of file FOCMotor.cpp.
|
virtual |
Function executing the control loops set by the controller.
| target | Either voltage, angle or velocity based on the motor.controller If it is not set the motor will use the target set in its variable motor.target |
This function doesn't need to be run upon each loop execution - depends of the use case
Definition at line 673 of file FOCMotor.cpp.
|
pure virtual |
Method using FOC to set Uq to the motor at the optimal angle Heart of the FOC algorithm
| Uq | Current voltage in q axis to set to the motor |
| Ud | Current voltage in d axis to set to the motor |
| angle_el | current electrical angle of the motor |
Implemented in BLDCMotor, HybridStepperMotor, and StepperMotor.
| float FOCMotor::shaftAngle | ( | ) |
Shaft angle calculation in radians [rad]
Definition at line 65 of file FOCMotor.cpp.
| float FOCMotor::shaftVelocity | ( | ) |
Shaft angle calculation function in radian per second [rad/s] It implements low pass filtering
Definition at line 71 of file FOCMotor.cpp.
| int FOCMotor::tuneCurrentController | ( | float | bandwidth | ) |
Auto-tune the current controller PID parameters based on desired bandwidth. Uses a simple method that assumes a first order system and requires knowledge of the motor phase resistance and inductance (if not set, the characteriseMotor function can be used).
| bandwidth | Desired closed-loop bandwidth in Hz. |
Definition at line 538 of file FOCMotor.cpp.
| void FOCMotor::updateCurrentLimit | ( | float | new_current_limit | ) |
Update limit values in controllers when changed
| new_current_limit | - new current limit value |
Definition at line 453 of file FOCMotor.cpp.
|
inlineprotected |
Function udating loop time measurement time between two loopFOC executions in microseconds It filters the value using low pass filtering alpha = 0.1
Definition at line 393 of file FOCMotor.h.
|
inlineprotected |
| void FOCMotor::updateMotionControlType | ( | MotionControlType | new_motion_controller | ) |
Update motion control type and related target values
| new_motion_controller | - new motion control type |
Definition at line 495 of file FOCMotor.cpp.
| void FOCMotor::updateTorqueControlType | ( | TorqueControlType | new_torque_controller | ) |
Update torque control type and related controller limit values
| new_torque_controller | - new torque control type |
Definition at line 480 of file FOCMotor.cpp.
| void FOCMotor::updateVelocityLimit | ( | float | new_velocity_limit | ) |
Update limit values in controllers when changed
| new_velocity_limit | - new velocity limit value |
Definition at line 446 of file FOCMotor.cpp.
| void FOCMotor::updateVoltageLimit | ( | float | new_voltage_limit | ) |
Update limit values in controllers when changed
| new_voltage_limit | - new voltage limit value |
Definition at line 466 of file FOCMotor.cpp.
| void FOCMotor::useMonitoring | ( | Print & | ) |
Function providing BLDCMotor class with the Serial interface and enabling monitoring mode
| serial | Monitoring Serial class reference |
Monitoring functions
Definition at line 87 of file FOCMotor.cpp.
| float FOCMotor::velocityOpenloop | ( | float | target_velocity | ) |
Function (iterative) generating open loop movement for target velocity it uses voltage_limit variable
| target_velocity | - rad/s |
Definition at line 389 of file FOCMotor.cpp.
motor direct axis phase inductance
Definition at line 235 of file FOCMotor.h.
| MotionControlType FOCMotor::controller |
parameter determining the control loop to be used
Definition at line 253 of file FOCMotor.h.
| DQCurrent_s FOCMotor::current |
current d and q current measured
Definition at line 219 of file FOCMotor.h.
| float FOCMotor::current_limit |
Current limiting variable - global limit.
Definition at line 239 of file FOCMotor.h.
| CurrentSense* FOCMotor::current_sense |
Definition at line 302 of file FOCMotor.h.
| float FOCMotor::current_sp |
target current ( q current )
Definition at line 215 of file FOCMotor.h.
| float FOCMotor::electrical_angle |
current electrical angle
Definition at line 213 of file FOCMotor.h.
| int8_t FOCMotor::enabled = 0 |
enabled or disabled motor flag
Definition at line 243 of file FOCMotor.h.
| DQCurrent_s FOCMotor::feed_forward_current |
current d and q current measured
Definition at line 223 of file FOCMotor.h.
| float FOCMotor::feed_forward_velocity = 0.0f |
current feed forward velocity
Definition at line 211 of file FOCMotor.h.
| DQVoltage_s FOCMotor::feed_forward_voltage |
current d and q voltage set to the motor
Definition at line 224 of file FOCMotor.h.
| FOCModulationType FOCMotor::foc_modulation |
parameter determining modulation algorithm
Definition at line 247 of file FOCMotor.h.
| float FOCMotor::KV_rating |
motor KV rating
Definition at line 233 of file FOCMotor.h.
|
protected |
last elapsed time of loopFOC in microseconds
Definition at line 409 of file FOCMotor.h.
|
protected |
timestamp of the last loopFOC execution in microseconds
Definition at line 408 of file FOCMotor.h.
|
protected |
last elapsed time of move in microseconds
Definition at line 411 of file FOCMotor.h.
|
protected |
timestamp of the last move execution in microseconds
Definition at line 410 of file FOCMotor.h.
| uint32_t FOCMotor::loopfoc_time_us = 0 |
filtered loop times
Definition at line 308 of file FOCMotor.h.
| LowPassFilter FOCMotor::LPF_angle {0.0} |
parameter determining the angle low pass filter configuration
Definition at line 263 of file FOCMotor.h.
| LowPassFilter FOCMotor::LPF_current_d {DEF_CURR_FILTER_Tf} |
parameter determining the current Low pass filter configuration
Definition at line 259 of file FOCMotor.h.
| LowPassFilter FOCMotor::LPF_current_q {DEF_CURR_FILTER_Tf} |
parameter determining the current Low pass filter configuration
Definition at line 258 of file FOCMotor.h.
| LowPassFilter FOCMotor::LPF_velocity {DEF_VEL_FILTER_Tf} |
parameter determining the velocity Low pass filter configuration
Definition at line 262 of file FOCMotor.h.
| int8_t FOCMotor::modulation_centered = 1 |
flag (1) centered modulation around driver limit /2 or (0) pulled to 0
Definition at line 248 of file FOCMotor.h.
| unsigned int FOCMotor::monitor_decimals = 4 |
monitor outputs decimal places
Definition at line 290 of file FOCMotor.h.
| unsigned int FOCMotor::monitor_downsample = DEF_MON_DOWNSMAPLE |
show monitor outputs each monitor_downsample calls
Definition at line 286 of file FOCMotor.h.
| char FOCMotor::monitor_end_char = '\0' |
monitor outputs ending character
Definition at line 288 of file FOCMotor.h.
| Print* FOCMotor::monitor_port |
Serial terminal variable if provided.
time between two loopFOC executions in microseconds
Definition at line 305 of file FOCMotor.h.
| char FOCMotor::monitor_separator = '\t' |
monitor outputs separation character
Definition at line 289 of file FOCMotor.h.
| char FOCMotor::monitor_start_char = '\0' |
monitor starting character
Definition at line 287 of file FOCMotor.h.
| uint8_t FOCMotor::monitor_variables = _MON_TARGET | _MON_VOLT_Q | _MON_VEL | _MON_ANGLE |
Bit array holding the map of variables the user wants to monitor.
Definition at line 292 of file FOCMotor.h.
| unsigned int FOCMotor::motion_cnt = 0 |
counting variable for downsampling for move commad
Definition at line 265 of file FOCMotor.h.
| unsigned int FOCMotor::motion_downsample = DEF_MOTION_DOWNSMAPLE |
parameter defining the ratio of downsampling for move commad
Definition at line 264 of file FOCMotor.h.
| FOCMotorStatus FOCMotor::motor_status = FOCMotorStatus::motor_uninitialized |
motor status
Definition at line 244 of file FOCMotor.h.
| uint32_t FOCMotor::move_time_us = 0 |
Definition at line 309 of file FOCMotor.h.
| PIDController FOCMotor::P_angle {DEF_P_ANGLE_P,0,0,0,DEF_VEL_LIM} |
parameter determining the position PID configuration
Definition at line 261 of file FOCMotor.h.
| float FOCMotor::phase_inductance |
motor phase inductance q axis - FOR BACKWARDS COMPATIBILITY
Definition at line 234 of file FOCMotor.h.
| float FOCMotor::phase_resistance |
motor phase resistance
Definition at line 231 of file FOCMotor.h.
| PIDController FOCMotor::PID_current_d {DEF_PID_CURR_P,DEF_PID_CURR_I,DEF_PID_CURR_D,DEF_PID_CURR_RAMP, DEF_POWER_SUPPLY} |
parameter determining the d current PID config
Definition at line 257 of file FOCMotor.h.
| PIDController FOCMotor::PID_current_q {DEF_PID_CURR_P,DEF_PID_CURR_I,DEF_PID_CURR_D,DEF_PID_CURR_RAMP, DEF_POWER_SUPPLY} |
parameter determining the q current PID config
Definition at line 256 of file FOCMotor.h.
| PIDController FOCMotor::PID_velocity {DEF_PID_VEL_P,DEF_PID_VEL_I,DEF_PID_VEL_D,DEF_PID_VEL_RAMP,DEF_PID_VEL_LIMIT} |
parameter determining the velocity PID configuration
Definition at line 260 of file FOCMotor.h.
| int FOCMotor::pole_pairs |
motor pole pairs number
Definition at line 232 of file FOCMotor.h.
| bool FOCMotor::pp_check_result = false |
the result of the PP check, if run during loopFOC
Definition at line 271 of file FOCMotor.h.
| Sensor* FOCMotor::sensor |
CurrentSense link.
Sensor link:
Definition at line 300 of file FOCMotor.h.
| Direction FOCMotor::sensor_direction = Direction::UNKNOWN |
default is CW. if sensor_direction == Direction::CCW then direction will be flipped compared to CW. Set to UNKNOWN to set by calibration
Definition at line 270 of file FOCMotor.h.
| float FOCMotor::sensor_offset |
user defined sensor zero offset
Definition at line 268 of file FOCMotor.h.
| float FOCMotor::shaft_angle |
current motor angle
Definition at line 212 of file FOCMotor.h.
| float FOCMotor::shaft_angle_sp |
current target angle
Definition at line 217 of file FOCMotor.h.
| float FOCMotor::shaft_velocity |
current motor velocity
Definition at line 214 of file FOCMotor.h.
| float FOCMotor::shaft_velocity_sp |
current target velocity
Definition at line 216 of file FOCMotor.h.
| float FOCMotor::target |
current target value - depends of the controller
Definition at line 210 of file FOCMotor.h.
| TorqueControlType FOCMotor::torque_controller |
parameter determining the torque control type
Definition at line 252 of file FOCMotor.h.
| float FOCMotor::Ualpha |
Definition at line 221 of file FOCMotor.h.
| float FOCMotor::Ubeta |
Phase voltages U alpha and U beta used for inverse Park and Clarke transform.
Definition at line 221 of file FOCMotor.h.
| float FOCMotor::velocity_index_search |
target velocity for index search
Definition at line 228 of file FOCMotor.h.
| float FOCMotor::velocity_limit |
Velocity limiting variable - global limit.
Definition at line 240 of file FOCMotor.h.
| DQVoltage_s FOCMotor::voltage |
current d and q voltage set to the motor
Definition at line 218 of file FOCMotor.h.
| float FOCMotor::voltage_bemf |
estimated backemf voltage (if provided KV constant)
Definition at line 220 of file FOCMotor.h.
| float FOCMotor::voltage_limit |
Voltage limiting variable - global limit.
Definition at line 238 of file FOCMotor.h.
| float FOCMotor::voltage_sensor_align |
sensor and motor align voltage parameter
Definition at line 227 of file FOCMotor.h.
| float FOCMotor::zero_electric_angle = NOT_SET |
absolute zero electric angle - if available
Definition at line 269 of file FOCMotor.h.