enhanced functionality in V01 vs. V00, V02 finished, conversion to double precsision in V03

Dependencies:   mbed

Committer:
pmic
Date:
Tue Mar 13 20:23:44 2018 +0000
Revision:
9:4e6f3404d473
Parent:
8:3b98b6e1ead3
Child:
10:b7f142952df9
implementation updated, frequency dependent magnitude (1/fexc), Gyu Gye established

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rtlabor 0:15be70d21d7c 1 #include "mbed.h"
rtlabor 0:15be70d21d7c 2 #include "math.h"
pmic 8:3b98b6e1ead3 3 #define pi 3.1415927f
pmic 5:e443d5ad409f 4
rtlabor 0:15be70d21d7c 5 #include "EncoderCounter.h"
rtlabor 0:15be70d21d7c 6 #include "DiffCounter.h"
rtlabor 0:15be70d21d7c 7 #include "PI_Cntrl.h"
rtlabor 0:15be70d21d7c 8 #include "IIR_filter.h"
rtlabor 0:15be70d21d7c 9 #include "LinearCharacteristics.h"
pmic 8:3b98b6e1ead3 10 #include "GPA.h"
pmic 5:e443d5ad409f 11
rtlabor 0:15be70d21d7c 12 /* Cuboid balance on one edge on Nucleo F446RE
pmic 5:e443d5ad409f 13 // -----------------------------------------------------------------------------
altb 3:a951d699878b 14
pmic 5:e443d5ad409f 15 IMPORTANT: use ..\T-RT\Messen_Ausstellungen\Praesentationen_im_Labor\Wuerfel_nucleo\Escon_Parameter_4nucleo_stark.edc
pmic 5:e443d5ad409f 16 settings for Maxon ESCON controller (upload via ESCON Studio)
pmic 8:3b98b6e1ead3 17
rtlabor 0:15be70d21d7c 18 hardware Connections:
pmic 8:3b98b6e1ead3 19
rtlabor 0:15be70d21d7c 20 CN7 CN10
altb 3:a951d699878b 21 : :
altb 3:a951d699878b 22 : :
altb 3:a951d699878b 23 .. ..
altb 3:a951d699878b 24 .. .. 15.
altb 3:a951d699878b 25 .. AOUT i_des on (PA_5)o.
altb 3:a951d699878b 26 .. ..
altb 3:a951d699878b 27 .. ..
altb 3:a951d699878b 28 .. ENC CH A o.
altb 3:a951d699878b 29 o. GND .. 10.
altb 3:a951d699878b 30 o. ENC CH B ..
altb 3:a951d699878b 31 .. ..
altb 3:a951d699878b 32 .. ..
pmic 8:3b98b6e1ead3 33 .o AIN acx (PA_0) ..
altb 3:a951d699878b 34 .o AIN acy (PA_1) .. 5.
altb 3:a951d699878b 35 .o AIN Gyro(PA_4) .o Analog GND
altb 3:a951d699878b 36 .. ..
pmic 8:3b98b6e1ead3 37 .. ..
pmic 8:3b98b6e1ead3 38 .. .. 1.
altb 3:a951d699878b 39 ----------------------------
rtlabor 0:15be70d21d7c 40 CN7 CN10
rtlabor 0:15be70d21d7c 41 */
pmic 5:e443d5ad409f 42
pmic 5:e443d5ad409f 43 Serial pc(SERIAL_TX, SERIAL_RX); // serial connection via USB - programmer
pmic 7:b3c5116e9fab 44 InterruptIn Button(USER_BUTTON); // User Button, short presses: reduce speed, long presses: increase speed
pmic 5:e443d5ad409f 45 AnalogIn ax(PA_0); // analog IN (acc x) on PA_0
pmic 5:e443d5ad409f 46 AnalogIn ay(PA_1); // analog IN (acc y) on PA_1
pmic 5:e443d5ad409f 47 AnalogIn gz(PA_4); // analog IN (gyr z) on PB_0
pmic 5:e443d5ad409f 48 AnalogOut out(PA_5); // analog OUT 1.6 V -> 0A 3.2A -> 2A (see ESCON)
pmic 5:e443d5ad409f 49 Ticker ControllerLoopTimer; // interrupt for control loop
pmic 7:b3c5116e9fab 50 Timer t; // timer to analyse Button
pmic 5:e443d5ad409f 51
pmic 6:7ea1a4bac3c2 52 // controller parameters etc.
rtlabor 0:15be70d21d7c 53 float out_value = 1.6f; // set voltage on 1.6 V (0 A current)
pmic 7:b3c5116e9fab 54 // float kp = 3.0f; // speed control gain for motor speed cntrl.
pmic 7:b3c5116e9fab 55 // float Tn = 0.10f; // integral time " " "
rtlabor 0:15be70d21d7c 56 float Ts = 0.0025; // sample time
rtlabor 0:15be70d21d7c 57 float v_max = 200; // maximum speed rad/s
pmic 9:4e6f3404d473 58 float n_soll = 15.0f; // nominal speed for speed control tests
pmic 7:b3c5116e9fab 59 float tau = 1.0f; // time constant of complementary filter
pmic 7:b3c5116e9fab 60 float fg = 300.0f;
pmic 5:e443d5ad409f 61
pmic 6:7ea1a4bac3c2 62 // output and statemachine
pmic 6:7ea1a4bac3c2 63 unsigned int k = 0; // counter for serial output
pmic 6:7ea1a4bac3c2 64 bool doesStand = 0; // state if the cube is standing or not
pmic 5:e443d5ad409f 65 bool shouldBalance = 0; // state if the controller is active
pmic 5:e443d5ad409f 66
pmic 8:3b98b6e1ead3 67 // set up encoder
pmic 7:b3c5116e9fab 68 EncoderCounter MotorEncoder(PB_6, PB_7); // initialize counter on PB_6 and PB_7
pmic 9:4e6f3404d473 69 DiffCounter MotorDiff(1/(2.0f*pi*30.0f), Ts); // discrete differentiate, based on encoder data
pmic 7:b3c5116e9fab 70
pmic 5:e443d5ad409f 71 PI_Cntrl pi_w2zero(-.01f, 1.0f, 0.4f); // controller to bring motor speed to zero while balancing
pmic 9:4e6f3404d473 72 PI_Cntrl pi_w(0.5f, 0.2f, 12.0f); // PI controller for test purposes motor speed (no balance)
pmic 7:b3c5116e9fab 73
pmic 7:b3c5116e9fab 74 IIR_filter FilterACCx(tau, Ts, 1.0f); // 1st order LP for complementary filter acc_x
pmic 7:b3c5116e9fab 75 IIR_filter FilterACCy(tau, Ts, 1.0f); // 1st order LP for complementary filter acc_y
pmic 7:b3c5116e9fab 76 IIR_filter FilterGYRZ(tau, Ts, tau); // 1st order LP for complementary filter gyro
pmic 7:b3c5116e9fab 77
pmic 8:3b98b6e1ead3 78 float fMin = 1.0f;
pmic 8:3b98b6e1ead3 79 float fMax = 1.0f/2.0f/Ts*0.9f;
pmic 9:4e6f3404d473 80 int NfexcDes = 100;
pmic 9:4e6f3404d473 81 float Aexc0 = 6.0f;
pmic 9:4e6f3404d473 82 float Aexc1 = 0.5f; //Aexc0/fMax;
pmic 9:4e6f3404d473 83 int NperMin = 3;
pmic 9:4e6f3404d473 84 float TmeasMin = 1.0;
pmic 8:3b98b6e1ead3 85 int NmeasMin = (int)ceil(TmeasMin/Ts);
pmic 9:4e6f3404d473 86 GPA Wobble(fMin, fMax, NfexcDes, NperMin, NmeasMin, Ts, Aexc0, Aexc1);
pmic 8:3b98b6e1ead3 87 float inpWobble = 0.0f;
pmic 8:3b98b6e1ead3 88 float outWobble = 0.0f;
pmic 8:3b98b6e1ead3 89 float excWobble = 0.0f;
pmic 8:3b98b6e1ead3 90 short counts = 0;
pmic 8:3b98b6e1ead3 91 float omega = 0.0f;
pmic 9:4e6f3404d473 92 float desCurrent = 0.0f;
pmic 9:4e6f3404d473 93
pmic 9:4e6f3404d473 94 /* // simulation on nucleo
pmic 9:4e6f3404d473 95 float Oexc = 1.0f;
pmic 9:4e6f3404d473 96 float fMin = 0.7f;
pmic 9:4e6f3404d473 97 float fMax = 1.0f/2.0f/Ts*0.72f;
pmic 9:4e6f3404d473 98 int NfexcDes = 100;
pmic 9:4e6f3404d473 99 float Aexc0 = 1.0f;
pmic 9:4e6f3404d473 100 float Aexc1 = Aexc0/fMax;
pmic 9:4e6f3404d473 101 int NperMin = 1;
pmic 9:4e6f3404d473 102 float TmeasMin = 1.0;
pmic 9:4e6f3404d473 103 int NmeasMin = 1;//(int)ceil(TmeasMin/Ts);
pmic 9:4e6f3404d473 104 GPA Wobble(fMin, fMax, NfexcDes, NperMin, NmeasMin, Ts, Aexc0, Aexc1);
pmic 9:4e6f3404d473 105 float w0 = 2.0f*pi*3.0f;
pmic 9:4e6f3404d473 106 float D = 0.02f;
pmic 9:4e6f3404d473 107 IIR_filter SysSecOrder(w0, D, Ts, 10.0f);
pmic 9:4e6f3404d473 108 float u = Oexc;
pmic 9:4e6f3404d473 109 float y = 10.0f*u;
pmic 9:4e6f3404d473 110 float exc = 0.0f;
pmic 9:4e6f3404d473 111 */
pmic 8:3b98b6e1ead3 112
pmic 7:b3c5116e9fab 113 // IIR_filter FilterANG(1.0f/(2.0f*PI*fg), Ts, 1.0f);
pmic 7:b3c5116e9fab 114 // IIR_filter FilterDiffANG(1.0f/(2.0f*PI*fg), Ts);
pmic 5:e443d5ad409f 115
pmic 6:7ea1a4bac3c2 116 // linear characteristics
pmic 4:47581778e863 117 LinearCharacteristics i2u(0.1067f,-15.0f); // full range, convert desired current (Amps) -> voltage 0..3.3V
rtlabor 0:15be70d21d7c 118 LinearCharacteristics u2n(312.5f,1.6f); // convert input voltage (0..3.3V) -> speed (1/min)
rtlabor 0:15be70d21d7c 119 LinearCharacteristics u2w(32.725,1.6f); // convert input voltage (0..3.3V) -> speed (rad/sec)
rtlabor 0:15be70d21d7c 120 LinearCharacteristics u2ax(14.67f,1.6378f); // convert input voltage (0..3.3V) -> acc_x m/s^2
rtlabor 0:15be70d21d7c 121 LinearCharacteristics u2ay(15.02f ,1.6673f); // convert input voltage (0..3.3V) -> acc_y m/s^2
rtlabor 0:15be70d21d7c 122 LinearCharacteristics u2gz(-4.652f,1.4949f); // convert input voltage (0..3.3V) -> w_x rad/s
rtlabor 0:15be70d21d7c 123 LinearCharacteristics u3k3_TO_1V(0.303030303f,0,3.3f,0.0f);// normalize output voltage (0..3.3)V -> (0..1) V
rtlabor 0:15be70d21d7c 124
pmic 5:e443d5ad409f 125 // user defined functions
rtlabor 0:15be70d21d7c 126 void updateControllers(void); // speed controller loop (via interrupt)
pmic 7:b3c5116e9fab 127 void pressed(void); // user Button pressed
pmic 7:b3c5116e9fab 128 void released(void); // user Button released
rtlabor 0:15be70d21d7c 129
pmic 6:7ea1a4bac3c2 130 // main program and control loop
pmic 5:e443d5ad409f 131 // -----------------------------------------------------------------------------
rtlabor 0:15be70d21d7c 132 int main()
rtlabor 0:15be70d21d7c 133 {
pmic 5:e443d5ad409f 134 // for serial comm.
pmic 8:3b98b6e1ead3 135 pc.baud(2000000);
pmic 8:3b98b6e1ead3 136
pmic 5:e443d5ad409f 137 // reset encoder, controller and filters
pmic 7:b3c5116e9fab 138 MotorEncoder.reset();
pmic 7:b3c5116e9fab 139 MotorDiff.reset(0.0f,0.0f);
rtlabor 0:15be70d21d7c 140 pi_w2zero.reset(0.0f);
pmic 7:b3c5116e9fab 141 pi_w.reset(0.0f);
pmic 8:3b98b6e1ead3 142
pmic 7:b3c5116e9fab 143 FilterACCx.reset(u2ax(3.3f*ax.read()));
pmic 7:b3c5116e9fab 144 FilterACCy.reset(u2ay(3.3f*ay.read()));
pmic 7:b3c5116e9fab 145 FilterGYRZ.reset(u2gz(3.3f*gz.read()));
pmic 8:3b98b6e1ead3 146
pmic 8:3b98b6e1ead3 147 Wobble.reset();
pmic 8:3b98b6e1ead3 148 // SysSecOrder.reset(u);
pmic 8:3b98b6e1ead3 149
pmic 8:3b98b6e1ead3 150 Wobble.printGPAmeasPara();
pmic 8:3b98b6e1ead3 151
pmic 7:b3c5116e9fab 152 // FilterANG.reset(atan2(-u2ax(3.3f*ax.read()), u2ay(3.3f*ay.read())) + PI/4.0f);
pmic 7:b3c5116e9fab 153 // FilterDiffANG.reset(0.0f);
pmic 8:3b98b6e1ead3 154
pmic 5:e443d5ad409f 155 // reset output
pmic 8:3b98b6e1ead3 156 out.write(u3k3_TO_1V(i2u(0.0f)));
pmic 8:3b98b6e1ead3 157
pmic 5:e443d5ad409f 158 // attach controller loop to timer interrupt
rtlabor 0:15be70d21d7c 159 ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = 400Hz;
pmic 7:b3c5116e9fab 160 Button.fall(&pressed); // attach key pressed function
pmic 7:b3c5116e9fab 161 Button.rise(&released); // attach key pressed function
rtlabor 0:15be70d21d7c 162 }
pmic 5:e443d5ad409f 163
pmic 8:3b98b6e1ead3 164 void updateControllers(void)
pmic 8:3b98b6e1ead3 165 {
pmic 9:4e6f3404d473 166
pmic 8:3b98b6e1ead3 167 counts = MotorEncoder; // counts in 1
pmic 8:3b98b6e1ead3 168 omega = MotorDiff(counts); // angular velocity motor
pmic 8:3b98b6e1ead3 169
pmic 8:3b98b6e1ead3 170 if(shouldBalance) {
pmic 9:4e6f3404d473 171 inpWobble = desCurrent;
pmic 8:3b98b6e1ead3 172 outWobble = omega;
pmic 8:3b98b6e1ead3 173 excWobble = Wobble(inpWobble, outWobble);
pmic 8:3b98b6e1ead3 174 }
pmic 9:4e6f3404d473 175 desCurrent = pi_w((n_soll + excWobble - omega)/0.217f);
pmic 9:4e6f3404d473 176 out.write(u3k3_TO_1V(i2u(desCurrent)));
pmic 9:4e6f3404d473 177
pmic 8:3b98b6e1ead3 178 /*
pmic 8:3b98b6e1ead3 179 u = Wobble(u, y) + Oexc;
pmic 8:3b98b6e1ead3 180 y = SysSecOrder(u);
pmic 9:4e6f3404d473 181 k += 1;
pmic 8:3b98b6e1ead3 182
pmic 9:4e6f3404d473 183 if(k == 11000) {
pmic 8:3b98b6e1ead3 184 Wobble.printGPAmeasTime();
pmic 8:3b98b6e1ead3 185 }
pmic 8:3b98b6e1ead3 186 */
pmic 9:4e6f3404d473 187
pmic 8:3b98b6e1ead3 188 /*
pmic 7:b3c5116e9fab 189 // read encoder data
pmic 7:b3c5116e9fab 190 short counts = MotorEncoder; // counts in 1
pmic 8:3b98b6e1ead3 191 float omega = MotorDiff(counts); // angular velocity motor
pmic 8:3b98b6e1ead3 192
pmic 7:b3c5116e9fab 193 // read imu data
pmic 7:b3c5116e9fab 194 float accx = u2ax(3.3f*ax.read());
pmic 7:b3c5116e9fab 195 float accy = u2ay(3.3f*ay.read());
pmic 7:b3c5116e9fab 196 float gyrz = u2gz(3.3f*gz.read());
pmic 8:3b98b6e1ead3 197
pmic 7:b3c5116e9fab 198 // perform complementary filter
pmic 7:b3c5116e9fab 199 float ang = atan2(-FilterACCx(accx), FilterACCy(accy)) + FilterGYRZ(gyrz) + PI/4.0f;
pmic 8:3b98b6e1ead3 200
pmic 7:b3c5116e9fab 201 // float angf = FilterANG(ang);
pmic 7:b3c5116e9fab 202 // float dangf = FilterDiffANG(ang);
pmic 8:3b98b6e1ead3 203
pmic 5:e443d5ad409f 204 // get current state of the cube
pmic 5:e443d5ad409f 205 float actualAngleDegree = ang*180.0f/PI;
pmic 5:e443d5ad409f 206 if(actualAngleDegree > -10.0f && actualAngleDegree < 10.0f){
pmic 6:7ea1a4bac3c2 207 doesStand = 1;
pmic 5:e443d5ad409f 208 }
pmic 5:e443d5ad409f 209 else{
pmic 6:7ea1a4bac3c2 210 doesStand = 0;
pmic 5:e443d5ad409f 211 }
pmic 8:3b98b6e1ead3 212
pmic 7:b3c5116e9fab 213 // update controllers
pmic 7:b3c5116e9fab 214 float desTorque = 0.0f;
pmic 8:3b98b6e1ead3 215 if(shouldBalance){
pmic 5:e443d5ad409f 216 // K matrix: -5.2142 -0.6247 // from Matlab
pmic 8:3b98b6e1ead3 217 desTorque = pi_w2zero(n_soll-omega)-(-5.2142f*ang-0.6247f*gyrz); // state space controller for balance, calc desired Torque
pmic 5:e443d5ad409f 218 }
pmic 5:e443d5ad409f 219 else{
pmic 8:3b98b6e1ead3 220 desTorque = pi_w(n_soll-omega); // state space controller for balance, calc desired Torque
pmic 7:b3c5116e9fab 221 }
pmic 8:3b98b6e1ead3 222 // convert Nm -> A and write to AOUT
pmic 8:3b98b6e1ead3 223 out.write(u3k3_TO_1V(i2u(desTorque/0.217f)));
pmic 8:3b98b6e1ead3 224
pmic 8:3b98b6e1ead3 225 //out.write(u3k3_TO_1V(i2u(pi_w(n_soll-omega)))); // test speed controller
rtlabor 1:2e118d67eeae 226 if(++k >= 199){
rtlabor 0:15be70d21d7c 227 k = 0;
pmic 7:b3c5116e9fab 228 pc.printf("phi=%3.2f omega=%3.2f \r\n", actualAngleDegree, omega);
altb 2:252a61a7e8f9 229 //pc.printf("ax=%3.2f ay=%3.2f gz=%3.2f \r\n",u2ax(3.3f*ax.read()),u2ay(3.3f*ay.read()),wz);
rtlabor 0:15be70d21d7c 230 }
pmic 8:3b98b6e1ead3 231 */
rtlabor 0:15be70d21d7c 232 }
pmic 5:e443d5ad409f 233
pmic 7:b3c5116e9fab 234 // Buttonhandling and statemachine
pmic 5:e443d5ad409f 235 // -----------------------------------------------------------------------------
pmic 7:b3c5116e9fab 236 // start timer as soon as Button is pressed
pmic 8:3b98b6e1ead3 237 void pressed()
pmic 8:3b98b6e1ead3 238 {
pmic 5:e443d5ad409f 239 t.start();
rtlabor 0:15be70d21d7c 240 }
pmic 5:e443d5ad409f 241
pmic 5:e443d5ad409f 242 // evaluating statemachine
pmic 8:3b98b6e1ead3 243 void released()
pmic 8:3b98b6e1ead3 244 {
pmic 8:3b98b6e1ead3 245
pmic 5:e443d5ad409f 246 // readout, stop and reset timer
pmic 7:b3c5116e9fab 247 float ButtonTime = t.read();
rtlabor 0:15be70d21d7c 248 t.stop();
pmic 5:e443d5ad409f 249 t.reset();
pmic 8:3b98b6e1ead3 250
pmic 6:7ea1a4bac3c2 251 // if the cube doesStand
pmic 8:3b98b6e1ead3 252 if(doesStand) {
pmic 8:3b98b6e1ead3 253 // in - or decrease speed
pmic 8:3b98b6e1ead3 254 if(ButtonTime < 2.0f) {
pmic 8:3b98b6e1ead3 255 // press Button long -> increase speed 5 rev/min
pmic 8:3b98b6e1ead3 256 if(ButtonTime > 0.5f) {
pmic 5:e443d5ad409f 257 n_soll -= 5.0f;
pmic 5:e443d5ad409f 258 }
pmic 7:b3c5116e9fab 259 // press Button short -> decrease speed 5 rev/min
pmic 8:3b98b6e1ead3 260 else {
pmic 5:e443d5ad409f 261 n_soll += 5.0f;
pmic 5:e443d5ad409f 262 }
pmic 5:e443d5ad409f 263 // constrain n_soll
pmic 5:e443d5ad409f 264 if(n_soll > v_max)
pmic 8:3b98b6e1ead3 265 n_soll = v_max;
pmic 5:e443d5ad409f 266 if(n_soll < -v_max)
pmic 5:e443d5ad409f 267 n_soll = -v_max;
rtlabor 0:15be70d21d7c 268 }
pmic 5:e443d5ad409f 269 // stop balancing
pmic 8:3b98b6e1ead3 270 else {
pmic 8:3b98b6e1ead3 271 n_soll = 0.0f;
pmic 5:e443d5ad409f 272 shouldBalance = 0;
pmic 5:e443d5ad409f 273 pi_w2zero.reset(0.0f);
pmic 5:e443d5ad409f 274 }
pmic 8:3b98b6e1ead3 275 } else {
pmic 7:b3c5116e9fab 276 if(ButtonTime > 2.0f)
pmic 8:3b98b6e1ead3 277 shouldBalance = 1;
pmic 8:3b98b6e1ead3 278 pi_w.reset(0.0f);
pmic 5:e443d5ad409f 279 }
rtlabor 0:15be70d21d7c 280 }