
Minor BioRobotics BMT Hierbij publish ik mijn code public ter inspiratie voor komende jaarlagen. Het gaat om een serial robot met twee links en een haak als end-effector. Veel plezier ermee!
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@9:c4fa72ffa1c2, 2019-10-22 (annotated)
- Committer:
- fb07
- Date:
- Tue Oct 22 22:37:32 2019 +0000
- Revision:
- 9:c4fa72ffa1c2
- Parent:
- 5:7e2c6d2235fe
- Child:
- 10:a60b369c1711
Project met motor kalibratie erin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fb07 | 2:45a85caaebfb | 1 | // Project BioRobotics - Opening a Door - Group 13 2019/2020 |
fb07 | 2:45a85caaebfb | 2 | // Dion ten Berge - s1864734 |
fb07 | 2:45a85caaebfb | 3 | // Bas Rutteman - s1854305 |
fb07 | 2:45a85caaebfb | 4 | // Nick in het Veld - s1915584 |
fb07 | 2:45a85caaebfb | 5 | // Marleen van der Weij - s1800078 |
fb07 | 2:45a85caaebfb | 6 | // Mevlid Yildirim - s2005735 |
fb07 | 2:45a85caaebfb | 7 | |
fb07 | 5:7e2c6d2235fe | 8 | /* To-Do |
fb07 | 5:7e2c6d2235fe | 9 | 1. Kd, Ki, Kp waardes bepalen |
fb07 | 5:7e2c6d2235fe | 10 | 2. Filter cutoff frequentie bepalen, zie https://github.com/tomlankhorst/biquad |
fb07 | 5:7e2c6d2235fe | 11 | 3. Grenswaarde EMG signaal na het filteren |
fb07 | 5:7e2c6d2235fe | 12 | */ |
fb07 | 2:45a85caaebfb | 13 | |
fb07 | 2:45a85caaebfb | 14 | //***************************************************************************** |
fb07 | 2:45a85caaebfb | 15 | // 1. Libraries ****************************************************************** |
fb07 | 2:45a85caaebfb | 16 | //***************************************************************************** |
RobertoO | 0:67c50348f842 | 17 | #include "mbed.h" |
fb07 | 2:45a85caaebfb | 18 | #include "HIDScope.h" |
fb07 | 2:45a85caaebfb | 19 | #include "QEI.h" |
RobertoO | 1:b862262a9d14 | 20 | #include "MODSERIAL.h" |
fb07 | 2:45a85caaebfb | 21 | #include "BiQuad.h" |
fb07 | 2:45a85caaebfb | 22 | #include "FastPWM.h" |
fb07 | 2:45a85caaebfb | 23 | |
fb07 | 2:45a85caaebfb | 24 | //***************************************************************************** |
fb07 | 2:45a85caaebfb | 25 | // 2. States ****************************************************************** |
fb07 | 2:45a85caaebfb | 26 | //***************************************************************************** |
fb07 | 5:7e2c6d2235fe | 27 | enum States {StartWait, MotorCalibration, EMGCalibration, Homing, Operating, Emergency, Demo}; //All robot states |
fb07 | 5:7e2c6d2235fe | 28 | States state; |
RobertoO | 0:67c50348f842 | 29 | |
fb07 | 2:45a85caaebfb | 30 | //***************************************************************************** |
fb07 | 5:7e2c6d2235fe | 31 | // 3. (Global) Variables *********************************************************** |
fb07 | 2:45a85caaebfb | 32 | //***************************************************************************** |
fb07 | 5:7e2c6d2235fe | 33 | // 3.1 Tickers ***************************************************************** |
fb07 | 5:7e2c6d2235fe | 34 | Ticker ticker_mainloop; // The ticker which runs the mainloop |
fb07 | 5:7e2c6d2235fe | 35 | Ticker ticker_hidscope; // The ticker which sends data to the HIDScope server |
fb07 | 5:7e2c6d2235fe | 36 | |
fb07 | 5:7e2c6d2235fe | 37 | // 3.2 General variables ******************************************************* |
fb07 | 5:7e2c6d2235fe | 38 | |
fb07 | 5:7e2c6d2235fe | 39 | MODSERIAL pc(USBTX, USBRX); // Serial communication with the board |
fb07 | 5:7e2c6d2235fe | 40 | QEI encoder_motor1(D12,D13,NC,64); // Defines encoder for motor 1 |
fb07 | 5:7e2c6d2235fe | 41 | QEI encoder_motor2(D10,D11,NC,64); // Defines encoder for motor 1 |
fb07 | 5:7e2c6d2235fe | 42 | double f=1/100; // Frequency |
fb07 | 5:7e2c6d2235fe | 43 | const double Ts = f/10; // Sampletime |
fb07 | 5:7e2c6d2235fe | 44 | HIDScope scope(2); // Amount of HIDScope servers |
fb07 | 9:c4fa72ffa1c2 | 45 | double positie_verschil_motor1; |
fb07 | 9:c4fa72ffa1c2 | 46 | double positie_prev_motor1; |
fb07 | 5:7e2c6d2235fe | 47 | |
fb07 | 5:7e2c6d2235fe | 48 | // 3.3 BiQuad Filters ********************************************************** |
fb07 | 5:7e2c6d2235fe | 49 | static BiQuad notchfilter(9.97761e-01, -1.97095e+00, 9.97761e-01, -1.97095e+00, 9.95522e-01); |
fb07 | 5:7e2c6d2235fe | 50 | static BiQuad highfilter(9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01); |
fb07 | 5:7e2c6d2235fe | 51 | static BiQuad LowPassFilter( 4.12535e-02, 8.25071e-02, 4.12535e-02, -1.34897e+00, 5.13982e-01 ); |
RobertoO | 0:67c50348f842 | 52 | |
fb07 | 5:7e2c6d2235fe | 53 | // 3.4 Hardware *************************************************************** |
fb07 | 5:7e2c6d2235fe | 54 | //3.4a Leds |
fb07 | 5:7e2c6d2235fe | 55 | DigitalOut led_red(LED_RED); // Defines the red led on the K64 board (0=on, 1 = off) |
fb07 | 5:7e2c6d2235fe | 56 | DigitalOut led_green(LED_GREEN); // Defines the green led on the K64 board (0=on, 1 = off) |
fb07 | 5:7e2c6d2235fe | 57 | DigitalOut led_blue(LED_BLUE); // Defines the blue led on the K64 board (0=on, 1 = off) |
fb07 | 5:7e2c6d2235fe | 58 | // FastPWM led1(D8); //CODE DOES NOT WORK WITH D8 PIN DEFINED //Defines Led1 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 59 | FastPWM led2(D9); //Defines Led2 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 60 | |
fb07 | 5:7e2c6d2235fe | 61 | //3.4b Potmeters and buttons |
fb07 | 5:7e2c6d2235fe | 62 | AnalogIn pot1_links(A5); //Defines potmeter1 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 63 | AnalogIn pot2_rechts(A4); //Defines potmeter2 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 64 | DigitalIn button1(D2); //Defines button1 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 65 | DigitalIn button2(D3); //Defines button2 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 66 | DigitalIn sw2(SW2); //Defines button SW2 on the K64 board |
fb07 | 5:7e2c6d2235fe | 67 | DigitalIn sw3(SW3); //Defines button SW3 on the K64 board |
fb07 | 5:7e2c6d2235fe | 68 | |
fb07 | 5:7e2c6d2235fe | 69 | //3.4c Motors |
fb07 | 5:7e2c6d2235fe | 70 | DigitalOut motor1DirectionPin(D7); // motor 1 direction control (1=cw, 0=ccw) |
fb07 | 5:7e2c6d2235fe | 71 | FastPWM motor1(D6); // motor 1 velocity control (between 0-1) |
fb07 | 5:7e2c6d2235fe | 72 | FastPWM motor2(D5); // motor 2 velocity control (between 0-1) |
fb07 | 5:7e2c6d2235fe | 73 | DigitalOut motor2DirectionPin(D4); // motor 2 direction control (1=cw, 0=ccw) |
fb07 | 5:7e2c6d2235fe | 74 | |
fb07 | 5:7e2c6d2235fe | 75 | // 3.5 PID variabelen *************************************************************** |
fb07 | 5:7e2c6d2235fe | 76 | //3.5a PID-controller motor 1 |
fb07 | 5:7e2c6d2235fe | 77 | double counts_per_rad_motor1 = (131.25*32)/(2*3.14159265359); // (gear ratio * counts per revolution) / (2* pi) = ~668.45 counts per rad |
fb07 | 5:7e2c6d2235fe | 78 | static double error_integral_motor1 = 0; |
fb07 | 5:7e2c6d2235fe | 79 | double Yref_motor1; |
fb07 | 5:7e2c6d2235fe | 80 | double kp_motor1; |
fb07 | 5:7e2c6d2235fe | 81 | double Ki_motor1; |
fb07 | 5:7e2c6d2235fe | 82 | double Kd_motor1; |
fb07 | 2:45a85caaebfb | 83 | |
fb07 | 5:7e2c6d2235fe | 84 | double positie_motor1; //counts encoder |
fb07 | 5:7e2c6d2235fe | 85 | double error1_motor1; |
fb07 | 5:7e2c6d2235fe | 86 | double error1_prev_motor1; |
fb07 | 5:7e2c6d2235fe | 87 | double error1_derivative_motor1; |
fb07 | 5:7e2c6d2235fe | 88 | double error1_derivative_filtered_motor1; |
fb07 | 5:7e2c6d2235fe | 89 | double P_motor1; |
fb07 | 5:7e2c6d2235fe | 90 | |
fb07 | 5:7e2c6d2235fe | 91 | //3.5b PID-controller motor 2 |
fb07 | 5:7e2c6d2235fe | 92 | double counts_per_rad_motor2 = (131.25*32)/(2*3.14159265359); // (gear ratio * counts per revolution) / (2* pi) = ~668.45 counts per rad |
fb07 | 5:7e2c6d2235fe | 93 | static double error_integral_motor2 = 0; |
fb07 | 5:7e2c6d2235fe | 94 | double Yref_motor2; |
fb07 | 5:7e2c6d2235fe | 95 | double kp_motor2; |
fb07 | 5:7e2c6d2235fe | 96 | double Ki_motor2; |
fb07 | 5:7e2c6d2235fe | 97 | double Kd_motor2; |
fb07 | 2:45a85caaebfb | 98 | |
fb07 | 5:7e2c6d2235fe | 99 | double positie_motor2; //counts encoder |
fb07 | 5:7e2c6d2235fe | 100 | double error1_motor2; |
fb07 | 5:7e2c6d2235fe | 101 | double error1_prev_motor2; |
fb07 | 5:7e2c6d2235fe | 102 | double error1_derivative_motor2; |
fb07 | 5:7e2c6d2235fe | 103 | double error1_derivative_filtered_motor2; |
fb07 | 5:7e2c6d2235fe | 104 | double P_motor2; |
RobertoO | 0:67c50348f842 | 105 | |
fb07 | 2:45a85caaebfb | 106 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 107 | // 4. Functions **************************************************************** |
fb07 | 5:7e2c6d2235fe | 108 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 109 | |
fb07 | 5:7e2c6d2235fe | 110 | // 4.1 Hidscope **************************************************************** |
fb07 | 5:7e2c6d2235fe | 111 | void HIDScope() //voor HIDscope |
fb07 | 5:7e2c6d2235fe | 112 | { |
fb07 | 9:c4fa72ffa1c2 | 113 | scope.set(0, positie_motor1); |
fb07 | 9:c4fa72ffa1c2 | 114 | scope.set(1, positie_prev_motor1); //nog te definieren wat we willen weergeven |
fb07 | 9:c4fa72ffa1c2 | 115 | scope.set(2, positie_verschil_motor1); //nog te definieren wat we willen weergeven |
fb07 | 9:c4fa72ffa1c2 | 116 | |
fb07 | 5:7e2c6d2235fe | 117 | scope.send(); |
fb07 | 5:7e2c6d2235fe | 118 | } |
fb07 | 9:c4fa72ffa1c2 | 119 | |
fb07 | 9:c4fa72ffa1c2 | 120 | double fencoder_motor1() // bepaalt de positie van de motor |
fb07 | 9:c4fa72ffa1c2 | 121 | { |
fb07 | 9:c4fa72ffa1c2 | 122 | positie_motor1 = encoder_motor1.getPulses(); // haalt encoder waardes op |
fb07 | 9:c4fa72ffa1c2 | 123 | positie_verschil_motor1 = (positie_motor1-positie_prev_motor1)/Ts; |
fb07 | 9:c4fa72ffa1c2 | 124 | positie_prev_motor1 = positie_motor1; |
fb07 | 9:c4fa72ffa1c2 | 125 | |
fb07 | 9:c4fa72ffa1c2 | 126 | return positie_motor1; //geeft positie van motor |
fb07 | 9:c4fa72ffa1c2 | 127 | } |
fb07 | 9:c4fa72ffa1c2 | 128 | |
fb07 | 5:7e2c6d2235fe | 129 | // 4.2a PID-Controller motor 1************************************************** |
fb07 | 5:7e2c6d2235fe | 130 | double PID_controller_motor1() |
fb07 | 5:7e2c6d2235fe | 131 | { |
fb07 | 5:7e2c6d2235fe | 132 | //Proportional part |
fb07 | 5:7e2c6d2235fe | 133 | kp_motor1 = 0.01 ; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 134 | double Up_motor1 = kp_motor1 * error1_motor1; |
fb07 | 5:7e2c6d2235fe | 135 | |
fb07 | 5:7e2c6d2235fe | 136 | //Integral part |
fb07 | 5:7e2c6d2235fe | 137 | Ki_motor1 = 0.0001; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 138 | error_integral_motor1 = error_integral_motor1 + (Ts*error1_motor1); // integrale fout + (de sample tijd * fout) |
fb07 | 5:7e2c6d2235fe | 139 | double Ui_motor1 = Ki_motor1 * error_integral_motor1; // (fout * integrale fout) |
fb07 | 5:7e2c6d2235fe | 140 | |
fb07 | 5:7e2c6d2235fe | 141 | //Derivative part |
fb07 | 5:7e2c6d2235fe | 142 | Kd_motor1 = 0.01 ;// moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 143 | error1_derivative_motor1 = (error1_motor1 - error1_prev_motor1)/Ts; // (Fout - de vorige fout) / tijdstap = afgeleide |
fb07 | 5:7e2c6d2235fe | 144 | error1_derivative_filtered_motor1 = LowPassFilter.step(error1_derivative_motor1); //derivative wordt gefiltered |
fb07 | 5:7e2c6d2235fe | 145 | double Ud_motor1 = Kd_motor1 * error1_derivative_filtered_motor1; // (afgeleide gain) * (afgeleide gefilterde fout) |
fb07 | 5:7e2c6d2235fe | 146 | error1_prev_motor1 = error1_motor1; |
fb07 | 5:7e2c6d2235fe | 147 | |
fb07 | 5:7e2c6d2235fe | 148 | double P_motor1 = Up_motor1 + Ui_motor1 + Ud_motor1; //sommatie van de u's |
fb07 | 5:7e2c6d2235fe | 149 | |
fb07 | 5:7e2c6d2235fe | 150 | return P_motor1; |
fb07 | 5:7e2c6d2235fe | 151 | } |
fb07 | 5:7e2c6d2235fe | 152 | |
fb07 | 5:7e2c6d2235fe | 153 | // 4.2b PID-Controller motor 2************************************************** |
fb07 | 5:7e2c6d2235fe | 154 | double PID_controller_motor2() |
fb07 | 5:7e2c6d2235fe | 155 | { |
fb07 | 5:7e2c6d2235fe | 156 | //Proportional part |
fb07 | 5:7e2c6d2235fe | 157 | kp_motor2 = 0.01 ; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 158 | double Up_motor2 = kp_motor2 * error1_motor2; |
fb07 | 5:7e2c6d2235fe | 159 | |
fb07 | 5:7e2c6d2235fe | 160 | //Integral part |
fb07 | 5:7e2c6d2235fe | 161 | Ki_motor2 = 0.0001; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 162 | error_integral_motor2 = error_integral_motor2 + (Ts*error1_motor2); // integrale fout + (de sample tijd * fout) |
fb07 | 5:7e2c6d2235fe | 163 | double Ui_motor2 = Ki_motor2 * error_integral_motor2; //de fout keer de integrale fout |
fb07 | 5:7e2c6d2235fe | 164 | |
fb07 | 5:7e2c6d2235fe | 165 | //Derivative part |
fb07 | 5:7e2c6d2235fe | 166 | Kd_motor2 = 0.01 ;// moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 167 | error1_derivative_motor2 = (error1_motor2 - error1_prev_motor2)/Ts; |
fb07 | 5:7e2c6d2235fe | 168 | error1_derivative_filtered_motor2 = LowPassFilter.step(error1_derivative_motor2); //derivative wordt gefiltered, dit later aanpassen |
fb07 | 5:7e2c6d2235fe | 169 | double Ud_motor2 = Kd_motor2 * error1_derivative_filtered_motor2; |
fb07 | 5:7e2c6d2235fe | 170 | error1_prev_motor2 = error1_motor2; |
fb07 | 5:7e2c6d2235fe | 171 | |
fb07 | 5:7e2c6d2235fe | 172 | double P_motor2 = Up_motor2 + Ui_motor2 + Ud_motor2; //sommatie van de u's |
fb07 | 5:7e2c6d2235fe | 173 | |
fb07 | 5:7e2c6d2235fe | 174 | return P_motor2; |
fb07 | 5:7e2c6d2235fe | 175 | } |
fb07 | 5:7e2c6d2235fe | 176 | // 4.3 State-Machine ******************************************************* |
fb07 | 9:c4fa72ffa1c2 | 177 | void motor1_calibration() |
fb07 | 9:c4fa72ffa1c2 | 178 | { |
fb07 | 9:c4fa72ffa1c2 | 179 | motor1DirectionPin=0; //direction of the motor |
fb07 | 9:c4fa72ffa1c2 | 180 | motor1=0.2; |
fb07 | 9:c4fa72ffa1c2 | 181 | wait(1); |
fb07 | 9:c4fa72ffa1c2 | 182 | while (abs(positie_verschil_motor1)>5) |
fb07 | 9:c4fa72ffa1c2 | 183 | { |
fb07 | 9:c4fa72ffa1c2 | 184 | motor1=0.2 ; |
fb07 | 9:c4fa72ffa1c2 | 185 | pc.printf("\r\n Kalibratie werkt "); |
fb07 | 9:c4fa72ffa1c2 | 186 | } |
fb07 | 9:c4fa72ffa1c2 | 187 | motor1=0.0; |
fb07 | 9:c4fa72ffa1c2 | 188 | pc.printf("\r\n Kalibratie werkt niet"); |
fb07 | 9:c4fa72ffa1c2 | 189 | |
fb07 | 9:c4fa72ffa1c2 | 190 | } |
fb07 | 9:c4fa72ffa1c2 | 191 | |
fb07 | 5:7e2c6d2235fe | 192 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 193 | // 5. Main Loop **************************************************************** |
fb07 | 2:45a85caaebfb | 194 | //****************************************************************************** |
fb07 | 2:45a85caaebfb | 195 | |
fb07 | 2:45a85caaebfb | 196 | void main_loop() { //Beginning of main_loop() |
fb07 | 5:7e2c6d2235fe | 197 | // pc.printf("main_loop is running succesfully \r\n"); //confirmation that main_loop is running (als je dit erin zet krijg je elke duizendste dit bericht. Dit is niet gewenst) |
fb07 | 9:c4fa72ffa1c2 | 198 | fencoder_motor1() ; |
fb07 | 9:c4fa72ffa1c2 | 199 | |
fb07 | 9:c4fa72ffa1c2 | 200 | |
fb07 | 5:7e2c6d2235fe | 201 | // 5.1 Measure Analog and Digital input signals ******************************** |
fb07 | 5:7e2c6d2235fe | 202 | // 5.2 Run state-machine(s) **************************************************** |
fb07 | 5:7e2c6d2235fe | 203 | // 5.3 Run controller(s) ******************************************************* |
fb07 | 5:7e2c6d2235fe | 204 | // 5.4 Send output signals to digital and PWM output pins ********************** |
fb07 | 2:45a85caaebfb | 205 | |
fb07 | 2:45a85caaebfb | 206 | |
fb07 | 2:45a85caaebfb | 207 | } //Ending of main_loop() |
fb07 | 2:45a85caaebfb | 208 | |
fb07 | 2:45a85caaebfb | 209 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 210 | // 6. Main function ************************************************************ |
fb07 | 2:45a85caaebfb | 211 | //****************************************************************************** |
fb07 | 2:45a85caaebfb | 212 | int main() |
fb07 | 5:7e2c6d2235fe | 213 | { //Beginning of Main() Function //All the things we do only once, some relevant things are now missing here: set pwmperiod to 60 microsec. Set Serial comm. Etc. Etc. |
fb07 | 5:7e2c6d2235fe | 214 | // 6.1 Initialization ********************************************************** |
RobertoO | 0:67c50348f842 | 215 | pc.baud(115200); |
fb07 | 2:45a85caaebfb | 216 | pc.printf("\r\nStarting Project BioRobotics - Opening a Door " //print Project information |
fb07 | 2:45a85caaebfb | 217 | "- Group 13 2019/2020 \r\n" |
fb07 | 2:45a85caaebfb | 218 | "Dion ten Berge - s1864734 \r\n" |
fb07 | 2:45a85caaebfb | 219 | "Bas Rutteman - s1854305 \r\n" |
fb07 | 2:45a85caaebfb | 220 | "Nick in het Veld - s1915584 \r\n" |
fb07 | 2:45a85caaebfb | 221 | "Marleen van der Weij - s1800078 \r\n" |
fb07 | 2:45a85caaebfb | 222 | "Mevlid Yildirim - s2005735 \r\n"); |
fb07 | 5:7e2c6d2235fe | 223 | led_green.write(1); |
fb07 | 5:7e2c6d2235fe | 224 | led_red.write(1); |
fb07 | 5:7e2c6d2235fe | 225 | led_blue.write(0); |
fb07 | 9:c4fa72ffa1c2 | 226 | |
fb07 | 9:c4fa72ffa1c2 | 227 | ticker_mainloop.attach(&main_loop,0.001); // change back to 0.001f //Run the function main_loop 1000 times per second |
fb07 | 9:c4fa72ffa1c2 | 228 | ticker_hidscope.attach(&HIDScope, 0.001); //Ticker for Hidscope, different frequency compared to motors |
fb07 | 9:c4fa72ffa1c2 | 229 | motor1_calibration(); |
fb07 | 5:7e2c6d2235fe | 230 | // 6.2 While loop in main function********************************************** |
fb07 | 2:45a85caaebfb | 231 | while (true) { } //Is not used but has to remain in the code!! |
fb07 | 2:45a85caaebfb | 232 | |
fb07 | 2:45a85caaebfb | 233 | } //Ending of Main() Function |