P-controller geordend

Dependencies:   Encoder HIDScope MODSERIAL mbed

Committer:
Annelotte
Date:
Thu Nov 02 23:36:43 2017 +0000
Revision:
17:1246d6b0c5d0
Parent:
16:0a0b1c3be4d0
Child:
18:b42a884bca02
Inclusief RKI en compilen is mogelijk. (Annelotte)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Miriam 3:f755b4d41aa8 1 //libaries
Miriam 0:2a99f692f683 2 #include "mbed.h"
Miriam 0:2a99f692f683 3 #include "HIDScope.h"
Miriam 0:2a99f692f683 4 #include "encoder.h"
Miriam 0:2a99f692f683 5 #include "MODSERIAL.h"
Miriam 0:2a99f692f683 6
Miriam 0:2a99f692f683 7
Miriam 3:f755b4d41aa8 8 // globale variables
Miriam 3:f755b4d41aa8 9 Ticker AInTicker; //We make a ticker named AIn (use for HIDScope)
Miriam 0:2a99f692f683 10
Miriam 3:f755b4d41aa8 11 Ticker Treecko; //We make a awesome ticker for our control system
Miriam 3:f755b4d41aa8 12 AnalogIn potMeter2(A1); //Analoge input of potmeter 2 (will be use for te reference position)
Miriam 3:f755b4d41aa8 13 PwmOut M1E(D6); //Biorobotics Motor 1 PWM control of the speed
Miriam 3:f755b4d41aa8 14 DigitalOut M1D(D7); //Biorobotics Motor 1 diraction control
Miriam 0:2a99f692f683 15
Annelotte 15:a5849f3a60fc 16 double pi = 3.14159265359;
Annelotte 15:a5849f3a60fc 17 double q1 = (pi/2); //Reference position hoek 1 in radiance
Annelotte 15:a5849f3a60fc 18 double q2 = -(pi/2); //Reference position hoek 2 in radiance
Annelotte 15:a5849f3a60fc 19 const double L1 = 0.30; //Length arm 1 in mm
Annelotte 15:a5849f3a60fc 20 const double L2 = 0.38; //Length arm 2 in mm
Annelotte 15:a5849f3a60fc 21 double B1 = 1; //Friction constant motor 1
Annelotte 15:a5849f3a60fc 22 double B2 = 1; //Friction constant motor 2
Annelotte 15:a5849f3a60fc 23 double K = 1; //Spring constant movement from end-effector position to setpoint position
Annelotte 15:a5849f3a60fc 24 double Tijd = 1; //Timestep value
Annelotte 15:a5849f3a60fc 25 double Rsx = 0.38; //Reference x-component of the setpoint radius
Annelotte 15:a5849f3a60fc 26 double Rsy = 0.30; //Reference y-component of the setpoint radius
Annelotte 17:1246d6b0c5d0 27 double refP = 0; //Reference position motor 1
Annelotte 17:1246d6b0c5d0 28 double refP2 = 0.5*pi; //Reference position motor 2
Annelotte 15:a5849f3a60fc 29 double Rex = cos(q1)*L1 - sin(q2)*L2; //The x-component of the end-effector radius
Annelotte 15:a5849f3a60fc 30 double Rey = sin(q1)*L1 + cos(q2)*L2; //The y-component of the end-effector radius
Annelotte 15:a5849f3a60fc 31 double R1x = 0; //The x-component of the joint 1 radius
Annelotte 15:a5849f3a60fc 32 double R1y = 0; //The y-component of the joint 1 radius
Annelotte 15:a5849f3a60fc 33 double R2x = cos(q1)*L1; //The x-component of the joint 2 radius
Annelotte 15:a5849f3a60fc 34 double R2y = sin(q1)*L1; //The y-component of the joint 1 radius
Annelotte 15:a5849f3a60fc 35 double Fx = 0;
Annelotte 15:a5849f3a60fc 36 double Fy = 0;
Annelotte 15:a5849f3a60fc 37 double Tor1 = 0;
Annelotte 15:a5849f3a60fc 38 double Tor2 = 0;
Annelotte 16:0a0b1c3be4d0 39 double w1= 0;
Annelotte 16:0a0b1c3be4d0 40 double w2= 0;
Annelotte 15:a5849f3a60fc 41
Miriam 0:2a99f692f683 42 Encoder motor1(D13,D12,true);
Miriam 0:2a99f692f683 43 MODSERIAL pc(USBTX,USBRX);
Miriam 0:2a99f692f683 44
Miriam 3:f755b4d41aa8 45 float PwmPeriod = 1.0/5000.0; //set up of PWM periode (5000 Hz, want 5000 periodes in 1 seconde)
Gerber 14:5534b8282a06 46 const float Ts = 0.1; // tickettijd/ sample time
Miriam 2:b504e35af662 47 float e_prev = 0;
Miriam 2:b504e35af662 48 float e_int = 0;
Miriam 0:2a99f692f683 49
paulineoonk 7:05495acc08b0 50 //tweede motor
Annelotte 17:1246d6b0c5d0 51 AnalogIn potMeter1(A2);
paulineoonk 7:05495acc08b0 52 PwmOut M2E(D5);
paulineoonk 7:05495acc08b0 53 DigitalOut M2D(D4);
paulineoonk 7:05495acc08b0 54 Encoder motor2(D9,D8,true);
paulineoonk 7:05495acc08b0 55 Ticker DubbelTreecko;
paulineoonk 7:05495acc08b0 56
paulineoonk 12:e125b9fa77b9 57 //motors
paulineoonk 12:e125b9fa77b9 58 //float Huidigepositie2;
paulineoonk 12:e125b9fa77b9 59 //float Huidigepositie;
paulineoonk 12:e125b9fa77b9 60
paulineoonk 7:05495acc08b0 61 float PwmPeriod2 = 1.0/5000.0; //set up of PWM periode (5000 Hz, want 5000 periodes in 1 seconde)
paulineoonk 7:05495acc08b0 62 float e_prev2 = 0;
paulineoonk 7:05495acc08b0 63 float e_int2 = 0;
paulineoonk 7:05495acc08b0 64
Annelotte 15:a5849f3a60fc 65 void RKI()
Miriam 0:2a99f692f683 66 {
Annelotte 15:a5849f3a60fc 67 Rex = cos(q1)*L1 - sin(q2)*L2;
Annelotte 15:a5849f3a60fc 68 Rey = sin(q1)*L1 + cos(q2)*L2;
Annelotte 15:a5849f3a60fc 69 R2x = cos(q1)*L1;
Annelotte 15:a5849f3a60fc 70 R2y = sin(q1)*L1;
Annelotte 15:a5849f3a60fc 71 Fx = (Rsx-Rex)*K;
Annelotte 15:a5849f3a60fc 72 Fy = (Rsy-Rey)*K;
Annelotte 15:a5849f3a60fc 73 Tor1 = (Rex-R1x)*Fy + (R1y-Rey)*Fx;
Annelotte 15:a5849f3a60fc 74 Tor2 = (Rex-R2x)*Fy + (R2y-Rey)*Fx;
Annelotte 15:a5849f3a60fc 75 w1 = Tor1/B1;
Annelotte 15:a5849f3a60fc 76 w2 = Tor2/B2;
Annelotte 15:a5849f3a60fc 77 q1 = q1 + w1*Tijd;
Annelotte 15:a5849f3a60fc 78 q2 = q2 + w2*Tijd;
Annelotte 15:a5849f3a60fc 79
Miriam 0:2a99f692f683 80 int maxwaarde = 4096; // = 64x64
Annelotte 15:a5849f3a60fc 81 refP = (((0.5*pi) - q1)/(2*pi))*maxwaarde;
Annelotte 15:a5849f3a60fc 82 refP2 = (((0.5*pi) + q1 - q2)/(2*pi))*maxwaarde; //Get reference positions
Miriam 0:2a99f692f683 83 }
paulineoonk 7:05495acc08b0 84
Annelotte 15:a5849f3a60fc 85 void SetpointRobot()
Annelotte 15:a5849f3a60fc 86 {
Annelotte 17:1246d6b0c5d0 87 double Potmeterwaarde2 = potMeter2.read();
Annelotte 17:1246d6b0c5d0 88 double Potmeterwaarde1 = potMeter1.read();
Annelotte 17:1246d6b0c5d0 89
Annelotte 15:a5849f3a60fc 90 if (Potmeterwaarde2>0.6) {
Annelotte 15:a5849f3a60fc 91 Rsx += 0.001; //het gaat telkens 1 mm verder wanneer de potmeter boven de 0.6 staat
Annelotte 15:a5849f3a60fc 92 }
Annelotte 15:a5849f3a60fc 93 else if (Potmeterwaarde2<0.4) {
Annelotte 15:a5849f3a60fc 94 Rsx -= 0.001; //het gaat telkens 1 mm terug wanneer de potmeter onder de 0.4 staat
Annelotte 15:a5849f3a60fc 95 }
Annelotte 15:a5849f3a60fc 96 else { //de x-waarde van de setpoint verandert niet
Annelotte 15:a5849f3a60fc 97 }
Annelotte 15:a5849f3a60fc 98
Annelotte 15:a5849f3a60fc 99 if (Potmeterwaarde1>0.6) { //het gaat telkens 1 mm verder wanneer de potmeter boven de 0.6 staat
Annelotte 15:a5849f3a60fc 100 Rsy += 0.001;
Annelotte 15:a5849f3a60fc 101 }
Annelotte 15:a5849f3a60fc 102 else if (Potmeterwaarde1<0.4) { //het gaat telkens 1 mm terug wanneer de potmeter onder de 0.4
Annelotte 15:a5849f3a60fc 103 Rsy -= 0.001;
Annelotte 15:a5849f3a60fc 104 }
Annelotte 15:a5849f3a60fc 105 else { //de y-waarde van de setpoint verandert niet
Annelotte 15:a5849f3a60fc 106 }
paulineoonk 7:05495acc08b0 107 }
Miriam 0:2a99f692f683 108
Miriam 2:b504e35af662 109 float FeedBackControl(float error, float &e_prev, float &e_int) // schaalt de snelheid naar de snelheid zodat onze chip het begrijpt (is nog niet in werking)
Miriam 0:2a99f692f683 110 {
paulineoonk 12:e125b9fa77b9 111 float kp = 0.001; // kind of scaled.
Miriam 2:b504e35af662 112 float Proportional= kp*error;
Miriam 2:b504e35af662 113
Miriam 6:083bd713670b 114 float kd = 0.0004; // kind of scaled.
Miriam 2:b504e35af662 115 float VelocityError = (error - e_prev)/Ts;
Miriam 2:b504e35af662 116 float Derivative = kd*VelocityError;
Miriam 2:b504e35af662 117 e_prev = error;
Miriam 2:b504e35af662 118
Gerber 14:5534b8282a06 119 float ki = 0.0005; // kind of scaled.
Miriam 2:b504e35af662 120 e_int = e_int+Ts*error;
Miriam 2:b504e35af662 121 float Integrator = ki*e_int;
Miriam 2:b504e35af662 122
Miriam 2:b504e35af662 123
Miriam 2:b504e35af662 124 float motorValue = Proportional + Integrator + Derivative;
Miriam 0:2a99f692f683 125 return motorValue;
Miriam 0:2a99f692f683 126 }
Miriam 0:2a99f692f683 127
paulineoonk 7:05495acc08b0 128 float FeedBackControl2(float error2, float &e_prev2, float &e_int2) // schaalt de snelheid naar de snelheid zodat onze chip het begrijpt (is nog niet in werking)
paulineoonk 7:05495acc08b0 129 {
Gerber 13:eaaeb41e22d2 130 float kp2 = 0.001; // kind of scaled.
paulineoonk 7:05495acc08b0 131 float Proportional2= kp2*error2;
paulineoonk 7:05495acc08b0 132
Gerber 13:eaaeb41e22d2 133 float kd2 = 0.001; // kind of scaled.
paulineoonk 7:05495acc08b0 134 float VelocityError2 = (error2 - e_prev2)/Ts;
paulineoonk 7:05495acc08b0 135 float Derivative2 = kd2*VelocityError2;
paulineoonk 7:05495acc08b0 136 e_prev2 = error2;
paulineoonk 7:05495acc08b0 137
Gerber 14:5534b8282a06 138 float ki2 = 0.005; // kind of scaled.
paulineoonk 7:05495acc08b0 139 e_int2 = e_int2+Ts*error2;
paulineoonk 7:05495acc08b0 140 float Integrator2 = ki2*e_int2;
paulineoonk 7:05495acc08b0 141
paulineoonk 7:05495acc08b0 142
paulineoonk 7:05495acc08b0 143 float motorValue2 = Proportional2 + Integrator2 + Derivative2;
paulineoonk 7:05495acc08b0 144 return motorValue2;
paulineoonk 7:05495acc08b0 145 }
paulineoonk 7:05495acc08b0 146
paulineoonk 7:05495acc08b0 147
Miriam 0:2a99f692f683 148 void SetMotor1(float motorValue)
Miriam 0:2a99f692f683 149 {
Miriam 1:609671b1c96c 150 if (motorValue >= 0)
Miriam 0:2a99f692f683 151 {
Miriam 0:2a99f692f683 152 M1D = 0;
Miriam 0:2a99f692f683 153 }
Miriam 0:2a99f692f683 154 else
Miriam 0:2a99f692f683 155 {
Miriam 0:2a99f692f683 156 M1D = 1;
Miriam 0:2a99f692f683 157 }
Miriam 0:2a99f692f683 158
Miriam 0:2a99f692f683 159 if (fabs(motorValue) > 1)
Miriam 0:2a99f692f683 160 {
Miriam 3:f755b4d41aa8 161 M1E = 1; //de snelheid wordt teruggeschaald naar 8.4 rad/s (maximale snelheid, dus waarde 1)
Miriam 0:2a99f692f683 162 }
Miriam 0:2a99f692f683 163 else
Miriam 0:2a99f692f683 164 {
Miriam 0:2a99f692f683 165 M1E = fabs(motorValue); //de absolute snelheid wordt bepaald, de motor staat uit bij een waarde 0
Miriam 0:2a99f692f683 166 }
Miriam 0:2a99f692f683 167 }
Miriam 0:2a99f692f683 168
paulineoonk 7:05495acc08b0 169 void SetMotor2(float motorValue2)
paulineoonk 7:05495acc08b0 170 {
paulineoonk 7:05495acc08b0 171 if (motorValue2 >= 0)
paulineoonk 7:05495acc08b0 172 {
paulineoonk 12:e125b9fa77b9 173 M2D = 1;
paulineoonk 7:05495acc08b0 174 }
paulineoonk 7:05495acc08b0 175 else
paulineoonk 7:05495acc08b0 176 {
paulineoonk 12:e125b9fa77b9 177 M2D =0;
paulineoonk 7:05495acc08b0 178 }
paulineoonk 7:05495acc08b0 179
paulineoonk 7:05495acc08b0 180 if (fabs(motorValue2) > 1)
paulineoonk 7:05495acc08b0 181 {
paulineoonk 7:05495acc08b0 182 M2E = 1; //de snelheid wordt teruggeschaald naar 8.4 rad/s (maximale snelheid, dus waarde 1)
paulineoonk 7:05495acc08b0 183 }
paulineoonk 7:05495acc08b0 184 else
paulineoonk 7:05495acc08b0 185 {
paulineoonk 7:05495acc08b0 186 M2E = fabs(motorValue2); //de absolute snelheid wordt bepaald, de motor staat uit bij een waarde 0
paulineoonk 7:05495acc08b0 187 }
paulineoonk 7:05495acc08b0 188 }
paulineoonk 7:05495acc08b0 189
Miriam 0:2a99f692f683 190 float Encoder ()
Miriam 0:2a99f692f683 191 {
Miriam 0:2a99f692f683 192 float Huidigepositie = motor1.getPosition ();
Miriam 3:f755b4d41aa8 193 return Huidigepositie; // huidige positie = current position
Miriam 0:2a99f692f683 194 }
Miriam 0:2a99f692f683 195
paulineoonk 7:05495acc08b0 196 float Encoder2 ()
paulineoonk 7:05495acc08b0 197 {
paulineoonk 7:05495acc08b0 198 float Huidigepositie2 = motor2.getPosition ();
paulineoonk 7:05495acc08b0 199 return Huidigepositie2; // huidige positie = current position
paulineoonk 7:05495acc08b0 200 }
paulineoonk 7:05495acc08b0 201
Miriam 0:2a99f692f683 202 void MeasureAndControl(void)
Miriam 0:2a99f692f683 203 {
Annelotte 15:a5849f3a60fc 204 SetpointRobot();
Annelotte 15:a5849f3a60fc 205 // RKI aanroepen
Annelotte 15:a5849f3a60fc 206 RKI();
Annelotte 15:a5849f3a60fc 207
Miriam 3:f755b4d41aa8 208 // hier the control of the control system
Miriam 0:2a99f692f683 209 float Huidigepositie = Encoder();
Miriam 3:f755b4d41aa8 210 float error = (refP - Huidigepositie);// make an error
Miriam 2:b504e35af662 211 float motorValue = FeedBackControl(error, e_prev, e_int);
Miriam 0:2a99f692f683 212 SetMotor1(motorValue);
Miriam 0:2a99f692f683 213
paulineoonk 7:05495acc08b0 214 float Huidigepositie2 = Encoder2();
paulineoonk 7:05495acc08b0 215 float error2 = (refP2 - Huidigepositie2);// make an error
paulineoonk 7:05495acc08b0 216 float motorValue2 = FeedBackControl2(error2, e_prev2, e_int2);
paulineoonk 7:05495acc08b0 217 SetMotor2(motorValue2);
paulineoonk 12:e125b9fa77b9 218 //pc.printf("encoder 2 = %f\r\n",Huidigepositie2);
Gerber 13:eaaeb41e22d2 219 pc.printf("refP2 = %f, Huidigepositie2 = %f, error = %f, motorValue2 = %f \r\n", refP2, Huidigepositie2, error2, motorValue2);
paulineoonk 7:05495acc08b0 220 }
paulineoonk 7:05495acc08b0 221
Miriam 0:2a99f692f683 222 int main()
Miriam 0:2a99f692f683 223 {
Miriam 5:987cc578988e 224 M1E.period(PwmPeriod);
Miriam 4:c119259c1ba5 225 Treecko.attach(MeasureAndControl, Ts); //Elke 1 seconde zorgt de ticker voor het runnen en uitlezen van de verschillende
Miriam 0:2a99f692f683 226 //functies en analoge signalen. Veranderingen worden elke 1 seconde doorgevoerd.
Annelotte 15:a5849f3a60fc 227 pc.baud(115200);
Miriam 4:c119259c1ba5 228
Miriam 0:2a99f692f683 229 while(1)
Miriam 0:2a99f692f683 230 {
Miriam 0:2a99f692f683 231 wait(0.2);
paulineoonk 12:e125b9fa77b9 232 // pc.printf(" encoder 1 %f, encoder 2 %f\r\n",Huidigepositie,Huidigepositie2);
paulineoonk 12:e125b9fa77b9 233
paulineoonk 12:e125b9fa77b9 234 //float B = motor1.getPosition();
paulineoonk 12:e125b9fa77b9 235 //float Potmeterwaarde = potMeter2.read();
Miriam 0:2a99f692f683 236 //float positie = B%4096;
paulineoonk 12:e125b9fa77b9 237 // pc.printf("pos: %f, \r\n pos2 = %f",motor1.getPosition(),motor2.getPosition); //potmeter uitlezen. tussen 0-1. voltage, dus *3.3V
Miriam 0:2a99f692f683 238 }
Miriam 0:2a99f692f683 239 }