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
Revision 45:d47b1b112a04, committed 2022-05-20
- Comitter:
- pmic
- Date:
- Fri May 20 07:20:10 2022 +0000
- Parent:
- 44:a65bc3e11481
- Commit message:
- Restored final solution
Changed in this revision
| PM2_Libary.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r a65bc3e11481 -r d47b1b112a04 PM2_Libary.lib --- a/PM2_Libary.lib Wed May 18 07:53:31 2022 +0200 +++ b/PM2_Libary.lib Fri May 20 07:20:10 2022 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/pmic/code/PM2_Libary/#05abc1d2a2b9 +https://os.mbed.com/users/pmic/code/PM2_Libary/#f62a64ffb273
diff -r a65bc3e11481 -r d47b1b112a04 main.cpp
--- a/main.cpp Wed May 18 07:53:31 2022 +0200
+++ b/main.cpp Fri May 20 07:20:10 2022 +0000
@@ -14,6 +14,8 @@
void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
void user_button_released_fcn();
+float ir_distance_mV2cm(float ir_distance_mV);
+
int main()
{
// while loop gets executed every main_task_period_ms milliseconds
@@ -26,6 +28,44 @@
// led on nucleo board
DigitalOut user_led(LED1); // create DigitalOut object to command user led
+ // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
+ float ir_distance_mV = 0.0f; // define variable to store measurement
+ float ir_distance_cm = 0.0f; // compensated sensor value in cm
+ AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
+
+ // create SensorBar object for sparkfun line follower array, only use this if it is connected (blocking your code if not)
+ float sensor_bar_avgAngleRad = 0.0f;
+ I2C i2c(PB_9, PB_8);
+ //SensorBar sensor_bar(i2c, 0.1175f); // second input argument is distance from bar to wheel axis
+
+ // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
+ DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
+
+ FastPWM pwm_M1(PB_13); // motor M1 is closed-loop speed controlled (angle velocity)
+ FastPWM pwm_M2(PA_9); // motor M2 is closed-loop position controlled (angle controlled)
+
+ EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values
+ EncoderCounter encoder_M2(PB_6, PB_7);
+
+ // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
+ const float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
+ const float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio
+ const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
+ //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
+ //const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
+
+ SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1); // default 78.125:1 gear box with default contoller parameters
+ //SpeedController speedController_M1(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M1, encoder_M1); // parameters adjusted to 100:1 gear
+ speedController_M1.setMaxAccelerationRPS(999.0f); // disable internal trajectory planer
+
+ PositionController positionController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear with default contoller parameters
+ //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
+ //positionController_M2.setSpeedCntrlGain(kp * k_gear);
+ positionController_M2.setMaxAccelerationRPS(999.0f); // disable internal trajectory planer
+ // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage
+ float max_speed_rps = 2.0f;
+ positionController_M2.setMaxVelocityRPS(max_speed_rps);
+
// attach button fall and rise functions to user button object
user_button.fall(&user_button_pressed_fcn);
user_button.rise(&user_button_released_fcn);
@@ -33,14 +73,31 @@
// start timer
main_task_timer.start();
+ // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled
+ enable_motors = 1;
+
while (true) { // this loop will run forever
main_task_timer.reset();
-
+ // read analog input
+ ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
+ ir_distance_cm = ir_distance_mV2cm(ir_distance_mV);
+
+ // read SensorBar, only use this if it is connected (blocking your code if not)
+ //if (sensor_bar.isAnyLedActive()) {
+ // sensor_bar_avgAngleRad = sensor_bar.getAvgAngleRad();
+ //}
+
if (do_execute_main_task) {
+ speedController_M1.setDesiredSpeedRPS(2.0f);
+ positionController_M2.setDesiredRotation(3.0f);
+
} else {
+
+ speedController_M1.setDesiredSpeedRPS(0.0f);
+ positionController_M2.setDesiredRotation(0.0f);
}
@@ -80,4 +137,13 @@
if (user_button_elapsed_time_ms > 200) {
do_execute_main_task = !do_execute_main_task;
}
+}
+
+float ir_distance_mV2cm(float ir_distance_mV)
+{
+ // defining these variables static makes them persistent within the function
+ static float a = -4.685f; // (-6.581, -2.79)
+ static float c = 3.017e+04f; // (2.853e+04, 3.181e+04)
+
+ return c/(ir_distance_mV + 1) + a;
}
\ No newline at end of file
