code die in het verslag komt

Dependencies:   FastPWM HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
mefix
Date:
Mon Oct 31 11:25:32 2016 +0000
Revision:
0:3c99f1705565
Child:
1:ba63033da653
final controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mefix 0:3c99f1705565 1 #include "mbed.h"
mefix 0:3c99f1705565 2 #include "HIDScope.h"
mefix 0:3c99f1705565 3 #include "BiQuad.h"
mefix 0:3c99f1705565 4 #include "MODSERIAL.h"
mefix 0:3c99f1705565 5 #include "QEI.h"
mefix 0:3c99f1705565 6 #include "FastPWM.h"
mefix 0:3c99f1705565 7
mefix 0:3c99f1705565 8 // in gebruik: D(0(TX),1(RX),4(motor2dir),5(motor2pwm),6(motor1pwm),7(motor1dir),
mefix 0:3c99f1705565 9 //8(pushbutton),9(servoPWM),10(encoder),11(encoder),12(encoder),13(encoder)) A(0,1,2)(emg)
mefix 0:3c99f1705565 10
mefix 0:3c99f1705565 11 MODSERIAL pc(USBTX, USBRX);
mefix 0:3c99f1705565 12 HIDScope scope(6); // the amount of scopes to send to the pc
mefix 0:3c99f1705565 13
mefix 0:3c99f1705565 14 //Define objects
mefix 0:3c99f1705565 15
mefix 0:3c99f1705565 16 //Define the EMG inputs
mefix 0:3c99f1705565 17 AnalogIn emg1( A0 );
mefix 0:3c99f1705565 18 AnalogIn emg2( A1 );
mefix 0:3c99f1705565 19 AnalogIn emg3( A2 );
mefix 0:3c99f1705565 20
mefix 0:3c99f1705565 21 //Define motor outputs
mefix 0:3c99f1705565 22 DigitalOut motor1dir(D7); //direction of motor 1, attach at m1, set to 0: cw
mefix 0:3c99f1705565 23 FastPWM motor1(D6); // speed of motor 1
mefix 0:3c99f1705565 24 FastPWM motor2(D5); //speed of motor 2
mefix 0:3c99f1705565 25 DigitalOut motor2dir(D4); //direction of motor 2, attach at m2, set to 0: ccw
mefix 0:3c99f1705565 26 FastPWM servo(D9); //servo pwm
mefix 0:3c99f1705565 27
mefix 0:3c99f1705565 28 QEI Encoder1(D13,D12,NC,64,QEI::X4_ENCODING); //defining encoder
mefix 0:3c99f1705565 29 QEI Encoder2(D11,D10,NC,64,QEI::X4_ENCODING); //defining encoder
mefix 0:3c99f1705565 30
mefix 0:3c99f1705565 31 //Define the Tickers
mefix 0:3c99f1705565 32 Ticker pos_timer; // the timer which is used to print the position every second
mefix 0:3c99f1705565 33 Ticker sample_timer; // the timer which is used to decide when a sample needs to be taken
mefix 0:3c99f1705565 34 Ticker control; // Ticker for processing encoder input to motor output
mefix 0:3c99f1705565 35 Ticker servo_control; // Ticker for calling servo_control
mefix 0:3c99f1705565 36
mefix 0:3c99f1705565 37 //Initialize all variables
mefix 0:3c99f1705565 38 volatile bool sampletimer = false; // go flag
mefix 0:3c99f1705565 39 volatile bool controller_go=false;
mefix 0:3c99f1705565 40 volatile bool servo_go=false;
mefix 0:3c99f1705565 41
mefix 0:3c99f1705565 42 double threshold = 0.04; // the threshold which the emg signals need to surpass to do something
mefix 0:3c99f1705565 43 double samplefreq=0.002; // every 0.002 sec a sample will be taken this is a frequency of 500 Hz
mefix 0:3c99f1705565 44 double emg02; // the first emg signal
mefix 0:3c99f1705565 45 double emg12; // the second emg signal
mefix 0:3c99f1705565 46 double emg22; // the third emg signal
mefix 0:3c99f1705565 47 double ref_x=0.0000; // the x reference position
mefix 0:3c99f1705565 48 double ref_y=0.0000; // the y reference position
mefix 0:3c99f1705565 49 double old_ref_x; // the old x reference
mefix 0:3c99f1705565 50 double old_ref_y; // the old y reference
mefix 0:3c99f1705565 51 double speed=0.00008; // the variable with which a speed is reached of 1cm/s
mefix 0:3c99f1705565 52 double theta=0.0; // angle of the arm
mefix 0:3c99f1705565 53 double radius=0.0; // radius of the arm
mefix 0:3c99f1705565 54
mefix 0:3c99f1705565 55 const double minRadius=0.43; // minimum radius of arm
mefix 0:3c99f1705565 56 const double maxRadius=0.62; // maximum radius of arm
mefix 0:3c99f1705565 57 const double min_y=-0.26; // minimum height which the spatula can reach
mefix 0:3c99f1705565 58 char key; // variable to place the keyboard input
mefix 0:3c99f1705565 59
mefix 0:3c99f1705565 60 double m1_pwm=0; //variable for PWM control motor 1
mefix 0:3c99f1705565 61 double m2_pwm=0; //variable for PWM control motor 2
mefix 0:3c99f1705565 62
mefix 0:3c99f1705565 63 const double m1_Kp = 35.16, m1_Ki = 108.8, m1_Kd = 2.84, m1_N = 100; // controller constants motor 1
mefix 0:3c99f1705565 64 double m1_v1 = 0, m1_v2 = 0; // Memory variables
mefix 0:3c99f1705565 65 const double m1_Ts = 0.01; // Controller sample time
mefix 0:3c99f1705565 66
mefix 0:3c99f1705565 67 const double m2_Kp = 9.974, m2_Ki = 16.49, m2_Kd = 1.341, m2_N = 100; // controller constants motor 2
mefix 0:3c99f1705565 68 double m2_v1 = 0, m2_v2 = 0; // Memory variables
mefix 0:3c99f1705565 69 const double m2_Ts = 0.01; // Controller sample time
mefix 0:3c99f1705565 70
mefix 0:3c99f1705565 71 const double pi=3.14159265359;
mefix 0:3c99f1705565 72 const double res = 64.0/(1.0/131.25*2.0*pi); // resolution on gearbox shaft per pulse
mefix 0:3c99f1705565 73 const double V_max=9.0; // maximum voltage supplied by trafo
mefix 0:3c99f1705565 74 const double pulleyRadius=0.0398/2.0; // pulley radius
mefix 0:3c99f1705565 75
mefix 0:3c99f1705565 76 double servo_pwm=0.0023; // duty cycle 1.5 ms 7.5%, min 0.5 ms 2.5%, max 2.5 ms 12.5%
mefix 0:3c99f1705565 77 const double minTheta=-43.0/180.0*pi; //minimum angle robot
mefix 0:3c99f1705565 78 const double maxTheta=-32.0/180.0*pi; // maximum angle to which the spatula can stabilise
mefix 0:3c99f1705565 79 const double diffTheta=maxTheta-minTheta; //difference between max and min angle of theta for stabilisation
mefix 0:3c99f1705565 80 const double min_servo_pwm=0.00217; // corresponds to angle of theta -32 degrees
mefix 0:3c99f1705565 81 const double max_servo_pwm=0.0023; // corresponds to angle of theta -43 degrees
mefix 0:3c99f1705565 82 const double res_servo=max_servo_pwm-min_servo_pwm; //resolution of servo pwm signal between min and max angle
mefix 0:3c99f1705565 83 const double servo_Ts=0.02;
mefix 0:3c99f1705565 84 bool z_push=false;
mefix 0:3c99f1705565 85
mefix 0:3c99f1705565 86 //Define the needed Biquad chains
mefix 0:3c99f1705565 87 BiQuadChain bqc11;
mefix 0:3c99f1705565 88 BiQuadChain bqc13;
mefix 0:3c99f1705565 89 BiQuadChain bqc21;
mefix 0:3c99f1705565 90 BiQuadChain bqc23;
mefix 0:3c99f1705565 91 BiQuadChain bqc31;
mefix 0:3c99f1705565 92 BiQuadChain bqc33;
mefix 0:3c99f1705565 93
mefix 0:3c99f1705565 94 //Define the BiQuads for the filter of the first emg signal
mefix 0:3c99f1705565 95 //Notch filter
mefix 0:3c99f1705565 96 BiQuad bq111(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
mefix 0:3c99f1705565 97 BiQuad bq112(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
mefix 0:3c99f1705565 98 BiQuad bq113(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
mefix 0:3c99f1705565 99 //High pass filter
mefix 0:3c99f1705565 100 //BiQuad bq121( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 ); //Old biquad values
mefix 0:3c99f1705565 101 BiQuad bq121( 0.8956, -1.7911, 0.8956, 1.0000, -1.7814, 0.7941);
mefix 0:3c99f1705565 102 BiQuad bq122( 0.9192, -1.8385, 0.9192, 1.0000, -1.8319, 0.8450);
mefix 0:3c99f1705565 103 BiQuad bq123( 0.9649, -1.9298, 0.9649, 1.0000, -1.9266, 0.9403);
mefix 0:3c99f1705565 104 //Low pass filter
mefix 0:3c99f1705565 105 BiQuad bq131( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
mefix 0:3c99f1705565 106
mefix 0:3c99f1705565 107 //Define the BiQuads for the filter of the second emg signal
mefix 0:3c99f1705565 108 //Notch filter
mefix 0:3c99f1705565 109 BiQuad bq211 = bq111;
mefix 0:3c99f1705565 110 BiQuad bq212 = bq112;
mefix 0:3c99f1705565 111 BiQuad bq213 = bq113;
mefix 0:3c99f1705565 112 //High pass filter
mefix 0:3c99f1705565 113 BiQuad bq221 = bq121;
mefix 0:3c99f1705565 114 BiQuad bq222 = bq122;
mefix 0:3c99f1705565 115 BiQuad bq223 = bq123;
mefix 0:3c99f1705565 116 //Low pass filter
mefix 0:3c99f1705565 117 BiQuad bq231 = bq131;
mefix 0:3c99f1705565 118
mefix 0:3c99f1705565 119 //Define the BiQuads for the filter of the third emg signal
mefix 0:3c99f1705565 120 //notch filter
mefix 0:3c99f1705565 121 BiQuad bq311 = bq111;
mefix 0:3c99f1705565 122 BiQuad bq312 = bq112;
mefix 0:3c99f1705565 123 BiQuad bq313 = bq113;
mefix 0:3c99f1705565 124 //High pass filter
mefix 0:3c99f1705565 125 BiQuad bq321 = bq121;
mefix 0:3c99f1705565 126 BiQuad bq323 = bq122;
mefix 0:3c99f1705565 127 BiQuad bq322 = bq123;
mefix 0:3c99f1705565 128 //low pass filter
mefix 0:3c99f1705565 129 BiQuad bq331 = bq131;
mefix 0:3c99f1705565 130
mefix 0:3c99f1705565 131 void sampleflag()
mefix 0:3c99f1705565 132 {
mefix 0:3c99f1705565 133 if (sampletimer==true) {
mefix 0:3c99f1705565 134 // this if statement is used to see if the code takes too long before it is called again
mefix 0:3c99f1705565 135 pc.printf("rate too high error in sampleflag\n\r");
mefix 0:3c99f1705565 136 }
mefix 0:3c99f1705565 137 //This sets the go flag for when the function sample needs to be called
mefix 0:3c99f1705565 138 sampletimer=true;
mefix 0:3c99f1705565 139 }
mefix 0:3c99f1705565 140
mefix 0:3c99f1705565 141 void activate_controller()
mefix 0:3c99f1705565 142 {
mefix 0:3c99f1705565 143 if (controller_go==true) {
mefix 0:3c99f1705565 144 // this if statement is used to see if the code takes too long before it is called again
mefix 0:3c99f1705565 145 pc.printf("rate too high error in activate_controller()\n\r");
mefix 0:3c99f1705565 146 }
mefix 0:3c99f1705565 147 controller_go=true; //activate go flag
mefix 0:3c99f1705565 148 }
mefix 0:3c99f1705565 149
mefix 0:3c99f1705565 150 void activate_servo_control()
mefix 0:3c99f1705565 151 {
mefix 0:3c99f1705565 152 if (servo_go==true) {
mefix 0:3c99f1705565 153 pc.printf("error servo");
mefix 0:3c99f1705565 154 }
mefix 0:3c99f1705565 155 servo_go=true; //activate go flag
mefix 0:3c99f1705565 156 }
mefix 0:3c99f1705565 157
mefix 0:3c99f1705565 158 void sample()
mefix 0:3c99f1705565 159 {
mefix 0:3c99f1705565 160 //This checks if a key is pressed and changes the variable key in the pressed key
mefix 0:3c99f1705565 161 if (pc.readable()==1) {
mefix 0:3c99f1705565 162 key=pc.getc();
mefix 0:3c99f1705565 163 }
mefix 0:3c99f1705565 164 //Read the emg signals and filter it
mefix 0:3c99f1705565 165
mefix 0:3c99f1705565 166 emg02=bqc13.step(fabs(bqc11.step(emg1.read()))); //filtered signal 0
mefix 0:3c99f1705565 167 emg12=bqc23.step(fabs(bqc21.step(emg2.read()))); //filtered signal 1
mefix 0:3c99f1705565 168 emg22=bqc33.step(fabs(bqc31.step(emg3.read()))); //filtered signal 2
mefix 0:3c99f1705565 169
mefix 0:3c99f1705565 170 //remember what the reference was
mefix 0:3c99f1705565 171 old_ref_x=ref_x;
mefix 0:3c99f1705565 172 old_ref_y=ref_y;
mefix 0:3c99f1705565 173 //look if the emg signals go over the threshold and change the reference accordingly
mefix 0:3c99f1705565 174 if (emg02>threshold&&emg12>threshold&&emg22>threshold || key=='d') {
mefix 0:3c99f1705565 175 ref_x=ref_x-speed;
mefix 0:3c99f1705565 176 ref_y=ref_y-speed;
mefix 0:3c99f1705565 177
mefix 0:3c99f1705565 178 } else if (emg02>threshold&&emg12>threshold || key == 'a' || key == 'z') {
mefix 0:3c99f1705565 179 ref_x=ref_x-speed;
mefix 0:3c99f1705565 180
mefix 0:3c99f1705565 181 } else if (emg02>threshold&&emg22>threshold || key == 's') {
mefix 0:3c99f1705565 182 ref_y=ref_y-speed;
mefix 0:3c99f1705565 183
mefix 0:3c99f1705565 184 } else if (emg12>threshold&&emg22>threshold || key == 'e' ) {
mefix 0:3c99f1705565 185 ref_x=ref_x+speed;
mefix 0:3c99f1705565 186 ref_y=ref_y+speed;
mefix 0:3c99f1705565 187
mefix 0:3c99f1705565 188 } else if (emg12>threshold || key == 'q' ) {
mefix 0:3c99f1705565 189 ref_x=ref_x+speed;
mefix 0:3c99f1705565 190
mefix 0:3c99f1705565 191 } else if (emg22>threshold || key == 'w') {
mefix 0:3c99f1705565 192 ref_y=ref_y+speed;
mefix 0:3c99f1705565 193 }
mefix 0:3c99f1705565 194
mefix 0:3c99f1705565 195 if (key != 'z' && z_push) {
mefix 0:3c99f1705565 196 ref_x=0.0;
mefix 0:3c99f1705565 197 ref_y=0.0;
mefix 0:3c99f1705565 198 Encoder1.reset();
mefix 0:3c99f1705565 199 Encoder2.reset();
mefix 0:3c99f1705565 200 z_push=false;
mefix 0:3c99f1705565 201 }
mefix 0:3c99f1705565 202
mefix 0:3c99f1705565 203 // convert the x and y reference to the theta and radius reference
mefix 0:3c99f1705565 204 theta=atan(ref_y/(ref_x+minRadius));
mefix 0:3c99f1705565 205 radius=sqrt(pow(ref_x+minRadius,2)+pow(ref_y,2));
mefix 0:3c99f1705565 206
mefix 0:3c99f1705565 207 //look if the new reference is outside the possible range and revert back to the old reference if it is outside the range
mefix 0:3c99f1705565 208 if (radius < minRadius) {
mefix 0:3c99f1705565 209 if (key != 'z') {
mefix 0:3c99f1705565 210 ref_x=old_ref_x;
mefix 0:3c99f1705565 211 ref_y=old_ref_y;
mefix 0:3c99f1705565 212 } else if (key == 'z') {
mefix 0:3c99f1705565 213 z_push=true;
mefix 0:3c99f1705565 214 }
mefix 0:3c99f1705565 215 } else if ( radius > maxRadius) {
mefix 0:3c99f1705565 216 ref_x=old_ref_x;
mefix 0:3c99f1705565 217 ref_y=old_ref_y;
mefix 0:3c99f1705565 218 } else if (ref_y<min_y) {
mefix 0:3c99f1705565 219 ref_y=old_ref_y;
mefix 0:3c99f1705565 220 }
mefix 0:3c99f1705565 221 theta=atan(ref_y/(ref_x+minRadius));
mefix 0:3c99f1705565 222 radius=sqrt(pow(ref_x+minRadius,2)+pow(ref_y,2));
mefix 0:3c99f1705565 223 }
mefix 0:3c99f1705565 224
mefix 0:3c99f1705565 225 double PID( double err, const double Kp, const double Ki, const double Kd,
mefix 0:3c99f1705565 226 const double Ts, const double N, double &v1, double &v2 ) //discrete PIDF filter
mefix 0:3c99f1705565 227 {
mefix 0:3c99f1705565 228 const double a1 =-4/(N*Ts+2),
mefix 0:3c99f1705565 229 a2=-(N*Ts-2)/(N*Ts+2),
mefix 0:3c99f1705565 230 b0=(4*Kp + 4*Kd*N + 2*Ki*Ts+2*Kp*N*Ts+Ki*N*pow(Ts,2))/(2*N*Ts+4),
mefix 0:3c99f1705565 231 b1=(Ki*N*pow(Ts,2)-4*Kp-4*Kd*N)/(N*Ts+2),
mefix 0:3c99f1705565 232 b2=(4*Kp+4*Kd*N-2*Ki*Ts-2*Kp*N*Ts+Ki*N*pow(Ts,2))/(2*N*Ts+4);
mefix 0:3c99f1705565 233
mefix 0:3c99f1705565 234 double v=err-a1*v1-a2*v2;
mefix 0:3c99f1705565 235 double u=b0*v+b1*v1+b2*v2;
mefix 0:3c99f1705565 236 v2=v1;
mefix 0:3c99f1705565 237 v1=v;
mefix 0:3c99f1705565 238 return u;
mefix 0:3c99f1705565 239 }
mefix 0:3c99f1705565 240
mefix 0:3c99f1705565 241 void controller() //function for executing controller action
mefix 0:3c99f1705565 242 {
mefix 0:3c99f1705565 243
mefix 0:3c99f1705565 244 //converting radius and theta to gearbox angle
mefix 0:3c99f1705565 245 double ref_angle1=16*theta;
mefix 0:3c99f1705565 246 double ref_angle2=(-radius+minRadius)/pulleyRadius;
mefix 0:3c99f1705565 247
mefix 0:3c99f1705565 248 double angle1 = Encoder1.getPulses()/res; //get number of pulses (counterclockwise is positive)
mefix 0:3c99f1705565 249 double angle2 = Encoder2.getPulses()/res; //get number of pulses
mefix 0:3c99f1705565 250 m1_pwm = (PID(ref_angle1-angle1,m1_Kp,m1_Ki,m1_Kd,m1_Ts,m1_N,m1_v1,m1_v2))/V_max;
mefix 0:3c99f1705565 251 //divide by voltage to get pwm duty cycle percentage)
mefix 0:3c99f1705565 252 m2_pwm = (PID(ref_angle2-angle2,m2_Kp,m2_Ki,m2_Kd,m2_Ts,m2_N,m2_v1,m2_v2))/V_max;
mefix 0:3c99f1705565 253
mefix 0:3c99f1705565 254 //limit pwm value and change motor direction when pwm becomes either negative or positive
mefix 0:3c99f1705565 255 if (m1_pwm >=0.0f && m1_pwm <=1.0f) {
mefix 0:3c99f1705565 256 motor1dir=0;
mefix 0:3c99f1705565 257 motor1.write(m1_pwm);
mefix 0:3c99f1705565 258 } else if (m1_pwm < 0.0f && m1_pwm >= -1.0f) {
mefix 0:3c99f1705565 259 motor1dir=1;
mefix 0:3c99f1705565 260 motor1.write(-m1_pwm);
mefix 0:3c99f1705565 261 }
mefix 0:3c99f1705565 262
mefix 0:3c99f1705565 263 if (m2_pwm >=0.0f && m2_pwm <=1.0f) {
mefix 0:3c99f1705565 264 motor2dir=0;
mefix 0:3c99f1705565 265 motor2.write(m2_pwm);
mefix 0:3c99f1705565 266 } else if (m2_pwm < 0.0f && m2_pwm >= -1.0f) {
mefix 0:3c99f1705565 267 motor2dir=1;
mefix 0:3c99f1705565 268 motor2.write(-m2_pwm);
mefix 0:3c99f1705565 269 }
mefix 0:3c99f1705565 270
mefix 0:3c99f1705565 271 //hidsopce to check what the code does exactly
mefix 0:3c99f1705565 272 scope.set(0,ref_angle1-angle1); //error
mefix 0:3c99f1705565 273 scope.set(1,ref_angle1);
mefix 0:3c99f1705565 274 scope.set(2,m1_pwm);
mefix 0:3c99f1705565 275 scope.set(3,ref_angle2-angle2);
mefix 0:3c99f1705565 276 scope.set(4,ref_angle2);
mefix 0:3c99f1705565 277 scope.set(5,servo_pwm);
mefix 0:3c99f1705565 278 scope.send();
mefix 0:3c99f1705565 279 }
mefix 0:3c99f1705565 280
mefix 0:3c99f1705565 281 void servo_controller()
mefix 0:3c99f1705565 282 {
mefix 0:3c99f1705565 283 if (theta < maxTheta ) {
mefix 0:3c99f1705565 284 servo_pwm=min_servo_pwm+(theta-minTheta)/diffTheta*res_servo;
mefix 0:3c99f1705565 285 } else {
mefix 0:3c99f1705565 286 servo_pwm=max_servo_pwm;
mefix 0:3c99f1705565 287 }
mefix 0:3c99f1705565 288
mefix 0:3c99f1705565 289 servo.pulsewidth(servo_pwm);
mefix 0:3c99f1705565 290
mefix 0:3c99f1705565 291 }
mefix 0:3c99f1705565 292
mefix 0:3c99f1705565 293
mefix 0:3c99f1705565 294 void my_pos()
mefix 0:3c99f1705565 295 {
mefix 0:3c99f1705565 296 //This function is attached to a ticker so that the reference position is printed every second.
mefix 0:3c99f1705565 297 pc.printf("x_pos=%.4f\ty_pos=%.4f\tradius=%.4f\tangle=%.4f\n\r",ref_x,ref_y,radius,theta);
mefix 0:3c99f1705565 298
mefix 0:3c99f1705565 299 }
mefix 0:3c99f1705565 300
mefix 0:3c99f1705565 301 int main()
mefix 0:3c99f1705565 302 {
mefix 0:3c99f1705565 303 pc.printf("RESET\n\r");
mefix 0:3c99f1705565 304 pc.baud(115200);
mefix 0:3c99f1705565 305
mefix 0:3c99f1705565 306 //Attach the Biquads to the Biquad chains
mefix 0:3c99f1705565 307 bqc11.add( &bq111 ).add( &bq112 ).add( &bq113 ).add( &bq121 ).add( &bq122 ).add( &bq123 );
mefix 0:3c99f1705565 308 bqc13.add( &bq131);
mefix 0:3c99f1705565 309 bqc21.add( &bq211 ).add( &bq212 ).add( &bq213 ).add( &bq221 ).add( &bq222 ).add( &bq223 );
mefix 0:3c99f1705565 310 bqc23.add( &bq231);
mefix 0:3c99f1705565 311 bqc31.add( &bq311 ).add( &bq312 ).add( &bq313 ).add( &bq321 ).add( &bq322 ).add( &bq323 );
mefix 0:3c99f1705565 312 bqc33.add( &bq331);
mefix 0:3c99f1705565 313
mefix 0:3c99f1705565 314 motor1.period(0.02f); //period of pwm signal for motor 1
mefix 0:3c99f1705565 315 motor2.period(0.02f); // period of pwm signal for motor 2
mefix 0:3c99f1705565 316 motor1dir=0; // setting direction to ccw
mefix 0:3c99f1705565 317 motor2dir=0; // setting direction to ccw
mefix 0:3c99f1705565 318
mefix 0:3c99f1705565 319 //Attach the 'sample' function to the timer 'sample_timer'.
mefix 0:3c99f1705565 320 //this ensures that 'sample' is executed every 0.002 seconds = 500 Hz
mefix 0:3c99f1705565 321 sample_timer.attach(&sampleflag, samplefreq);
mefix 0:3c99f1705565 322
mefix 0:3c99f1705565 323 //Attach the function my_pos to the timer pos_timer.
mefix 0:3c99f1705565 324 //This ensures that the position is printed every second.
mefix 0:3c99f1705565 325 pos_timer.attach(&my_pos, 1);
mefix 0:3c99f1705565 326 control.attach(&activate_controller,m1_Ts); //Ticker for processing encoder input
mefix 0:3c99f1705565 327 servo_control.attach(&activate_servo_control,servo_Ts);
mefix 0:3c99f1705565 328
mefix 0:3c99f1705565 329 while(1) {
mefix 0:3c99f1705565 330 //Only take a sample when the go flag is true.
mefix 0:3c99f1705565 331 if (sampletimer==true) {
mefix 0:3c99f1705565 332 sample();
mefix 0:3c99f1705565 333 sampletimer = false; //change sampletimer to false if sample() is finished
mefix 0:3c99f1705565 334 }
mefix 0:3c99f1705565 335 if(controller_go) { // go flag
mefix 0:3c99f1705565 336 controller();
mefix 0:3c99f1705565 337 controller_go=false;
mefix 0:3c99f1705565 338 }
mefix 0:3c99f1705565 339 if(servo_go) {
mefix 0:3c99f1705565 340 servo_controller();
mefix 0:3c99f1705565 341 servo_go=false;
mefix 0:3c99f1705565 342 }
mefix 0:3c99f1705565 343 }
mefix 0:3c99f1705565 344 }