control flow doet nog niks

Dependencies:   Encoder HIDScope MODSERIAL mbed

Committer:
Zeekat
Date:
Mon Oct 12 08:44:50 2015 +0000
Revision:
7:4df9f6ebe744
Parent:
6:9c8e91bb1316
Child:
8:00c3992af9f4
redelijk goed. mar doet niks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Zeekat 0:f6306e179750 1 #include "mbed.h"
Zeekat 0:f6306e179750 2 #include "MODSERIAL.h"
Zeekat 0:f6306e179750 3 #include "encoder.h"
Zeekat 2:2563df4ee829 4 #include "HIDScope.h"
Zeekat 0:f6306e179750 5
Zeekat 0:f6306e179750 6 Serial pc(USBTX,USBRX);
Zeekat 2:2563df4ee829 7 HIDScope scope(2); // definieerd het aantal kanalen van de scope
Zeekat 0:f6306e179750 8
Zeekat 2:2563df4ee829 9 // Define Tickers and control frequencies
Zeekat 0:f6306e179750 10 Ticker controller1, controller2; // definieer de ticker die controler1 doet
Zeekat 2:2563df4ee829 11 // Go flag variables
Zeekat 2:2563df4ee829 12 volatile bool motor1_go = false, motor2_go = false;
Zeekat 2:2563df4ee829 13
Zeekat 2:2563df4ee829 14 // Frequency control
Zeekat 2:2563df4ee829 15 double controlfreq = 50 ; // controlloops frequentie (Hz)
Zeekat 2:2563df4ee829 16 double controlstep = 1/controlfreq; // timestep derived from controlfreq
Zeekat 0:f6306e179750 17
Zeekat 2:2563df4ee829 18
Zeekat 2:2563df4ee829 19 //MOTOR OUTPUTPINS
Zeekat 0:f6306e179750 20 // motor 1
Zeekat 2:2563df4ee829 21 PwmOut motor1_aan(D6); // PWM signaal motor 2 (uit sheets)
Zeekat 2:2563df4ee829 22 DigitalOut motor1_rich(D7); // digitaal signaal voor richting
Zeekat 0:f6306e179750 23 // motor 2
Zeekat 2:2563df4ee829 24 PwmOut motor2_aan(D5);
Zeekat 2:2563df4ee829 25 DigitalOut motor2_rich(D4);
Zeekat 0:f6306e179750 26
Zeekat 2:2563df4ee829 27 // ENCODER INPUTPINS
Zeekat 0:f6306e179750 28 Encoder motor1_enc(D12,D11); // encoder outputpins
Zeekat 0:f6306e179750 29 Encoder motor2_enc(D10,D9);
Zeekat 0:f6306e179750 30
Zeekat 0:f6306e179750 31 int reference1 = 0; // set the reference position of the encoders
Zeekat 0:f6306e179750 32 int reference2 = 0;
Zeekat 0:f6306e179750 33
Zeekat 2:2563df4ee829 34
Zeekat 2:2563df4ee829 35 // EXTRA INPUTS AND REQUIRED VARIABLES
Zeekat 2:2563df4ee829 36 //POTMETERS
Zeekat 2:2563df4ee829 37 AnalogIn potright(A0); // define the potmeter outputpins
Zeekat 2:2563df4ee829 38 AnalogIn potleft(A1);
Zeekat 2:2563df4ee829 39
Zeekat 6:9c8e91bb1316 40 // BUTTONS
Zeekat 6:9c8e91bb1316 41 // control flow
Zeekat 4:072b99947fc6 42 DigitalIn buttonlinks(PTA4);
Zeekat 4:072b99947fc6 43 DigitalIn buttonrechts(PTC6);
Zeekat 4:072b99947fc6 44 // init values
Zeekat 4:072b99947fc6 45 bool loop_start = false;
Zeekat 4:072b99947fc6 46 bool calib_start = false;
Zeekat 6:9c8e91bb1316 47
Zeekat 6:9c8e91bb1316 48 // direction control
Zeekat 6:9c8e91bb1316 49 DigitalIn reverse_button_links(D0);
Zeekat 6:9c8e91bb1316 50 DigitalIn reverse_button_rechts(D1);
Zeekat 6:9c8e91bb1316 51 // init values
Zeekat 6:9c8e91bb1316 52 bool reverse_links = false;
Zeekat 6:9c8e91bb1316 53 bool reverse_rechts = false;
Zeekat 2:2563df4ee829 54
Zeekat 4:072b99947fc6 55 // LED
Zeekat 4:072b99947fc6 56 DigitalOut ledred(LED1);
Zeekat 4:072b99947fc6 57 DigitalOut ledgreen(LED2);
Zeekat 4:072b99947fc6 58 DigitalOut ledblue(LED3);
Zeekat 2:2563df4ee829 59
Zeekat 3:7273bbe6aa02 60 // REFERENCE SIGNAL SETTINGS
Zeekat 7:4df9f6ebe744 61 double input_threshold = 0.25; // the minimum value the signal must have to change the reference.
Zeekat 3:7273bbe6aa02 62 // Define signal amplification (needed with EMG, used in control loop, precise amp determination is a work in progress!!!!) ??
Zeekat 3:7273bbe6aa02 63 double signalamp1 = 1;
Zeekat 3:7273bbe6aa02 64 double signalamp2 = 1;
Zeekat 6:9c8e91bb1316 65 // Define storage variables for reference values
Zeekat 3:7273bbe6aa02 66 double c_reference1 = 0;
Zeekat 3:7273bbe6aa02 67 double c_reference2 = 0;
Zeekat 3:7273bbe6aa02 68 // Define the maximum rate of change for the reference (velocity)
Zeekat 2:2563df4ee829 69 double Vmax = 5; // rad/sec
Zeekat 2:2563df4ee829 70
Zeekat 0:f6306e179750 71 // CONTROLLER SETTINGS
Zeekat 1:2f23368e8638 72 // motor 1
Zeekat 1:2f23368e8638 73 const double m1_Kp = 1; // Proportional constant
Zeekat 2:2563df4ee829 74 const double m1_Ki = 1; // integration constant
Zeekat 1:2f23368e8638 75 const double m1_Kd = 0; // differentiation constant
Zeekat 1:2f23368e8638 76 // motor 2
Zeekat 1:2f23368e8638 77 const double m2_Kp = 1;
Zeekat 1:2f23368e8638 78 const double m2_Ki = 0;
Zeekat 1:2f23368e8638 79 const double m2_Kd = 0;
Zeekat 1:2f23368e8638 80 // storage variables
Zeekat 1:2f23368e8638 81 // motor 1
Zeekat 1:2f23368e8638 82 double m1_err_int = 0;
Zeekat 1:2f23368e8638 83 double m1_prev_err = 0;
Zeekat 1:2f23368e8638 84 // motor 2
Zeekat 1:2f23368e8638 85 double m2_err_int = 0;
Zeekat 1:2f23368e8638 86 double m2_prev_err = 0;
Zeekat 0:f6306e179750 87
Zeekat 0:f6306e179750 88
Zeekat 1:2f23368e8638 89 //// FILTER VARIABLES
Zeekat 1:2f23368e8638 90 // storage variables
Zeekat 1:2f23368e8638 91 // differential action filter, same is used for both controllers
Zeekat 1:2f23368e8638 92 double m_f_v1 = 0;
Zeekat 1:2f23368e8638 93 double m_f_v2 = 0;
Zeekat 1:2f23368e8638 94
Zeekat 1:2f23368e8638 95 // Filter coefficients
Zeekat 6:9c8e91bb1316 96 // differential action filter
Zeekat 6:9c8e91bb1316 97 const double m_f_a1 = 1.0, m_f_a2 = 2.0, m_f_b0 = 1.0, m_f_b1 = 3.0, m_f_b2 = 4.0; // coefficients from sheets are used as first test.
Zeekat 5:67afe491a1e3 98
Zeekat 5:67afe491a1e3 99 // tweede orde notch filter 50 Hz
Zeekat 5:67afe491a1e3 100 // biquad 1 coefficienten
Zeekat 5:67afe491a1e3 101 const double numnotch50biq1_1 = 1;
Zeekat 5:67afe491a1e3 102 const double numnotch50biq1_2 = -1.61816178466632;
Zeekat 5:67afe491a1e3 103 const double numnotch50biq1_3 = 1.00000006127058;
Zeekat 5:67afe491a1e3 104 const double dennotch50biq1_2 = -1.59325742941798;
Zeekat 5:67afe491a1e3 105 const double dennotch50biq1_3 = 0.982171881701431;
Zeekat 5:67afe491a1e3 106 // biquad 2 coefficienten
Zeekat 5:67afe491a1e3 107 const double numnotch50biq2_1 = 1;
Zeekat 5:67afe491a1e3 108 const double numnotch50biq2_2 = -1.61816171933244;
Zeekat 5:67afe491a1e3 109 const double numnotch50biq2_3 = 0.999999938729428;
Zeekat 5:67afe491a1e3 110 const double dennotch50biq2_2 = -1.61431180968071;
Zeekat 5:67afe491a1e3 111 const double dennotch50biq2_3 = 0.982599066293075;
Zeekat 5:67afe491a1e3 112 // highpass filter 20 Hz coefficienten
Zeekat 5:67afe491a1e3 113 const double numhigh20_1 = 0.837089190566345;
Zeekat 5:67afe491a1e3 114 const double numhigh20_2 = -1.67417838113269;
Zeekat 5:67afe491a1e3 115 const double numhigh20_3 = 0.837089190566345;
Zeekat 5:67afe491a1e3 116 const double denhigh20_2 = -1.64745998107698;
Zeekat 5:67afe491a1e3 117 const double denhigh20_3 = 0.700896781188403;
Zeekat 5:67afe491a1e3 118 // lowpass 5 Hz coefficienten
Zeekat 5:67afe491a1e3 119 const double numlow5_1 =0.000944691843840162;
Zeekat 5:67afe491a1e3 120 const double numlow5_2 =0.00188938368768032;
Zeekat 5:67afe491a1e3 121 const double numlow5_3 =0.000944691843840162;
Zeekat 5:67afe491a1e3 122 const double denlow5_2 =-1.91119706742607;
Zeekat 5:67afe491a1e3 123 const double denlow5_3 =0.914975834801434;
Zeekat 5:67afe491a1e3 124
Zeekat 5:67afe491a1e3 125 // Define the storage variables and filter coeicients for two filters
Zeekat 5:67afe491a1e3 126 double f1_v1 = 0, f1_v2 = 0, f2_v1 = 0, f2_v2 = 0, f3_v1 = 0, f3_v2 = 0,f4_v1 = 0, f4_v2 = 0;
Zeekat 0:f6306e179750 127
Zeekat 0:f6306e179750 128
Zeekat 0:f6306e179750 129
Zeekat 0:f6306e179750 130 ////////////////////////////////////////////////////////////////
Zeekat 0:f6306e179750 131 /////////////////// START OF SIDE FUNCTIONS ////////////////////
Zeekat 0:f6306e179750 132 //////////////////////////////////////////////////////////////
Zeekat 0:f6306e179750 133 // these functions are tailored to perform 1 specific function
Zeekat 0:f6306e179750 134
Zeekat 5:67afe491a1e3 135 // this funtion flips leds on and off accordin to input with 0 being on
Zeekat 5:67afe491a1e3 136 void LED(int red,int green,int blue)
Zeekat 5:67afe491a1e3 137 {
Zeekat 5:67afe491a1e3 138 ledred.write(red);
Zeekat 5:67afe491a1e3 139 ledgreen.write(green);
Zeekat 5:67afe491a1e3 140 ledblue.write(blue);
Zeekat 5:67afe491a1e3 141 }
Zeekat 5:67afe491a1e3 142
Zeekat 0:f6306e179750 143 // counts 2 radians
Zeekat 0:f6306e179750 144 // this function takes counts from the encoder and converts it to the amount of radians from the zero position.
Zeekat 0:f6306e179750 145 // It has been set up for standard 2X DECODING!!!!!!
Zeekat 0:f6306e179750 146 double get_radians(double counts)
Zeekat 0:f6306e179750 147 {
Zeekat 0:f6306e179750 148 double pi = 3.14159265359;
Zeekat 0:f6306e179750 149 double radians = (counts/4200)*2*pi; // 2X DECODING!!!!! ((32 counts/rotation, extra warning)
Zeekat 0:f6306e179750 150 return radians;
Zeekat 0:f6306e179750 151 }
Zeekat 0:f6306e179750 152
Zeekat 0:f6306e179750 153
Zeekat 7:4df9f6ebe744 154 // This functions takes a 0->1 input, uses passing by reference (&c_reference)
Zeekat 7:4df9f6ebe744 155 // to create a reference that moves with a variable speed. It is now optimised for 0->1 values
Zeekat 3:7273bbe6aa02 156 double reference_f(double input, double &c_reference)
Zeekat 0:f6306e179750 157 {
Zeekat 6:9c8e91bb1316 158 double reference = c_reference + input * controlstep * Vmax ;
Zeekat 3:7273bbe6aa02 159 c_reference = reference; // change the global variable to the latest location.
Zeekat 3:7273bbe6aa02 160 return reference;
Zeekat 0:f6306e179750 161 }
Zeekat 0:f6306e179750 162
Zeekat 0:f6306e179750 163
Zeekat 3:7273bbe6aa02 164 // This function takes the controller outputvalue and ensures it is between -1 and 1
Zeekat 0:f6306e179750 165 // this is done to limit the motor input to possible values (the motor takes 0 to 1 and the sign changes the direction).
Zeekat 3:7273bbe6aa02 166 // needs more work to use it for the wind-up prevention.
Zeekat 2:2563df4ee829 167 double outputlimiter (double output, double limit)
Zeekat 0:f6306e179750 168 {
Zeekat 2:2563df4ee829 169 if(output> limit)
Zeekat 0:f6306e179750 170 {
Zeekat 0:f6306e179750 171 output = 1;
Zeekat 0:f6306e179750 172 }
Zeekat 2:2563df4ee829 173 else if(output < limit && output > 0)
Zeekat 0:f6306e179750 174 {
Zeekat 0:f6306e179750 175 output = output;
Zeekat 0:f6306e179750 176 }
Zeekat 2:2563df4ee829 177 else if(output > -limit && output < 0)
Zeekat 0:f6306e179750 178 {
Zeekat 0:f6306e179750 179 output = output;
Zeekat 0:f6306e179750 180 }
Zeekat 2:2563df4ee829 181 else if(output < -limit)
Zeekat 0:f6306e179750 182 {
Zeekat 0:f6306e179750 183 (output = -1);
Zeekat 0:f6306e179750 184 }
Zeekat 0:f6306e179750 185 return output;
Zeekat 0:f6306e179750 186 }
Zeekat 0:f6306e179750 187
Zeekat 0:f6306e179750 188
Zeekat 1:2f23368e8638 189 // BIQUADFILTER CODE GIVEN IN SHEETS
Zeekat 1:2f23368e8638 190 // used for d-control
Zeekat 1:2f23368e8638 191 double biquadfilter(double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2)
Zeekat 1:2f23368e8638 192 {
Zeekat 3:7273bbe6aa02 193 double v = u - a1*v1 - a2*v2;
Zeekat 3:7273bbe6aa02 194 double y = b0*v + b1*v1 + b2*v2;
Zeekat 1:2f23368e8638 195 v2 = v1;
Zeekat 1:2f23368e8638 196 v1 = v;
Zeekat 1:2f23368e8638 197 return y;
Zeekat 1:2f23368e8638 198 }
Zeekat 1:2f23368e8638 199
Zeekat 5:67afe491a1e3 200 double EMG_Filter(double u1)
Zeekat 5:67afe491a1e3 201 {
Zeekat 5:67afe491a1e3 202 // double u1 = potright.read();
Zeekat 5:67afe491a1e3 203 double y1 = biquadfilter( u1, f1_v1, f1_v2,dennotch50biq1_2, dennotch50biq1_3,numnotch50biq1_1,numnotch50biq1_2,numnotch50biq1_3);
Zeekat 5:67afe491a1e3 204 double y2 = biquadfilter( y1, f2_v1, f2_v2,dennotch50biq2_2, dennotch50biq2_3,numnotch50biq2_1,numnotch50biq2_2,numnotch50biq2_3);
Zeekat 5:67afe491a1e3 205 double y3 = biquadfilter( y2, f3_v1, f3_v2, denhigh20_2,denhigh20_3,numhigh20_1, numhigh20_2, numhigh20_3);
Zeekat 5:67afe491a1e3 206 double y4 = abs(y3);
Zeekat 5:67afe491a1e3 207 double y5 = biquadfilter( y4, f4_v1, f4_v2, denlow5_2,denlow5_3,numlow5_1, numlow5_2, numlow5_3);
Zeekat 5:67afe491a1e3 208 return y5;
Zeekat 5:67afe491a1e3 209 }
Zeekat 1:2f23368e8638 210
Zeekat 1:2f23368e8638 211 // PID Controller given in sheets
Zeekat 1:2f23368e8638 212 // aangepast om zelfde filter te gebruiken en om de termen later gesplitst te kunnen limiteren (windup preventie!!)
Zeekat 2:2563df4ee829 213 double PID(double e, const double Kp, const double Ki, const double Kd, double Ts,double &e_int, double &e_prev)
Zeekat 1:2f23368e8638 214 {
Zeekat 1:2f23368e8638 215 // Proportional
Zeekat 1:2f23368e8638 216 double P = Kp * e;
Zeekat 3:7273bbe6aa02 217 pc.printf("P = %f, ",P);
Zeekat 1:2f23368e8638 218 // Integral
Zeekat 1:2f23368e8638 219 e_int = e_int + Ts * e;
Zeekat 1:2f23368e8638 220 double I = e_int * Ki;
Zeekat 3:7273bbe6aa02 221 pc.printf("I = %f, ",I);
Zeekat 3:7273bbe6aa02 222 // Derivative (Disabled for now because of NaN problem from filter
Zeekat 3:7273bbe6aa02 223 double e_derr = (e - e_prev)/Ts;
Zeekat 3:7273bbe6aa02 224 pc.printf("e_derr %f, ",e_derr);
Zeekat 3:7273bbe6aa02 225 e_derr = biquadfilter(e_derr, m_f_v1, m_f_v2, m_f_a1, m_f_a2, m_f_b0, m_f_b1, m_f_b2);
Zeekat 3:7273bbe6aa02 226 pc.printf("fil_e_derr %f, ",e_derr); // check for NaN ??
Zeekat 3:7273bbe6aa02 227 e_prev = e;
Zeekat 3:7273bbe6aa02 228 // double D = Kd* e_derr;
Zeekat 1:2f23368e8638 229
Zeekat 1:2f23368e8638 230 // PID
Zeekat 2:2563df4ee829 231 double output = P + I ;// + D;
Zeekat 1:2f23368e8638 232 return output;
Zeekat 1:2f23368e8638 233 }
Zeekat 1:2f23368e8638 234
Zeekat 1:2f23368e8638 235
Zeekat 1:2f23368e8638 236
Zeekat 0:f6306e179750 237 // this function is a simple K control called by the motor function
Zeekat 0:f6306e179750 238 double K_control(double error,double K)
Zeekat 0:f6306e179750 239 {
Zeekat 0:f6306e179750 240 double output = K*error; // controller output K*e
Zeekat 2:2563df4ee829 241 output = outputlimiter(output,1); // limit the output to -1->1
Zeekat 0:f6306e179750 242 return output;
Zeekat 0:f6306e179750 243 }
Zeekat 0:f6306e179750 244
Zeekat 0:f6306e179750 245 /////////////////////////////////////////////////////////////////////
Zeekat 0:f6306e179750 246 ////////////////// PRIMARY CONTROL FUNCTIONS ///////////////////////
Zeekat 0:f6306e179750 247 ///////////////////////////////////////////////////////////////////
Zeekat 0:f6306e179750 248 // these functions are used to control all aspects of a single electric motor and are called by the main function
Zeekat 0:f6306e179750 249
Zeekat 0:f6306e179750 250 // MOTOR 1
Zeekat 0:f6306e179750 251 void motor1_control()
Zeekat 0:f6306e179750 252 {
Zeekat 5:67afe491a1e3 253
Zeekat 7:4df9f6ebe744 254 double input1 = potright.read()*signalamp1; // this line must be seperated for emg usage
Zeekat 7:4df9f6ebe744 255 // first data limiter
Zeekat 7:4df9f6ebe744 256 if(input1 < input_threshold) {input1 = 0;}
Zeekat 6:9c8e91bb1316 257 if(input1 > 1) {input1 = 1;}
Zeekat 7:4df9f6ebe744 258 // reverse direction
Zeekat 6:9c8e91bb1316 259 if(reverse_rechts == true) {input1 = -input1;}
Zeekat 3:7273bbe6aa02 260 double reference1 = reference_f(input1, c_reference1); // determine the reference that has been set by the inputsignal
Zeekat 0:f6306e179750 261 double rads1 = get_radians(motor1_enc.getPosition()); // determine the position of the motor
Zeekat 3:7273bbe6aa02 262 double error1 = (reference1 - rads1); // determine the error (reference - position)
Zeekat 1:2f23368e8638 263 double output1 = PID(error1, m1_Kp, m1_Ki, m1_Kd, controlstep, m1_err_int, m1_prev_err);
Zeekat 2:2563df4ee829 264 pc.printf("output 1 %f \n",output1);
Zeekat 0:f6306e179750 265 if(output1 > 0) { // uses the calculated output to determine the direction of the motor
Zeekat 0:f6306e179750 266 motor1_rich.write(0);
Zeekat 0:f6306e179750 267 motor1_aan.write(output1);
Zeekat 0:f6306e179750 268 } else if(output1 < 0) {
Zeekat 0:f6306e179750 269 motor1_rich.write(1);
Zeekat 0:f6306e179750 270 motor1_aan.write(abs(output1));
Zeekat 0:f6306e179750 271 }
Zeekat 0:f6306e179750 272 }
Zeekat 0:f6306e179750 273
Zeekat 0:f6306e179750 274 // MOTOR 2
Zeekat 0:f6306e179750 275 void motor2_control()
Zeekat 0:f6306e179750 276 {
Zeekat 5:67afe491a1e3 277 double input2 = potleft.read()*signalamp2; // replace potleft with filter
Zeekat 7:4df9f6ebe744 278 // first input limiter
Zeekat 7:4df9f6ebe744 279 if(input2 < input_threshold) {input2 = 0;}
Zeekat 6:9c8e91bb1316 280 if(input2 > 1) {input2 = 1;}
Zeekat 7:4df9f6ebe744 281 // reverse direction
Zeekat 6:9c8e91bb1316 282 if(reverse_links == true) {input2 = -input2;}
Zeekat 6:9c8e91bb1316 283 double reference2 = reference_f(input2, c_reference2); // determine the reference that has been set by the clamped inputsignal
Zeekat 0:f6306e179750 284 double rads2 = get_radians(motor2_enc.getPosition()); // determine the position of the motor
Zeekat 3:7273bbe6aa02 285 double error2 = (reference2 - rads2); // determine the error (reference - position)
Zeekat 1:2f23368e8638 286 double output2 = PID(error2, m2_Kp, m2_Ki, m2_Kd, controlstep, m2_err_int, m2_prev_err);
Zeekat 0:f6306e179750 287 if(output2 > 0) { // uses the calculated output to determine the direction of the motor
Zeekat 0:f6306e179750 288 motor2_rich.write(0);
Zeekat 0:f6306e179750 289 motor2_aan.write(output2);
Zeekat 0:f6306e179750 290 } else if(output2 < 0) {
Zeekat 0:f6306e179750 291 motor2_rich.write(1);
Zeekat 0:f6306e179750 292 motor2_aan.write(abs(output2));
Zeekat 0:f6306e179750 293 }
Zeekat 0:f6306e179750 294 }
Zeekat 0:f6306e179750 295
Zeekat 0:f6306e179750 296
Zeekat 2:2563df4ee829 297 //////////////////////////////////////////////////////////////////
Zeekat 2:2563df4ee829 298 //////////// DEFINE GO-FLAG FUNCTIONS ///////////////////////////
Zeekat 2:2563df4ee829 299 ////////////////////////////////////////////////////////////////
Zeekat 2:2563df4ee829 300
Zeekat 2:2563df4ee829 301 void motor1_activate()
Zeekat 2:2563df4ee829 302 {
Zeekat 2:2563df4ee829 303 motor1_go = true;
Zeekat 2:2563df4ee829 304 }
Zeekat 2:2563df4ee829 305
Zeekat 2:2563df4ee829 306 void motor2_activate()
Zeekat 2:2563df4ee829 307 {
Zeekat 2:2563df4ee829 308 motor2_go = true;
Zeekat 2:2563df4ee829 309 }
Zeekat 2:2563df4ee829 310
Zeekat 0:f6306e179750 311 int main()
Zeekat 0:f6306e179750 312 {
Zeekat 2:2563df4ee829 313 pc.baud(115200);
Zeekat 4:072b99947fc6 314 controller1.attach(&motor1_activate, controlstep); // call a go-flag
Zeekat 4:072b99947fc6 315 // controller2.attach(&motor2_activate, controlstep); Disabled while debugging PID-controller ??
Zeekat 0:f6306e179750 316 while(true)
Zeekat 0:f6306e179750 317 {
Zeekat 6:9c8e91bb1316 318 // button press functions
Zeekat 6:9c8e91bb1316 319 // flow buttons
Zeekat 4:072b99947fc6 320 if(buttonlinks.read() == 0)
Zeekat 2:2563df4ee829 321 {
Zeekat 6:9c8e91bb1316 322 loop_start = !loop_start;
Zeekat 4:072b99947fc6 323 wait(buttonlinks.read() == 1);
Zeekat 6:9c8e91bb1316 324 wait(0.3);
Zeekat 2:2563df4ee829 325 }
Zeekat 4:072b99947fc6 326 if(buttonrechts.read() == 0)
Zeekat 4:072b99947fc6 327 {
Zeekat 6:9c8e91bb1316 328 calib_start = !calib_start;
Zeekat 4:072b99947fc6 329 wait(buttonrechts.read() == 1);
Zeekat 6:9c8e91bb1316 330 wait(0.3);
Zeekat 4:072b99947fc6 331 }
Zeekat 6:9c8e91bb1316 332 // reverse buttons
Zeekat 6:9c8e91bb1316 333 if(reverse_button_links.read() == 0)
Zeekat 6:9c8e91bb1316 334 {
Zeekat 7:4df9f6ebe744 335 reverse_links = !reverse_links;
Zeekat 7:4df9f6ebe744 336 wait(reverse_button_links.read() == 1);
Zeekat 7:4df9f6ebe744 337 wait(0.3);
Zeekat 6:9c8e91bb1316 338 }
Zeekat 7:4df9f6ebe744 339 if(reverse_button_rechts.read() == 0)
Zeekat 6:9c8e91bb1316 340 {
Zeekat 6:9c8e91bb1316 341 reverse_rechts = !reverse_rechts;
Zeekat 6:9c8e91bb1316 342 wait(reverse_button_rechts.read() == 1);
Zeekat 6:9c8e91bb1316 343 wait(0.3);
Zeekat 6:9c8e91bb1316 344 }
Zeekat 4:072b99947fc6 345 // Main Control stuff and options
Zeekat 4:072b99947fc6 346 if(loop_start == true && calib_start == false) // check if start button = true then start the main control loops
Zeekat 2:2563df4ee829 347 {
Zeekat 5:67afe491a1e3 348 LED(1,1,0); // turn blue led on
Zeekat 7:4df9f6ebe744 349 if(motor1_go) { motor1_go = false; motor1_control();}
Zeekat 7:4df9f6ebe744 350 if(motor2_go) { motor2_go = false; motor2_control();}
Zeekat 2:2563df4ee829 351 }
Zeekat 7:4df9f6ebe744 352 // turn green led on // start calibration procedures
Zeekat 7:4df9f6ebe744 353 if(loop_start == false && calib_start == true) { LED(1,0,1);}
Zeekat 7:4df9f6ebe744 354 // turn red led on
Zeekat 7:4df9f6ebe744 355 if(loop_start == true && calib_start == true) { LED(0,1,1);}
Zeekat 7:4df9f6ebe744 356 // turn leds off (both buttons false)
Zeekat 7:4df9f6ebe744 357 else { LED(1,1,1);}
Zeekat 0:f6306e179750 358 }
Zeekat 0:f6306e179750 359 }