control flow doet nog niks
Dependencies: Encoder HIDScope MODSERIAL mbed
main.cpp@5:67afe491a1e3, 2015-10-10 (annotated)
- Committer:
- Zeekat
- Date:
- Sat Oct 10 20:02:16 2015 +0000
- Revision:
- 5:67afe491a1e3
- Parent:
- 4:072b99947fc6
- Child:
- 6:9c8e91bb1316
added in filter stuff, not yet applied to control, current stuff must be tested first
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:072b99947fc6 | 40 | // BUTTONS |
Zeekat | 4:072b99947fc6 | 41 | DigitalIn buttonlinks(PTA4); |
Zeekat | 4:072b99947fc6 | 42 | DigitalIn buttonrechts(PTC6); |
Zeekat | 4:072b99947fc6 | 43 | // init values |
Zeekat | 4:072b99947fc6 | 44 | bool loop_start = false; |
Zeekat | 4:072b99947fc6 | 45 | bool calib_start = false; |
Zeekat | 2:2563df4ee829 | 46 | |
Zeekat | 4:072b99947fc6 | 47 | // LED |
Zeekat | 4:072b99947fc6 | 48 | DigitalOut ledred(LED1); |
Zeekat | 4:072b99947fc6 | 49 | DigitalOut ledgreen(LED2); |
Zeekat | 4:072b99947fc6 | 50 | DigitalOut ledblue(LED3); |
Zeekat | 2:2563df4ee829 | 51 | |
Zeekat | 3:7273bbe6aa02 | 52 | // REFERENCE SIGNAL SETTINGS |
Zeekat | 3:7273bbe6aa02 | 53 | // Define signal amplification (needed with EMG, used in control loop, precise amp determination is a work in progress!!!!) ?? |
Zeekat | 3:7273bbe6aa02 | 54 | double signalamp1 = 1; |
Zeekat | 3:7273bbe6aa02 | 55 | double signalamp2 = 1; |
Zeekat | 3:7273bbe6aa02 | 56 | // Define gain and offset of the input(input between 0 and 1 is optimized). For EMG use 0, 1 and false, for POT use 0.5, 2 and true |
Zeekat | 3:7273bbe6aa02 | 57 | double offset = 0.5; |
Zeekat | 3:7273bbe6aa02 | 58 | double gain = 2; |
Zeekat | 3:7273bbe6aa02 | 59 | bool POT_in = true; // show potmeter is attached, increases the range for which 0 is the output. |
Zeekat | 3:7273bbe6aa02 | 60 | // Define storage variables for reference values |
Zeekat | 3:7273bbe6aa02 | 61 | double c_reference1 = 0; |
Zeekat | 3:7273bbe6aa02 | 62 | double c_reference2 = 0; |
Zeekat | 3:7273bbe6aa02 | 63 | // Define the maximum rate of change for the reference (velocity) |
Zeekat | 2:2563df4ee829 | 64 | double Vmax = 5; // rad/sec |
Zeekat | 2:2563df4ee829 | 65 | |
Zeekat | 0:f6306e179750 | 66 | // CONTROLLER SETTINGS |
Zeekat | 1:2f23368e8638 | 67 | // motor 1 |
Zeekat | 1:2f23368e8638 | 68 | const double m1_Kp = 1; // Proportional constant |
Zeekat | 2:2563df4ee829 | 69 | const double m1_Ki = 1; // integration constant |
Zeekat | 1:2f23368e8638 | 70 | const double m1_Kd = 0; // differentiation constant |
Zeekat | 1:2f23368e8638 | 71 | // motor 2 |
Zeekat | 1:2f23368e8638 | 72 | const double m2_Kp = 1; |
Zeekat | 1:2f23368e8638 | 73 | const double m2_Ki = 0; |
Zeekat | 1:2f23368e8638 | 74 | const double m2_Kd = 0; |
Zeekat | 1:2f23368e8638 | 75 | // storage variables |
Zeekat | 1:2f23368e8638 | 76 | // motor 1 |
Zeekat | 1:2f23368e8638 | 77 | double m1_err_int = 0; |
Zeekat | 1:2f23368e8638 | 78 | double m1_prev_err = 0; |
Zeekat | 1:2f23368e8638 | 79 | // motor 2 |
Zeekat | 1:2f23368e8638 | 80 | double m2_err_int = 0; |
Zeekat | 1:2f23368e8638 | 81 | double m2_prev_err = 0; |
Zeekat | 0:f6306e179750 | 82 | |
Zeekat | 0:f6306e179750 | 83 | |
Zeekat | 1:2f23368e8638 | 84 | //// FILTER VARIABLES |
Zeekat | 1:2f23368e8638 | 85 | // storage variables |
Zeekat | 1:2f23368e8638 | 86 | // differential action filter, same is used for both controllers |
Zeekat | 1:2f23368e8638 | 87 | double m_f_v1 = 0; |
Zeekat | 1:2f23368e8638 | 88 | double m_f_v2 = 0; |
Zeekat | 1:2f23368e8638 | 89 | |
Zeekat | 1:2f23368e8638 | 90 | // Filter coefficients |
Zeekat | 1:2f23368e8638 | 91 | // differential action filter |
Zeekat | 1:2f23368e8638 | 92 | 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 | 93 | |
Zeekat | 5:67afe491a1e3 | 94 | // tweede orde notch filter 50 Hz |
Zeekat | 5:67afe491a1e3 | 95 | // biquad 1 coefficienten |
Zeekat | 5:67afe491a1e3 | 96 | const double numnotch50biq1_1 = 1; |
Zeekat | 5:67afe491a1e3 | 97 | const double numnotch50biq1_2 = -1.61816178466632; |
Zeekat | 5:67afe491a1e3 | 98 | const double numnotch50biq1_3 = 1.00000006127058; |
Zeekat | 5:67afe491a1e3 | 99 | const double dennotch50biq1_2 = -1.59325742941798; |
Zeekat | 5:67afe491a1e3 | 100 | const double dennotch50biq1_3 = 0.982171881701431; |
Zeekat | 5:67afe491a1e3 | 101 | // biquad 2 coefficienten |
Zeekat | 5:67afe491a1e3 | 102 | const double numnotch50biq2_1 = 1; |
Zeekat | 5:67afe491a1e3 | 103 | const double numnotch50biq2_2 = -1.61816171933244; |
Zeekat | 5:67afe491a1e3 | 104 | const double numnotch50biq2_3 = 0.999999938729428; |
Zeekat | 5:67afe491a1e3 | 105 | const double dennotch50biq2_2 = -1.61431180968071; |
Zeekat | 5:67afe491a1e3 | 106 | const double dennotch50biq2_3 = 0.982599066293075; |
Zeekat | 5:67afe491a1e3 | 107 | // highpass filter 20 Hz coefficienten |
Zeekat | 5:67afe491a1e3 | 108 | const double numhigh20_1 = 0.837089190566345; |
Zeekat | 5:67afe491a1e3 | 109 | const double numhigh20_2 = -1.67417838113269; |
Zeekat | 5:67afe491a1e3 | 110 | const double numhigh20_3 = 0.837089190566345; |
Zeekat | 5:67afe491a1e3 | 111 | const double denhigh20_2 = -1.64745998107698; |
Zeekat | 5:67afe491a1e3 | 112 | const double denhigh20_3 = 0.700896781188403; |
Zeekat | 5:67afe491a1e3 | 113 | // lowpass 5 Hz coefficienten |
Zeekat | 5:67afe491a1e3 | 114 | const double numlow5_1 =0.000944691843840162; |
Zeekat | 5:67afe491a1e3 | 115 | const double numlow5_2 =0.00188938368768032; |
Zeekat | 5:67afe491a1e3 | 116 | const double numlow5_3 =0.000944691843840162; |
Zeekat | 5:67afe491a1e3 | 117 | const double denlow5_2 =-1.91119706742607; |
Zeekat | 5:67afe491a1e3 | 118 | const double denlow5_3 =0.914975834801434; |
Zeekat | 5:67afe491a1e3 | 119 | |
Zeekat | 5:67afe491a1e3 | 120 | // Define the storage variables and filter coeicients for two filters |
Zeekat | 5:67afe491a1e3 | 121 | 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 | 122 | |
Zeekat | 0:f6306e179750 | 123 | |
Zeekat | 0:f6306e179750 | 124 | |
Zeekat | 0:f6306e179750 | 125 | //////////////////////////////////////////////////////////////// |
Zeekat | 0:f6306e179750 | 126 | /////////////////// START OF SIDE FUNCTIONS //////////////////// |
Zeekat | 0:f6306e179750 | 127 | ////////////////////////////////////////////////////////////// |
Zeekat | 0:f6306e179750 | 128 | // these functions are tailored to perform 1 specific function |
Zeekat | 0:f6306e179750 | 129 | |
Zeekat | 5:67afe491a1e3 | 130 | // this funtion flips leds on and off accordin to input with 0 being on |
Zeekat | 5:67afe491a1e3 | 131 | void LED(int red,int green,int blue) |
Zeekat | 5:67afe491a1e3 | 132 | { |
Zeekat | 5:67afe491a1e3 | 133 | ledred.write(red); |
Zeekat | 5:67afe491a1e3 | 134 | ledgreen.write(green); |
Zeekat | 5:67afe491a1e3 | 135 | ledblue.write(blue); |
Zeekat | 5:67afe491a1e3 | 136 | } |
Zeekat | 5:67afe491a1e3 | 137 | |
Zeekat | 0:f6306e179750 | 138 | // counts 2 radians |
Zeekat | 0:f6306e179750 | 139 | // this function takes counts from the encoder and converts it to the amount of radians from the zero position. |
Zeekat | 0:f6306e179750 | 140 | // It has been set up for standard 2X DECODING!!!!!! |
Zeekat | 0:f6306e179750 | 141 | double get_radians(double counts) |
Zeekat | 0:f6306e179750 | 142 | { |
Zeekat | 0:f6306e179750 | 143 | double pi = 3.14159265359; |
Zeekat | 0:f6306e179750 | 144 | double radians = (counts/4200)*2*pi; // 2X DECODING!!!!! ((32 counts/rotation, extra warning) |
Zeekat | 0:f6306e179750 | 145 | return radians; |
Zeekat | 0:f6306e179750 | 146 | } |
Zeekat | 0:f6306e179750 | 147 | |
Zeekat | 0:f6306e179750 | 148 | |
Zeekat | 3:7273bbe6aa02 | 149 | // This functions takes a 0->1 input converts it to -1->1 and uses passing by reference (&c_reference) |
Zeekat | 0:f6306e179750 | 150 | // to create a reference that moves with a variable speed. It is now set up for potmeter values. |
Zeekat | 3:7273bbe6aa02 | 151 | double reference_f(double input, double &c_reference) |
Zeekat | 0:f6306e179750 | 152 | { |
Zeekat | 3:7273bbe6aa02 | 153 | double potset = (input-offset)*gain; |
Zeekat | 5:67afe491a1e3 | 154 | if(POT_in == true && potset < 0.15 && potset > -0.15) // larger area for potmeter to get a zero value |
Zeekat | 2:2563df4ee829 | 155 | { |
Zeekat | 2:2563df4ee829 | 156 | potset = 0; |
Zeekat | 2:2563df4ee829 | 157 | } |
Zeekat | 3:7273bbe6aa02 | 158 | double reference = c_reference + potset * 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 | 3:7273bbe6aa02 | 254 | double input1 = potright.read()*signalamp1; |
Zeekat | 3:7273bbe6aa02 | 255 | double reference1 = reference_f(input1, c_reference1); // determine the reference that has been set by the inputsignal |
Zeekat | 0:f6306e179750 | 256 | double rads1 = get_radians(motor1_enc.getPosition()); // determine the position of the motor |
Zeekat | 3:7273bbe6aa02 | 257 | double error1 = (reference1 - rads1); // determine the error (reference - position) |
Zeekat | 3:7273bbe6aa02 | 258 | scope.set(0,reference1); |
Zeekat | 2:2563df4ee829 | 259 | scope.set(1,rads1); |
Zeekat | 2:2563df4ee829 | 260 | scope.send(); |
Zeekat | 1:2f23368e8638 | 261 | double output1 = PID(error1, m1_Kp, m1_Ki, m1_Kd, controlstep, m1_err_int, m1_prev_err); |
Zeekat | 2:2563df4ee829 | 262 | pc.printf("output 1 %f \n",output1); |
Zeekat | 0:f6306e179750 | 263 | if(output1 > 0) { // uses the calculated output to determine the direction of the motor |
Zeekat | 0:f6306e179750 | 264 | motor1_rich.write(0); |
Zeekat | 0:f6306e179750 | 265 | motor1_aan.write(output1); |
Zeekat | 0:f6306e179750 | 266 | } else if(output1 < 0) { |
Zeekat | 0:f6306e179750 | 267 | motor1_rich.write(1); |
Zeekat | 0:f6306e179750 | 268 | motor1_aan.write(abs(output1)); |
Zeekat | 0:f6306e179750 | 269 | } |
Zeekat | 0:f6306e179750 | 270 | } |
Zeekat | 0:f6306e179750 | 271 | |
Zeekat | 0:f6306e179750 | 272 | // MOTOR 2 |
Zeekat | 0:f6306e179750 | 273 | void motor2_control() |
Zeekat | 0:f6306e179750 | 274 | { |
Zeekat | 5:67afe491a1e3 | 275 | double input2 = potleft.read()*signalamp2; // replace potleft with filter |
Zeekat | 3:7273bbe6aa02 | 276 | double reference2 = reference_f(input2, c_reference2); // determine the reference that has been set by the inputsignal |
Zeekat | 0:f6306e179750 | 277 | double rads2 = get_radians(motor2_enc.getPosition()); // determine the position of the motor |
Zeekat | 3:7273bbe6aa02 | 278 | double error2 = (reference2 - rads2); // determine the error (reference - position) |
Zeekat | 3:7273bbe6aa02 | 279 | // scope.set(3,reference2); |
Zeekat | 0:f6306e179750 | 280 | // scope.set(4,rads2); |
Zeekat | 0:f6306e179750 | 281 | // scope.send(); |
Zeekat | 1:2f23368e8638 | 282 | |
Zeekat | 1:2f23368e8638 | 283 | double output2 = PID(error2, m2_Kp, m2_Ki, m2_Kd, controlstep, m2_err_int, m2_prev_err); |
Zeekat | 0:f6306e179750 | 284 | if(output2 > 0) { // uses the calculated output to determine the direction of the motor |
Zeekat | 0:f6306e179750 | 285 | motor2_rich.write(0); |
Zeekat | 0:f6306e179750 | 286 | motor2_aan.write(output2); |
Zeekat | 0:f6306e179750 | 287 | } else if(output2 < 0) { |
Zeekat | 0:f6306e179750 | 288 | motor2_rich.write(1); |
Zeekat | 0:f6306e179750 | 289 | motor2_aan.write(abs(output2)); |
Zeekat | 0:f6306e179750 | 290 | } |
Zeekat | 0:f6306e179750 | 291 | } |
Zeekat | 0:f6306e179750 | 292 | |
Zeekat | 0:f6306e179750 | 293 | |
Zeekat | 2:2563df4ee829 | 294 | ////////////////////////////////////////////////////////////////// |
Zeekat | 2:2563df4ee829 | 295 | //////////// DEFINE GO-FLAG FUNCTIONS /////////////////////////// |
Zeekat | 2:2563df4ee829 | 296 | //////////////////////////////////////////////////////////////// |
Zeekat | 2:2563df4ee829 | 297 | |
Zeekat | 2:2563df4ee829 | 298 | void motor1_activate() |
Zeekat | 2:2563df4ee829 | 299 | { |
Zeekat | 2:2563df4ee829 | 300 | motor1_go = true; |
Zeekat | 2:2563df4ee829 | 301 | } |
Zeekat | 2:2563df4ee829 | 302 | |
Zeekat | 2:2563df4ee829 | 303 | void motor2_activate() |
Zeekat | 2:2563df4ee829 | 304 | { |
Zeekat | 2:2563df4ee829 | 305 | motor2_go = true; |
Zeekat | 2:2563df4ee829 | 306 | } |
Zeekat | 2:2563df4ee829 | 307 | |
Zeekat | 0:f6306e179750 | 308 | int main() |
Zeekat | 0:f6306e179750 | 309 | { |
Zeekat | 2:2563df4ee829 | 310 | pc.baud(115200); |
Zeekat | 4:072b99947fc6 | 311 | controller1.attach(&motor1_activate, controlstep); // call a go-flag |
Zeekat | 4:072b99947fc6 | 312 | // controller2.attach(&motor2_activate, controlstep); Disabled while debugging PID-controller ?? |
Zeekat | 0:f6306e179750 | 313 | while(true) |
Zeekat | 0:f6306e179750 | 314 | { |
Zeekat | 4:072b99947fc6 | 315 | // button functions |
Zeekat | 4:072b99947fc6 | 316 | if(buttonlinks.read() == 0) |
Zeekat | 2:2563df4ee829 | 317 | { |
Zeekat | 4:072b99947fc6 | 318 | loop_start = !loop_start; // reverse the boolean loop_start value |
Zeekat | 4:072b99947fc6 | 319 | wait(buttonlinks.read() == 1); |
Zeekat | 4:072b99947fc6 | 320 | wait(0.3); // check if it causes instability!! ?? |
Zeekat | 2:2563df4ee829 | 321 | } |
Zeekat | 4:072b99947fc6 | 322 | |
Zeekat | 4:072b99947fc6 | 323 | if(buttonrechts.read() == 0) |
Zeekat | 4:072b99947fc6 | 324 | { |
Zeekat | 4:072b99947fc6 | 325 | calib_start = !calib_start; // reverse the boolean calib_start value |
Zeekat | 4:072b99947fc6 | 326 | wait(buttonrechts.read() == 1); |
Zeekat | 4:072b99947fc6 | 327 | wait(0.3); // check if it causes instability!! ?? |
Zeekat | 4:072b99947fc6 | 328 | } |
Zeekat | 4:072b99947fc6 | 329 | |
Zeekat | 4:072b99947fc6 | 330 | // Main Control stuff and options |
Zeekat | 4:072b99947fc6 | 331 | if(loop_start == true && calib_start == false) // check if start button = true then start the main control loops |
Zeekat | 2:2563df4ee829 | 332 | { |
Zeekat | 5:67afe491a1e3 | 333 | LED(1,1,0); // turn blue led on |
Zeekat | 4:072b99947fc6 | 334 | |
Zeekat | 4:072b99947fc6 | 335 | if(motor1_go) |
Zeekat | 4:072b99947fc6 | 336 | { |
Zeekat | 4:072b99947fc6 | 337 | motor1_go = false; |
Zeekat | 4:072b99947fc6 | 338 | motor1_control(); |
Zeekat | 4:072b99947fc6 | 339 | } |
Zeekat | 4:072b99947fc6 | 340 | if(motor2_go) |
Zeekat | 4:072b99947fc6 | 341 | { |
Zeekat | 4:072b99947fc6 | 342 | motor2_go = false; |
Zeekat | 4:072b99947fc6 | 343 | motor2_control(); |
Zeekat | 4:072b99947fc6 | 344 | } |
Zeekat | 2:2563df4ee829 | 345 | } |
Zeekat | 4:072b99947fc6 | 346 | if(loop_start == false && calib_start == true) // start calibration procedures |
Zeekat | 4:072b99947fc6 | 347 | { |
Zeekat | 5:67afe491a1e3 | 348 | LED(1,0,1); // turn green led on |
Zeekat | 4:072b99947fc6 | 349 | } |
Zeekat | 4:072b99947fc6 | 350 | if(loop_start == true && calib_start == true) // check if both buttons are enabled and stop everything |
Zeekat | 0:f6306e179750 | 351 | { |
Zeekat | 5:67afe491a1e3 | 352 | LED(0,1,1); // turn red led on |
Zeekat | 4:072b99947fc6 | 353 | } |
Zeekat | 4:072b99947fc6 | 354 | else |
Zeekat | 4:072b99947fc6 | 355 | { |
Zeekat | 5:67afe491a1e3 | 356 | LED(1,1,1); // turn leds off (both buttons false) |
Zeekat | 0:f6306e179750 | 357 | } |
Zeekat | 0:f6306e179750 | 358 | } |
Zeekat | 0:f6306e179750 | 359 | } |