SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
HybridStepperMotor.cpp
Go to the documentation of this file.
3
4// HybridStepperMotor(int pp)
5// - pp - pole pair number
6// - R - motor phase resistance
7// - KV - motor kv rating (rmp/v)
8// - Lq - motor q-axis inductance [H]
9// - Ld - motor d-axis inductance [H]
10HybridStepperMotor::HybridStepperMotor(int pp, float _R, float _KV, float _Lq, float _Ld)
11 : FOCMotor()
12{
13 // number od pole pairs
14 pole_pairs = pp;
15 // save phase resistance number
17 // save back emf constant KV = 1/K_bemf
18 // usually used rms
19 KV_rating = _KV;
20 // save phase inductance
21 axis_inductance = {_Ld, _Lq};
22 phase_inductance = _Lq; // FOR BACKWARDS COMPATIBILITY
23
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{
35 driver = _driver;
36 SIMPLEFOC_MOTOR_DEBUG("BLDCDriver linked, using pin C as the mid-phase");
37}
38
39// override of the FOCMotor's current sense linking
40// setting the driver type directly
45
46// init hardware pins
48{
49 if (!driver || !driver->initialized)
50 {
52 SIMPLEFOC_MOTOR_ERROR("Init not possible, driver not init");
53 return 0;
54 }
57 // sanity check for the voltage limit configuration
60 // constrain voltage for sensor alignment
63
64 // update limits in the motor controllers
68
70 // if only single inductance value is set, use it for both d and q axis
72 }
73
74 // if using open loop control, set a CW as the default direction if not already set
75 // only if no sensor is used
76 if(!sensor){
81 }
82 }
83
84 _delay(500);
85 // enable motor
86 SIMPLEFOC_MOTOR_DEBUG("Enable driver.");
87 enable();
88 _delay(500);
89
91 return 1;
92}
93
94// disable motor driver
96{
97 // disable the current sense
99 // set zero to PWM
100 driver->setPwm(0, 0, 0);
101 // disable driver
102 driver->disable();
103 // motor status update
104 enabled = 0;
105}
106
107// enable motor driver
109{
110 // disable enable
111 driver->enable();
112 // set zero to PWM
113 driver->setPwm(0, 0, 0);
114 // enable the current sense
116 // reset the pids
118 P_angle.reset();
121 // motor status update
122 enabled = 1;
123}
124
125
127 // bemf constant is approximately 1/KV rating
128 // V_bemf = K_bemf * velocity
129 return vel/(KV_rating*_SQRT2)/_RPM_TO_RADS;
130}
131
132
133// Method using FOC to set Uq and Ud to the motor at the optimal angle
134// Function implementing Sine PWM and SVPWM algorithms
135void HybridStepperMotor::setPhaseVoltage(float Uq, float Ud, float angle_el)
136{
137 float center;
138 float _sa, _ca;
139
140 _sincos(angle_el, &_sa, &_ca);
141
142 switch (foc_modulation) {
145 // not handled
146 Ua = 0;
147 Ub = 0;
148 Uc = 0;
149 break;
151 // C phase is fixed at half-rail to provide bias point for A, B legs
152 Ua = (_ca * Ud) - (_sa * Uq);
153 Ub = (_sa * Ud) + (_ca * Uq);
154
155 center = driver->voltage_limit / 2;
156
157 Ua += center;
158 Ub += center;
159 Uc = center;
160 break;
161
163 // C phase moves in order to increase max bias on coils
164 Ua = (_ca * Ud) - (_sa * Uq);
165 Ub = (_sa * Ud) + (_ca * Uq);
166
167 float Umin = fmin(fmin(Ua, Ub), 0);
168 float Umax = fmax(fmax(Ua, Ub), 0);
169 float Vo = -(Umin + Umax)/2 + driver->voltage_limit/2;
170
171 Ua = Ua + Vo;
172 Ub = Ub + Vo;
173 Uc = Vo;
174 }
175 driver->setPwm(Ua, Ub, Uc);
176
177}
@ Hybrid
Definition FOCDriver.h:19
@ velocity_openloop
Definition FOCMotor.h:53
@ angle_openloop
Definition FOCMotor.h:54
#define SIMPLEFOC_MOTOR_DEBUG(msg,...)
Definition FOCMotor.h:27
@ Trapezoid_120
Definition FOCMotor.h:75
@ SpaceVectorPWM
Space vector modulation method.
Definition FOCMotor.h:74
@ Trapezoid_150
Definition FOCMotor.h:76
@ SinePWM
Sinusoidal PWM modulation.
Definition FOCMotor.h:73
@ 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 setPwm(float Ua, float Ub, float Uc)=0
DriverType driver_type
driver type (BLDC or Stepper)
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
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
void linkCurrentSense(CurrentSense *current_sense)
Definition FOCMotor.cpp:60
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 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
FOCModulationType foc_modulation
parameter determining modulation algorithm
Definition FOCMotor.h:247
void setPhaseVoltage(float Uq, float Ud, float angle_el) override
void linkDriver(BLDCDriver *driver)
float estimateBEMF(float velocity) override
BLDCDriver * driver
BLDCDriver instance.
float Uc
Phase voltages used for inverse Park and Clarke transform.
void linkCurrentSense(CurrentSense *current_sense)
HybridStepperMotor(int pp, float R=NOT_SET, float KV=NOT_SET, float L_q=NOT_SET, float L_d=NOT_SET)
void reset()
Definition pid.cpp:66
#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