Prototyp V2

Dependencies:   PM2_Libary

Committer:
raomen
Date:
Mon Apr 18 10:09:39 2022 +0200
Revision:
39:025d1bee1397
Parent:
34:9f779e91168e
Child:
40:e32c57763d92
MERGE

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>
raomen 39:025d1bee1397 4 #include "math.h"
raomen 39:025d1bee1397 5
raomen 39:025d1bee1397 6 //*******************************************************************************************************************************************************************
raomen 39:025d1bee1397 7 //
raomen 39:025d1bee1397 8
raomen 39:025d1bee1397 9 // Defined Variables in mm coming from Hardware-team. Need to be updated
raomen 39:025d1bee1397 10 float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn
raomen 39:025d1bee1397 11 float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint
raomen 39:025d1bee1397 12 float dist_arm_attach_distsensor = 20; // distance between pivot point arm on body to start distancesensor on top in horizontal
raomen 39:025d1bee1397 13 float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E
raomen 39:025d1bee1397 14 float dist_arm_ground = 51; // distance between pivotpoint arm and ground
raomen 39:025d1bee1397 15 float height_stairs = 100; // height to top of stairs in mm
raomen 39:025d1bee1397 16 float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge
raomen 39:025d1bee1397 17
raomen 39:025d1bee1397 18 int gripper_height_mm()
raomen 39:025d1bee1397 19 {
raomen 39:025d1bee1397 20
raomen 39:025d1bee1397 21 return NULL;
raomen 39:025d1bee1397 22 }
raomen 39:025d1bee1397 23
raomen 39:025d1bee1397 24 //***********************************************************************************************************************************************************
lupomic 31:24081337c9ed 25
pmic 24:86f1a63e35a0 26 // logical variable main task
pmic 24:86f1a63e35a0 27 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 28
pmic 24:86f1a63e35a0 29 // user button on nucleo board
pmic 24:86f1a63e35a0 30 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 31 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 32 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 33 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 34
pmic 24:86f1a63e35a0 35 // while loop gets executed every main_task_period_ms milliseconds
raomen 39:025d1bee1397 36 int main_task_period_ms = 30; // define main task period time in ms e.g. 30 ms -> main task runns ~33,33 times per second
pmic 24:86f1a63e35a0 37 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 38
pmic 24:86f1a63e35a0 39 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
raomen 39:025d1bee1397 40 float ir_distance_mV = 0.0f; // define variable to store measurement from infrared distancesensor in mVolt
pmic 24:86f1a63e35a0 41 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 42
lupomic 33:70ea029a69e8 43
pmic 24:86f1a63e35a0 44 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 45 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 17:c19b471f05cb 46
pmic 24:86f1a63e35a0 47 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
lupomic 33:70ea029a69e8 48 //motor pin declaration
lupomic 34:9f779e91168e 49 FastPWM pwm_M_right(PB_13);
lupomic 34:9f779e91168e 50 FastPWM pwm_M_left(PA_9);
lupomic 33:70ea029a69e8 51 FastPWM pwm_M_arm(PA_10);
pmic 17:c19b471f05cb 52
lupomic 33:70ea029a69e8 53 //Encoder pin declaration
lupomic 33:70ea029a69e8 54 EncoderCounter encoder_M_right(PA_6, PC_7); //encoder pin decalaration for wheels right side
lupomic 33:70ea029a69e8 55 EncoderCounter encoder_M_left(PB_6, PB_7); //encoder pin decalaration for wheels left side
lupomic 33:70ea029a69e8 56 EncoderCounter encoder_M_arm(PA_0, PA_1); //encoder pin decalaration for arm
pmic 17:c19b471f05cb 57
pmic 30:1e8295770bc1 58 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
pmic 24:86f1a63e35a0 59 float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
raomen 39:025d1bee1397 60 float counts_per_turn_wheels = 20.0f * 78.125f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
raomen 39:025d1bee1397 61 float counts_per_turn_arm = 20.0f * 78.125f * 10.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm
pmic 25:ea1d6e27c895 62 float kn = 180.0f / 12.0f; // define motor constant in rpm per V
raomen 39:025d1bee1397 63 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 (DC with 100:1 has 2'000 turns for 360°)
pmic 30:1e8295770bc1 64 float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
pmic 6:e1fa1a2d7483 65
lupomic 33:70ea029a69e8 66 //motors for tracks
lupomic 33:70ea029a69e8 67 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 68 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 69 //Arm Motor
lupomic 33:70ea029a69e8 70 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 71
lupomic 33:70ea029a69e8 72 //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 73 // 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 74 //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 75
pmic 17:c19b471f05cb 76
raomen 39:025d1bee1397 77 //Platzhalter Variabeln für die Positionierung sobald wie möglich löschen
lupomic 34:9f779e91168e 78 float PositionStair = 0.2;
lupomic 34:9f779e91168e 79 float PositionBackOff = -0.5;
lupomic 34:9f779e91168e 80 float degArmStart = 0.5;
lupomic 34:9f779e91168e 81 float degArmLift = -0.5;
lupomic 34:9f779e91168e 82 int ToNextFunction = 0;
raomen 39:025d1bee1397 83 float max_speed_rps = 0.5f;
raomen 39:025d1bee1397 84
lupomic 33:70ea029a69e8 85
raomen 39:025d1bee1397 86 int StartPosition(){
raomen 39:025d1bee1397 87 positionController_M_Arm.setDesiredRotation(1.0/360.0*45.0,0.5);
lupomic 33:70ea029a69e8 88 return NULL;
lupomic 33:70ea029a69e8 89 }
lupomic 34:9f779e91168e 90
lupomic 33:70ea029a69e8 91
raomen 39:025d1bee1397 92 //Drives forward into the next step
raomen 39:025d1bee1397 93 // calculatioin of acctual distance with wheels is needed
raomen 39:025d1bee1397 94 int Drive(float dist){
raomen 39:025d1bee1397 95 float distance;
raomen 39:025d1bee1397 96 distance=dist;
raomen 39:025d1bee1397 97 positionController_M_right.setDesiredRotation(distance,max_speed_rps);
raomen 39:025d1bee1397 98 positionController_M_left.setDesiredRotation(distance,max_speed_rps);
raomen 39:025d1bee1397 99 return 0;
lupomic 33:70ea029a69e8 100 }
lupomic 33:70ea029a69e8 101
lupomic 33:70ea029a69e8 102 //only turns the arm until the robot is on the next step
lupomic 33:70ea029a69e8 103 //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 104 int LiftUp(float deg){
lupomic 33:70ea029a69e8 105 int8_t i = 0; //prov condition variable
raomen 39:025d1bee1397 106 positionController_M_Arm.setDesiredRotation(deg);
lupomic 33:70ea029a69e8 107 return 0;
lupomic 33:70ea029a69e8 108 }
raomen 39:025d1bee1397 109
lupomic 33:70ea029a69e8 110 //pow function is here so we dont have to use the math.h library
lupomic 33:70ea029a69e8 111 //it takes 2 arguments the base can be any negative or positive floating point number the power has to be a hos to be an "integer" defined as a double
lupomic 33:70ea029a69e8 112 double powerx(double base, double pow2){
lupomic 33:70ea029a69e8 113 double result = -1;
lupomic 33:70ea029a69e8 114 double power = pow2;
lupomic 33:70ea029a69e8 115 double basis = base;
lupomic 33:70ea029a69e8 116 result = 1;
lupomic 33:70ea029a69e8 117 //handling negative exponents
lupomic 33:70ea029a69e8 118 if(power<0){
lupomic 33:70ea029a69e8 119 for(double i=1; i<=(power*(-1.0)); i++) {
lupomic 33:70ea029a69e8 120 result *= basis;
lupomic 33:70ea029a69e8 121 }
lupomic 33:70ea029a69e8 122 result = 1.0/result;
lupomic 33:70ea029a69e8 123 }
lupomic 33:70ea029a69e8 124 //handling positive exponents
lupomic 33:70ea029a69e8 125 else{
lupomic 33:70ea029a69e8 126 for(double i=1; i<=power; i++){
lupomic 33:70ea029a69e8 127 result *= basis;}}
lupomic 33:70ea029a69e8 128 return result;
lupomic 33:70ea029a69e8 129 }
lupomic 33:70ea029a69e8 130
lupomic 33:70ea029a69e8 131 double mapping(float adc_value_mV){
lupomic 33:70ea029a69e8 132 double distance = 0.0f; //distance in mm
lupomic 33:70ea029a69e8 133 double infY =360 , supY = 2360; //Window for sensor values
lupomic 33:70ea029a69e8 134 double voltage_mV = adc_value_mV;
lupomic 33:70ea029a69e8 135 double p1 = -1.127*powerx(10,-14), p2 = 8.881*powerx(10,-11), p3 = -2.76*powerx(10,-7), p4 = 0.0004262, p5 = -0.3363, p6 = 120.1 ; //faktoren für polynomkurve -> von matlab exportiert
lupomic 33:70ea029a69e8 136 if(voltage_mV > infY && voltage_mV < supY){
lupomic 33:70ea029a69e8 137 distance = p1*powerx(voltage_mV,5) + p2*powerx(voltage_mV,4) + p3*powerx(voltage_mV,3) + p4*powerx(voltage_mV,2) + p5*voltage_mV + p6;
lupomic 33:70ea029a69e8 138 }
lupomic 33:70ea029a69e8 139 return (distance);
lupomic 33:70ea029a69e8 140 }
lupomic 33:70ea029a69e8 141
lupomic 33:70ea029a69e8 142
raomen 39:025d1bee1397 143
raomen 39:025d1bee1397 144
lupomic 33:70ea029a69e8 145 int main(void)
pmic 23:26b3a25fc637 146 {
pmic 24:86f1a63e35a0 147 // attach button fall and rise functions to user button object
lupomic 33:70ea029a69e8 148 user_button.fall(&user_button_pressed_fcn);
lupomic 34:9f779e91168e 149 user_button.rise(&user_button_released_fcn);
lupomic 33:70ea029a69e8 150
pmic 24:86f1a63e35a0 151
pmic 6:e1fa1a2d7483 152
lupomic 33:70ea029a69e8 153 while (true){
lupomic 34:9f779e91168e 154 enable_motors = 1;
lupomic 34:9f779e91168e 155
lupomic 33:70ea029a69e8 156 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
lupomic 33:70ea029a69e8 157
pmic 6:e1fa1a2d7483 158
raomen 39:025d1bee1397 159 switch (ToNextFunction) {
raomen 39:025d1bee1397 160 case 1: StartPosition();
lupomic 34:9f779e91168e 161 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 162 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 163 break;
lupomic 33:70ea029a69e8 164 case 2: Drive(PositionStair);
lupomic 34:9f779e91168e 165 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 34:9f779e91168e 166 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 33:70ea029a69e8 167 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 168 break;
lupomic 34:9f779e91168e 169 case 3: LiftUp(degArmLift);
lupomic 33:70ea029a69e8 170 // ToNextFunction+=1;
lupomic 34:9f779e91168e 171 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 172 break;
lupomic 33:70ea029a69e8 173 case 4: Drive(PositionBackOff);
lupomic 34:9f779e91168e 174 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 34:9f779e91168e 175 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 33:70ea029a69e8 176 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 177 break;
lupomic 33:70ea029a69e8 178 case 5: LiftUp(degArmStart);
lupomic 34:9f779e91168e 179 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 180 // ToNextFunction = 0;
lupomic 33:70ea029a69e8 181 break;
lupomic 34:9f779e91168e 182 default: ;
lupomic 33:70ea029a69e8 183 }
lupomic 33:70ea029a69e8 184 }
lupomic 33:70ea029a69e8 185 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 186 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 187 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
lupomic 33:70ea029a69e8 188 return 0;
pmic 1:93d997d6b232 189 }
pmic 6:e1fa1a2d7483 190
lupomic 33:70ea029a69e8 191
pmic 24:86f1a63e35a0 192 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 193 {
pmic 26:28693b369945 194 user_button_timer.start();
pmic 6:e1fa1a2d7483 195 user_button_timer.reset();
pmic 6:e1fa1a2d7483 196 }
pmic 6:e1fa1a2d7483 197
lupomic 33:70ea029a69e8 198 void user_button_released_fcn() {
pmic 24:86f1a63e35a0 199 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 200 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 201 user_button_timer.stop();
pmic 24:86f1a63e35a0 202 if (user_button_elapsed_time_ms > 200) {
lupomic 33:70ea029a69e8 203 ToNextFunction += 1;}
lupomic 33:70ea029a69e8 204 }