workshop 1
Dependencies: PM2_Libary Eigen
Fork of PM2_Example_Summer_School by
Diff: main.cpp
- Revision:
- 36:8c75783c1eca
- Parent:
- 31:1b2a1bd1bccb
- Child:
- 37:15c19c21c499
--- a/main.cpp Sat May 14 13:48:08 2022 +0000 +++ b/main.cpp Sat May 14 14:08:20 2022 +0000 @@ -1,5 +1,13 @@ -#include "mbed.h" +#include <mbed.h> +#include <math.h> +#include <vector> + #include "PM2_Libary.h" +#include "Eigen/Dense.h" + +#include "IRSensor.h" + +# define M_PI 3.14159265358979323846 // number pi // logical variable main task 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 @@ -10,69 +18,71 @@ void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below void user_button_released_fcn(); -// while loop gets executed every main_task_period_ms milliseconds -int main_task_period_ms = 50; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second -Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms - -// led on nucleo board -DigitalOut user_led(LED1); // create DigitalOut object to command user led - -// additional Led -DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm) - -// mechanical button -DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate extra mechanical button, you need to specify the mode for proper usage, see below - -// Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor -float ir_distance_mV = 0.0f; // define variable to store measurement -AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 - -// 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB -DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors - -float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors -FastPWM pwm_M1(PB_13); // motor M1 is used open loop -FastPWM pwm_M2(PA_9); // motor M2 is closed-loop speed controlled (angle velocity) -FastPWM pwm_M3(PA_10); // motor M3 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); -EncoderCounter encoder_M3(PA_0, PA_1); - -// create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box -float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack -float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio -float kn = 180.0f / 12.0f; // define motor constant in rpm per V -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 -float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 - -// SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters -SpeedController speedController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 100:1 gear - -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 -// PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters -PositionController positionController_M3(counts_per_turn * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M3, encoder_M3); // parameters adjusted to 100:1 gear, we need a different speed controller gain here - -// Futaba Servo S3001 20mm 3kg Analog -Servo servo_S1(PB_2); // create servo objects -Servo servo_S2(PC_8); -float servo_S1_angle = 0; // servo S1 normalized angle -float servo_S2_angle = 0; // servo S2 normalized angle -int servo_period_mus = 20000; // define servo period time in mus - -int servo_counter = 0; // define servo counter, this is an additional variable to make the servos move -int loops_per_seconds = static_cast<int>(ceilf(1.0f/(0.001f*(float)main_task_period_ms))); // define loops per second - -// Groove Ultrasonic Ranger V2.0 -float us_distance_cm = 0.0f; // define variable to store measurement -RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 17500); // create range finder object (ultra sonic distance sensor), 20 Hz parametrization -// RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 7000); // create range finder object (ultra sonic distance sensor), 50 Hz parametrization - -// LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage) -// LSM9DS1 imu(PC_9, PA_8); // create LSM9DS1 comunication object, if you want to be able to use the imu you need to #include "LSM9DS1_i2c.h" - int main() { + // while loop gets executed every main_task_period_ms milliseconds + 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 + Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms + + // led on nucleo board + DigitalOut user_led(LED1); // create DigitalOut object to command user led + + // additional Led + DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm) + + // mechanical button + DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate extra mechanical button, you need to specify the mode for proper usage, see below + + // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor + float ir_distance_mV = 0.0f; // define variable to store measurement + AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 + + // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB + DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors + + const float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motor M1 + FastPWM pwm_M1(PB_13); // motor M1 is used open loop + FastPWM pwm_M2(PA_9); // motor M2 is closed-loop speed controlled (angle velocity) + FastPWM pwm_M3(PA_10); // motor M3 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); + EncoderCounter encoder_M3(PA_0, PA_1); + + // 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_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters + SpeedController speedController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 100:1 gear + + 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 + // PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters + PositionController positionController_M3(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M3, encoder_M3); // parameters adjusted to 100:1 gear, we need a different speed controller gain here + positionController_M3.setSpeedCntrlGain(kp * k_gear); + positionController_M3.setMaxVelocityRPS(max_speed_rps); + + // Futaba Servo S3001 20mm 3kg Analog + Servo servo_S1(PB_2); // create servo objects + Servo servo_S2(PC_8); + float servo_S1_angle = 0; // servo S1 normalized angle + float servo_S2_angle = 0; // servo S2 normalized angle + const int servo_period_mus = 20000; // define servo period time in mus + + int servo_counter = 0; // define servo counter, this is an additional variable to make the servos move + const int loops_per_seconds = static_cast<int>(ceilf(1.0f/(0.001f*(float)main_task_period_ms))); // define loops per second + + // Groove Ultrasonic Ranger V2.0 + float us_distance_cm = 0.0f; // define variable to store measurement + RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 17500); // create range finder object (ultra sonic distance sensor), 20 Hz parametrization + // RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 7000); // create range finder object (ultra sonic distance sensor), 50 Hz parametrization + + // LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage) + // LSM9DS1 imu(PC_9, PA_8); // create LSM9DS1 comunication object, if you want to be able to use the imu you need to #include "LSM9DS1_i2c.h" + // attach button fall and rise functions to user button object user_button.fall(&user_button_pressed_fcn); user_button.rise(&user_button_released_fcn); @@ -107,11 +117,11 @@ if (mechanical_button.read()) { pwm_M1.write(0.75f); // write output voltage to motor M1 speedController_M2.setDesiredSpeedRPS(0.5f); // set a desired speed for speed controlled dc motors M2 - positionController_M3.setDesiredRotation(1.5f, max_speed_rps); // set a desired rotation for position controlled dc motors M3 + positionController_M3.setDesiredRotation(1.5f); // set a desired rotation for position controlled dc motors M3 } else { pwm_M1.write(0.5f); speedController_M2.setDesiredSpeedRPS(0.0f); - positionController_M3.setDesiredRotation(0.0f, max_speed_rps); + positionController_M3.setDesiredRotation(0.0f); } // check if servos are enabled @@ -140,7 +150,7 @@ pwm_M1.write(0.5f); speedController_M2.setDesiredSpeedRPS(0.0f); - positionController_M3.setDesiredRotation(0.0f, max_speed_rps); + positionController_M3.setDesiredRotation(0.0f); servo_S1_angle = 0; servo_S2_angle = 0;