robot code for summer school
Dependencies: PM2_Libary Eigen
Fork of PM2_Example_Summer_School by
Robot_Library/robot.cpp@49:7da71f479dac, 2022-05-25 (annotated)
- Committer:
- seas726
- Date:
- Wed May 25 11:36:19 2022 +0200
- Revision:
- 49:7da71f479dac
- Child:
- 53:bfe5eaefa695
- Child:
- 54:b442660523df
- Child:
- 56:3fce0a9bb6df
speed control new files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
seas726 | 49:7da71f479dac | 1 | #include "robot.h" |
seas726 | 49:7da71f479dac | 2 | #include "EncoderCounter.h" |
seas726 | 49:7da71f479dac | 3 | #include "PeripheralNames.h" |
seas726 | 49:7da71f479dac | 4 | #include "PinNames.h" |
seas726 | 49:7da71f479dac | 5 | #include <cstdio> |
seas726 | 49:7da71f479dac | 6 | |
seas726 | 49:7da71f479dac | 7 | Robot::Robot() : dist(PB_1), |
seas726 | 49:7da71f479dac | 8 | bit0(PH_1), |
seas726 | 49:7da71f479dac | 9 | bit1(PC_2), |
seas726 | 49:7da71f479dac | 10 | bit2(PC_3), |
seas726 | 49:7da71f479dac | 11 | ir_sensor_0(dist, bit0, bit1, bit2, 0), // one IR sendor |
seas726 | 49:7da71f479dac | 12 | i2c2(PB_9, PB_8), |
seas726 | 49:7da71f479dac | 13 | line_sensor(i2c2), |
seas726 | 49:7da71f479dac | 14 | pwm_M1(PA_9), |
seas726 | 49:7da71f479dac | 15 | pwm_M2(PA_8), |
seas726 | 49:7da71f479dac | 16 | encoder_M1(PA_6, PC_7), |
seas726 | 49:7da71f479dac | 17 | encoder_M2(PB_6,PB_7) |
seas726 | 49:7da71f479dac | 18 | { |
seas726 | 49:7da71f479dac | 19 | // initialize all variables |
seas726 | 49:7da71f479dac | 20 | wheel_to_robot << -WHEEL_RADIUS / 2.0f, WHEEL_RADIUS / 2.0f, |
seas726 | 49:7da71f479dac | 21 | -WHEEL_RADIUS / DISTANCE_BETWEEN_WHEELS, -WHEEL_RADIUS / DISTANCE_BETWEEN_WHEELS; // transformation matri |
seas726 | 49:7da71f479dac | 22 | robot_to_wheel = wheel_to_robot.inverse(); |
seas726 | 49:7da71f479dac | 23 | |
seas726 | 49:7da71f479dac | 24 | robot_speed_desired.setZero(); // zero out all speed |
seas726 | 49:7da71f479dac | 25 | wheel_speed_desired.setZero(); |
seas726 | 49:7da71f479dac | 26 | wheel_speed_smooth.setZero(); |
seas726 | 49:7da71f479dac | 27 | robot_speed_actual.setZero(); |
seas726 | 49:7da71f479dac | 28 | wheel_speed_actual.setZero(); |
seas726 | 49:7da71f479dac | 29 | |
seas726 | 49:7da71f479dac | 30 | // MOTORS + MOTION |
seas726 | 49:7da71f479dac | 31 | |
seas726 | 49:7da71f479dac | 32 | // TRAJECTORY PLANNERS |
seas726 | 49:7da71f479dac | 33 | trajectoryPlanners[0] = new Motion(); |
seas726 | 49:7da71f479dac | 34 | trajectoryPlanners[1] = new Motion(); |
seas726 | 49:7da71f479dac | 35 | |
seas726 | 49:7da71f479dac | 36 | trajectoryPlanners[0]->setProfileVelocity(MAX_MOTOR_VOLTAGE * KN / 60.0f); |
seas726 | 49:7da71f479dac | 37 | trajectoryPlanners[1]->setProfileVelocity(MAX_MOTOR_VOLTAGE * KN / 60.0f); |
seas726 | 49:7da71f479dac | 38 | trajectoryPlanners[0]->setProfileAcceleration(10.0f); |
seas726 | 49:7da71f479dac | 39 | trajectoryPlanners[1]->setProfileAcceleration(10.0f); |
seas726 | 49:7da71f479dac | 40 | trajectoryPlanners[0]->setProfileDeceleration(10.0f); |
seas726 | 49:7da71f479dac | 41 | trajectoryPlanners[1]->setProfileDeceleration(10.0f); |
seas726 | 49:7da71f479dac | 42 | |
seas726 | 49:7da71f479dac | 43 | // SPEED CONTROLLERS |
seas726 | 49:7da71f479dac | 44 | speedControllers[0] = new SpeedController(COUNTS_PER_TURN, KN, MAX_MOTOR_VOLTAGE, pwm_M1, encoder_M1); |
seas726 | 49:7da71f479dac | 45 | speedControllers[1] = new SpeedController(COUNTS_PER_TURN, KN, MAX_MOTOR_VOLTAGE, pwm_M2, encoder_M2); |
seas726 | 49:7da71f479dac | 46 | |
seas726 | 49:7da71f479dac | 47 | speedControllers[0]->setSpeedCntrlGain(0.04f); // adjust speedcontroller gains |
seas726 | 49:7da71f479dac | 48 | speedControllers[1]->setSpeedCntrlGain(0.04f); |
seas726 | 49:7da71f479dac | 49 | speedControllers[0]->setMaxAccelerationRPS(999.0f); // adjust max. acceleration for smooth movement |
seas726 | 49:7da71f479dac | 50 | speedControllers[1]->setMaxAccelerationRPS(999.0f); |
seas726 | 49:7da71f479dac | 51 | |
seas726 | 49:7da71f479dac | 52 | } |
seas726 | 49:7da71f479dac | 53 | |
seas726 | 49:7da71f479dac | 54 | void Robot::Update() { |
seas726 | 49:7da71f479dac | 55 | |
seas726 | 49:7da71f479dac | 56 | controller.Update(); |
seas726 | 49:7da71f479dac | 57 | |
seas726 | 49:7da71f479dac | 58 | printf("STATE: %d \r\n", state); |
seas726 | 49:7da71f479dac | 59 | switch (state) { |
seas726 | 49:7da71f479dac | 60 | case INITIAL: |
seas726 | 49:7da71f479dac | 61 | Initial(); |
seas726 | 49:7da71f479dac | 62 | break; |
seas726 | 49:7da71f479dac | 63 | case IDLE: |
seas726 | 49:7da71f479dac | 64 | Idle(); |
seas726 | 49:7da71f479dac | 65 | break; |
seas726 | 49:7da71f479dac | 66 | case FOLLOWING_LINE: |
seas726 | 49:7da71f479dac | 67 | FollowingLine(); |
seas726 | 49:7da71f479dac | 68 | break; |
seas726 | 49:7da71f479dac | 69 | case RIGHT_TURN_90: |
seas726 | 49:7da71f479dac | 70 | RightTurn(); |
seas726 | 49:7da71f479dac | 71 | break; |
seas726 | 49:7da71f479dac | 72 | case LEFT_TURN_90: |
seas726 | 49:7da71f479dac | 73 | LeftTurn(); |
seas726 | 49:7da71f479dac | 74 | break; |
seas726 | 49:7da71f479dac | 75 | default: state = IDLE; // on default, stop the car |
seas726 | 49:7da71f479dac | 76 | } |
seas726 | 49:7da71f479dac | 77 | } |
seas726 | 49:7da71f479dac | 78 | |
seas726 | 49:7da71f479dac | 79 | void Robot::Initial() |
seas726 | 49:7da71f479dac | 80 | { |
seas726 | 49:7da71f479dac | 81 | printf("Initial State\n"); // TODO: REMOVE PRINT |
seas726 | 49:7da71f479dac | 82 | // initialize the robot. |
seas726 | 49:7da71f479dac | 83 | // enable_motors = 1; |
seas726 | 49:7da71f479dac | 84 | // motors_enabled = false; |
seas726 | 49:7da71f479dac | 85 | robot_speed_desired(0) = 0.0f; // set speed and rotational velocity to zero |
seas726 | 49:7da71f479dac | 86 | robot_speed_desired(1) = 0.0f; |
seas726 | 49:7da71f479dac | 87 | |
seas726 | 49:7da71f479dac | 88 | if(controller.GetTurnedOn()) // check to see if blue button is toggled |
seas726 | 49:7da71f479dac | 89 | { |
seas726 | 49:7da71f479dac | 90 | state = FOLLOWING_LINE; |
seas726 | 49:7da71f479dac | 91 | } |
seas726 | 49:7da71f479dac | 92 | } |
seas726 | 49:7da71f479dac | 93 | |
seas726 | 49:7da71f479dac | 94 | void Robot::Idle() |
seas726 | 49:7da71f479dac | 95 | { |
seas726 | 49:7da71f479dac | 96 | printf("Idle\n"); // TODO: REMOVE PRINT |
seas726 | 49:7da71f479dac | 97 | robot_speed_desired(0) = 0.0f; // set speed and rotational velocity to zero |
seas726 | 49:7da71f479dac | 98 | robot_speed_desired(1) = 0.0f; |
seas726 | 49:7da71f479dac | 99 | } |
seas726 | 49:7da71f479dac | 100 | |
seas726 | 49:7da71f479dac | 101 | void Robot::FollowingLine() |
seas726 | 49:7da71f479dac | 102 | { |
seas726 | 49:7da71f479dac | 103 | printf("FollowingLine\n"); // TODO: REMOVE PRINT |
seas726 | 49:7da71f479dac | 104 | robot_speed_desired(0) = TRANSLATIONAL_VELOCITY; |
seas726 | 49:7da71f479dac | 105 | robot_speed_desired(1) = 0.0f; // set |
seas726 | 49:7da71f479dac | 106 | |
seas726 | 49:7da71f479dac | 107 | printf("%d", line_sensor.getRaw()); |
seas726 | 49:7da71f479dac | 108 | |
seas726 | 49:7da71f479dac | 109 | |
seas726 | 49:7da71f479dac | 110 | // somehow make veloity relative to the angle of the line? |
seas726 | 49:7da71f479dac | 111 | |
seas726 | 49:7da71f479dac | 112 | } |
seas726 | 49:7da71f479dac | 113 | |
seas726 | 49:7da71f479dac | 114 | |
seas726 | 49:7da71f479dac | 115 |