Raphael Schmidt
/
GrannyStepper
Projekt
main.cpp@40:47997e887235, 2022-04-20 (annotated)
- Committer:
- schmir06
- Date:
- Wed Apr 20 08:22:36 2022 +0200
- Revision:
- 40:47997e887235
- Parent:
- 39:c0c2e33a2181
- Child:
- 41:e86f6f2e2bfc
Geschwindigkeit bei DC-Motoren kontrollieren
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
schmir06 | 32:2dc6fbd004fd | 1 | //GrannyStepper |
pmic | 1:93d997d6b232 | 2 | #include "mbed.h" |
pmic | 17:c19b471f05cb | 3 | #include "PM2_Libary.h" |
schmir06 | 34:f035a1869c34 | 4 | #include <iostream> |
schmir06 | 34:f035a1869c34 | 5 | using namespace std; |
pmic | 6:e1fa1a2d7483 | 6 | |
schmir06 | 33:cce9a88a307a | 7 | //Eingänge |
schmir06 | 34:f035a1869c34 | 8 | DigitalIn user_button(PC_13); |
schmir06 | 40:47997e887235 | 9 | DigitalIn mechanical_button(PC_5); |
schmir06 | 34:f035a1869c34 | 10 | |
schmir06 | 33:cce9a88a307a | 11 | //Ausgänge |
schmir06 | 33:cce9a88a307a | 12 | DigitalOut enable_motors(PB_15); // Motoren freischalten |
pmic | 17:c19b471f05cb | 13 | |
schmir06 | 34:f035a1869c34 | 14 | // while loop gets executed every main_task_period_ms milliseconds |
schmir06 | 34:f035a1869c34 | 15 | int main_task_period_ms = 50; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second |
schmir06 | 34:f035a1869c34 | 16 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
schmir06 | 34:f035a1869c34 | 17 | |
schmir06 | 33:cce9a88a307a | 18 | //DC-Motoren |
schmir06 | 33:cce9a88a307a | 19 | float pwm_period_s = 0.00005f; //PWM-Periode fürd DC-Motoren definiert |
schmir06 | 33:cce9a88a307a | 20 | FastPWM pwm_M1(PB_13); |
schmir06 | 33:cce9a88a307a | 21 | //Endcoder |
schmir06 | 33:cce9a88a307a | 22 | EncoderCounter encoder_M1(PA_6, PC_7); |
pmic | 17:c19b471f05cb | 23 | |
pmic | 30:1e8295770bc1 | 24 | // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box |
pmic | 24:86f1a63e35a0 | 25 | float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack |
pmic | 24:86f1a63e35a0 | 26 | float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio |
pmic | 25:ea1d6e27c895 | 27 | float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
pmic | 30:1e8295770bc1 | 28 | float k_gear = 100.0f / 78.125f; // define additional ratio in case you are using a dc motor with a different gear box, e.g. 100:1 |
pmic | 30:1e8295770bc1 | 29 | float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 6:e1fa1a2d7483 | 30 | |
pmic | 30:1e8295770bc1 | 31 | // SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters |
schmir06 | 40:47997e887235 | 32 | //SpeedController speedController_M1(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M1, encoder_M1); // parameters adjusted to 100:1 gear |
pmic | 24:86f1a63e35a0 | 33 | float max_speed_rps = 0.5f; // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage |
pmic | 6:e1fa1a2d7483 | 34 | |
schmir06 | 40:47997e887235 | 35 | // PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters |
schmir06 | 40:47997e887235 | 36 | PositionController positionController_M1(counts_per_turn * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M1, encoder_M1); // parameters adjusted to 100:1 gear, we need a different speed controller gain here |
pmic | 6:e1fa1a2d7483 | 37 | |
schmir06 | 34:f035a1869c34 | 38 | int main(){ |
schmir06 | 35:57b39290aaea | 39 | |
schmir06 | 35:57b39290aaea | 40 | |
schmir06 | 35:57b39290aaea | 41 | int counter_user_button = 0; |
schmir06 | 35:57b39290aaea | 42 | bool counter_stopp = 0; |
schmir06 | 39:c0c2e33a2181 | 43 | |
schmir06 | 36:f678d693969f | 44 | |
schmir06 | 35:57b39290aaea | 45 | while(1){ |
schmir06 | 37:3c427322799d | 46 | |
schmir06 | 36:f678d693969f | 47 | if ((user_button.read() == 0) & (counter_stopp == 0)) { |
schmir06 | 35:57b39290aaea | 48 | counter_user_button++; |
schmir06 | 35:57b39290aaea | 49 | counter_stopp = 1; |
schmir06 | 34:f035a1869c34 | 50 | |
schmir06 | 35:57b39290aaea | 51 | } |
schmir06 | 36:f678d693969f | 52 | if (user_button.read()){ |
schmir06 | 35:57b39290aaea | 53 | counter_stopp = 0; |
schmir06 | 35:57b39290aaea | 54 | } |
schmir06 | 40:47997e887235 | 55 | if (counter_user_button >= 5){ |
schmir06 | 35:57b39290aaea | 56 | counter_user_button = 1; |
schmir06 | 35:57b39290aaea | 57 | } |
schmir06 | 37:3c427322799d | 58 | float speed_counter; |
schmir06 | 35:57b39290aaea | 59 | switch (counter_user_button){ |
schmir06 | 35:57b39290aaea | 60 | case 1: |
schmir06 | 40:47997e887235 | 61 | enable_motors = 1; |
schmir06 | 39:c0c2e33a2181 | 62 | speed_counter = 0.0f; |
schmir06 | 40:47997e887235 | 63 | positionController_M1.setDesiredRotation(0.0f, 0.0f); |
schmir06 | 35:57b39290aaea | 64 | break; |
schmir06 | 35:57b39290aaea | 65 | case 2: |
schmir06 | 39:c0c2e33a2181 | 66 | enable_motors = 1; |
schmir06 | 39:c0c2e33a2181 | 67 | speed_counter = 0.1f; |
schmir06 | 40:47997e887235 | 68 | positionController_M1.setDesiredRotation(1.5f, 0.2f); |
schmir06 | 39:c0c2e33a2181 | 69 | break; |
schmir06 | 39:c0c2e33a2181 | 70 | case 3: |
schmir06 | 40:47997e887235 | 71 | enable_motors = 1; |
schmir06 | 40:47997e887235 | 72 | speed_counter = 0.0f; |
schmir06 | 40:47997e887235 | 73 | positionController_M1.setDesiredRotation(0.0f, 0.0f); |
schmir06 | 40:47997e887235 | 74 | break; |
schmir06 | 40:47997e887235 | 75 | case 4: |
schmir06 | 40:47997e887235 | 76 | enable_motors = 1; |
schmir06 | 39:c0c2e33a2181 | 77 | speed_counter = 1.0f; |
schmir06 | 40:47997e887235 | 78 | positionController_M1.setDesiredRotation(1.5f, 1.0f); |
schmir06 | 36:f678d693969f | 79 | break; |
schmir06 | 35:57b39290aaea | 80 | } |
schmir06 | 39:c0c2e33a2181 | 81 | |
schmir06 | 40:47997e887235 | 82 | //positionController_M1.setDesiredRotation(1.5f, speed_counter); |
schmir06 | 39:c0c2e33a2181 | 83 | |
schmir06 | 39:c0c2e33a2181 | 84 | /*if (counter_user_button == 1 || counter_user_button == 3 || counter_user_button == 5){ |
schmir06 | 39:c0c2e33a2181 | 85 | speedController_M1.setDesiredSpeedRPS(speed_counter); |
schmir06 | 39:c0c2e33a2181 | 86 | //pwm_M1.write(speed_counter); |
schmir06 | 39:c0c2e33a2181 | 87 | counter_user_button++; |
schmir06 | 39:c0c2e33a2181 | 88 | }*/ |
schmir06 | 39:c0c2e33a2181 | 89 | |
schmir06 | 39:c0c2e33a2181 | 90 | printf( "counter %d speed %f \n",counter_user_button, speed_counter); |
schmir06 | 39:c0c2e33a2181 | 91 | |
schmir06 | 37:3c427322799d | 92 | } |
schmir06 | 37:3c427322799d | 93 | |
schmir06 | 36:f678d693969f | 94 | } |
pmic | 24:86f1a63e35a0 | 95 | |
schmir06 | 34:f035a1869c34 | 96 | |
schmir06 | 34:f035a1869c34 | 97 | |
schmir06 | 34:f035a1869c34 | 98 | |
schmir06 | 34:f035a1869c34 | 99 | |
schmir06 | 36:f678d693969f | 100 |