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