Thomas Burgers / Mbed 2 deprecated ZZ-TheChenneRobot

Dependencies:   Encoder HIDScope MODSERIAL QEI biquadFilter mbed

Committer:
ThomasBNL
Date:
Thu Oct 08 10:32:07 2015 +0000
Revision:
17:aa167ab3cf75
Parent:
16:a8d2c721cf56
Child:
18:6c065915f474
moved location reference turn defined and added reference change button

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasBNL 0:40052f5ca77b 1 #include "mbed.h"
ThomasBNL 0:40052f5ca77b 2 //#include "HIDScope.h"
ThomasBNL 0:40052f5ca77b 3 #include "QEI.h"
ThomasBNL 0:40052f5ca77b 4 #include "MODSERIAL.h"
ThomasBNL 8:50d6e2323d3b 5 #include "biquadFilter.h"
ThomasBNL 0:40052f5ca77b 6 #include "encoder.h"
ThomasBNL 0:40052f5ca77b 7
ThomasBNL 7:ddd7fb357786 8 MODSERIAL pc(USBTX,USBRX);
ThomasBNL 15:f029351f1f3a 9 DigitalOut debug_led(LED_RED); // Debug LED
ThomasBNL 8:50d6e2323d3b 10
ThomasBNL 14:599896acf576 11 //HIDScope scope(2); // HIDSCOPE declared
ThomasBNL 14:599896acf576 12
ThomasBNL 14:599896acf576 13 //Ticker Sample_Ticker; // HIDSCOPE voor main
ThomasBNL 14:599896acf576 14 //volatile bool get_sample; // HIDSCOPE voor main
ThomasBNL 14:599896acf576 15
ThomasBNL 14:599896acf576 16
ThomasBNL 8:50d6e2323d3b 17 // (DEBUGGING AND TESTING BUTTONS) (0 when pressed and 1 when not pressed)
ThomasBNL 8:50d6e2323d3b 18 DigitalIn buttonL1(PTC6); // Button 1 (laag op board) for testing at the lower board
ThomasBNL 8:50d6e2323d3b 19 DigitalIn buttonL2(PTA4); // Button 2 (laag op board) for testing at the lower board
ThomasBNL 8:50d6e2323d3b 20 DigitalIn buttonH1(D2); // Button 3 (hoog op board) for testing at the top board
ThomasBNL 8:50d6e2323d3b 21 DigitalIn buttonH2(D6); // Button 4 (hoog op board) for testing at the top board
ThomasBNL 0:40052f5ca77b 22
ThomasBNL 15:f029351f1f3a 23
ThomasBNL 0:40052f5ca77b 24 volatile bool looptimerflag;
ThomasBNL 8:50d6e2323d3b 25 const double cw=0; // zero is clockwise (front view)
ThomasBNL 8:50d6e2323d3b 26 const double ccw=1; // one is counterclockwise (front view)
ThomasBNL 17:aa167ab3cf75 27 double reference_turn = 0;
ThomasBNL 8:50d6e2323d3b 28
ThomasBNL 8:50d6e2323d3b 29 const double Gain_P_turn=0.0067;
ThomasBNL 8:50d6e2323d3b 30 // stel setpoint tussen (0 en 360) en position tussen (0 en 360)
ThomasBNL 8:50d6e2323d3b 31 // max verschil: 360 -> dan pwm_to_motor 1 tot aan een verschil van 15 graden-> bij 15 moet pwm_to_motor ong 0.1 zijn
ThomasBNL 8:50d6e2323d3b 32 // dus 0.1=15*gain gain=0.0067
ThomasBNL 14:599896acf576 33 // Als 3 graden verschil 0.1 dan 0.1/3=gain=0.33
ThomasBNL 13:bcf8ec7120ab 34
ThomasBNL 13:bcf8ec7120ab 35 double Gain_I_turn=0.025; //(1/2000) //0.00000134
ThomasBNL 13:bcf8ec7120ab 36 // pwm_motor_I=(integrate_error_turn + sample_time*error)*gain; pwm = (4*0.01 + 4)* Gain => 0.1 pwm gewenst (na 1 seconde een verschil van 4 graden)
ThomasBNL 13:bcf8ec7120ab 37 // 0.1 / (4.01) = Gain = 0.025
ThomasBNL 13:bcf8ec7120ab 38
ThomasBNL 13:bcf8ec7120ab 39 double Gain_D_turn=1;
ThomasBNL 13:bcf8ec7120ab 40 // error_derivative_turn=(error - previous_error_turn)/sample_time
ThomasBNL 14:599896acf576 41 //
ThomasBNL 8:50d6e2323d3b 42
ThomasBNL 8:50d6e2323d3b 43 double conversion_counts_to_degrees=0.085877862594198;
ThomasBNL 8:50d6e2323d3b 44 // gear ratio motor = 131
ThomasBNL 8:50d6e2323d3b 45 // ticks per magnet rotation = 32 (X2 Encoder)
ThomasBNL 8:50d6e2323d3b 46 // One revolution = 360 degrees
ThomasBNL 8:50d6e2323d3b 47 // degrees_per_encoder_tick = 360/(gear_ratio*ticks_per_magnet_rotation)=360/131*32=0.085877862594198
ThomasBNL 8:50d6e2323d3b 48
ThomasBNL 8:50d6e2323d3b 49 const double sample_time=0.01; // tijd voor een sample (100Hz)
ThomasBNL 8:50d6e2323d3b 50
ThomasBNL 8:50d6e2323d3b 51 // PID motor constants
ThomasBNL 8:50d6e2323d3b 52 double integrate_error_turn=0; // integration error turn motor
ThomasBNL 8:50d6e2323d3b 53 double previous_error_turn=0; // previous error turn motor
ThomasBNL 8:50d6e2323d3b 54
ThomasBNL 1:dc683e88b44e 55
ThomasBNL 7:ddd7fb357786 56 // Functions used (described after main)
ThomasBNL 7:ddd7fb357786 57 void keep_in_range(double * in, double min, double max);
ThomasBNL 7:ddd7fb357786 58 void setlooptimerflag(void);
ThomasBNL 8:50d6e2323d3b 59 double get_reference(double input);
ThomasBNL 14:599896acf576 60 // void get_sample(); //HIDSCOPE
ThomasBNL 8:50d6e2323d3b 61
ThomasBNL 0:40052f5ca77b 62
ThomasBNL 7:ddd7fb357786 63 // MAIN function
ThomasBNL 0:40052f5ca77b 64 int main() {
ThomasBNL 8:50d6e2323d3b 65 AnalogIn potmeter(A0); // Potmeter that can read a reference value (DEBUG TOOL)
ThomasBNL 8:50d6e2323d3b 66 QEI motor_turn(D12,D13,NC,32); // Encoder for motor Turn
ThomasBNL 8:50d6e2323d3b 67 PwmOut pwm_motor_turn(D5); // Pwm for motor Turn
ThomasBNL 8:50d6e2323d3b 68 DigitalOut motordirection_turn(D4); // Direction of the motor Turn
ThomasBNL 8:50d6e2323d3b 69 double reference_turn; // Set constant to store reference value of the Turn motor
ThomasBNL 8:50d6e2323d3b 70 double position_turn; // Set constant to store current position of the Turn motor
ThomasBNL 8:50d6e2323d3b 71 double error;
ThomasBNL 10:09ba965045a7 72 double pwm_to_motor_turn;
ThomasBNL 10:09ba965045a7 73 double pwm_motor_turn_P;
ThomasBNL 10:09ba965045a7 74 double pwm_motor_turn_I;
ThomasBNL 10:09ba965045a7 75 double pwm_motor_turn_D;
ThomasBNL 0:40052f5ca77b 76
ThomasBNL 7:ddd7fb357786 77 //START OF CODE
ThomasBNL 7:ddd7fb357786 78 pc.printf("Start of code \n\r");
ThomasBNL 0:40052f5ca77b 79
ThomasBNL 8:50d6e2323d3b 80 pc.baud(9600); // Set the baudrate
ThomasBNL 0:40052f5ca77b 81
ThomasBNL 7:ddd7fb357786 82 // Tickers
ThomasBNL 8:50d6e2323d3b 83 Ticker looptimer; // Ticker called looptimer to set a looptimerflag
ThomasBNL 8:50d6e2323d3b 84 looptimer.attach(setlooptimerflag,sample_time); // calls the looptimer flag every 0.01s
ThomasBNL 0:40052f5ca77b 85
ThomasBNL 14:599896acf576 86 // Sample_Ticker.attach(&get_sample, (float)1/Fs); HIDSCOPE sample Ticker
ThomasBNL 14:599896acf576 87
ThomasBNL 7:ddd7fb357786 88 pc.printf("Start infinite loop \n\r");
ThomasBNL 16:a8d2c721cf56 89 wait (3); // Wait before starting system
ThomasBNL 0:40052f5ca77b 90
ThomasBNL 0:40052f5ca77b 91 //INFINITE LOOP
ThomasBNL 5:8fb74a22fe3c 92 while(1)
ThomasBNL 8:50d6e2323d3b 93 { // Start while loop
ThomasBNL 8:50d6e2323d3b 94 // DEBUGGING BUTTON: interrupt button Disbalances the current motor position
ThomasBNL 8:50d6e2323d3b 95 if (buttonL2.read() < 0.5){ //if button pressed
ThomasBNL 8:50d6e2323d3b 96 motordirection_turn = cw;
ThomasBNL 8:50d6e2323d3b 97 pwm_motor_turn = 0.5f; // motorspeed
ThomasBNL 8:50d6e2323d3b 98 pc.printf("positie = %d \r\n", motor_turn.getPulses()); }
ThomasBNL 8:50d6e2323d3b 99
ThomasBNL 17:aa167ab3cf75 100 // Change Reference button
ThomasBNL 17:aa167ab3cf75 101 if (buttonL1.read() < 0.5){ //if button pressed
ThomasBNL 17:aa167ab3cf75 102 pc.printf("Reference after = %d \r\n", Reference);
ThomasBNL 17:aa167ab3cf75 103 change_reference_positive(Reference_turn)
ThomasBNL 17:aa167ab3cf75 104 pc.printf("Reference after = %d \r\n", Reference); }
ThomasBNL 17:aa167ab3cf75 105
ThomasBNL 8:50d6e2323d3b 106 // Wait until looptimer flag is true then execute PID controller.
ThomasBNL 5:8fb74a22fe3c 107 else
ThomasBNL 8:50d6e2323d3b 108 {
ThomasBNL 0:40052f5ca77b 109 while(looptimerflag != true);
ThomasBNL 0:40052f5ca77b 110
ThomasBNL 0:40052f5ca77b 111 looptimerflag = false;
ThomasBNL 0:40052f5ca77b 112
ThomasBNL 8:50d6e2323d3b 113 //reference = (potmeter.read()-0.5)*2000; // Potmeter bepaald reference (uitgeschakeld)
ThomasBNL 17:aa167ab3cf75 114 //reference_turn = 15; //BOVENAAN IN SCRIPT GEPLAATST
ThomasBNL 0:40052f5ca77b 115
ThomasBNL 8:50d6e2323d3b 116 // Keep motor position between -4200 and 4200 counts
ThomasBNL 8:50d6e2323d3b 117 if ((motor_turn.getPulses()>4200) || (motor_turn.getPulses()<-4200)) // If value is outside -4200 and 4200 (number of counts equal to one revolution) reset to zero
ThomasBNL 3:11a7da46e093 118 {
ThomasBNL 8:50d6e2323d3b 119 motor_turn.reset();
ThomasBNL 3:11a7da46e093 120 pc.printf("RESET \n\r");
ThomasBNL 3:11a7da46e093 121 }
ThomasBNL 3:11a7da46e093 122
ThomasBNL 8:50d6e2323d3b 123 // Convert position to degrees
ThomasBNL 8:50d6e2323d3b 124 position_turn = conversion_counts_to_degrees * motor_turn.getPulses();
ThomasBNL 0:40052f5ca77b 125
ThomasBNL 8:50d6e2323d3b 126 pc.printf("calibrated setpoint: %f, calibrated position motor %i, position %f \n\r", reference_turn, motor_turn.getPulses(), position_turn);
ThomasBNL 3:11a7da46e093 127
ThomasBNL 0:40052f5ca77b 128
ThomasBNL 8:50d6e2323d3b 129 // P-CONTROLLER
ThomasBNL 8:50d6e2323d3b 130 // Calculate error then multiply it with the gain, and store in pwm_to_motor
ThomasBNL 8:50d6e2323d3b 131
ThomasBNL 10:09ba965045a7 132 error=(reference_turn - position_turn); // Current error (input controller)
ThomasBNL 8:50d6e2323d3b 133
ThomasBNL 10:09ba965045a7 134 integrate_error_turn=integrate_error_turn + sample_time*error; // integral error output
ThomasBNL 8:50d6e2323d3b 135 // // overwrite previous integrate error by adding the current error multiplied by the sample time.
ThomasBNL 8:50d6e2323d3b 136 //
ThomasBNL 11:ecd83b303252 137 double error_derivative_turn=(error - previous_error_turn)/sample_time; // derivative error output
ThomasBNL 8:50d6e2323d3b 138
ThomasBNL 8:50d6e2323d3b 139 // FILTER error_derivative_turn (lowpassfilter)
ThomasBNL 11:ecd83b303252 140
ThomasBNL 12:26759959c960 141 const double mT_f_a1=-1.965293372622690e+00;
ThomasBNL 12:26759959c960 142 const double mT_f_a2=9.658854605688177e-01;
ThomasBNL 12:26759959c960 143 const double mT_f_b0=1.480219865318266e-04;
ThomasBNL 12:26759959c960 144 const double mT_f_b1=2.960439730636533e-04;
ThomasBNL 12:26759959c960 145 const double mT_f_b2=1.480219865318266e-04; // Motor Turn filter constants
ThomasBNL 11:ecd83b303252 146
ThomasBNL 12:26759959c960 147 biquadFilter Lowpassfilter(mT_f_a1,mT_f_a2,mT_f_b0,mT_f_b1,mT_f_b2);
ThomasBNL 11:ecd83b303252 148
ThomasBNL 12:26759959c960 149 error_derivative_turn=Lowpassfilter.step(error_derivative_turn);
ThomasBNL 3:11a7da46e093 150
ThomasBNL 11:ecd83b303252 151 previous_error_turn=error; // current error is saved to memory constant to be used in
ThomasBNL 8:50d6e2323d3b 152 // the next loop for calculating the derivative error
ThomasBNL 8:50d6e2323d3b 153
ThomasBNL 10:09ba965045a7 154 pwm_to_motor_turn = error*Gain_P_turn; // output P controller to pwm
ThomasBNL 8:50d6e2323d3b 155
ThomasBNL 10:09ba965045a7 156 pwm_motor_turn_P = error*Gain_P_turn; // output P controller to pwm
ThomasBNL 10:09ba965045a7 157 pwm_motor_turn_I = integrate_error_turn*Gain_I_turn; // output I controller to pwm
ThomasBNL 11:ecd83b303252 158 pwm_motor_turn_D = error_derivative_turn*Gain_D_turn; // output D controller to pwm
ThomasBNL 10:09ba965045a7 159
ThomasBNL 13:bcf8ec7120ab 160 pwm_to_motor_turn = pwm_motor_turn_P + pwm_motor_turn_I + pwm_motor_turn_D;
ThomasBNL 10:09ba965045a7 161
ThomasBNL 8:50d6e2323d3b 162 //
ThomasBNL 10:09ba965045a7 163 // double pwm_to_motor_turn = pwm_motor_turn_P + pwm_motor_turn_I + pwm_motor_turn_D; // Total output PID controller to pwm
ThomasBNL 8:50d6e2323d3b 164 //
ThomasBNL 0:40052f5ca77b 165
ThomasBNL 8:50d6e2323d3b 166 // Keep Pwm between -1 and 1
ThomasBNL 10:09ba965045a7 167 keep_in_range(&pwm_to_motor_turn, -1,1); // Pass to motor controller but keep it in range!
ThomasBNL 10:09ba965045a7 168 pc.printf("pwm %f \n\r", pwm_to_motor_turn);
ThomasBNL 0:40052f5ca77b 169
ThomasBNL 8:50d6e2323d3b 170 // Check error and decide direction to turn
ThomasBNL 10:09ba965045a7 171 if(pwm_to_motor_turn > 0)
ThomasBNL 3:11a7da46e093 172 {
ThomasBNL 8:50d6e2323d3b 173 motordirection_turn=ccw;
ThomasBNL 15:f029351f1f3a 174 pc.printf("if loop pwm > 0 \n\r");
ThomasBNL 3:11a7da46e093 175 }
ThomasBNL 0:40052f5ca77b 176 else
ThomasBNL 3:11a7da46e093 177 {
ThomasBNL 8:50d6e2323d3b 178 motordirection_turn=cw;
ThomasBNL 15:f029351f1f3a 179 pc.printf("else loop pwm < 0 \n\r");
ThomasBNL 3:11a7da46e093 180 }
ThomasBNL 8:50d6e2323d3b 181
ThomasBNL 8:50d6e2323d3b 182 // Put pwm_motor to the motor
ThomasBNL 10:09ba965045a7 183 pwm_motor_turn=(abs(pwm_to_motor_turn));
ThomasBNL 14:599896acf576 184
ThomasBNL 14:599896acf576 185 // if(sample_go) // HIDSCOPE input => sample_go nu nog niet nodig opzich // BLINK LEDS TOEVOEGEN
ThomasBNL 14:599896acf576 186 // {
ThomasBNL 14:599896acf576 187 // //sample_filter; (filter function zie EMG filter working script)
ThomasBNL 16:a8d2c721cf56 188 // scope.set(0,reference_turn); // HIDSCOPE channel 0 : Current Reference
ThomasBNL 16:a8d2c721cf56 189 // scope.set(0,position_turn); // HIDSCOPE channel 0 : Position_turn
ThomasBNL 16:a8d2c721cf56 190 // scope.set(1,pwm_to_motor_turn); // HIDSCOPE channel 1 : Pwm_to_motor_turn
ThomasBNL 14:599896acf576 191 // scope.send(); // Send channel info to HIDSCOPE
ThomasBNL 14:599896acf576 192 // sample_go = 0;
ThomasBNL 14:599896acf576 193 // }
ThomasBNL 16:a8d2c721cf56 194 debug_led = !debug_led; // should flicker with freq 50 Hz
ThomasBNL 0:40052f5ca77b 195 }
ThomasBNL 0:40052f5ca77b 196 }
ThomasBNL 5:8fb74a22fe3c 197 }
ThomasBNL 0:40052f5ca77b 198
ThomasBNL 0:40052f5ca77b 199 // Keep in range function
ThomasBNL 0:40052f5ca77b 200 void keep_in_range(double * in, double min, double max)
ThomasBNL 0:40052f5ca77b 201 {
ThomasBNL 0:40052f5ca77b 202 *in > min ? *in < max? : *in = max: *in = min;
ThomasBNL 0:40052f5ca77b 203 }
ThomasBNL 0:40052f5ca77b 204
ThomasBNL 0:40052f5ca77b 205 // Looptimerflag function
ThomasBNL 0:40052f5ca77b 206 void setlooptimerflag(void)
ThomasBNL 0:40052f5ca77b 207 {
ThomasBNL 0:40052f5ca77b 208 looptimerflag = true;
ThomasBNL 1:dc683e88b44e 209 }
ThomasBNL 1:dc683e88b44e 210
ThomasBNL 8:50d6e2323d3b 211 // Get setpoint -> potmeter (MOMENTEEL UITGESCHAKELD)
ThomasBNL 8:50d6e2323d3b 212 double get_reference(double input)
ThomasBNL 1:dc683e88b44e 213 {
ThomasBNL 1:dc683e88b44e 214 const float offset = 0.5;
ThomasBNL 1:dc683e88b44e 215 const float gain = 4.0;
ThomasBNL 1:dc683e88b44e 216 return (input-offset)*gain;
ThomasBNL 5:8fb74a22fe3c 217 }
ThomasBNL 14:599896acf576 218
ThomasBNL 14:599896acf576 219 //// Get sample
ThomasBNL 14:599896acf576 220 //void get_sample() // HIDSCOPE sample fuction
ThomasBNL 14:599896acf576 221 //{
ThomasBNL 14:599896acf576 222 // get_sample = 1;
ThomasBNL 17:aa167ab3cf75 223 //}
ThomasBNL 17:aa167ab3cf75 224
ThomasBNL 17:aa167ab3cf75 225
ThomasBNL 17:aa167ab3cf75 226 void change_reference_positive (double Reference)
ThomasBNL 17:aa167ab3cf75 227 {
ThomasBNL 17:aa167ab3cf75 228 Reference=Reference+45;
ThomasBNL 17:aa167ab3cf75 229 }