gpa in double prescision

Dependencies:   mbed

Committer:
pmic
Date:
Fri Mar 02 17:11:34 2018 +0000
Revision:
7:b3c5116e9fab
Parent:
6:7ea1a4bac3c2
Child:
8:3b98b6e1ead3
additional filter implementation, corrections and get rid of some spaghetti code

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 5:e443d5ad409f 3
rtlabor 0:15be70d21d7c 4 #define PI 3.1415927f
pmic 5:e443d5ad409f 5
rtlabor 0:15be70d21d7c 6 #include "EncoderCounter.h"
rtlabor 0:15be70d21d7c 7 #include "DiffCounter.h"
rtlabor 0:15be70d21d7c 8 #include "PI_Cntrl.h"
rtlabor 0:15be70d21d7c 9 #include "IIR_filter.h"
rtlabor 0:15be70d21d7c 10 #include "LinearCharacteristics.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 5:e443d5ad409f 17
rtlabor 0:15be70d21d7c 18 hardware Connections:
rtlabor 0:15be70d21d7c 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 .. ..
altb 3:a951d699878b 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 .. ..
altb 3:a951d699878b 37 .. ..
altb 3:a951d699878b 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 5:e443d5ad409f 58 float n_soll = 0.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 7:b3c5116e9fab 67 // set up encoder
pmic 7:b3c5116e9fab 68 EncoderCounter MotorEncoder(PB_6, PB_7); // initialize counter on PB_6 and PB_7
pmic 7:b3c5116e9fab 69 DiffCounter MotorDiff(0.01, 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 7:b3c5116e9fab 72 PI_Cntrl pi_w(1.0f, 0.1f, 2.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 7:b3c5116e9fab 78 // IIR_filter FilterANG(1.0f/(2.0f*PI*fg), Ts, 1.0f);
pmic 7:b3c5116e9fab 79 // IIR_filter FilterDiffANG(1.0f/(2.0f*PI*fg), Ts);
pmic 5:e443d5ad409f 80
pmic 6:7ea1a4bac3c2 81 // linear characteristics
pmic 4:47581778e863 82 LinearCharacteristics i2u(0.1067f,-15.0f); // full range, convert desired current (Amps) -> voltage 0..3.3V
rtlabor 0:15be70d21d7c 83 LinearCharacteristics u2n(312.5f,1.6f); // convert input voltage (0..3.3V) -> speed (1/min)
rtlabor 0:15be70d21d7c 84 LinearCharacteristics u2w(32.725,1.6f); // convert input voltage (0..3.3V) -> speed (rad/sec)
rtlabor 0:15be70d21d7c 85 LinearCharacteristics u2ax(14.67f,1.6378f); // convert input voltage (0..3.3V) -> acc_x m/s^2
rtlabor 0:15be70d21d7c 86 LinearCharacteristics u2ay(15.02f ,1.6673f); // convert input voltage (0..3.3V) -> acc_y m/s^2
rtlabor 0:15be70d21d7c 87 LinearCharacteristics u2gz(-4.652f,1.4949f); // convert input voltage (0..3.3V) -> w_x rad/s
rtlabor 0:15be70d21d7c 88 LinearCharacteristics u3k3_TO_1V(0.303030303f,0,3.3f,0.0f);// normalize output voltage (0..3.3)V -> (0..1) V
rtlabor 0:15be70d21d7c 89
pmic 5:e443d5ad409f 90 // user defined functions
rtlabor 0:15be70d21d7c 91 void updateControllers(void); // speed controller loop (via interrupt)
pmic 7:b3c5116e9fab 92 void pressed(void); // user Button pressed
pmic 7:b3c5116e9fab 93 void released(void); // user Button released
rtlabor 0:15be70d21d7c 94
pmic 6:7ea1a4bac3c2 95 // main program and control loop
pmic 5:e443d5ad409f 96 // -----------------------------------------------------------------------------
rtlabor 0:15be70d21d7c 97 int main()
rtlabor 0:15be70d21d7c 98 {
pmic 5:e443d5ad409f 99 // for serial comm.
pmic 5:e443d5ad409f 100 pc.baud(2000000);
pmic 5:e443d5ad409f 101
pmic 5:e443d5ad409f 102 // reset encoder, controller and filters
pmic 7:b3c5116e9fab 103 MotorEncoder.reset();
pmic 7:b3c5116e9fab 104 MotorDiff.reset(0.0f,0.0f);
rtlabor 0:15be70d21d7c 105 pi_w2zero.reset(0.0f);
pmic 7:b3c5116e9fab 106 pi_w.reset(0.0f);
pmic 7:b3c5116e9fab 107
pmic 7:b3c5116e9fab 108 FilterACCx.reset(u2ax(3.3f*ax.read()));
pmic 7:b3c5116e9fab 109 FilterACCy.reset(u2ay(3.3f*ay.read()));
pmic 7:b3c5116e9fab 110 FilterGYRZ.reset(u2gz(3.3f*gz.read()));
pmic 7:b3c5116e9fab 111
pmic 7:b3c5116e9fab 112 // FilterANG.reset(atan2(-u2ax(3.3f*ax.read()), u2ay(3.3f*ay.read())) + PI/4.0f);
pmic 7:b3c5116e9fab 113 // FilterDiffANG.reset(0.0f);
pmic 5:e443d5ad409f 114
pmic 5:e443d5ad409f 115 // reset output
pmic 5:e443d5ad409f 116 out.write(u3k3_TO_1V(i2u(0.0f)));
pmic 5:e443d5ad409f 117
pmic 5:e443d5ad409f 118 // attach controller loop to timer interrupt
rtlabor 0:15be70d21d7c 119 ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = 400Hz;
pmic 7:b3c5116e9fab 120 Button.fall(&pressed); // attach key pressed function
pmic 7:b3c5116e9fab 121 Button.rise(&released); // attach key pressed function
rtlabor 0:15be70d21d7c 122 }
pmic 5:e443d5ad409f 123
rtlabor 0:15be70d21d7c 124 void updateControllers(void){
pmic 5:e443d5ad409f 125
pmic 7:b3c5116e9fab 126 // read encoder data
pmic 7:b3c5116e9fab 127 short counts = MotorEncoder; // counts in 1
pmic 7:b3c5116e9fab 128 float omega = MotorDiff(counts); // angular velofity motor
pmic 7:b3c5116e9fab 129
pmic 7:b3c5116e9fab 130 // read imu data
pmic 7:b3c5116e9fab 131 float accx = u2ax(3.3f*ax.read());
pmic 7:b3c5116e9fab 132 float accy = u2ay(3.3f*ay.read());
pmic 7:b3c5116e9fab 133 float gyrz = u2gz(3.3f*gz.read());
pmic 7:b3c5116e9fab 134
pmic 7:b3c5116e9fab 135 // perform complementary filter
pmic 7:b3c5116e9fab 136 float ang = atan2(-FilterACCx(accx), FilterACCy(accy)) + FilterGYRZ(gyrz) + PI/4.0f;
pmic 7:b3c5116e9fab 137
pmic 7:b3c5116e9fab 138 // float angf = FilterANG(ang);
pmic 7:b3c5116e9fab 139 // float dangf = FilterDiffANG(ang);
pmic 5:e443d5ad409f 140
pmic 5:e443d5ad409f 141 // get current state of the cube
pmic 5:e443d5ad409f 142 float actualAngleDegree = ang*180.0f/PI;
pmic 5:e443d5ad409f 143 if(actualAngleDegree > -10.0f && actualAngleDegree < 10.0f){
pmic 6:7ea1a4bac3c2 144 doesStand = 1;
pmic 5:e443d5ad409f 145 }
pmic 5:e443d5ad409f 146 else{
pmic 6:7ea1a4bac3c2 147 doesStand = 0;
pmic 5:e443d5ad409f 148 }
pmic 5:e443d5ad409f 149
pmic 7:b3c5116e9fab 150 // update controllers
pmic 7:b3c5116e9fab 151 float desTorque = 0.0f;
pmic 5:e443d5ad409f 152 if(shouldBalance){
pmic 5:e443d5ad409f 153 // K matrix: -5.2142 -0.6247 // from Matlab
pmic 7:b3c5116e9fab 154 desTorque = pi_w2zero(n_soll-omega)-(-5.2142f*ang-0.6247f*gyrz); // state space controller for balance, calc desired Torque
pmic 5:e443d5ad409f 155 }
pmic 5:e443d5ad409f 156 else{
pmic 7:b3c5116e9fab 157 desTorque = pi_w(n_soll-omega); // state space controller for balance, calc desired Torque
pmic 7:b3c5116e9fab 158 }
pmic 7:b3c5116e9fab 159 // convert Nm -> A and write to AOUT
pmic 7:b3c5116e9fab 160 out.write(u3k3_TO_1V(i2u(desTorque/0.217f)));
pmic 5:e443d5ad409f 161
pmic 7:b3c5116e9fab 162 //out.write(u3k3_TO_1V(i2u(pi_w(n_soll-omega)))); // test speed controller
rtlabor 1:2e118d67eeae 163 if(++k >= 199){
rtlabor 0:15be70d21d7c 164 k = 0;
pmic 7:b3c5116e9fab 165 pc.printf("phi=%3.2f omega=%3.2f \r\n", actualAngleDegree, omega);
altb 2:252a61a7e8f9 166 //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 167 }
rtlabor 0:15be70d21d7c 168 }
pmic 5:e443d5ad409f 169
pmic 7:b3c5116e9fab 170 // Buttonhandling and statemachine
pmic 5:e443d5ad409f 171 // -----------------------------------------------------------------------------
pmic 7:b3c5116e9fab 172 // start timer as soon as Button is pressed
pmic 5:e443d5ad409f 173 void pressed(){
pmic 5:e443d5ad409f 174 t.start();
rtlabor 0:15be70d21d7c 175 }
pmic 5:e443d5ad409f 176
pmic 5:e443d5ad409f 177 // evaluating statemachine
pmic 5:e443d5ad409f 178 void released(){
pmic 5:e443d5ad409f 179
pmic 5:e443d5ad409f 180 // readout, stop and reset timer
pmic 7:b3c5116e9fab 181 float ButtonTime = t.read();
rtlabor 0:15be70d21d7c 182 t.stop();
pmic 5:e443d5ad409f 183 t.reset();
pmic 5:e443d5ad409f 184
pmic 6:7ea1a4bac3c2 185 // if the cube doesStand
pmic 6:7ea1a4bac3c2 186 if(doesStand)
rtlabor 0:15be70d21d7c 187 {
pmic 5:e443d5ad409f 188 // in - or decrease speed
pmic 7:b3c5116e9fab 189 if(ButtonTime < 2.0f){
pmic 7:b3c5116e9fab 190 // press Button long -> increase speed 5 rev/min
pmic 7:b3c5116e9fab 191 if(ButtonTime > 0.5f){
pmic 5:e443d5ad409f 192 n_soll -= 5.0f;
pmic 5:e443d5ad409f 193 }
pmic 7:b3c5116e9fab 194 // press Button short -> decrease speed 5 rev/min
pmic 5:e443d5ad409f 195 else{
pmic 5:e443d5ad409f 196 n_soll += 5.0f;
pmic 5:e443d5ad409f 197 }
pmic 5:e443d5ad409f 198 // constrain n_soll
pmic 5:e443d5ad409f 199 if(n_soll > v_max)
pmic 5:e443d5ad409f 200 n_soll = v_max;
pmic 5:e443d5ad409f 201 if(n_soll < -v_max)
pmic 5:e443d5ad409f 202 n_soll = -v_max;
rtlabor 0:15be70d21d7c 203 }
pmic 5:e443d5ad409f 204 // stop balancing
pmic 5:e443d5ad409f 205 else{
pmic 5:e443d5ad409f 206 n_soll = 0.0f;
pmic 5:e443d5ad409f 207 shouldBalance = 0;
pmic 5:e443d5ad409f 208 pi_w2zero.reset(0.0f);
pmic 5:e443d5ad409f 209 }
pmic 5:e443d5ad409f 210 }
pmic 5:e443d5ad409f 211 else{
pmic 7:b3c5116e9fab 212 if(ButtonTime > 2.0f)
pmic 5:e443d5ad409f 213 shouldBalance = 1;
pmic 7:b3c5116e9fab 214 pi_w.reset(0.0f);
pmic 5:e443d5ad409f 215 }
rtlabor 0:15be70d21d7c 216 }