Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <memory> |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 4 | |
Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 5 | #include "aos/common/util/phased_loop.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 6 | #include "aos/common/time.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 7 | #include "aos/common/util/trapezoid_profile.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 8 | #include "aos/common/logging/logging.h" |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame] | 9 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 10 | |
| 11 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 12 | #include "frc971/constants.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 14 | #include "frc971/actions/action_client.h" |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 15 | #include "frc971/actions/drivetrain_action.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 16 | |
| 17 | using ::aos::time::Time; |
| 18 | |
| 19 | namespace frc971 { |
| 20 | namespace autonomous { |
| 21 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 22 | namespace time = ::aos::time; |
| 23 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 24 | static double left_initial_position, right_initial_position; |
| 25 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 26 | bool ShouldExitAuto() { |
| 27 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 28 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 29 | if (ans) { |
| 30 | LOG(INFO, "Time to exit auto mode\n"); |
| 31 | } |
| 32 | return ans; |
| 33 | } |
| 34 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 35 | void StopDrivetrain() { |
| 36 | LOG(INFO, "Stopping the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 37 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 38 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 39 | .left_goal(left_initial_position) |
| 40 | .left_velocity_goal(0) |
| 41 | .right_goal(right_initial_position) |
| 42 | .right_velocity_goal(0) |
| 43 | .quickturn(false) |
| 44 | .Send(); |
| 45 | } |
| 46 | |
| 47 | void ResetDrivetrain() { |
| 48 | LOG(INFO, "resetting the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 49 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 50 | .control_loop_driving(false) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 51 | //.highgear(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 52 | .steering(0.0) |
| 53 | .throttle(0.0) |
Brian Silverman | 38ea9bf | 2014-04-19 22:57:54 -0700 | [diff] [blame] | 54 | .left_goal(left_initial_position) |
| 55 | .left_velocity_goal(0) |
| 56 | .right_goal(right_initial_position) |
| 57 | .right_velocity_goal(0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 58 | .Send(); |
| 59 | } |
| 60 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 61 | void DriveSpin(double radians) { |
| 62 | LOG(INFO, "going to spin %f\n", radians); |
| 63 | |
| 64 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 65 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 66 | const double goal_velocity = 0.0; |
| 67 | const double epsilon = 0.01; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 68 | // in drivetrain "meters" |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 69 | const double kRobotWidth = 0.4544; |
| 70 | |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 71 | profile.set_maximum_acceleration(1.5); |
| 72 | profile.set_maximum_velocity(0.8); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 73 | |
| 74 | const double side_offset = kRobotWidth * radians / 2.0; |
| 75 | |
| 76 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 77 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 78 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 79 | |
| 80 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 81 | if (ShouldExitAuto()) return; |
| 82 | |
| 83 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 84 | left_initial_position - driveTrainState(0, 0), |
| 85 | right_initial_position + driveTrainState(0, 0)); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 86 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 87 | .control_loop_driving(true) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 88 | //.highgear(false) |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 89 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 90 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 91 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 92 | .right_velocity_goal(driveTrainState(1, 0)) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 93 | .Send(); |
| 94 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 95 | left_initial_position -= side_offset; |
| 96 | right_initial_position += side_offset; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 97 | LOG(INFO, "Done moving\n"); |
| 98 | } |
| 99 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 100 | void WaitUntilDoneOrCanceled(Action *action) { |
| 101 | while (true) { |
| 102 | // Poll the running bit and auto done bits. |
| 103 | ::aos::time::PhasedLoop10MS(5000); |
| 104 | if (!action->Running() || ShouldExitAuto()) { |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 110 | ::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>> |
Natalia Frumkin | d1fa52f | 2014-06-21 15:24:25 -0700 | [diff] [blame] | 111 | SetDriveGoal(double distance, bool slow_acceleration, |
| 112 | double maximum_velocity = 1.7, double theta = 0) { |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 113 | LOG(INFO, "Driving to %f\n", distance); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 114 | auto drivetrain_action = actions::MakeDrivetrainAction(); |
| 115 | drivetrain_action->GetGoal()->left_initial_position = left_initial_position; |
| 116 | drivetrain_action->GetGoal()->right_initial_position = right_initial_position; |
| 117 | drivetrain_action->GetGoal()->y_offset = distance; |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 118 | drivetrain_action->GetGoal()->theta_offset = theta; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 119 | drivetrain_action->GetGoal()->maximum_velocity = maximum_velocity; |
Natalia Frumkin | d1fa52f | 2014-06-21 15:24:25 -0700 | [diff] [blame] | 120 | drivetrain_action->GetGoal()->maximum_acceleration = |
| 121 | slow_acceleration ? 2.5 : 3.0; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 122 | drivetrain_action->Start(); |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 123 | left_initial_position += |
| 124 | distance - theta * constants::GetValues().turn_width / 2.0; |
| 125 | right_initial_position += |
Daniel Petti | b0733be | 2014-11-14 22:44:03 -0800 | [diff] [blame] | 126 | distance + theta * constants::GetValues().turn_width / 2.0; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 127 | return ::std::move(drivetrain_action); |
| 128 | } |
| 129 | |
| 130 | void InitializeEncoders() { |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 131 | control_loops::drivetrain_queue.status.FetchLatest(); |
| 132 | while (!control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 2c1e034 | 2014-04-11 16:15:01 -0700 | [diff] [blame] | 133 | LOG(WARNING, |
| 134 | "No previous drivetrain position packet, trying to fetch again\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 135 | control_loops::drivetrain_queue.status.FetchNextBlocking(); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 136 | } |
| 137 | left_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 138 | control_loops::drivetrain_queue.status->filtered_left_position; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 139 | right_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame^] | 140 | control_loops::drivetrain_queue.status->filtered_right_position; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void HandleAuto() { |
Austin Schuh | 577edf6 | 2014-04-13 10:33:05 -0700 | [diff] [blame] | 144 | ::aos::time::Time start_time = ::aos::time::Time::Now(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 145 | LOG(INFO, "Handling auto mode at %f\n", start_time.ToSeconds()); |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame] | 146 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 147 | ResetDrivetrain(); |
| 148 | |
| 149 | if (ShouldExitAuto()) return; |
| 150 | InitializeEncoders(); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace autonomous |
| 154 | } // namespace frc971 |