Raphael Schmidt
/
GrannyStepper
Projekt
main.cpp@34:f035a1869c34, 2022-04-06 (annotated)
- Committer:
- schmir06
- Date:
- Wed Apr 06 09:32:30 2022 +0200
- Revision:
- 34:f035a1869c34
- Parent:
- 33:cce9a88a307a
- Child:
- 35:57b39290aaea
Counter
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 | 34:f035a1869c34 | 9 | |
schmir06 | 33:cce9a88a307a | 10 | //Ausgänge |
schmir06 | 33:cce9a88a307a | 11 | DigitalOut enable_motors(PB_15); // Motoren freischalten |
pmic | 17:c19b471f05cb | 12 | |
schmir06 | 34:f035a1869c34 | 13 | // while loop gets executed every main_task_period_ms milliseconds |
schmir06 | 34:f035a1869c34 | 14 | 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 | 15 | 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 | 16 | |
schmir06 | 33:cce9a88a307a | 17 | //DC-Motoren |
schmir06 | 33:cce9a88a307a | 18 | float pwm_period_s = 0.00005f; //PWM-Periode fürd DC-Motoren definiert |
schmir06 | 33:cce9a88a307a | 19 | FastPWM pwm_M1(PB_13); |
schmir06 | 33:cce9a88a307a | 20 | //Endcoder |
schmir06 | 33:cce9a88a307a | 21 | EncoderCounter encoder_M1(PA_6, PC_7); |
pmic | 17:c19b471f05cb | 22 | |
pmic | 30:1e8295770bc1 | 23 | // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box |
pmic | 24:86f1a63e35a0 | 24 | 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 | 25 | float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio |
pmic | 25:ea1d6e27c895 | 26 | float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
pmic | 30:1e8295770bc1 | 27 | 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 | 28 | float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 6:e1fa1a2d7483 | 29 | |
pmic | 30:1e8295770bc1 | 30 | // SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters |
schmir06 | 33:cce9a88a307a | 31 | SpeedController speedController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M1, encoder_M1); // parameters adjusted to 100:1 gear |
pmic | 24:86f1a63e35a0 | 32 | 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 | 33 | |
pmic | 6:e1fa1a2d7483 | 34 | |
schmir06 | 34:f035a1869c34 | 35 | int main(){ |
schmir06 | 34:f035a1869c34 | 36 | enable_motors = 1; |
schmir06 | 34:f035a1869c34 | 37 | user_button.read(); |
schmir06 | 34:f035a1869c34 | 38 | |
schmir06 | 34:f035a1869c34 | 39 | int counter_user_button, counter_stopp = 0; |
schmir06 | 34:f035a1869c34 | 40 | if (user_button == 1 && counter_stopp == 0) { |
schmir06 | 34:f035a1869c34 | 41 | counter_user_button++; |
schmir06 | 34:f035a1869c34 | 42 | counter_stopp = 1; |
schmir06 | 34:f035a1869c34 | 43 | |
schmir06 | 34:f035a1869c34 | 44 | } |
schmir06 | 34:f035a1869c34 | 45 | if (user_button == 0){ |
schmir06 | 34:f035a1869c34 | 46 | counter_stopp = 0; |
schmir06 | 34:f035a1869c34 | 47 | } |
schmir06 | 34:f035a1869c34 | 48 | if (counter_user_button >= 3){ |
schmir06 | 34:f035a1869c34 | 49 | counter_user_button = 1; |
schmir06 | 34:f035a1869c34 | 50 | } |
schmir06 | 34:f035a1869c34 | 51 | switch (counter_user_button){ |
schmir06 | 34:f035a1869c34 | 52 | case 1: |
schmir06 | 34:f035a1869c34 | 53 | cout <<"case1"; |
schmir06 | 34:f035a1869c34 | 54 | break; |
schmir06 | 34:f035a1869c34 | 55 | case 2: |
schmir06 | 34:f035a1869c34 | 56 | cout <<"case2"; |
schmir06 | 34:f035a1869c34 | 57 | break; |
schmir06 | 34:f035a1869c34 | 58 | } |
pmic | 6:e1fa1a2d7483 | 59 | |
pmic | 24:86f1a63e35a0 | 60 | |
schmir06 | 34:f035a1869c34 | 61 | |
schmir06 | 34:f035a1869c34 | 62 | |
schmir06 | 34:f035a1869c34 | 63 | |
schmir06 | 34:f035a1869c34 | 64 | } |
schmir06 | 34:f035a1869c34 | 65 |