Workshop 1

Dependencies:   PM2_Libary Eigen

Committer:
colehelgeson
Date:
Thu May 19 09:19:03 2022 +0200
Revision:
46:31e06f30e91c
Parent:
45:a8a2670980cd
Child:
47:1fe4d9135c03
added speed control

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 36:8c75783c1eca 1 #include <mbed.h>
pmic 36:8c75783c1eca 2
pmic 17:c19b471f05cb 3 #include "PM2_Libary.h"
pmic 36:8c75783c1eca 4 #include "Eigen/Dense.h"
pmic 36:8c75783c1eca 5
pmic 36:8c75783c1eca 6 # define M_PI 3.14159265358979323846 // number pi
pmic 6:e1fa1a2d7483 7
colehelgeson 45:a8a2670980cd 8 //WORKSHOP 1
colehelgeson 45:a8a2670980cd 9
pmic 24:86f1a63e35a0 10 // logical variable main task
pmic 24:86f1a63e35a0 11 bool do_execute_main_task = false; // this variable will be toggled via the user button (blue button) to or not to execute the main task
pmic 17:c19b471f05cb 12
pmic 24:86f1a63e35a0 13 // user button on nucleo board
pmic 24:86f1a63e35a0 14 Timer user_button_timer; // create Timer object which we use to check if user button was pressed for a certain time (robust against signal bouncing)
pmic 24:86f1a63e35a0 15 InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR)
pmic 24:86f1a63e35a0 16 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 17 void user_button_released_fcn();
colehelgeson 46:31e06f30e91c 18 float dist_conversion(float dist);
pmic 6:e1fa1a2d7483 19
pmic 1:93d997d6b232 20 int main()
pmic 23:26b3a25fc637 21 {
pmic 36:8c75783c1eca 22 // while loop gets executed every main_task_period_ms milliseconds
pmic 41:8a63b01edd7e 23 const int main_task_period_ms = 10; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second
pmic 36:8c75783c1eca 24 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
pmic 36:8c75783c1eca 25
pmic 40:7e6b7aec3947 26 // a coutner
pmic 40:7e6b7aec3947 27 uint32_t main_task_cntr = 0;
pmic 40:7e6b7aec3947 28
pmic 36:8c75783c1eca 29 // led on nucleo board
pmic 36:8c75783c1eca 30 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 36:8c75783c1eca 31
colehelgeson 46:31e06f30e91c 32 DigitalOut enable_motors(PB_15);
colehelgeson 46:31e06f30e91c 33
colehelgeson 46:31e06f30e91c 34
colehelgeson 46:31e06f30e91c 35
colehelgeson 46:31e06f30e91c 36 FastPWM pwm_M1(PB_13);
colehelgeson 46:31e06f30e91c 37 FastPWM pwm_M2(PA_9);
colehelgeson 46:31e06f30e91c 38
colehelgeson 46:31e06f30e91c 39 enable_motors = 1;
colehelgeson 46:31e06f30e91c 40
colehelgeson 46:31e06f30e91c 41 EncoderCounter encoder_M1(PA_6, PC_7);
colehelgeson 46:31e06f30e91c 42 EncoderCounter encoder_M2(PB_6, PB_7);
colehelgeson 46:31e06f30e91c 43
colehelgeson 46:31e06f30e91c 44 const float max_voltage = 12.0;
colehelgeson 46:31e06f30e91c 45 const float counts_per_turn = 20.0 * 78.125;
colehelgeson 46:31e06f30e91c 46 const float kn = 180.0/12.0;
colehelgeson 46:31e06f30e91c 47
colehelgeson 46:31e06f30e91c 48
colehelgeson 46:31e06f30e91c 49 SpeedController s1(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1);
colehelgeson 46:31e06f30e91c 50 s1.setMaxAccelerationRPM(99999.0);
colehelgeson 46:31e06f30e91c 51 s1.setSpeedCntrlGain(0.025);
colehelgeson 46:31e06f30e91c 52
colehelgeson 46:31e06f30e91c 53 // AnalogIn ir_analog_in(PC_2);
colehelgeson 46:31e06f30e91c 54 // float ir_distance_mV = 0.0f;
colehelgeson 46:31e06f30e91c 55 // float ir_distance_cm;
colehelgeson 46:31e06f30e91c 56
pmic 24:86f1a63e35a0 57 // attach button fall and rise functions to user button object
pmic 24:86f1a63e35a0 58 user_button.fall(&user_button_pressed_fcn);
pmic 24:86f1a63e35a0 59 user_button.rise(&user_button_released_fcn);
pmic 17:c19b471f05cb 60
pmic 29:d6f1ccf42a31 61 // start timer
pmic 24:86f1a63e35a0 62 main_task_timer.start();
pmic 6:e1fa1a2d7483 63
colehelgeson 46:31e06f30e91c 64 while (true) { // this loop will run foreverd
pmic 6:e1fa1a2d7483 65
pmic 24:86f1a63e35a0 66 main_task_timer.reset();
colehelgeson 46:31e06f30e91c 67
colehelgeson 46:31e06f30e91c 68 // ir_distance_mV = ir_analog_in.read() *3.3f *1.0e3f;
colehelgeson 46:31e06f30e91c 69 // ir_distance_cm = dist_conversion(ir_distance_mV);
pmic 40:7e6b7aec3947 70
pmic 44:a65bc3e11481 71
pmic 24:86f1a63e35a0 72 if (do_execute_main_task) {
pmic 17:c19b471f05cb 73
colehelgeson 46:31e06f30e91c 74 s1.setDesiredSpeedRPS(2.0);
colehelgeson 46:31e06f30e91c 75
pmic 1:93d997d6b232 76 } else {
colehelgeson 46:31e06f30e91c 77 s1.setDesiredSpeedRPS(0.0);
pmic 1:93d997d6b232 78 }
pmic 6:e1fa1a2d7483 79
pmic 41:8a63b01edd7e 80 // user_led is switching its state every second
pmic 41:8a63b01edd7e 81 if ( (main_task_cntr%(1000 / main_task_period_ms) == 0) && (main_task_cntr!=0) ) {
pmic 40:7e6b7aec3947 82 user_led = !user_led;
pmic 40:7e6b7aec3947 83 }
pmic 40:7e6b7aec3947 84 main_task_cntr++;
colehelgeson 46:31e06f30e91c 85
colehelgeson 46:31e06f30e91c 86 printf("%f\n", s1.getSpeedRPS());
colehelgeson 46:31e06f30e91c 87
colehelgeson 46:31e06f30e91c 88 // printf("IR sensor (mV): %f\nCM:%f\n", ir_distance_mV, ir_distance_cm);
pmic 40:7e6b7aec3947 89
pmic 24:86f1a63e35a0 90 // do only output via serial what's really necessary (this makes your code slow)
pmic 41:8a63b01edd7e 91 /*
pmic 41:8a63b01edd7e 92 printf("IR sensor (mV): %3.3f, IR sensor (cm): %3.3f, SensorBar angle (rad): %3.3f, Speed M1 (rps) %3.3f, Position M2 (rot): %3.3f\r\n",
pmic 24:86f1a63e35a0 93 ir_distance_mV,
pmic 41:8a63b01edd7e 94 ir_distance_cm,
pmic 40:7e6b7aec3947 95 sensor_bar_avgAngleRad,
pmic 40:7e6b7aec3947 96 speedController_M1.getSpeedRPS(),
pmic 40:7e6b7aec3947 97 positionController_M2.getRotation());
pmic 41:8a63b01edd7e 98 */
pmic 17:c19b471f05cb 99
pmic 24:86f1a63e35a0 100 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 101 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 102 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
pmic 1:93d997d6b232 103 }
pmic 1:93d997d6b232 104 }
pmic 6:e1fa1a2d7483 105
pmic 24:86f1a63e35a0 106 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 107 {
pmic 26:28693b369945 108 user_button_timer.start();
pmic 6:e1fa1a2d7483 109 user_button_timer.reset();
pmic 6:e1fa1a2d7483 110 }
pmic 6:e1fa1a2d7483 111
pmic 24:86f1a63e35a0 112 void user_button_released_fcn()
pmic 6:e1fa1a2d7483 113 {
pmic 24:86f1a63e35a0 114 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 115 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 116 user_button_timer.stop();
pmic 24:86f1a63e35a0 117 if (user_button_elapsed_time_ms > 200) {
pmic 24:86f1a63e35a0 118 do_execute_main_task = !do_execute_main_task;
pmic 8:9bb806a7f585 119 }
colehelgeson 46:31e06f30e91c 120 }
colehelgeson 46:31e06f30e91c 121
colehelgeson 46:31e06f30e91c 122 // float dist_conversion(float dist){
colehelgeson 46:31e06f30e91c 123 // float a = 4.655;
colehelgeson 46:31e06f30e91c 124 // float c = 1.092e+04;
colehelgeson 46:31e06f30e91c 125
colehelgeson 46:31e06f30e91c 126 // return c/(dist + 1) * a;
colehelgeson 46:31e06f30e91c 127 // }