Repository for verBOT robot project, hopefully featuring two branches: Dev/Test and Prod.

Dependencies:   PM2_Libary Eigen

Committer:
pmic
Date:
Wed Mar 16 11:47:54 2022 +0100
Revision:
28:3760373c234d
Parent:
27:0772a14369ce
Child:
29:d6f1ccf42a31
removed test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 1:93d997d6b232 1 #include "mbed.h"
pmic 17:c19b471f05cb 2 #include "PM2_Libary.h"
pmic 6:e1fa1a2d7483 3
pmic 24:86f1a63e35a0 4 // logical variable main task
pmic 24:86f1a63e35a0 5 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 6
pmic 24:86f1a63e35a0 7 // user button on nucleo board
pmic 24:86f1a63e35a0 8 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 9 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 10 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 11 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 12
pmic 24:86f1a63e35a0 13 // while loop gets executed every main_task_period_ms milliseconds
pmic 24:86f1a63e35a0 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
pmic 24:86f1a63e35a0 15 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
pmic 6:e1fa1a2d7483 16
pmic 24:86f1a63e35a0 17 // led on nucleo board
pmic 24:86f1a63e35a0 18 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 17:c19b471f05cb 19
pmic 24:86f1a63e35a0 20 // additional Led
pmic 24:86f1a63e35a0 21 DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm)
pmic 17:c19b471f05cb 22
pmic 24:86f1a63e35a0 23 // mechanical button
pmic 24:86f1a63e35a0 24 DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate extra mechanical button, you need to specify the mode for proper usage, see below
pmic 24:86f1a63e35a0 25
pmic 24:86f1a63e35a0 26 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
pmic 24:86f1a63e35a0 27 float ir_distance_mV = 0.0f; // define variable to store measurement
pmic 24:86f1a63e35a0 28 AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
pmic 6:e1fa1a2d7483 29
pmic 24:86f1a63e35a0 30 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 31 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 17:c19b471f05cb 32
pmic 24:86f1a63e35a0 33 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
pmic 24:86f1a63e35a0 34 FastPWM pwm_M1(PB_13); // motor M1 is used open loop
pmic 24:86f1a63e35a0 35 FastPWM pwm_M2(PA_9); // motor M2 is speed controlled
pmic 24:86f1a63e35a0 36 FastPWM pwm_M3(PA_10); // motor M3 is position controlled (angle controlled)
pmic 17:c19b471f05cb 37
pmic 24:86f1a63e35a0 38 EncoderCounter encoder_M1(PA_6, PC_7); // create encoder read objects
pmic 17:c19b471f05cb 39 EncoderCounter encoder_M2(PB_6, PB_7);
pmic 17:c19b471f05cb 40 EncoderCounter encoder_M3(PA_0, PA_1);
pmic 17:c19b471f05cb 41
pmic 24:86f1a63e35a0 42 // create SpeedController and PositionController controller objects, default parametrization is for 78.125:1 gear box
pmic 24:86f1a63e35a0 43 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 44 float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio
pmic 25:ea1d6e27c895 45 float kn = 180.0f / 12.0f; // define motor constant in rpm per V
pmic 24:86f1a63e35a0 46 // float k_gear = 25.0f / 78.125f; // define additional ratio in case you are using a dc motor with a different gear box, e.g. 25:1
pmic 24:86f1a63e35a0 47 // float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
pmic 6:e1fa1a2d7483 48
pmic 24:86f1a63e35a0 49 SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters
pmic 17:c19b471f05cb 50 // SpeedController speedController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 25:1 gear
pmic 17:c19b471f05cb 51
pmic 24:86f1a63e35a0 52 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 17:c19b471f05cb 53 PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters
pmic 17:c19b471f05cb 54 // PositionController positionController_M3(counts_per_turn * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M3, encoder_M3); // parameters adjusted to 25:1 gear, we need a different speed controller gain here
pmic 17:c19b471f05cb 55
pmic 17:c19b471f05cb 56 // Futaba Servo S3001 20mm 3kg Analog
pmic 24:86f1a63e35a0 57 Servo servo_S1(PB_2); // create servo objects
pmic 10:c5d85e35758c 58 Servo servo_S2(PC_8);
pmic 24:86f1a63e35a0 59 int servo_pos_S1_mus = 0; // servo S1 position, the desired position gets commanded as a time
pmic 24:86f1a63e35a0 60 int servo_pos_S2_mus = 0; // servo S2 position
pmic 24:86f1a63e35a0 61 int servo_period_mus = 20000; // define servo period time in mus
pmic 17:c19b471f05cb 62
pmic 24:86f1a63e35a0 63 int servo_counter = 0; // define servo counter, this is an additional variable to make the servos move
pmic 24:86f1a63e35a0 64 int loops_per_second = static_cast<int>(ceilf(1.0f/(0.001f*(float)main_task_period_ms))); // define loops per second
pmic 1:93d997d6b232 65
pmic 11:af0f165f8761 66 // Groove Ultrasonic Ranger V2.0
pmic 24:86f1a63e35a0 67 float us_distance_cm = 0.0f; // define variable to store measurement
pmic 25:ea1d6e27c895 68 RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 17500);
pmic 24:86f1a63e35a0 69 // RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 7000); // create range finder object (ultra sonic distance sensor), 1/main_task_period_ms = 20 Hz parametrization
pmic 17:c19b471f05cb 70
pmic 24:86f1a63e35a0 71 // LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage)
pmic 25:ea1d6e27c895 72 // 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"
pmic 20:7e7325edcf5c 73
pmic 1:93d997d6b232 74 int main()
pmic 23:26b3a25fc637 75 {
pmic 24:86f1a63e35a0 76 // attach button fall and rise functions to user button object
pmic 24:86f1a63e35a0 77 user_button.fall(&user_button_pressed_fcn);
pmic 24:86f1a63e35a0 78 user_button.rise(&user_button_released_fcn);
pmic 17:c19b471f05cb 79
pmic 24:86f1a63e35a0 80 // start timers
pmic 24:86f1a63e35a0 81 main_task_timer.start();
pmic 6:e1fa1a2d7483 82
pmic 24:86f1a63e35a0 83 // set pullup mode: add resistor between pin and 3.3 V, so that there is a defined potential
pmic 24:86f1a63e35a0 84 mechanical_button.mode(PullUp);
pmic 24:86f1a63e35a0 85
pmic 24:86f1a63e35a0 86 // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled
pmic 10:c5d85e35758c 87 enable_motors = 1;
pmic 17:c19b471f05cb 88
pmic 24:86f1a63e35a0 89 // motor M1 is used open-loop, we need to initialize the pwm and set pwm output to zero at the beginning, range: 0...1 -> u_min...u_max: 0.5 -> 0 V
pmic 25:ea1d6e27c895 90 pwm_M1.period(pwm_period_s);
pmic 17:c19b471f05cb 91 pwm_M1.write(0.5);
pmic 9:f10b974d01e0 92
pmic 24:86f1a63e35a0 93 // enable servos, you can also disable them at any point in your program if you don't want your servos to become warm
pmic 24:86f1a63e35a0 94 servo_S1.Enable(servo_pos_S1_mus, servo_period_mus);
pmic 24:86f1a63e35a0 95 servo_S2.Enable(servo_pos_S2_mus, servo_period_mus);
pmic 6:e1fa1a2d7483 96
pmic 24:86f1a63e35a0 97 while (true) { // this loop will run forever
pmic 6:e1fa1a2d7483 98
pmic 24:86f1a63e35a0 99 main_task_timer.reset();
pmic 6:e1fa1a2d7483 100
pmic 24:86f1a63e35a0 101 if (do_execute_main_task) {
pmic 17:c19b471f05cb 102
pmic 24:86f1a63e35a0 103 // read analog input
pmic 24:86f1a63e35a0 104 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
pmic 6:e1fa1a2d7483 105
pmic 24:86f1a63e35a0 106 // command dc motors if mechanical button is pressed
pmic 24:86f1a63e35a0 107 if (mechanical_button) {
pmic 24:86f1a63e35a0 108 pwm_M1.write(0.75); // write output voltage to motor M1
pmic 24:86f1a63e35a0 109 speedController_M2.setDesiredSpeedRPS(0.5f); // set a desired speed for speed controlled dc motors M2
pmic 24:86f1a63e35a0 110 positionController_M3.setDesiredRotation(1.5f, max_speed_rps); // set a desired rotation for position controlled dc motors M3
pmic 24:86f1a63e35a0 111 } else {
pmic 24:86f1a63e35a0 112 pwm_M1.write(0.5);
pmic 24:86f1a63e35a0 113 speedController_M2.setDesiredSpeedRPS(0.0f);
pmic 24:86f1a63e35a0 114 positionController_M3.setDesiredRotation(0.0f, max_speed_rps);
pmic 24:86f1a63e35a0 115 }
pmic 6:e1fa1a2d7483 116
pmic 24:86f1a63e35a0 117 // command servo position, this needs to be calibrated
pmic 24:86f1a63e35a0 118 servo_S1.SetPosition(servo_pos_S1_mus);
pmic 24:86f1a63e35a0 119 servo_S2.SetPosition(servo_pos_S2_mus);
pmic 24:86f1a63e35a0 120 if (servo_pos_S1_mus <= servo_period_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) {
pmic 24:86f1a63e35a0 121 servo_pos_S1_mus += 100;
pmic 8:9bb806a7f585 122 }
pmic 24:86f1a63e35a0 123 if (servo_pos_S2_mus <= servo_period_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) {
pmic 24:86f1a63e35a0 124 servo_pos_S2_mus += 100;
pmic 8:9bb806a7f585 125 }
pmic 10:c5d85e35758c 126 servo_counter++;
pmic 6:e1fa1a2d7483 127
pmic 24:86f1a63e35a0 128 // read ultra sonic distance sensor
pmic 24:86f1a63e35a0 129 us_distance_cm = us_range_finder.read_cm();
pmic 11:af0f165f8761 130
pmic 24:86f1a63e35a0 131 // visual feedback that the main task is executed
pmic 24:86f1a63e35a0 132 extra_led = 1;
pmic 9:f10b974d01e0 133
pmic 1:93d997d6b232 134 } else {
pmic 6:e1fa1a2d7483 135
pmic 24:86f1a63e35a0 136 ir_distance_mV = 0.0f;
pmic 1:93d997d6b232 137
pmic 17:c19b471f05cb 138 pwm_M1.write(0.5);
pmic 17:c19b471f05cb 139 speedController_M2.setDesiredSpeedRPS(0.0f);
pmic 17:c19b471f05cb 140 positionController_M3.setDesiredRotation(0.0f, max_speed_rps);
pmic 6:e1fa1a2d7483 141
pmic 24:86f1a63e35a0 142 servo_pos_S1_mus = 0;
pmic 24:86f1a63e35a0 143 servo_pos_S2_mus = 0;
pmic 24:86f1a63e35a0 144 servo_S1.SetPosition(servo_pos_S1_mus);
pmic 24:86f1a63e35a0 145 servo_S2.SetPosition(servo_pos_S2_mus);
pmic 17:c19b471f05cb 146
pmic 24:86f1a63e35a0 147 us_distance_cm = 0.0f;
pmic 6:e1fa1a2d7483 148
pmic 24:86f1a63e35a0 149 extra_led = 0;
pmic 1:93d997d6b232 150 }
pmic 6:e1fa1a2d7483 151
pmic 24:86f1a63e35a0 152 user_led = !user_led;
pmic 24:86f1a63e35a0 153
pmic 24:86f1a63e35a0 154 // do only output via serial what's really necessary (this makes your code slow)
pmic 24:86f1a63e35a0 155 printf("IR sensor (mV): %3.3f, Encoder M1: %3d, Speed M2 (rps) %3.3f, Position M3 (rot): %3.3f, Servo S1 position (ms): %3d, Servo S2 position (ms): %3d, US sensor (cm): %3.3f\r\n",
pmic 24:86f1a63e35a0 156 ir_distance_mV,
pmic 17:c19b471f05cb 157 encoder_M1.read(),
pmic 17:c19b471f05cb 158 speedController_M2.getSpeedRPS(),
pmic 17:c19b471f05cb 159 positionController_M3.getRotation(),
pmic 24:86f1a63e35a0 160 servo_pos_S1_mus,
pmic 24:86f1a63e35a0 161 servo_pos_S2_mus,
pmic 24:86f1a63e35a0 162 us_distance_cm);
pmic 6:e1fa1a2d7483 163
pmic 24:86f1a63e35a0 164 // read out the imu, the actual frames of the sensor reading needs to be figured out
pmic 24:86f1a63e35a0 165 // imu.updateGyro();
pmic 24:86f1a63e35a0 166 // imu.updateAcc();
pmic 24:86f1a63e35a0 167 // imu.updateMag();
pmic 24:86f1a63e35a0 168 // printf("%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\r\n", imu.readGyroX(), imu.readGyroY(), imu.readGyroZ(),
pmic 24:86f1a63e35a0 169 // imu.readAccX(), imu.readAccY(), imu.readAccZ(), imu.readMagX(), imu.readMagY(), imu.readMagZ());
pmic 17:c19b471f05cb 170
pmic 24:86f1a63e35a0 171 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 172 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 173 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
pmic 1:93d997d6b232 174 }
pmic 1:93d997d6b232 175 }
pmic 6:e1fa1a2d7483 176
pmic 24:86f1a63e35a0 177 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 178 {
pmic 26:28693b369945 179 user_button_timer.start();
pmic 6:e1fa1a2d7483 180 user_button_timer.reset();
pmic 6:e1fa1a2d7483 181 }
pmic 6:e1fa1a2d7483 182
pmic 24:86f1a63e35a0 183 void user_button_released_fcn()
pmic 6:e1fa1a2d7483 184 {
pmic 24:86f1a63e35a0 185 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 186 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 187 user_button_timer.stop();
pmic 24:86f1a63e35a0 188 if (user_button_elapsed_time_ms > 200) {
pmic 24:86f1a63e35a0 189 do_execute_main_task = !do_execute_main_task;
pmic 8:9bb806a7f585 190 }
pmic 6:e1fa1a2d7483 191 }