|
| 1 | +#include "BLDCMotor.h" |
| 2 | + |
| 3 | +/* |
| 4 | +BLDCMotorint(int phA,int phB,int phC, long* counter, int encA, int encB , int pp, int cpr) |
| 5 | +- phA, phB, phC - motor A,B,C phase pwm pins |
| 6 | +- *counter - encoder counter variable |
| 7 | +- encA, encB - encoder A and B pins |
| 8 | +- pp - pole pair number |
| 9 | +- cpr - counts per rotation number (cpm=ppm*4) |
| 10 | +- enable pin - (optional input) |
| 11 | +*/ |
| 12 | +BLDCMotor::BLDCMotor(int phA,int phB,int phC, long* counter, int encA, int encB , int pp, int cpr) |
| 13 | +{ |
| 14 | + // set enable pin to zero |
| 15 | + BLDCMotor(phA, phB, phC, counter, encA, encB, pp, cpr, 0); |
| 16 | +} |
| 17 | +BLDCMotor::BLDCMotor(int phA,int phB,int phC, long* counter, int encA, int encB , int pp, int cpr, int en) |
| 18 | +{ |
| 19 | + // Pin intialization |
| 20 | + _phA = phA; |
| 21 | + _phB = phB; |
| 22 | + _phC = phC; |
| 23 | + |
| 24 | + // enable pin |
| 25 | + _en = en; |
| 26 | + |
| 27 | + // encoder pins |
| 28 | + _encA = encA; |
| 29 | + _encB = encB; |
| 30 | + |
| 31 | + // Encoder value |
| 32 | + _encoderPosition = counter; |
| 33 | + _cpr = cpr; |
| 34 | + _pp = pp; |
| 35 | + |
| 36 | +} |
| 37 | +/* |
| 38 | + initialization function |
| 39 | +*/ |
| 40 | +void BLDCMotor::init(){ |
| 41 | + // enable motor |
| 42 | + enableMotor(); |
| 43 | + // encoder alignment |
| 44 | + delay(500); |
| 45 | + alignEncoder(); |
| 46 | + delay(500); |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | + disable motor |
| 51 | +*/ |
| 52 | +void BLDCMotor::disableMotor() |
| 53 | +{ |
| 54 | + // disable the driver - if enable pin available |
| 55 | + if(_en) digitalWrite(_en, LOW); |
| 56 | + // set zero to PWM |
| 57 | + setPwm(_phA,0); |
| 58 | + setPwm(_phB,0); |
| 59 | + setPwm(_phC,0); |
| 60 | +} |
| 61 | +/* |
| 62 | + disable motor |
| 63 | +*/ |
| 64 | +void BLDCMotor::enableMotor() |
| 65 | +{ |
| 66 | + // set zero to PWM |
| 67 | + setPwm(_phA,0); |
| 68 | + setPwm(_phB,0); |
| 69 | + setPwm(_phC,0); |
| 70 | + // enable the driver - if enable pin available |
| 71 | + if(_en) digitalWrite(_en, HIGH); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +/* |
| 78 | + Encoder alignment to electrical 0 angle |
| 79 | +*/ |
| 80 | +void BLDCMotor::alignEncoder(){ |
| 81 | + setPhaseVoltage(10,M_PI/2); |
| 82 | + delay(500); |
| 83 | + *_encoderPosition = 0; |
| 84 | + disableMotor(); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + State calculation methods |
| 89 | +*/ |
| 90 | +/* |
| 91 | + Shaft angle calculation |
| 92 | +*/ |
| 93 | +float BLDCMotor::ShaftAngle(){ |
| 94 | + return (*_encoderPosition)/((float)_cpr)*(2.0*M_PI); |
| 95 | +} |
| 96 | +/* |
| 97 | + Shaft velocity calculation |
| 98 | +*/ |
| 99 | +float BLDCMotor::ShaftVelocity(float shaftAngle){ |
| 100 | + static float dt; |
| 101 | + static float shaftAngle_1; |
| 102 | + |
| 103 | + dt = (micros() - dt)*1e-6; |
| 104 | + float d_angle = -(shaftAngle - shaftAngle_1)/(dt); |
| 105 | + dt = micros(); |
| 106 | + shaftAngle_1 = shaftAngle; |
| 107 | + return d_angle; |
| 108 | +} |
| 109 | +/* |
| 110 | + Electrical angle calculation |
| 111 | +*/ |
| 112 | +float BLDCMotor::ElectricAngle(float shaftAngle){ |
| 113 | + return normalizeAngle(shaftAngle*_pp); |
| 114 | +} |
| 115 | +/* |
| 116 | + Update motor angles and velocities |
| 117 | +*/ |
| 118 | +void BLDCMotor::updateStates(){ |
| 119 | + _shaftAngle = ShaftAngle(); |
| 120 | + _angleElec = ElectricAngle(_shaftAngle); |
| 121 | + _shaftVel = ShaftVelocity(_shaftAngle); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +/** |
| 126 | + FOC methods |
| 127 | +*/ |
| 128 | +/* |
| 129 | + Method using FOC to set Uq to the motor at the optimal angle |
| 130 | +*/ |
| 131 | +void BLDCMotor::setVoltage(float Uq){ |
| 132 | + updateStates(); |
| 133 | + setPhaseVoltage(Uq, _angleElec); |
| 134 | +} |
| 135 | +void BLDCMotor::setPhaseVoltage(double Uq, double angle_el){ |
| 136 | + |
| 137 | + // Park transform |
| 138 | + Ualpha = Uq*cos(angle_el); |
| 139 | + Ubeta = Uq*sin(angle_el); |
| 140 | + // negative Uq compensation |
| 141 | + float angle = Uq > 0 ? angle_el : normalizeAngle( angle_el + M_PI ); |
| 142 | + |
| 143 | + // determine the segment I, II, III |
| 144 | + if((angle >= 0)&&(angle <= _120_D2R)){ |
| 145 | + // section I |
| 146 | + Ua = Ualpha + _1_SQRT3*abs(Ubeta); |
| 147 | + Ub = _2_SQRT3*abs(Ubeta); |
| 148 | + Uc = 0; |
| 149 | + |
| 150 | + }else if((angle > _120_D2R)&&(angle <= (2*_120_D2R))){ |
| 151 | + // section III |
| 152 | + Ua = 0; |
| 153 | + Ub = _1_SQRT3*Ubeta + abs(Ualpha); |
| 154 | + Uc = -_1_SQRT3*Ubeta + abs(Ualpha); |
| 155 | + |
| 156 | + }else if((angle > (2*_120_D2R))&&(angle <= (3*_120_D2R))){ |
| 157 | + // section II |
| 158 | + Ua = Ualpha + _1_SQRT3*abs(Ubeta); |
| 159 | + Ub = 0; |
| 160 | + Uc = _2_SQRT3*abs(Ubeta); |
| 161 | + } |
| 162 | + |
| 163 | + // set phase voltages |
| 164 | + setPwm(_phA,Ua); |
| 165 | + setPwm(_phB,Ub); |
| 166 | + setPwm(_phC,Uc); |
| 167 | +} |
| 168 | +/* |
| 169 | + Set voltage to the pwm pin |
| 170 | +*/ |
| 171 | +void BLDCMotor::setPwm(int pinPwm, float U){ |
| 172 | + int U_pwm = U <= U_MAX ? 255.0*U/U_MAX : 255; |
| 173 | + analogWrite(pinPwm, U_pwm); |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + Utility funcitons |
| 178 | +*/ |
| 179 | +/* |
| 180 | + normalizing radian angle to [0,2PI] |
| 181 | +*/ |
| 182 | +double BLDCMotor::normalizeAngle(double angle) |
| 183 | +{ |
| 184 | + double a = fmod(angle, 2 * M_PI); |
| 185 | + return a >= 0 ? a : (a + 2 * M_PI); |
| 186 | +} |
| 187 | +/* |
| 188 | + Reference low pass filter |
| 189 | +*/ |
| 190 | +float BLDCMotor::filterLP(float u){ |
| 191 | + static float dt,yk_1; |
| 192 | + float M_Tau = 0.01; |
| 193 | + dt = (micros() - dt)*1e-6; |
| 194 | + float y_k = dt/(M_Tau+dt)*u +(1-dt/(M_Tau+dt))*yk_1; |
| 195 | + dt = micros(); |
| 196 | + yk_1 = y_k; |
| 197 | + return y_k; |
| 198 | +} |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +/** |
| 204 | + Motor control functions |
| 205 | +*/ |
| 206 | +float BLDCMotor::velocityPI(float ek){ |
| 207 | + static float ek_1, uk_1; |
| 208 | + static float dT; |
| 209 | + dT = (micros() - dT)*1e-6; |
| 210 | + |
| 211 | + float uk = uk_1 + M_Kr*(dT/(2*M_Ti)+1)*ek + M_Kr*(dT/(2*M_Ti)-1)*ek_1; |
| 212 | + if(abs(uk) > U_MAX) uk = uk > 0 ? U_MAX : -U_MAX; |
| 213 | + |
| 214 | + uk_1 = uk; |
| 215 | + ek_1 = ek; |
| 216 | + dT = micros(); |
| 217 | + return uk; |
| 218 | +} |
| 219 | + |
| 220 | +float BLDCMotor::positionP(float ek){ |
| 221 | + float uk = M_P * ek; |
| 222 | + if(abs(uk) > W_MAX) uk = uk > 0 ? W_MAX : -W_MAX; |
| 223 | + return uk; |
| 224 | +} |
| 225 | + |
| 226 | + |
| 227 | +void BLDCMotor::setVelocity(float vel){ |
| 228 | + updateStates(); |
| 229 | + setVoltage(velocityPI(vel-_shaftVel)); |
| 230 | +} |
| 231 | + |
| 232 | +void BLDCMotor::setPosition(float pos){ |
| 233 | + updateStates(); |
| 234 | + setVoltage(velocityPI(-positionP( filterLP(pos) - _shaftAngle ) - _shaftVel)); |
| 235 | +} |
| 236 | + |
| 237 | + |
| 238 | + |
0 commit comments