Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 1 | #include "stdio.h" |
| 2 | |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 3 | #include "aos/common/control_loop/Timing.h" |
| 4 | #include "aos/common/time.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 5 | #include "aos/common/util/trapezoid_profile.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 6 | #include "aos/common/logging/logging.h" |
Brian Silverman | 96d9cea | 2013-11-12 21:10:50 -0800 | [diff] [blame] | 7 | #include "aos/common/network/team_number.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 8 | |
| 9 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 10 | #include "frc971/constants.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 12 | |
| 13 | using ::aos::time::Time; |
| 14 | |
| 15 | namespace frc971 { |
| 16 | namespace autonomous { |
| 17 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 18 | static double left_initial_position, right_initial_position; |
| 19 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 20 | bool ShouldExitAuto() { |
| 21 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 22 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 23 | if (ans) { |
| 24 | LOG(INFO, "Time to exit auto mode\n"); |
| 25 | } |
| 26 | return ans; |
| 27 | } |
| 28 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 29 | void StopDrivetrain() { |
| 30 | LOG(INFO, "Stopping the drivetrain\n"); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 31 | control_loops::drivetrain.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 32 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 33 | .left_goal(left_initial_position) |
| 34 | .left_velocity_goal(0) |
| 35 | .right_goal(right_initial_position) |
| 36 | .right_velocity_goal(0) |
| 37 | .quickturn(false) |
| 38 | .Send(); |
| 39 | } |
| 40 | |
| 41 | void ResetDrivetrain() { |
| 42 | LOG(INFO, "resetting the drivetrain\n"); |
| 43 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 44 | .control_loop_driving(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 45 | .highgear(false) |
| 46 | .steering(0.0) |
| 47 | .throttle(0.0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 48 | .Send(); |
| 49 | } |
| 50 | |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 51 | void SetDriveGoal(double yoffset, double maximum_velocity = 1.5) { |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 52 | LOG(INFO, "Going to move %f\n", yoffset); |
| 53 | |
| 54 | // Measured conversion to get the distance right. |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 55 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 56 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 57 | const double goal_velocity = 0.0; |
| 58 | const double epsilon = 0.01; |
| 59 | |
Austin Schuh | fae5336 | 2013-03-23 04:39:48 +0000 | [diff] [blame] | 60 | profile.set_maximum_acceleration(2.0); |
Brian Silverman | ed2cbde | 2013-03-31 01:56:14 -0700 | [diff] [blame] | 61 | profile.set_maximum_velocity(maximum_velocity); |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 62 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 63 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 64 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 65 | driveTrainState = profile.Update(yoffset, goal_velocity); |
| 66 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 67 | if (::std::abs(driveTrainState(0, 0) - yoffset) < epsilon) break; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 68 | if (ShouldExitAuto()) return; |
| 69 | |
| 70 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 71 | driveTrainState(0, 0) + left_initial_position, |
| 72 | driveTrainState(0, 0) + right_initial_position); |
| 73 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 74 | .control_loop_driving(true) |
| 75 | .highgear(false) |
| 76 | .left_goal(driveTrainState(0, 0) + left_initial_position) |
| 77 | .right_goal(driveTrainState(0, 0) + right_initial_position) |
| 78 | .left_velocity_goal(driveTrainState(1, 0)) |
| 79 | .right_velocity_goal(driveTrainState(1, 0)) |
| 80 | .Send(); |
| 81 | } |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 82 | left_initial_position += yoffset; |
| 83 | right_initial_position += yoffset; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 84 | LOG(INFO, "Done moving\n"); |
| 85 | } |
| 86 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 87 | void DriveSpin(double radians) { |
| 88 | LOG(INFO, "going to spin %f\n", radians); |
| 89 | |
| 90 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 91 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 92 | const double goal_velocity = 0.0; |
| 93 | const double epsilon = 0.01; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 94 | // in drivetrain "meters" |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 95 | const double kRobotWidth = 0.4544; |
| 96 | |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 97 | profile.set_maximum_acceleration(1.5); |
| 98 | profile.set_maximum_velocity(0.8); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 99 | |
| 100 | const double side_offset = kRobotWidth * radians / 2.0; |
| 101 | |
| 102 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 103 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 104 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 105 | |
| 106 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 107 | if (ShouldExitAuto()) return; |
| 108 | |
| 109 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 110 | left_initial_position - driveTrainState(0, 0), |
| 111 | right_initial_position + driveTrainState(0, 0)); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 112 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 113 | .control_loop_driving(true) |
| 114 | .highgear(false) |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 115 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 116 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 117 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 118 | .right_velocity_goal(driveTrainState(1, 0)) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 119 | .Send(); |
| 120 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 121 | left_initial_position -= side_offset; |
| 122 | right_initial_position += side_offset; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 123 | LOG(INFO, "Done moving\n"); |
| 124 | } |
| 125 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 126 | void HandleAuto() { |
| 127 | LOG(INFO, "Handling auto mode\n"); |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 128 | |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 129 | ResetDrivetrain(); |
| 130 | |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 131 | if (ShouldExitAuto()) return; |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 132 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 133 | control_loops::drivetrain.position.FetchLatest(); |
| 134 | while (!control_loops::drivetrain.position.get()) { |
| 135 | LOG(WARNING, "No previous drivetrain position packet, trying to fetch again\n"); |
| 136 | control_loops::drivetrain.position.FetchNextBlocking(); |
| 137 | } |
| 138 | left_initial_position = |
| 139 | control_loops::drivetrain.position->left_encoder; |
| 140 | right_initial_position = |
| 141 | control_loops::drivetrain.position->right_encoder; |
| 142 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 143 | StopDrivetrain(); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace autonomous |
| 147 | } // namespace frc971 |