Projekt

Dependencies:   PM2_Libary

Committer:
schmir06
Date:
Wed Apr 06 11:48:51 2022 +0200
Revision:
38:ba15402d9c94
Parent:
37:3c427322799d
Child:
39:c0c2e33a2181
motor control

Who changed what in which revision?

UserRevisionLine numberNew 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 37:3c427322799d 31 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 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 35:57b39290aaea 36
schmir06 35:57b39290aaea 37
schmir06 35:57b39290aaea 38 int counter_user_button = 0;
schmir06 35:57b39290aaea 39 bool counter_stopp = 0;
schmir06 37:3c427322799d 40 enable_motors = 1;
schmir06 36:f678d693969f 41
schmir06 35:57b39290aaea 42 while(1){
schmir06 37:3c427322799d 43
schmir06 36:f678d693969f 44 if ((user_button.read() == 0) & (counter_stopp == 0)) {
schmir06 35:57b39290aaea 45 counter_user_button++;
schmir06 35:57b39290aaea 46 counter_stopp = 1;
schmir06 34:f035a1869c34 47
schmir06 35:57b39290aaea 48 }
schmir06 36:f678d693969f 49 if (user_button.read()){
schmir06 35:57b39290aaea 50 counter_stopp = 0;
schmir06 35:57b39290aaea 51 }
schmir06 35:57b39290aaea 52 if (counter_user_button >= 3){
schmir06 35:57b39290aaea 53 counter_user_button = 1;
schmir06 35:57b39290aaea 54 }
schmir06 37:3c427322799d 55 float speed_counter;
schmir06 35:57b39290aaea 56 switch (counter_user_button){
schmir06 35:57b39290aaea 57 case 1:
schmir06 38:ba15402d9c94 58 speed_counter = 1.0f;
schmir06 35:57b39290aaea 59 break;
schmir06 35:57b39290aaea 60 case 2:
schmir06 38:ba15402d9c94 61 speed_counter = 0.5f;
schmir06 36:f678d693969f 62 break;
schmir06 35:57b39290aaea 63 }
schmir06 38:ba15402d9c94 64 pwm_M1.write(speed_counter);
schmir06 38:ba15402d9c94 65 printf( "counter %d stopp %d button %d speed %f \n",counter_user_button, counter_stopp, !user_button.read(), speed_counter);
schmir06 37:3c427322799d 66 }
schmir06 37:3c427322799d 67
schmir06 36:f678d693969f 68 }
pmic 24:86f1a63e35a0 69
schmir06 34:f035a1869c34 70
schmir06 34:f035a1869c34 71
schmir06 34:f035a1869c34 72
schmir06 34:f035a1869c34 73
schmir06 36:f678d693969f 74