first commit

Dependencies:   PM2_Libary

Committer:
lupomic
Date:
Wed Apr 13 09:40:06 2022 +0200
Branch:
lupo
Revision:
36:a48b21a9635c
Parent:
35:96ed18b1af94
Child:
37:05252c4a2d4e
New branch lupo

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"
lupomic 33:70ea029a69e8 3 #include <cstdint>
lupomic 36:a48b21a9635c 4 #include "sensor.cpp"
lupomic 31:24081337c9ed 5
lupomic 35:96ed18b1af94 6
lupomic 35:96ed18b1af94 7
pmic 24:86f1a63e35a0 8 // logical variable main task
pmic 24:86f1a63e35a0 9 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 10
pmic 24:86f1a63e35a0 11 // user button on nucleo board
pmic 24:86f1a63e35a0 12 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 13 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 14 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 15 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 16
pmic 24:86f1a63e35a0 17 // while loop gets executed every main_task_period_ms milliseconds
lupomic 34:9f779e91168e 18 int main_task_period_ms = 30; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second
pmic 24:86f1a63e35a0 19 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 20
pmic 24:86f1a63e35a0 21 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
pmic 24:86f1a63e35a0 22 float ir_distance_mV = 0.0f; // define variable to store measurement
pmic 24:86f1a63e35a0 23 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 24
lupomic 33:70ea029a69e8 25
lupomic 33:70ea029a69e8 26
pmic 24:86f1a63e35a0 27 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 28 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 17:c19b471f05cb 29
pmic 24:86f1a63e35a0 30 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
lupomic 33:70ea029a69e8 31 //motor pin declaration
lupomic 34:9f779e91168e 32 FastPWM pwm_M_right(PB_13);
lupomic 34:9f779e91168e 33 FastPWM pwm_M_left(PA_9);
lupomic 33:70ea029a69e8 34 FastPWM pwm_M_arm(PA_10);
pmic 17:c19b471f05cb 35
lupomic 33:70ea029a69e8 36 //Encoder pin declaration
lupomic 33:70ea029a69e8 37 EncoderCounter encoder_M_right(PA_6, PC_7); //encoder pin decalaration for wheels right side
lupomic 33:70ea029a69e8 38 EncoderCounter encoder_M_left(PB_6, PB_7); //encoder pin decalaration for wheels left side
lupomic 33:70ea029a69e8 39 EncoderCounter encoder_M_arm(PA_0, PA_1); //encoder pin decalaration for arm
pmic 17:c19b471f05cb 40
pmic 30:1e8295770bc1 41 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
pmic 24:86f1a63e35a0 42 float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
lupomic 33:70ea029a69e8 43 float counts_per_turn_wheels = 2000.0f * 100.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
lupomic 34:9f779e91168e 44 float counts_per_turn_arm = 2000.0f * 100.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm
pmic 25:ea1d6e27c895 45 float kn = 180.0f / 12.0f; // define motor constant in rpm per V
pmic 30:1e8295770bc1 46 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
pmic 30:1e8295770bc1 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
lupomic 33:70ea029a69e8 49 //motors for tracks
lupomic 33:70ea029a69e8 50 PositionController positionController_M_right(counts_per_turn_wheels * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M_right, encoder_M_right); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
lupomic 33:70ea029a69e8 51 PositionController positionController_M_left(counts_per_turn_wheels * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M_left, encoder_M_left); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
lupomic 33:70ea029a69e8 52 //Arm Motor
lupomic 33:70ea029a69e8 53 PositionController positionController_M_Arm(counts_per_turn_arm * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M_arm, encoder_M_arm); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
pmic 17:c19b471f05cb 54
lupomic 33:70ea029a69e8 55 //float max_speed_rps = 0.5f; not sure if needed // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage
lupomic 33:70ea029a69e8 56 // PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters
lupomic 33:70ea029a69e8 57 //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
pmic 17:c19b471f05cb 58
pmic 17:c19b471f05cb 59
pmic 24:86f1a63e35a0 60 // LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage)
pmic 25:ea1d6e27c895 61 // 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 62
lupomic 33:70ea029a69e8 63 //Platzhalter Variabeln für die Positionierung
lupomic 34:9f779e91168e 64 float PositionStair = 0.2;
lupomic 34:9f779e91168e 65 float PositionBackOff = -0.5;
lupomic 34:9f779e91168e 66 float degArmStart = 0.5;
lupomic 34:9f779e91168e 67 float degArmLift = -0.5;
lupomic 34:9f779e91168e 68 int ToNextFunction = 0;
lupomic 34:9f779e91168e 69 float max_speed_rps = 0.5f;
lupomic 33:70ea029a69e8 70
lupomic 36:a48b21a9635c 71
lupomic 34:9f779e91168e 72 int StartPosition(float deg){
lupomic 33:70ea029a69e8 73
lupomic 33:70ea029a69e8 74 positionController_M_Arm.setDesiredRotation(deg);
lupomic 33:70ea029a69e8 75
lupomic 33:70ea029a69e8 76 return NULL;
lupomic 33:70ea029a69e8 77 }
lupomic 33:70ea029a69e8 78 //Drives forward into the next step
lupomic 34:9f779e91168e 79 int Drive(float dist){
lupomic 34:9f779e91168e 80
lupomic 34:9f779e91168e 81 float distance;
lupomic 33:70ea029a69e8 82
lupomic 34:9f779e91168e 83 distance=dist;
lupomic 34:9f779e91168e 84
lupomic 33:70ea029a69e8 85
lupomic 34:9f779e91168e 86 positionController_M_right.setDesiredRotation(distance,max_speed_rps);
lupomic 34:9f779e91168e 87 positionController_M_left.setDesiredRotation(distance,max_speed_rps);
lupomic 34:9f779e91168e 88
lupomic 33:70ea029a69e8 89
lupomic 33:70ea029a69e8 90 return 0;
lupomic 33:70ea029a69e8 91 }
lupomic 33:70ea029a69e8 92
lupomic 33:70ea029a69e8 93 //only turns the arm until the robot is on the next step
lupomic 33:70ea029a69e8 94 //not yet clear if the motor controler function drives to a absolute poition or if it drives the given distance relative to the current position
lupomic 34:9f779e91168e 95 int LiftUp(float deg){
lupomic 34:9f779e91168e 96
lupomic 33:70ea029a69e8 97 int8_t i = 0; //prov condition variable
lupomic 33:70ea029a69e8 98
lupomic 33:70ea029a69e8 99 positionController_M_Arm.setDesiredRotation(deg);
lupomic 33:70ea029a69e8 100
lupomic 33:70ea029a69e8 101
lupomic 33:70ea029a69e8 102 return 0;
lupomic 33:70ea029a69e8 103 }
lupomic 33:70ea029a69e8 104
lupomic 33:70ea029a69e8 105
lupomic 33:70ea029a69e8 106 int main(void)
pmic 23:26b3a25fc637 107 {
pmic 24:86f1a63e35a0 108 // attach button fall and rise functions to user button object
lupomic 33:70ea029a69e8 109 user_button.fall(&user_button_pressed_fcn);
lupomic 34:9f779e91168e 110 user_button.rise(&user_button_released_fcn);
lupomic 33:70ea029a69e8 111
pmic 24:86f1a63e35a0 112
pmic 6:e1fa1a2d7483 113
lupomic 33:70ea029a69e8 114 while (true){
lupomic 34:9f779e91168e 115 enable_motors = 1;
lupomic 34:9f779e91168e 116
lupomic 33:70ea029a69e8 117 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
lupomic 33:70ea029a69e8 118
lupomic 34:9f779e91168e 119 // printf("test pow function 2 ^ 2 %lf\n",powerx(2,2));
lupomic 34:9f779e91168e 120 //printf("test mapping function %f\n", mapping(ir_distance_mV));
pmic 6:e1fa1a2d7483 121
lupomic 34:9f779e91168e 122 //printf("IR sensor (mV): %3.3f\n", ir_distance_mV);
lupomic 33:70ea029a69e8 123
pmic 6:e1fa1a2d7483 124
lupomic 33:70ea029a69e8 125 switch (ToNextFunction) {
lupomic 33:70ea029a69e8 126 case 1: StartPosition(degArmStart);
lupomic 34:9f779e91168e 127 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 128 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 129 break;
lupomic 33:70ea029a69e8 130 case 2: Drive(PositionStair);
lupomic 34:9f779e91168e 131 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 34:9f779e91168e 132 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 33:70ea029a69e8 133 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 134 break;
lupomic 34:9f779e91168e 135 case 3: LiftUp(degArmLift);
lupomic 33:70ea029a69e8 136 // ToNextFunction+=1;
lupomic 34:9f779e91168e 137 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 138 break;
lupomic 33:70ea029a69e8 139 case 4: Drive(PositionBackOff);
lupomic 34:9f779e91168e 140 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 34:9f779e91168e 141 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 33:70ea029a69e8 142 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 143 break;
lupomic 33:70ea029a69e8 144 case 5: LiftUp(degArmStart);
lupomic 34:9f779e91168e 145 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 146 // ToNextFunction = 0;
lupomic 33:70ea029a69e8 147 break;
lupomic 34:9f779e91168e 148 default: ;
lupomic 33:70ea029a69e8 149 }
pmic 6:e1fa1a2d7483 150
lupomic 33:70ea029a69e8 151
lupomic 33:70ea029a69e8 152
lupomic 33:70ea029a69e8 153 }
lupomic 33:70ea029a69e8 154 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 155 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 156 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
lupomic 33:70ea029a69e8 157 return 0;
pmic 1:93d997d6b232 158 }
pmic 6:e1fa1a2d7483 159
lupomic 33:70ea029a69e8 160
pmic 24:86f1a63e35a0 161 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 162 {
pmic 26:28693b369945 163 user_button_timer.start();
pmic 6:e1fa1a2d7483 164 user_button_timer.reset();
pmic 6:e1fa1a2d7483 165 }
pmic 6:e1fa1a2d7483 166
lupomic 33:70ea029a69e8 167 void user_button_released_fcn() {
pmic 24:86f1a63e35a0 168 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 169 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 170 user_button_timer.stop();
pmic 24:86f1a63e35a0 171 if (user_button_elapsed_time_ms > 200) {
lupomic 33:70ea029a69e8 172 ToNextFunction += 1;}
lupomic 33:70ea029a69e8 173 }