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" |
Daniel Petti | 3b1e48f | 2015-02-15 15:57:53 -0800 | [diff] [blame] | 14 | #include "frc971/actors/drivetrain_actor.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 15 | |
| 16 | using ::aos::time::Time; |
| 17 | |
| 18 | namespace frc971 { |
| 19 | namespace autonomous { |
| 20 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 21 | namespace time = ::aos::time; |
| 22 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 23 | static double left_initial_position, right_initial_position; |
| 24 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 25 | bool ShouldExitAuto() { |
| 26 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 27 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 28 | if (ans) { |
| 29 | LOG(INFO, "Time to exit auto mode\n"); |
| 30 | } |
| 31 | return ans; |
| 32 | } |
| 33 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 34 | void StopDrivetrain() { |
| 35 | LOG(INFO, "Stopping the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 36 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 37 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 38 | .left_goal(left_initial_position) |
| 39 | .left_velocity_goal(0) |
| 40 | .right_goal(right_initial_position) |
| 41 | .right_velocity_goal(0) |
| 42 | .quickturn(false) |
| 43 | .Send(); |
| 44 | } |
| 45 | |
| 46 | void ResetDrivetrain() { |
| 47 | LOG(INFO, "resetting the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 48 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 49 | .control_loop_driving(false) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 50 | //.highgear(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 51 | .steering(0.0) |
| 52 | .throttle(0.0) |
Brian Silverman | 38ea9bf | 2014-04-19 22:57:54 -0700 | [diff] [blame] | 53 | .left_goal(left_initial_position) |
| 54 | .left_velocity_goal(0) |
| 55 | .right_goal(right_initial_position) |
| 56 | .right_velocity_goal(0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 57 | .Send(); |
| 58 | } |
| 59 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 60 | void DriveSpin(double radians) { |
| 61 | LOG(INFO, "going to spin %f\n", radians); |
| 62 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 63 | // TODO(sensors): update this time thing maybe? |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 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 | 547abb3 | 2015-02-16 00:37:48 -0500 | [diff] [blame] | 77 | ::aos::time::PhasedLoopXMS(5, 2500); |
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 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 100 | void WaitUntilDoneOrCanceled(aos::common::actions::Action *action) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 101 | while (true) { |
| 102 | // Poll the running bit and auto done bits. |
Brian Silverman | 547abb3 | 2015-02-16 00:37:48 -0500 | [diff] [blame] | 103 | ::aos::time::PhasedLoopXMS(5, 2500); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 104 | if (!action->Running() || ShouldExitAuto()) { |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 110 | ::std::unique_ptr<::frc971::actors::DrivetrainAction> SetDriveGoal( |
| 111 | double distance, bool slow_acceleration, double maximum_velocity = 1.7, |
| 112 | double theta = 0) { |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 113 | LOG(INFO, "Driving to %f\n", distance); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 114 | |
| 115 | ::frc971::actors::DrivetrainActionParams params; |
| 116 | params.left_initial_position = left_initial_position; |
| 117 | params.right_initial_position = right_initial_position; |
| 118 | params.y_offset = distance; |
| 119 | params.theta_offset = theta; |
| 120 | params.maximum_velocity = maximum_velocity; |
| 121 | params.maximum_acceleration = slow_acceleration ? 2.5 : 3.0; |
| 122 | auto drivetrain_action = actors::MakeDrivetrainAction(params); |
| 123 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 124 | drivetrain_action->Start(); |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 125 | left_initial_position += |
| 126 | distance - theta * constants::GetValues().turn_width / 2.0; |
| 127 | right_initial_position += |
Daniel Petti | b0733be | 2014-11-14 22:44:03 -0800 | [diff] [blame] | 128 | distance + theta * constants::GetValues().turn_width / 2.0; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 129 | return ::std::move(drivetrain_action); |
| 130 | } |
| 131 | |
| 132 | void InitializeEncoders() { |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 133 | control_loops::drivetrain_queue.status.FetchLatest(); |
| 134 | while (!control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 2c1e034 | 2014-04-11 16:15:01 -0700 | [diff] [blame] | 135 | LOG(WARNING, |
| 136 | "No previous drivetrain position packet, trying to fetch again\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 137 | control_loops::drivetrain_queue.status.FetchNextBlocking(); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 138 | } |
| 139 | left_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 140 | control_loops::drivetrain_queue.status->filtered_left_position; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 141 | right_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 142 | control_loops::drivetrain_queue.status->filtered_right_position; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void HandleAuto() { |
Austin Schuh | 577edf6 | 2014-04-13 10:33:05 -0700 | [diff] [blame] | 146 | ::aos::time::Time start_time = ::aos::time::Time::Now(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 147 | LOG(INFO, "Handling auto mode at %f\n", start_time.ToSeconds()); |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame] | 148 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 149 | ResetDrivetrain(); |
| 150 | |
| 151 | if (ShouldExitAuto()) return; |
| 152 | InitializeEncoders(); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace autonomous |
| 156 | } // namespace frc971 |