Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PM2_Libary Eigen
main.cpp
00001 #include <mbed.h> 00002 00003 #include "PM2_Libary.h" 00004 #include "Eigen/Dense.h" 00005 00006 # define M_PI 3.14159265358979323846 // number pi 00007 00008 // logical variable main task 00009 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 00010 00011 // user button on nucleo board 00012 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) 00013 InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR) 00014 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below 00015 void user_button_released_fcn(); 00016 00017 float ir_distance_mV2cm(float ir_distance_mV); 00018 00019 int main() 00020 { 00021 // while loop gets executed every main_task_period_ms milliseconds 00022 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 00023 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms 00024 00025 // a coutner 00026 uint32_t main_task_cntr = 0; 00027 00028 // led on nucleo board 00029 DigitalOut user_led(LED1); // create DigitalOut object to command user led 00030 00031 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor 00032 float ir_distance_mV = 0.0f; // define variable to store measurement 00033 float ir_distance_cm = 0.0f; // compensated sensor value in cm 00034 AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 00035 00036 // create SensorBar object for sparkfun line follower array, only use this if it is connected (blocking your code if not) 00037 float sensor_bar_avgAngleRad = 0.0f; 00038 I2C i2c(PB_9, PB_8); 00039 //SensorBar sensor_bar(i2c, 0.1175f); // second input argument is distance from bar to wheel axis 00040 00041 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB 00042 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors 00043 00044 FastPWM pwm_M1(PB_13); // motor M1 is closed-loop speed controlled (angle velocity) 00045 FastPWM pwm_M2(PA_9); // motor M2 is closed-loop position controlled (angle controlled) 00046 00047 EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values 00048 EncoderCounter encoder_M2(PB_6, PB_7); 00049 00050 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box 00051 const float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack 00052 const float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio 00053 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V 00054 //const 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 00055 //const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 00056 00057 SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1); // default 78.125:1 gear box with default contoller parameters 00058 //SpeedController speedController_M1(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M1, encoder_M1); // parameters adjusted to 100:1 gear 00059 speedController_M1.setMaxAccelerationRPS(999.0f); // disable internal trajectory planer 00060 00061 PositionController positionController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear with default contoller parameters 00062 //PositionController positionController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 100:1 gear, we need a different speed controller gain here 00063 //positionController_M2.setSpeedCntrlGain(kp * k_gear); 00064 positionController_M2.setMaxAccelerationRPS(999.0f); // disable internal trajectory planer 00065 // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage 00066 float max_speed_rps = 2.0f; 00067 positionController_M2.setMaxVelocityRPS(max_speed_rps); 00068 00069 // attach button fall and rise functions to user button object 00070 user_button.fall(&user_button_pressed_fcn); 00071 user_button.rise(&user_button_released_fcn); 00072 00073 // start timer 00074 main_task_timer.start(); 00075 00076 // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled 00077 enable_motors = 1; 00078 00079 while (true) { // this loop will run forever 00080 00081 main_task_timer.reset(); 00082 00083 // read analog input 00084 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f; 00085 ir_distance_cm = ir_distance_mV2cm(ir_distance_mV); 00086 00087 // read SensorBar, only use this if it is connected (blocking your code if not) 00088 //if (sensor_bar.isAnyLedActive()) { 00089 // sensor_bar_avgAngleRad = sensor_bar.getAvgAngleRad(); 00090 //} 00091 00092 if (do_execute_main_task) { 00093 00094 speedController_M1.setDesiredSpeedRPS(2.0f); 00095 positionController_M2.setDesiredRotation(3.0f); 00096 00097 } else { 00098 00099 speedController_M1.setDesiredSpeedRPS(0.0f); 00100 positionController_M2.setDesiredRotation(0.0f); 00101 00102 } 00103 00104 // user_led is switching its state every second 00105 if ( (main_task_cntr%(1000 / main_task_period_ms) == 0) && (main_task_cntr!=0) ) { 00106 user_led = !user_led; 00107 } 00108 main_task_cntr++; 00109 00110 // do only output via serial what's really necessary (this makes your code slow) 00111 /* 00112 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", 00113 ir_distance_mV, 00114 ir_distance_cm, 00115 sensor_bar_avgAngleRad, 00116 speedController_M1.getSpeedRPS(), 00117 positionController_M2.getRotation()); 00118 */ 00119 00120 // read timer and make the main thread sleep for the remaining time span (non blocking) 00121 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); 00122 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); 00123 } 00124 } 00125 00126 void user_button_pressed_fcn() 00127 { 00128 user_button_timer.start(); 00129 user_button_timer.reset(); 00130 } 00131 00132 void user_button_released_fcn() 00133 { 00134 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time 00135 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); 00136 user_button_timer.stop(); 00137 if (user_button_elapsed_time_ms > 200) { 00138 do_execute_main_task = !do_execute_main_task; 00139 } 00140 } 00141 00142 float ir_distance_mV2cm(float ir_distance_mV) 00143 { 00144 // defining these variables static makes them persistent within the function 00145 static float a = -4.685f; // (-6.581, -2.79) 00146 static float c = 3.017e+04f; // (2.853e+04, 3.181e+04) 00147 00148 return c/(ir_distance_mV + 1) + a; 00149 }
Generated on Wed Aug 3 2022 07:28:44 by
1.7.2