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

Dependencies:   mbed

Committer:
pmic
Date:
Mon Apr 09 15:10:33 2018 +0000
Revision:
21:55b11670959e
Parent:
10:b7f142952df9
gpa in float prescision

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 10:b7f142952df9 78 /*
pmic 10:b7f142952df9 79 // gpa measurement
pmic 8:3b98b6e1ead3 80 float fMin = 1.0f;
pmic 8:3b98b6e1ead3 81 float fMax = 1.0f/2.0f/Ts*0.9f;
pmic 9:4e6f3404d473 82 int NfexcDes = 100;
pmic 9:4e6f3404d473 83 float Aexc0 = 6.0f;
pmic 9:4e6f3404d473 84 float Aexc1 = 0.5f; //Aexc0/fMax;
pmic 9:4e6f3404d473 85 int NperMin = 3;
pmic 9:4e6f3404d473 86 float TmeasMin = 1.0;
pmic 8:3b98b6e1ead3 87 int NmeasMin = (int)ceil(TmeasMin/Ts);
pmic 9:4e6f3404d473 88 GPA Wobble(fMin, fMax, NfexcDes, NperMin, NmeasMin, Ts, Aexc0, Aexc1);
pmic 8:3b98b6e1ead3 89 float inpWobble = 0.0f;
pmic 8:3b98b6e1ead3 90 float outWobble = 0.0f;
pmic 8:3b98b6e1ead3 91 float excWobble = 0.0f;
pmic 8:3b98b6e1ead3 92 short counts = 0;
pmic 8:3b98b6e1ead3 93 float omega = 0.0f;
pmic 9:4e6f3404d473 94 float desCurrent = 0.0f;
pmic 10:b7f142952df9 95 */
pmic 9:4e6f3404d473 96
pmic 10:b7f142952df9 97 // simulation on nucleo
pmic 9:4e6f3404d473 98 float Oexc = 1.0f;
pmic 9:4e6f3404d473 99 float fMin = 0.7f;
pmic 9:4e6f3404d473 100 float fMax = 1.0f/2.0f/Ts*0.72f;
pmic 9:4e6f3404d473 101 int NfexcDes = 100;
pmic 9:4e6f3404d473 102 float Aexc0 = 1.0f;
pmic 9:4e6f3404d473 103 float Aexc1 = Aexc0/fMax;
pmic 9:4e6f3404d473 104 int NperMin = 1;
pmic 9:4e6f3404d473 105 float TmeasMin = 1.0;
pmic 9:4e6f3404d473 106 int NmeasMin = 1;//(int)ceil(TmeasMin/Ts);
pmic 21:55b11670959e 107 // GPA Wobble(fMin, fMax, NfexcDes, NperMin, NmeasMin, Ts, Aexc0, Aexc1);
pmic 21:55b11670959e 108 GPA Wobble(1.0f, 200.0f,50, 5, 100, Ts, 1.0f,1.0f); // init GPA, see references there
pmic 21:55b11670959e 109 // float w0 = 2.0f*pi*3.0f;
pmic 21:55b11670959e 110 // float D = 0.02f;
pmic 21:55b11670959e 111 float w0 = 2.0f*pi*5.0f;
pmic 21:55b11670959e 112 float D = 0.2f;
pmic 9:4e6f3404d473 113 IIR_filter SysSecOrder(w0, D, Ts, 10.0f);
pmic 9:4e6f3404d473 114 float u = Oexc;
pmic 9:4e6f3404d473 115 float y = 10.0f*u;
pmic 8:3b98b6e1ead3 116
pmic 7:b3c5116e9fab 117 // IIR_filter FilterANG(1.0f/(2.0f*PI*fg), Ts, 1.0f);
pmic 7:b3c5116e9fab 118 // IIR_filter FilterDiffANG(1.0f/(2.0f*PI*fg), Ts);
pmic 5:e443d5ad409f 119
pmic 6:7ea1a4bac3c2 120 // linear characteristics
pmic 4:47581778e863 121 LinearCharacteristics i2u(0.1067f,-15.0f); // full range, convert desired current (Amps) -> voltage 0..3.3V
rtlabor 0:15be70d21d7c 122 LinearCharacteristics u2n(312.5f,1.6f); // convert input voltage (0..3.3V) -> speed (1/min)
rtlabor 0:15be70d21d7c 123 LinearCharacteristics u2w(32.725,1.6f); // convert input voltage (0..3.3V) -> speed (rad/sec)
rtlabor 0:15be70d21d7c 124 LinearCharacteristics u2ax(14.67f,1.6378f); // convert input voltage (0..3.3V) -> acc_x m/s^2
rtlabor 0:15be70d21d7c 125 LinearCharacteristics u2ay(15.02f ,1.6673f); // convert input voltage (0..3.3V) -> acc_y m/s^2
rtlabor 0:15be70d21d7c 126 LinearCharacteristics u2gz(-4.652f,1.4949f); // convert input voltage (0..3.3V) -> w_x rad/s
rtlabor 0:15be70d21d7c 127 LinearCharacteristics u3k3_TO_1V(0.303030303f,0,3.3f,0.0f);// normalize output voltage (0..3.3)V -> (0..1) V
rtlabor 0:15be70d21d7c 128
pmic 5:e443d5ad409f 129 // user defined functions
rtlabor 0:15be70d21d7c 130 void updateControllers(void); // speed controller loop (via interrupt)
pmic 7:b3c5116e9fab 131 void pressed(void); // user Button pressed
pmic 7:b3c5116e9fab 132 void released(void); // user Button released
rtlabor 0:15be70d21d7c 133
pmic 6:7ea1a4bac3c2 134 // main program and control loop
pmic 5:e443d5ad409f 135 // -----------------------------------------------------------------------------
rtlabor 0:15be70d21d7c 136 int main()
rtlabor 0:15be70d21d7c 137 {
pmic 5:e443d5ad409f 138 // for serial comm.
pmic 8:3b98b6e1ead3 139 pc.baud(2000000);
pmic 8:3b98b6e1ead3 140
pmic 5:e443d5ad409f 141 // reset encoder, controller and filters
pmic 7:b3c5116e9fab 142 MotorEncoder.reset();
pmic 7:b3c5116e9fab 143 MotorDiff.reset(0.0f,0.0f);
rtlabor 0:15be70d21d7c 144 pi_w2zero.reset(0.0f);
pmic 7:b3c5116e9fab 145 pi_w.reset(0.0f);
pmic 8:3b98b6e1ead3 146
pmic 7:b3c5116e9fab 147 FilterACCx.reset(u2ax(3.3f*ax.read()));
pmic 7:b3c5116e9fab 148 FilterACCy.reset(u2ay(3.3f*ay.read()));
pmic 7:b3c5116e9fab 149 FilterGYRZ.reset(u2gz(3.3f*gz.read()));
pmic 8:3b98b6e1ead3 150
pmic 8:3b98b6e1ead3 151 Wobble.reset();
pmic 21:55b11670959e 152 SysSecOrder.reset(u);
pmic 8:3b98b6e1ead3 153
pmic 8:3b98b6e1ead3 154 Wobble.printGPAmeasPara();
pmic 8:3b98b6e1ead3 155
pmic 7:b3c5116e9fab 156 // FilterANG.reset(atan2(-u2ax(3.3f*ax.read()), u2ay(3.3f*ay.read())) + PI/4.0f);
pmic 7:b3c5116e9fab 157 // FilterDiffANG.reset(0.0f);
pmic 8:3b98b6e1ead3 158
pmic 5:e443d5ad409f 159 // reset output
pmic 8:3b98b6e1ead3 160 out.write(u3k3_TO_1V(i2u(0.0f)));
pmic 8:3b98b6e1ead3 161
pmic 5:e443d5ad409f 162 // attach controller loop to timer interrupt
rtlabor 0:15be70d21d7c 163 ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = 400Hz;
pmic 7:b3c5116e9fab 164 Button.fall(&pressed); // attach key pressed function
pmic 7:b3c5116e9fab 165 Button.rise(&released); // attach key pressed function
rtlabor 0:15be70d21d7c 166 }
pmic 5:e443d5ad409f 167
pmic 8:3b98b6e1ead3 168 void updateControllers(void)
pmic 8:3b98b6e1ead3 169 {
pmic 10:b7f142952df9 170 /*
pmic 8:3b98b6e1ead3 171 counts = MotorEncoder; // counts in 1
pmic 8:3b98b6e1ead3 172 omega = MotorDiff(counts); // angular velocity motor
pmic 8:3b98b6e1ead3 173
pmic 8:3b98b6e1ead3 174 if(shouldBalance) {
pmic 9:4e6f3404d473 175 inpWobble = desCurrent;
pmic 8:3b98b6e1ead3 176 outWobble = omega;
pmic 8:3b98b6e1ead3 177 excWobble = Wobble(inpWobble, outWobble);
pmic 8:3b98b6e1ead3 178 }
pmic 9:4e6f3404d473 179 desCurrent = pi_w((n_soll + excWobble - omega)/0.217f);
pmic 9:4e6f3404d473 180 out.write(u3k3_TO_1V(i2u(desCurrent)));
pmic 10:b7f142952df9 181 */
pmic 9:4e6f3404d473 182
pmic 8:3b98b6e1ead3 183 u = Wobble(u, y) + Oexc;
pmic 8:3b98b6e1ead3 184 y = SysSecOrder(u);
pmic 9:4e6f3404d473 185 k += 1;
pmic 8:3b98b6e1ead3 186
pmic 21:55b11670959e 187 if(k == 22000) {
pmic 8:3b98b6e1ead3 188 Wobble.printGPAmeasTime();
pmic 8:3b98b6e1ead3 189 }
pmic 9:4e6f3404d473 190
pmic 8:3b98b6e1ead3 191 /*
pmic 7:b3c5116e9fab 192 // read encoder data
pmic 7:b3c5116e9fab 193 short counts = MotorEncoder; // counts in 1
pmic 8:3b98b6e1ead3 194 float omega = MotorDiff(counts); // angular velocity motor
pmic 8:3b98b6e1ead3 195
pmic 7:b3c5116e9fab 196 // read imu data
pmic 7:b3c5116e9fab 197 float accx = u2ax(3.3f*ax.read());
pmic 7:b3c5116e9fab 198 float accy = u2ay(3.3f*ay.read());
pmic 7:b3c5116e9fab 199 float gyrz = u2gz(3.3f*gz.read());
pmic 8:3b98b6e1ead3 200
pmic 7:b3c5116e9fab 201 // perform complementary filter
pmic 7:b3c5116e9fab 202 float ang = atan2(-FilterACCx(accx), FilterACCy(accy)) + FilterGYRZ(gyrz) + PI/4.0f;
pmic 8:3b98b6e1ead3 203
pmic 7:b3c5116e9fab 204 // float angf = FilterANG(ang);
pmic 7:b3c5116e9fab 205 // float dangf = FilterDiffANG(ang);
pmic 8:3b98b6e1ead3 206
pmic 5:e443d5ad409f 207 // get current state of the cube
pmic 5:e443d5ad409f 208 float actualAngleDegree = ang*180.0f/PI;
pmic 5:e443d5ad409f 209 if(actualAngleDegree > -10.0f && actualAngleDegree < 10.0f){
pmic 6:7ea1a4bac3c2 210 doesStand = 1;
pmic 5:e443d5ad409f 211 }
pmic 5:e443d5ad409f 212 else{
pmic 6:7ea1a4bac3c2 213 doesStand = 0;
pmic 5:e443d5ad409f 214 }
pmic 8:3b98b6e1ead3 215
pmic 7:b3c5116e9fab 216 // update controllers
pmic 7:b3c5116e9fab 217 float desTorque = 0.0f;
pmic 8:3b98b6e1ead3 218 if(shouldBalance){
pmic 5:e443d5ad409f 219 // K matrix: -5.2142 -0.6247 // from Matlab
pmic 8:3b98b6e1ead3 220 desTorque = pi_w2zero(n_soll-omega)-(-5.2142f*ang-0.6247f*gyrz); // state space controller for balance, calc desired Torque
pmic 5:e443d5ad409f 221 }
pmic 5:e443d5ad409f 222 else{
pmic 8:3b98b6e1ead3 223 desTorque = pi_w(n_soll-omega); // state space controller for balance, calc desired Torque
pmic 7:b3c5116e9fab 224 }
pmic 8:3b98b6e1ead3 225 // convert Nm -> A and write to AOUT
pmic 8:3b98b6e1ead3 226 out.write(u3k3_TO_1V(i2u(desTorque/0.217f)));
pmic 8:3b98b6e1ead3 227
pmic 8:3b98b6e1ead3 228 //out.write(u3k3_TO_1V(i2u(pi_w(n_soll-omega)))); // test speed controller
rtlabor 1:2e118d67eeae 229 if(++k >= 199){
rtlabor 0:15be70d21d7c 230 k = 0;
pmic 7:b3c5116e9fab 231 pc.printf("phi=%3.2f omega=%3.2f \r\n", actualAngleDegree, omega);
altb 2:252a61a7e8f9 232 //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 233 }
pmic 8:3b98b6e1ead3 234 */
rtlabor 0:15be70d21d7c 235 }
pmic 5:e443d5ad409f 236
pmic 7:b3c5116e9fab 237 // Buttonhandling and statemachine
pmic 5:e443d5ad409f 238 // -----------------------------------------------------------------------------
pmic 7:b3c5116e9fab 239 // start timer as soon as Button is pressed
pmic 8:3b98b6e1ead3 240 void pressed()
pmic 8:3b98b6e1ead3 241 {
pmic 5:e443d5ad409f 242 t.start();
rtlabor 0:15be70d21d7c 243 }
pmic 5:e443d5ad409f 244
pmic 5:e443d5ad409f 245 // evaluating statemachine
pmic 8:3b98b6e1ead3 246 void released()
pmic 8:3b98b6e1ead3 247 {
pmic 8:3b98b6e1ead3 248
pmic 5:e443d5ad409f 249 // readout, stop and reset timer
pmic 7:b3c5116e9fab 250 float ButtonTime = t.read();
rtlabor 0:15be70d21d7c 251 t.stop();
pmic 5:e443d5ad409f 252 t.reset();
pmic 8:3b98b6e1ead3 253
pmic 6:7ea1a4bac3c2 254 // if the cube doesStand
pmic 8:3b98b6e1ead3 255 if(doesStand) {
pmic 8:3b98b6e1ead3 256 // in - or decrease speed
pmic 8:3b98b6e1ead3 257 if(ButtonTime < 2.0f) {
pmic 8:3b98b6e1ead3 258 // press Button long -> increase speed 5 rev/min
pmic 8:3b98b6e1ead3 259 if(ButtonTime > 0.5f) {
pmic 5:e443d5ad409f 260 n_soll -= 5.0f;
pmic 5:e443d5ad409f 261 }
pmic 7:b3c5116e9fab 262 // press Button short -> decrease speed 5 rev/min
pmic 8:3b98b6e1ead3 263 else {
pmic 5:e443d5ad409f 264 n_soll += 5.0f;
pmic 5:e443d5ad409f 265 }
pmic 5:e443d5ad409f 266 // constrain n_soll
pmic 5:e443d5ad409f 267 if(n_soll > v_max)
pmic 8:3b98b6e1ead3 268 n_soll = v_max;
pmic 5:e443d5ad409f 269 if(n_soll < -v_max)
pmic 5:e443d5ad409f 270 n_soll = -v_max;
rtlabor 0:15be70d21d7c 271 }
pmic 5:e443d5ad409f 272 // stop balancing
pmic 8:3b98b6e1ead3 273 else {
pmic 8:3b98b6e1ead3 274 n_soll = 0.0f;
pmic 5:e443d5ad409f 275 shouldBalance = 0;
pmic 5:e443d5ad409f 276 pi_w2zero.reset(0.0f);
pmic 5:e443d5ad409f 277 }
pmic 8:3b98b6e1ead3 278 } else {
pmic 7:b3c5116e9fab 279 if(ButtonTime > 2.0f)
pmic 8:3b98b6e1ead3 280 shouldBalance = 1;
pmic 8:3b98b6e1ead3 281 pi_w.reset(0.0f);
pmic 5:e443d5ad409f 282 }
rtlabor 0:15be70d21d7c 283 }