Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | #include "aos/common/util/phased_loop.h" |
| 6 | #include "aos/common/time.h" |
| 7 | #include "aos/common/util/trapezoid_profile.h" |
| 8 | #include "aos/common/logging/logging.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 9 | #include "aos/common/logging/queue_logging.h" |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 10 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 12 | #include "y2014_bot3/autonomous/auto.q.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 13 | #include "y2014_bot3/control_loops/rollers/rollers.q.h" |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 14 | |
| 15 | using ::aos::time::Time; |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 16 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 17 | using ::y2014_bot3::control_loops::rollers_queue; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 18 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 19 | namespace y2014_bot3 { |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 20 | namespace autonomous { |
| 21 | |
| 22 | namespace time = ::aos::time; |
| 23 | |
| 24 | static double left_initial_position, right_initial_position; |
| 25 | |
| 26 | bool ShouldExitAuto() { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 27 | ::y2014_bot3::autonomous::autonomous.FetchLatest(); |
| 28 | bool ans = !::y2014_bot3::autonomous::autonomous->run_auto; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 29 | if (ans) { |
| 30 | LOG(INFO, "Time to exit auto mode\n"); |
| 31 | } |
| 32 | return ans; |
| 33 | } |
| 34 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 35 | void ResetDrivetrain() { |
| 36 | LOG(INFO, "resetting the drivetrain\n"); |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 37 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 38 | .control_loop_driving(false) |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 39 | .steering(0.0) |
| 40 | .throttle(0.0) |
| 41 | .left_goal(left_initial_position) |
| 42 | .left_velocity_goal(0) |
| 43 | .right_goal(right_initial_position) |
| 44 | .right_velocity_goal(0) |
| 45 | .Send(); |
| 46 | } |
| 47 | |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 48 | void InitializeEncoders() { |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 49 | ::frc971::control_loops::drivetrain_queue.status.FetchAnother(); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 50 | left_initial_position = |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 51 | ::frc971::control_loops::drivetrain_queue.status->estimated_left_position; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 52 | right_initial_position = |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 53 | ::frc971::control_loops::drivetrain_queue.status->estimated_right_position; |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void HandleAuto() { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 57 | ::aos::time::Time start_time = ::aos::time::Time::Now(); |
| 58 | LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds()); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 59 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 60 | // TODO(comran): Add various options for different autos down below. |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 61 | ResetDrivetrain(); |
| 62 | InitializeEncoders(); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 63 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 64 | LOG(INFO, "Driving\n"); |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 65 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 66 | .control_loop_driving(false) |
| 67 | .highgear(false) |
| 68 | .quickturn(false) |
| 69 | .steering(0.0) |
| 70 | .throttle(0.5) |
| 71 | .left_goal(left_initial_position) |
| 72 | .left_velocity_goal(0) |
| 73 | .right_goal(right_initial_position) |
| 74 | .right_velocity_goal(0) |
| 75 | .Send(); |
| 76 | time::SleepFor(time::Time::InSeconds(2.0)); |
| 77 | |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 78 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 79 | .control_loop_driving(false) |
| 80 | .highgear(false) |
| 81 | .quickturn(false) |
| 82 | .steering(0.0) |
| 83 | .throttle(0.0) |
| 84 | .left_goal(left_initial_position) |
| 85 | .left_velocity_goal(0) |
| 86 | .right_goal(right_initial_position) |
| 87 | .right_velocity_goal(0) |
| 88 | .Send(); |
Comran Morshed | e9b1292 | 2015-11-04 19:46:48 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace autonomous |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 92 | } // namespace y2014_bot3 |