
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@5:7e2c6d2235fe, 2019-10-21 (annotated)
- Committer:
- fb07
- Date:
- Mon Oct 21 23:28:27 2019 +0000
- Revision:
- 5:7e2c6d2235fe
- Parent:
- 4:784cc0f3c97b
- Child:
- 6:79e42e1b87cb
- Child:
- 9:c4fa72ffa1c2
Voor het toevoegen van de state-machine
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 | 5:7e2c6d2235fe | 45 | |
fb07 | 5:7e2c6d2235fe | 46 | // 3.3 BiQuad Filters ********************************************************** |
fb07 | 5:7e2c6d2235fe | 47 | static BiQuad notchfilter(9.97761e-01, -1.97095e+00, 9.97761e-01, -1.97095e+00, 9.95522e-01); |
fb07 | 5:7e2c6d2235fe | 48 | static BiQuad highfilter(9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01); |
fb07 | 5:7e2c6d2235fe | 49 | static BiQuad LowPassFilter( 4.12535e-02, 8.25071e-02, 4.12535e-02, -1.34897e+00, 5.13982e-01 ); |
RobertoO | 0:67c50348f842 | 50 | |
fb07 | 5:7e2c6d2235fe | 51 | // 3.4 Hardware *************************************************************** |
fb07 | 5:7e2c6d2235fe | 52 | //3.4a Leds |
fb07 | 5:7e2c6d2235fe | 53 | DigitalOut led_red(LED_RED); // Defines the red led on the K64 board (0=on, 1 = off) |
fb07 | 5:7e2c6d2235fe | 54 | DigitalOut led_green(LED_GREEN); // Defines the green led on the K64 board (0=on, 1 = off) |
fb07 | 5:7e2c6d2235fe | 55 | DigitalOut led_blue(LED_BLUE); // Defines the blue led on the K64 board (0=on, 1 = off) |
fb07 | 5:7e2c6d2235fe | 56 | // FastPWM led1(D8); //CODE DOES NOT WORK WITH D8 PIN DEFINED //Defines Led1 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 57 | FastPWM led2(D9); //Defines Led2 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 58 | |
fb07 | 5:7e2c6d2235fe | 59 | //3.4b Potmeters and buttons |
fb07 | 5:7e2c6d2235fe | 60 | AnalogIn pot1_links(A5); //Defines potmeter1 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 61 | AnalogIn pot2_rechts(A4); //Defines potmeter2 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 62 | DigitalIn button1(D2); //Defines button1 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 63 | DigitalIn button2(D3); //Defines button2 on the BioRobotics Shield |
fb07 | 5:7e2c6d2235fe | 64 | DigitalIn sw2(SW2); //Defines button SW2 on the K64 board |
fb07 | 5:7e2c6d2235fe | 65 | DigitalIn sw3(SW3); //Defines button SW3 on the K64 board |
fb07 | 5:7e2c6d2235fe | 66 | |
fb07 | 5:7e2c6d2235fe | 67 | //3.4c Motors |
fb07 | 5:7e2c6d2235fe | 68 | DigitalOut motor1DirectionPin(D7); // motor 1 direction control (1=cw, 0=ccw) |
fb07 | 5:7e2c6d2235fe | 69 | FastPWM motor1(D6); // motor 1 velocity control (between 0-1) |
fb07 | 5:7e2c6d2235fe | 70 | FastPWM motor2(D5); // motor 2 velocity control (between 0-1) |
fb07 | 5:7e2c6d2235fe | 71 | DigitalOut motor2DirectionPin(D4); // motor 2 direction control (1=cw, 0=ccw) |
fb07 | 5:7e2c6d2235fe | 72 | |
fb07 | 5:7e2c6d2235fe | 73 | // 3.5 PID variabelen *************************************************************** |
fb07 | 5:7e2c6d2235fe | 74 | //3.5a PID-controller motor 1 |
fb07 | 5:7e2c6d2235fe | 75 | 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 | 76 | static double error_integral_motor1 = 0; |
fb07 | 5:7e2c6d2235fe | 77 | double Yref_motor1; |
fb07 | 5:7e2c6d2235fe | 78 | double kp_motor1; |
fb07 | 5:7e2c6d2235fe | 79 | double Ki_motor1; |
fb07 | 5:7e2c6d2235fe | 80 | double Kd_motor1; |
fb07 | 2:45a85caaebfb | 81 | |
fb07 | 5:7e2c6d2235fe | 82 | double positie_motor1; //counts encoder |
fb07 | 5:7e2c6d2235fe | 83 | double error1_motor1; |
fb07 | 5:7e2c6d2235fe | 84 | double error1_prev_motor1; |
fb07 | 5:7e2c6d2235fe | 85 | double error1_derivative_motor1; |
fb07 | 5:7e2c6d2235fe | 86 | double error1_derivative_filtered_motor1; |
fb07 | 5:7e2c6d2235fe | 87 | double P_motor1; |
fb07 | 5:7e2c6d2235fe | 88 | |
fb07 | 5:7e2c6d2235fe | 89 | //3.5b PID-controller motor 2 |
fb07 | 5:7e2c6d2235fe | 90 | 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 | 91 | static double error_integral_motor2 = 0; |
fb07 | 5:7e2c6d2235fe | 92 | double Yref_motor2; |
fb07 | 5:7e2c6d2235fe | 93 | double kp_motor2; |
fb07 | 5:7e2c6d2235fe | 94 | double Ki_motor2; |
fb07 | 5:7e2c6d2235fe | 95 | double Kd_motor2; |
fb07 | 2:45a85caaebfb | 96 | |
fb07 | 5:7e2c6d2235fe | 97 | double positie_motor2; //counts encoder |
fb07 | 5:7e2c6d2235fe | 98 | double error1_motor2; |
fb07 | 5:7e2c6d2235fe | 99 | double error1_prev_motor2; |
fb07 | 5:7e2c6d2235fe | 100 | double error1_derivative_motor2; |
fb07 | 5:7e2c6d2235fe | 101 | double error1_derivative_filtered_motor2; |
fb07 | 5:7e2c6d2235fe | 102 | double P_motor2; |
RobertoO | 0:67c50348f842 | 103 | |
fb07 | 2:45a85caaebfb | 104 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 105 | // 4. Functions **************************************************************** |
fb07 | 5:7e2c6d2235fe | 106 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 107 | |
fb07 | 5:7e2c6d2235fe | 108 | // 4.1 Hidscope **************************************************************** |
fb07 | 5:7e2c6d2235fe | 109 | void HIDScope() //voor HIDscope |
fb07 | 5:7e2c6d2235fe | 110 | { |
fb07 | 5:7e2c6d2235fe | 111 | scope.set(0, Yref_motor1); //nog te definieren wat we willen weergeven |
fb07 | 5:7e2c6d2235fe | 112 | scope.set(1, positie_motor1); //nog te definieren wat we willen weergeven |
fb07 | 5:7e2c6d2235fe | 113 | scope.send(); |
fb07 | 5:7e2c6d2235fe | 114 | } |
fb07 | 5:7e2c6d2235fe | 115 | |
fb07 | 5:7e2c6d2235fe | 116 | // 4.2a PID-Controller motor 1************************************************** |
fb07 | 5:7e2c6d2235fe | 117 | double PID_controller_motor1() |
fb07 | 5:7e2c6d2235fe | 118 | { |
fb07 | 5:7e2c6d2235fe | 119 | //Proportional part |
fb07 | 5:7e2c6d2235fe | 120 | kp_motor1 = 0.01 ; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 121 | double Up_motor1 = kp_motor1 * error1_motor1; |
fb07 | 5:7e2c6d2235fe | 122 | |
fb07 | 5:7e2c6d2235fe | 123 | //Integral part |
fb07 | 5:7e2c6d2235fe | 124 | Ki_motor1 = 0.0001; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 125 | error_integral_motor1 = error_integral_motor1 + (Ts*error1_motor1); // integrale fout + (de sample tijd * fout) |
fb07 | 5:7e2c6d2235fe | 126 | double Ui_motor1 = Ki_motor1 * error_integral_motor1; // (fout * integrale fout) |
fb07 | 5:7e2c6d2235fe | 127 | |
fb07 | 5:7e2c6d2235fe | 128 | //Derivative part |
fb07 | 5:7e2c6d2235fe | 129 | Kd_motor1 = 0.01 ;// moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 130 | error1_derivative_motor1 = (error1_motor1 - error1_prev_motor1)/Ts; // (Fout - de vorige fout) / tijdstap = afgeleide |
fb07 | 5:7e2c6d2235fe | 131 | error1_derivative_filtered_motor1 = LowPassFilter.step(error1_derivative_motor1); //derivative wordt gefiltered |
fb07 | 5:7e2c6d2235fe | 132 | double Ud_motor1 = Kd_motor1 * error1_derivative_filtered_motor1; // (afgeleide gain) * (afgeleide gefilterde fout) |
fb07 | 5:7e2c6d2235fe | 133 | error1_prev_motor1 = error1_motor1; |
fb07 | 5:7e2c6d2235fe | 134 | |
fb07 | 5:7e2c6d2235fe | 135 | double P_motor1 = Up_motor1 + Ui_motor1 + Ud_motor1; //sommatie van de u's |
fb07 | 5:7e2c6d2235fe | 136 | |
fb07 | 5:7e2c6d2235fe | 137 | return P_motor1; |
fb07 | 5:7e2c6d2235fe | 138 | } |
fb07 | 5:7e2c6d2235fe | 139 | |
fb07 | 5:7e2c6d2235fe | 140 | // 4.2b PID-Controller motor 2************************************************** |
fb07 | 5:7e2c6d2235fe | 141 | double PID_controller_motor2() |
fb07 | 5:7e2c6d2235fe | 142 | { |
fb07 | 5:7e2c6d2235fe | 143 | //Proportional part |
fb07 | 5:7e2c6d2235fe | 144 | kp_motor2 = 0.01 ; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 145 | double Up_motor2 = kp_motor2 * error1_motor2; |
fb07 | 5:7e2c6d2235fe | 146 | |
fb07 | 5:7e2c6d2235fe | 147 | //Integral part |
fb07 | 5:7e2c6d2235fe | 148 | Ki_motor2 = 0.0001; // moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 149 | error_integral_motor2 = error_integral_motor2 + (Ts*error1_motor2); // integrale fout + (de sample tijd * fout) |
fb07 | 5:7e2c6d2235fe | 150 | double Ui_motor2 = Ki_motor2 * error_integral_motor2; //de fout keer de integrale fout |
fb07 | 5:7e2c6d2235fe | 151 | |
fb07 | 5:7e2c6d2235fe | 152 | //Derivative part |
fb07 | 5:7e2c6d2235fe | 153 | Kd_motor2 = 0.01 ;// moet nog getweaked worden |
fb07 | 5:7e2c6d2235fe | 154 | error1_derivative_motor2 = (error1_motor2 - error1_prev_motor2)/Ts; |
fb07 | 5:7e2c6d2235fe | 155 | error1_derivative_filtered_motor2 = LowPassFilter.step(error1_derivative_motor2); //derivative wordt gefiltered, dit later aanpassen |
fb07 | 5:7e2c6d2235fe | 156 | double Ud_motor2 = Kd_motor2 * error1_derivative_filtered_motor2; |
fb07 | 5:7e2c6d2235fe | 157 | error1_prev_motor2 = error1_motor2; |
fb07 | 5:7e2c6d2235fe | 158 | |
fb07 | 5:7e2c6d2235fe | 159 | double P_motor2 = Up_motor2 + Ui_motor2 + Ud_motor2; //sommatie van de u's |
fb07 | 5:7e2c6d2235fe | 160 | |
fb07 | 5:7e2c6d2235fe | 161 | return P_motor2; |
fb07 | 5:7e2c6d2235fe | 162 | } |
fb07 | 5:7e2c6d2235fe | 163 | // 4.3 State-Machine ******************************************************* |
fb07 | 5:7e2c6d2235fe | 164 | |
fb07 | 5:7e2c6d2235fe | 165 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 166 | // 5. Main Loop **************************************************************** |
fb07 | 2:45a85caaebfb | 167 | //****************************************************************************** |
fb07 | 2:45a85caaebfb | 168 | |
fb07 | 2:45a85caaebfb | 169 | void main_loop() { //Beginning of main_loop() |
fb07 | 5:7e2c6d2235fe | 170 | // 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 | 5:7e2c6d2235fe | 171 | |
fb07 | 5:7e2c6d2235fe | 172 | // 5.1 Measure Analog and Digital input signals ******************************** |
fb07 | 5:7e2c6d2235fe | 173 | // 5.2 Run state-machine(s) **************************************************** |
fb07 | 5:7e2c6d2235fe | 174 | // 5.3 Run controller(s) ******************************************************* |
fb07 | 5:7e2c6d2235fe | 175 | // 5.4 Send output signals to digital and PWM output pins ********************** |
fb07 | 2:45a85caaebfb | 176 | |
fb07 | 2:45a85caaebfb | 177 | |
fb07 | 2:45a85caaebfb | 178 | } //Ending of main_loop() |
fb07 | 2:45a85caaebfb | 179 | |
fb07 | 2:45a85caaebfb | 180 | //****************************************************************************** |
fb07 | 5:7e2c6d2235fe | 181 | // 6. Main function ************************************************************ |
fb07 | 2:45a85caaebfb | 182 | //****************************************************************************** |
fb07 | 2:45a85caaebfb | 183 | int main() |
fb07 | 5:7e2c6d2235fe | 184 | { //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 | 185 | // 6.1 Initialization ********************************************************** |
RobertoO | 0:67c50348f842 | 186 | pc.baud(115200); |
fb07 | 2:45a85caaebfb | 187 | pc.printf("\r\nStarting Project BioRobotics - Opening a Door " //print Project information |
fb07 | 2:45a85caaebfb | 188 | "- Group 13 2019/2020 \r\n" |
fb07 | 2:45a85caaebfb | 189 | "Dion ten Berge - s1864734 \r\n" |
fb07 | 2:45a85caaebfb | 190 | "Bas Rutteman - s1854305 \r\n" |
fb07 | 2:45a85caaebfb | 191 | "Nick in het Veld - s1915584 \r\n" |
fb07 | 2:45a85caaebfb | 192 | "Marleen van der Weij - s1800078 \r\n" |
fb07 | 2:45a85caaebfb | 193 | "Mevlid Yildirim - s2005735 \r\n"); |
fb07 | 5:7e2c6d2235fe | 194 | led_green.write(1); |
fb07 | 5:7e2c6d2235fe | 195 | led_red.write(1); |
fb07 | 5:7e2c6d2235fe | 196 | led_blue.write(0); |
RobertoO | 0:67c50348f842 | 197 | |
fb07 | 5:7e2c6d2235fe | 198 | ticker_mainloop.attach(&main_loop,0.001f); // change back to 0.001f //Run the function main_loop 1000 times per second |
fb07 | 5:7e2c6d2235fe | 199 | ticker_hidscope.attach(&HIDScope, 0.1f); //Ticker for Hidscope, different frequency compared to motors |
fb07 | 2:45a85caaebfb | 200 | |
fb07 | 5:7e2c6d2235fe | 201 | // 6.2 While loop in main function********************************************** |
fb07 | 2:45a85caaebfb | 202 | while (true) { } //Is not used but has to remain in the code!! |
fb07 | 2:45a85caaebfb | 203 | |
fb07 | 2:45a85caaebfb | 204 | } //Ending of Main() Function |