working PID + Kinematics + Motorcontrol

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
cmaas
Date:
Wed Oct 31 16:00:45 2018 +0000
Revision:
4:85770b268599
Parent:
2:44758d95cb0b
Child:
5:a54ea6514bc5
debugging file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmaas 0:a6f2b6cc83ca 1 // KINEMATICS + PID + MOTOR CONTROL
cmaas 0:a6f2b6cc83ca 2
cmaas 0:a6f2b6cc83ca 3 //----------------~INITIATING-------------------------
cmaas 0:a6f2b6cc83ca 4 #include "mbed.h"
cmaas 0:a6f2b6cc83ca 5
cmaas 0:a6f2b6cc83ca 6 // KINEMATICS -- DEPENDENCIES
cmaas 0:a6f2b6cc83ca 7 #include "stdio.h"
cmaas 0:a6f2b6cc83ca 8 #define _USE_MATH_DEFINES
cmaas 0:a6f2b6cc83ca 9 #include <math.h>
cmaas 0:a6f2b6cc83ca 10 #define M_PI 3.14159265358979323846 /* pi */
cmaas 0:a6f2b6cc83ca 11
cmaas 0:a6f2b6cc83ca 12 // PID CONTROLLER -- DEPENDENCIES
cmaas 0:a6f2b6cc83ca 13 #include "BiQuad.h"
cmaas 0:a6f2b6cc83ca 14 #include "QEI.h"
cmaas 0:a6f2b6cc83ca 15 #include "MODSERIAL.h"
cmaas 0:a6f2b6cc83ca 16 #include "HIDScope.h"
cmaas 0:a6f2b6cc83ca 17 //#include "Math.h"
cmaas 0:a6f2b6cc83ca 18
cmaas 0:a6f2b6cc83ca 19 // PID CONTROLLER -- PIN DEFENITIONS
cmaas 0:a6f2b6cc83ca 20 AnalogIn button2(A4);
cmaas 2:44758d95cb0b 21 AnalogIn button1(A3);
cmaas 4:85770b268599 22
cmaas 0:a6f2b6cc83ca 23 DigitalOut directionpin1(D7); // motor 1
cmaas 4:85770b268599 24 DigitalOut directionpin2(D4); // motor 2
cmaas 0:a6f2b6cc83ca 25 PwmOut pwmpin1(D6); // motor 1
cmaas 4:85770b268599 26 PwmOut pwmpin2(D5); // motor 2
cmaas 0:a6f2b6cc83ca 27
cmaas 0:a6f2b6cc83ca 28 QEI encoder1 (D9, D8, NC, 8400, QEI::X4_ENCODING);
cmaas 4:85770b268599 29 QEI encoder2 (D11, D10, NC, 8400, QEI::X4_ENCODING); // motor 2
cmaas 0:a6f2b6cc83ca 30 MODSERIAL pc(USBTX, USBRX);
cmaas 0:a6f2b6cc83ca 31 HIDScope scope(2);
cmaas 0:a6f2b6cc83ca 32
cmaas 0:a6f2b6cc83ca 33 // TICKERS
cmaas 0:a6f2b6cc83ca 34 Ticker ref_rot;
cmaas 0:a6f2b6cc83ca 35 Ticker show_counts;
cmaas 0:a6f2b6cc83ca 36 Ticker Scope_Data;
cmaas 0:a6f2b6cc83ca 37
cmaas 0:a6f2b6cc83ca 38 //----------------GLOBALS--------------------------
cmaas 0:a6f2b6cc83ca 39
cmaas 0:a6f2b6cc83ca 40 // CONSTANTS PID CONTROLLER
cmaas 0:a6f2b6cc83ca 41 double PI = M_PI;// CHANGE THIS INTO M_PI
cmaas 2:44758d95cb0b 42 double Kp = 14; //200 , 50
cmaas 0:a6f2b6cc83ca 43 double Ki = 0; //1, 0.5
cmaas 2:44758d95cb0b 44 double Kd = 3; //200, 10
cmaas 0:a6f2b6cc83ca 45 double Ts = 0.1; // Sample time in seconds
cmaas 0:a6f2b6cc83ca 46 double reference_rotation; //define as radians
cmaas 0:a6f2b6cc83ca 47 double motor_position;
cmaas 0:a6f2b6cc83ca 48 bool AlwaysTrue;
cmaas 0:a6f2b6cc83ca 49
cmaas 0:a6f2b6cc83ca 50 //CONSTANTS KINEMATICS
cmaas 0:a6f2b6cc83ca 51 // constants
cmaas 0:a6f2b6cc83ca 52 const float la = 0.256; // lengte actieve arm
cmaas 0:a6f2b6cc83ca 53 const float lp = 0.21; // lengte passieve arm
cmaas 0:a6f2b6cc83ca 54 const float rp = 0.052; // straal van midden end effector tot hoekpunt
cmaas 0:a6f2b6cc83ca 55 const float rm = 0.23; // straal van global midden tot motor
cmaas 0:a6f2b6cc83ca 56 const float a = 0.09; // zijde van de driehoek
cmaas 0:a6f2b6cc83ca 57 const float xas = 0.40; // afstand van motor 1 tot motor 3
cmaas 0:a6f2b6cc83ca 58 const float yas = 0.346; // afstand van xas tot motor 2
cmaas 0:a6f2b6cc83ca 59 const float thetap = 0; // rotatiehoek van de end effector
cmaas 0:a6f2b6cc83ca 60
cmaas 0:a6f2b6cc83ca 61 // motor locatie
cmaas 0:a6f2b6cc83ca 62 const int a1x = 0; //x locatie motor 1
cmaas 0:a6f2b6cc83ca 63 const int a1y = 0; //y locatie motor 1
cmaas 0:a6f2b6cc83ca 64 const float a2x = (0.5)*xas; // x locatie motor 2
cmaas 0:a6f2b6cc83ca 65 const float a2y = yas; // y locatie motor 2
cmaas 0:a6f2b6cc83ca 66 const float a3x = xas; // x locatie motor 3
cmaas 0:a6f2b6cc83ca 67 const int a3y = 0; // y locatie motor 3
cmaas 0:a6f2b6cc83ca 68
cmaas 0:a6f2b6cc83ca 69 // script voor het bepalen van de desired position aan de hand van emg (1/0)
cmaas 0:a6f2b6cc83ca 70
cmaas 0:a6f2b6cc83ca 71 // EMG OUTPUT
cmaas 0:a6f2b6cc83ca 72 int EMGxplus;
cmaas 0:a6f2b6cc83ca 73 int EMGxmin ;
cmaas 0:a6f2b6cc83ca 74 int EMGyplus;
cmaas 0:a6f2b6cc83ca 75 int EMGymin ;
cmaas 0:a6f2b6cc83ca 76
cmaas 0:a6f2b6cc83ca 77 // Dit moet experimenteel geperfectioneerd worden
cmaas 0:a6f2b6cc83ca 78 float tijdstap = 0.05; //nu wss heel langzaam, kan miss omhoog
cmaas 0:a6f2b6cc83ca 79 float v = 0.1; // snelheid kan wss ook hoger
cmaas 0:a6f2b6cc83ca 80
cmaas 0:a6f2b6cc83ca 81 float px = 0.2; //starting x
cmaas 0:a6f2b6cc83ca 82 float py = 0.155; // starting y
cmaas 0:a6f2b6cc83ca 83
cmaas 0:a6f2b6cc83ca 84 // limits (since no forward kinematics)
cmaas 0:a6f2b6cc83ca 85 float upperxlim = 0.36; //niet helemaal naar requierments ff kijken of ie groter kan
cmaas 0:a6f2b6cc83ca 86 float lowerxlim = 0.04;
cmaas 0:a6f2b6cc83ca 87 float upperylim = 0.30;
cmaas 0:a6f2b6cc83ca 88 float lowerylim = 0.04;
cmaas 0:a6f2b6cc83ca 89
cmaas 0:a6f2b6cc83ca 90
cmaas 0:a6f2b6cc83ca 91 //----------------FUNCTIONS--------------------------
cmaas 0:a6f2b6cc83ca 92
cmaas 0:a6f2b6cc83ca 93 // ~~~~~~~~~~~~~~ROBOT KINEMATICS ~~~~~~~~~~~~~~~~~~
cmaas 0:a6f2b6cc83ca 94
cmaas 0:a6f2b6cc83ca 95 // functie x positie
cmaas 0:a6f2b6cc83ca 96 float positionx(int EMGxplus,int EMGxmin)
cmaas 0:a6f2b6cc83ca 97 {
cmaas 0:a6f2b6cc83ca 98 float EMGx = EMGxplus - EMGxmin;
cmaas 0:a6f2b6cc83ca 99
cmaas 0:a6f2b6cc83ca 100 float verplaatsingx = EMGx * tijdstap * v;
cmaas 0:a6f2b6cc83ca 101 float pxnieuw = px + verplaatsingx;
cmaas 0:a6f2b6cc83ca 102 // x limit
cmaas 0:a6f2b6cc83ca 103 if (pxnieuw <= upperxlim && pxnieuw >= lowerxlim)
cmaas 0:a6f2b6cc83ca 104 {
cmaas 0:a6f2b6cc83ca 105 px = pxnieuw;
cmaas 0:a6f2b6cc83ca 106 }
cmaas 0:a6f2b6cc83ca 107 else
cmaas 0:a6f2b6cc83ca 108 {
cmaas 0:a6f2b6cc83ca 109 if (pxnieuw >= lowerxlim)
cmaas 0:a6f2b6cc83ca 110 {
cmaas 0:a6f2b6cc83ca 111 px = upperxlim;
cmaas 0:a6f2b6cc83ca 112 }
cmaas 0:a6f2b6cc83ca 113 else
cmaas 0:a6f2b6cc83ca 114 {
cmaas 0:a6f2b6cc83ca 115 px = lowerxlim;
cmaas 0:a6f2b6cc83ca 116 }
cmaas 0:a6f2b6cc83ca 117 }
cmaas 0:a6f2b6cc83ca 118 //printf("X eindpunt (%f) en verplaatsing: (%f)\n\r",px,verplaatsingx);
cmaas 0:a6f2b6cc83ca 119 return px;
cmaas 0:a6f2b6cc83ca 120 }
cmaas 0:a6f2b6cc83ca 121
cmaas 0:a6f2b6cc83ca 122
cmaas 0:a6f2b6cc83ca 123 // functie y positie
cmaas 0:a6f2b6cc83ca 124 float positiony(int EMGyplus,int EMGymin)
cmaas 0:a6f2b6cc83ca 125 {
cmaas 0:a6f2b6cc83ca 126 float EMGy = EMGyplus - EMGymin;
cmaas 0:a6f2b6cc83ca 127
cmaas 0:a6f2b6cc83ca 128 float verplaatsingy = EMGy * tijdstap * v;
cmaas 0:a6f2b6cc83ca 129 float pynieuw = py + verplaatsingy;
cmaas 0:a6f2b6cc83ca 130
cmaas 0:a6f2b6cc83ca 131 // y limit
cmaas 0:a6f2b6cc83ca 132 if (pynieuw <= upperylim && pynieuw >= lowerylim)
cmaas 0:a6f2b6cc83ca 133 {
cmaas 0:a6f2b6cc83ca 134 py = pynieuw;
cmaas 0:a6f2b6cc83ca 135 }
cmaas 0:a6f2b6cc83ca 136 else
cmaas 0:a6f2b6cc83ca 137 {
cmaas 0:a6f2b6cc83ca 138 if (pynieuw >= lowerylim)
cmaas 0:a6f2b6cc83ca 139 {
cmaas 0:a6f2b6cc83ca 140 py = upperylim;
cmaas 0:a6f2b6cc83ca 141 }
cmaas 0:a6f2b6cc83ca 142 else
cmaas 0:a6f2b6cc83ca 143 {
cmaas 0:a6f2b6cc83ca 144 py = lowerylim;
cmaas 0:a6f2b6cc83ca 145 }
cmaas 0:a6f2b6cc83ca 146 }
cmaas 0:a6f2b6cc83ca 147 //printf("Y eindpunt (%f) en verplaatsing: (%f) \n\r",py,verplaatsingy);
cmaas 0:a6f2b6cc83ca 148 return (py);
cmaas 0:a6f2b6cc83ca 149 }
cmaas 0:a6f2b6cc83ca 150
cmaas 0:a6f2b6cc83ca 151
cmaas 0:a6f2b6cc83ca 152 //~~~~~~~~~~~~CALCULATIING MOTOR ANGLES ~~~~~~~~
cmaas 0:a6f2b6cc83ca 153 // arm 1 --> reference angle motor 1
cmaas 0:a6f2b6cc83ca 154 float hoek1(float px, float py) // input: ref x, ref y
cmaas 0:a6f2b6cc83ca 155 {
cmaas 0:a6f2b6cc83ca 156 float c1x = px - rp * cos(thetap +(M_PI/6)); // x locatie hoekpunt end-effector
cmaas 0:a6f2b6cc83ca 157 float c1y = py - rp*sin(thetap+(M_PI/6)); // y locatie hoekpunt end-effector
cmaas 0:a6f2b6cc83ca 158 float alpha1 = atan2((c1y-a1y),(c1x-a1x)); // hoek tussen horizontaal en lijn van motor naar bijbehorende end-effector punt
cmaas 0:a6f2b6cc83ca 159 float psi1 = acos(( pow(la,2)-pow(lp,2)+pow((c1x-a1x),2)+pow((c1y-a1y),2))/(2*la*sqrt(pow ((c1x-a1x),2)+pow((c1y-a1y),2) ))); //Hoek tussen lijn van motor naar bijbehorende end=effector punt en actieve arm
cmaas 0:a6f2b6cc83ca 160 float a1 = alpha1 + psi1; //hoek tussen horizontaal en actieve arm
cmaas 0:a6f2b6cc83ca 161 //printf("arm 1 = %f \n\r",a1);
cmaas 0:a6f2b6cc83ca 162 return a1;
cmaas 0:a6f2b6cc83ca 163 }
cmaas 0:a6f2b6cc83ca 164
cmaas 0:a6f2b6cc83ca 165 // arm 2 --> reference angle motor 2
cmaas 0:a6f2b6cc83ca 166 float hoek2(float px, float py)
cmaas 0:a6f2b6cc83ca 167 {
cmaas 0:a6f2b6cc83ca 168 float c2x = px - rp * cos(thetap -(M_PI/2));
cmaas 0:a6f2b6cc83ca 169 float c2y = py - rp*sin(thetap-(M_PI/2));
cmaas 0:a6f2b6cc83ca 170 float alpha2 = atan2((c2y-a2y),(c2x-a2x));
cmaas 0:a6f2b6cc83ca 171 float psi2 = acos(( pow(la,2)-pow(lp,2)+pow((c2x-a2x),2)+pow((c2y-a2y),2))/(2*la*sqrt(pow ((c2x-a2x),2)+pow((c2y-a2y),2) )));
cmaas 0:a6f2b6cc83ca 172 float a2 = alpha2 + psi2;
cmaas 0:a6f2b6cc83ca 173 //printf("arm 2 = %f \n\r",a2);
cmaas 0:a6f2b6cc83ca 174 return a2;
cmaas 0:a6f2b6cc83ca 175 }
cmaas 0:a6f2b6cc83ca 176
cmaas 0:a6f2b6cc83ca 177 //arm 3 --> reference angle motor 3
cmaas 0:a6f2b6cc83ca 178 float hoek3(float px, float py)
cmaas 0:a6f2b6cc83ca 179 {
cmaas 0:a6f2b6cc83ca 180 float c3x = px - rp * cos(thetap +(5*M_PI/6));
cmaas 0:a6f2b6cc83ca 181 float c3y = py - rp*sin(thetap+(5*M_PI/6));
cmaas 0:a6f2b6cc83ca 182 float alpha3 = atan2((c3y-a3y),(c3x-a3x));
cmaas 0:a6f2b6cc83ca 183 float psi3 = acos(( pow(la,2)-pow(lp,2)+pow((c3x-a3x),2)+pow((c3y-a3y),2))/(2*la*sqrt(pow ((c3x-a3x),2)+pow((c3y-a3y),2) )));
cmaas 0:a6f2b6cc83ca 184 float a3 = alpha3 + psi3;
cmaas 0:a6f2b6cc83ca 185 //printf("arm 3 = %f \n\r",a3);
cmaas 0:a6f2b6cc83ca 186 return a3;
cmaas 0:a6f2b6cc83ca 187 }
cmaas 0:a6f2b6cc83ca 188
cmaas 0:a6f2b6cc83ca 189 // ~~~~~~~~~~~~~~PID CONTROLLER~~~~~~~~~~~~~~~~~~
cmaas 0:a6f2b6cc83ca 190
cmaas 0:a6f2b6cc83ca 191 double PID_controller(double error)
cmaas 0:a6f2b6cc83ca 192 {
cmaas 0:a6f2b6cc83ca 193 static double error_integral = 0;
cmaas 0:a6f2b6cc83ca 194 static double error_prev = error; // initialization with this value only done once!
cmaas 0:a6f2b6cc83ca 195 static BiQuad LowPassFilter(0.0640, 0.1279, 0.0640, -1.1683, 0.4241);
cmaas 0:a6f2b6cc83ca 196
cmaas 0:a6f2b6cc83ca 197 // Proportional part:
cmaas 0:a6f2b6cc83ca 198 double u_k = Kp * error;
cmaas 0:a6f2b6cc83ca 199
cmaas 0:a6f2b6cc83ca 200 // Integral part
cmaas 0:a6f2b6cc83ca 201 error_integral = error_integral + error * Ts;
cmaas 0:a6f2b6cc83ca 202 double u_i = Ki * error_integral;
cmaas 0:a6f2b6cc83ca 203
cmaas 0:a6f2b6cc83ca 204 // Derivative part
cmaas 0:a6f2b6cc83ca 205 double error_derivative = (error - error_prev)/Ts;
cmaas 0:a6f2b6cc83ca 206 double filtered_error_derivative = LowPassFilter.step(error_derivative);
cmaas 0:a6f2b6cc83ca 207 double u_d = Kd * filtered_error_derivative;
cmaas 0:a6f2b6cc83ca 208 error_prev = error;
cmaas 0:a6f2b6cc83ca 209
cmaas 0:a6f2b6cc83ca 210 // Sum all parts and return it
cmaas 0:a6f2b6cc83ca 211 return u_k + u_i + u_d;
cmaas 0:a6f2b6cc83ca 212 }
cmaas 0:a6f2b6cc83ca 213
cmaas 0:a6f2b6cc83ca 214
cmaas 0:a6f2b6cc83ca 215 // DIRECTON AND SPEED CONTROL
cmaas 0:a6f2b6cc83ca 216 void moter_control(double u)
cmaas 0:a6f2b6cc83ca 217 {
cmaas 0:a6f2b6cc83ca 218 directionpin1= u > 0.0f; //eithertrueor false
cmaas 0:a6f2b6cc83ca 219 pwmpin1= fabs(u); //pwmduty cycle canonlybepositive, floatingpoint absolute value
cmaas 0:a6f2b6cc83ca 220 }
cmaas 0:a6f2b6cc83ca 221
cmaas 0:a6f2b6cc83ca 222
cmaas 0:a6f2b6cc83ca 223 // CONTROLLING THE MOTOR
cmaas 0:a6f2b6cc83ca 224 void Motor_mover()
cmaas 0:a6f2b6cc83ca 225 {
cmaas 0:a6f2b6cc83ca 226 double motor_position = encoder1.getPulses(); //output in counts
cmaas 2:44758d95cb0b 227 double reference_rotation = hoek2(px, py);
cmaas 0:a6f2b6cc83ca 228 double error = reference_rotation - motor_position*(2*PI)/8400;
cmaas 0:a6f2b6cc83ca 229 double u = PID_controller(error);
cmaas 0:a6f2b6cc83ca 230 moter_control(u);
cmaas 0:a6f2b6cc83ca 231 }
cmaas 0:a6f2b6cc83ca 232
cmaas 0:a6f2b6cc83ca 233 //PRINT TICKER
cmaas 0:a6f2b6cc83ca 234 void PrintFlag()
cmaas 0:a6f2b6cc83ca 235 {
cmaas 0:a6f2b6cc83ca 236 AlwaysTrue = true;
cmaas 0:a6f2b6cc83ca 237 }
cmaas 0:a6f2b6cc83ca 238
cmaas 0:a6f2b6cc83ca 239 // HIDSCOPE
cmaas 0:a6f2b6cc83ca 240 void ScopeData()
cmaas 0:a6f2b6cc83ca 241 {
cmaas 0:a6f2b6cc83ca 242 double y = encoder1.getPulses();
cmaas 0:a6f2b6cc83ca 243 scope.set(0, y);
cmaas 0:a6f2b6cc83ca 244 scope.send();
cmaas 0:a6f2b6cc83ca 245 }
cmaas 0:a6f2b6cc83ca 246
cmaas 0:a6f2b6cc83ca 247
cmaas 0:a6f2b6cc83ca 248 //----------------------MAIN---------------------------------
cmaas 0:a6f2b6cc83ca 249 int main()
cmaas 0:a6f2b6cc83ca 250 {
cmaas 0:a6f2b6cc83ca 251 // ~~~~~~~~~~~~~~~~ INITIATING ~~~~~~~~~~~~
cmaas 0:a6f2b6cc83ca 252 pwmpin1.period_us(60); // setup motor
cmaas 0:a6f2b6cc83ca 253
cmaas 0:a6f2b6cc83ca 254 // setup printing service
cmaas 0:a6f2b6cc83ca 255 pc.baud(9600);
cmaas 0:a6f2b6cc83ca 256 pc.printf("test");
cmaas 0:a6f2b6cc83ca 257
cmaas 0:a6f2b6cc83ca 258 // Tickers
cmaas 0:a6f2b6cc83ca 259 //show_counts.attach(PrintFlag, 0.2);
cmaas 4:85770b268599 260 ref_rot.attach(Motor_mover, 0.1);
cmaas 4:85770b268599 261 //Scope_Data.attach(ScopeData, 0.01);
cmaas 0:a6f2b6cc83ca 262
cmaas 0:a6f2b6cc83ca 263
cmaas 2:44758d95cb0b 264 while(true){
cmaas 2:44758d95cb0b 265
cmaas 2:44758d95cb0b 266
cmaas 2:44758d95cb0b 267 if (button2 == false)
cmaas 2:44758d95cb0b 268 {
cmaas 2:44758d95cb0b 269 wait(0.05f);
cmaas 2:44758d95cb0b 270
cmaas 2:44758d95cb0b 271 // berekenen positie
cmaas 2:44758d95cb0b 272 float px = positionx(1,0); // EMG: +x, -x
cmaas 2:44758d95cb0b 273 float py = positiony(1,0); // EMG: +y, -y
cmaas 2:44758d95cb0b 274 //printf("positie (%f,%f)\n\r",px,py);
cmaas 2:44758d95cb0b 275 }
cmaas 0:a6f2b6cc83ca 276
cmaas 2:44758d95cb0b 277 if (button1 == false){
cmaas 2:44758d95cb0b 278 wait(0.05f);
cmaas 2:44758d95cb0b 279 // berekenen positie
cmaas 2:44758d95cb0b 280 float px = positionx(0,1); // EMG: +x, -x
cmaas 2:44758d95cb0b 281 float py = positiony(0,1); // EMG: +y, -y
cmaas 2:44758d95cb0b 282 //printf("positie (%f,%f)\n\r",px,py);
cmaas 2:44758d95cb0b 283 }
cmaas 2:44758d95cb0b 284 }
cmaas 0:a6f2b6cc83ca 285 // berekenen hoeken
cmaas 0:a6f2b6cc83ca 286 /*
cmaas 0:a6f2b6cc83ca 287 float a1 = hoek1(px, py);
cmaas 0:a6f2b6cc83ca 288 float a2 = hoek2(px, py);
cmaas 0:a6f2b6cc83ca 289 float a3 = hoek3(px, py);
cmaas 0:a6f2b6cc83ca 290
cmaas 0:a6f2b6cc83ca 291 printf("hoek(%f,%f,%f)\n\r",a1,a2,a3);
cmaas 0:a6f2b6cc83ca 292
cmaas 0:a6f2b6cc83ca 293 return 0;
cmaas 0:a6f2b6cc83ca 294 */
cmaas 0:a6f2b6cc83ca 295 }