SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
StepperMotor.cpp
Go to the documentation of this file.
1#include "StepperMotor.h"
3
4
5// StepperMotor(int pp)
6// - pp - pole pair number
7// - R - motor phase resistance
8// - KV - motor kv rating (rmp/v)
9// - Lq - motor q-axis inductance [H]
10// - Ld - motor d-axis inductance [H]
11StepperMotor::StepperMotor(int pp, float _R, float _KV, float _Lq, float _Ld)
12: FOCMotor()
13{
14 // number od pole pairs
15 pole_pairs = pp;
16 // save phase resistance number
18 // save back emf constant KV = 1/K_bemf
19 // usually used rms
20 KV_rating = _KV;
21 // save phase inductance
22 axis_inductance = {_Ld, _Lq};
23 phase_inductance = _Lq; // FOR BACKWARDS COMPATIBILITY
24
25 // torque control type is voltage by default
26 // current and foc_current not supported yet
28}
29
30/**
31 Link the driver which controls the motor
32*/
34 driver = _driver;
35}
36
37// init hardware pins
39 if (!driver || !driver->initialized) {
41 SIMPLEFOC_MOTOR_ERROR("Init not possible, driver not init");
42 return 0;
43 }
46
47 // sanity check for the voltage limit configuration
49 // constrain voltage for sensor alignment
51
52 // update limits in the motor controllers
56
58 // if only single inductance value is set, use it for both d and q axis
60 }
61
62
63 // if using open loop control, set a CW as the default direction if not already set
64 // only if no sensor is used
65 if(!sensor){
70 }
71 }
72
73 _delay(500);
74 // enable motor
75 SIMPLEFOC_MOTOR_DEBUG("Enable driver.");
76 enable();
77 _delay(500);
79 return 1;
80}
81
82
83// disable motor driver
85{
86 // disable the current sense
88 // set zero to PWM
89 driver->setPwm(0, 0);
90 // disable driver
91 driver->disable();
92 // motor status update
93 enabled = 0;
94}
95// enable motor driver
97{
98 // disable enable
99 driver->enable();
100 // set zero to PWM
101 driver->setPwm(0, 0);
102 // enable the current sense
104 // reset the pids
106 P_angle.reset();
109 // motor status update
110 enabled = 1;
111}
112
113
115 // bemf constant is approximately 1/KV rating
116 // V_bemf = K_bemf * velocity
117 return vel/(KV_rating*_SQRT2)/_RPM_TO_RADS;
118}
119
120// Method using FOC to set Uq and Ud to the motor at the optimal angle
121// Function implementing Sine PWM algorithms
122void StepperMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
123 // Sinusoidal PWM modulation
124 // Inverse Park transformation
125 float _sa, _ca;
126 _sincos(angle_el, &_sa, &_ca);
127
128 // Inverse park transform
129 Ualpha = _ca * Ud - _sa * Uq;
130 Ubeta = _sa * Ud + _ca * Uq;
131
132 // set the voltages in hardware
134}
@ velocity_openloop
Definition FOCMotor.h:53
@ angle_openloop
Definition FOCMotor.h:54
#define SIMPLEFOC_MOTOR_DEBUG(msg,...)
Definition FOCMotor.h:27
@ motor_uncalibrated
Motor is initialized, but not calibrated (open loop possible)
Definition FOCMotor.h:84
@ motor_initializing
Motor intiialization is in progress.
Definition FOCMotor.h:83
@ motor_init_failed
Motor initialization failed (not recoverable)
Definition FOCMotor.h:89
#define SIMPLEFOC_MOTOR_ERROR(msg,...)
Definition FOCMotor.h:23
@ voltage
Torque control using voltage.
Definition FOCMotor.h:63
@ UNKNOWN
Definition Sensor.h:12
@ CW
Definition Sensor.h:10
virtual void enable()
virtual void disable()
virtual void enable()=0
virtual void disable()=0
bool initialized
true if driver was successfully initialized
Definition FOCDriver.h:39
float voltage_limit
limiting voltage set to the motor
Definition FOCDriver.h:37
int8_t enabled
enabled or disabled motor flag
Definition FOCMotor.h:243
void updateCurrentLimit(float new_current_limit)
Definition FOCMotor.cpp:453
DQ_s axis_inductance
motor direct axis phase inductance
Definition FOCMotor.h:235
PIDController PID_current_q
parameter determining the q current PID config
Definition FOCMotor.h:256
float Ualpha
Definition FOCMotor.h:221
void updateVoltageLimit(float new_voltage_limit)
Definition FOCMotor.cpp:466
MotionControlType controller
parameter determining the control loop to be used
Definition FOCMotor.h:253
float phase_resistance
motor phase resistance
Definition FOCMotor.h:231
PIDController PID_current_d
parameter determining the d current PID config
Definition FOCMotor.h:257
Sensor * sensor
CurrentSense link.
Definition FOCMotor.h:300
CurrentSense * current_sense
Definition FOCMotor.h:302
float voltage_limit
Voltage limiting variable - global limit.
Definition FOCMotor.h:238
float velocity_limit
Velocity limiting variable - global limit.
Definition FOCMotor.h:240
PIDController P_angle
parameter determining the position PID configuration
Definition FOCMotor.h:261
FOCMotorStatus motor_status
motor status
Definition FOCMotor.h:244
float Ubeta
Phase voltages U alpha and U beta used for inverse Park and Clarke transform.
Definition FOCMotor.h:221
float KV_rating
motor KV rating
Definition FOCMotor.h:233
float voltage_sensor_align
sensor and motor align voltage parameter
Definition FOCMotor.h:227
Direction sensor_direction
default is CW. if sensor_direction == Direction::CCW then direction will be flipped compared to CW....
Definition FOCMotor.h:270
float current_limit
Current limiting variable - global limit.
Definition FOCMotor.h:239
TorqueControlType torque_controller
parameter determining the torque control type
Definition FOCMotor.h:252
PIDController PID_velocity
parameter determining the velocity PID configuration
Definition FOCMotor.h:260
float phase_inductance
motor phase inductance q axis - FOR BACKWARDS COMPATIBILITY
Definition FOCMotor.h:234
int pole_pairs
motor pole pairs number
Definition FOCMotor.h:232
void updateVelocityLimit(float new_velocity_limit)
Definition FOCMotor.cpp:446
void reset()
Definition pid.cpp:66
virtual void setPwm(float Ua, float Ub)=0
int init() override
void disable() override
void linkDriver(StepperDriver *driver)
void enable() override
void setPhaseVoltage(float Uq, float Ud, float angle_el) override
float estimateBEMF(float velocity) override
StepperDriver * driver
StepperMotor(int pp, float R=NOT_SET, float KV=NOT_SET, float L_q=NOT_SET, float L_d=NOT_SET)
#define _SQRT2
Definition foc_utils.h:24
#define _RPM_TO_RADS
Definition foc_utils.h:32
#define _isset(a)
Definition foc_utils.h:13
void _sincos(float a, float *s, float *c)
float q
Definition foc_utils.h:46
void _delay(unsigned long ms)
Definition time_utils.cpp:5