Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +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" |
| 9 | #include "aos/common/logging/queue_logging.h" |
| 10 | |
| 11 | #include "bot3/autonomous/auto.q.h" |
| 12 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 13 | #include "bot3/control_loops/drivetrain/drivetrain.h" |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 14 | |
| 15 | using ::aos::time::Time; |
| 16 | using ::bot3::control_loops::drivetrain_queue; |
| 17 | |
| 18 | namespace bot3 { |
| 19 | namespace autonomous { |
| 20 | |
| 21 | struct ProfileParams { |
| 22 | double velocity; |
| 23 | double acceleration; |
| 24 | }; |
| 25 | |
| 26 | namespace time = ::aos::time; |
| 27 | |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 28 | static double left_initial_position, right_initial_position; |
| 29 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 30 | bool ShouldExitAuto() { |
| 31 | ::bot3::autonomous::autonomous.FetchLatest(); |
| 32 | bool ans = !::bot3::autonomous::autonomous->run_auto; |
| 33 | if (ans) { |
| 34 | LOG(INFO, "Time to exit auto mode\n"); |
| 35 | } |
| 36 | return ans; |
| 37 | } |
| 38 | |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 39 | void ResetDrivetrain() { |
| 40 | LOG(INFO, "resetting the drivetrain\n"); |
| 41 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
| 42 | .control_loop_driving(false) |
| 43 | .steering(0.0) |
| 44 | .throttle(0.0) |
| 45 | .left_goal(left_initial_position) |
| 46 | .left_velocity_goal(0) |
| 47 | .right_goal(right_initial_position) |
| 48 | .right_velocity_goal(0) |
| 49 | .Send(); |
| 50 | } |
| 51 | |
| 52 | void InitializeEncoders() { |
| 53 | control_loops::drivetrain_queue.status.FetchAnother(); |
| 54 | left_initial_position = |
| 55 | control_loops::drivetrain_queue.status->filtered_left_position; |
| 56 | right_initial_position = |
| 57 | control_loops::drivetrain_queue.status->filtered_right_position; |
| 58 | } |
| 59 | |
| 60 | void GrabberForTime(double voltage, double wait_time) { |
| 61 | ::aos::time::Time now = ::aos::time::Time::Now(); |
| 62 | ::aos::time::Time end_time = now + time::Time::InSeconds(wait_time); |
| 63 | LOG(INFO, "Starting to grab at %f for %f seconds\n", voltage, wait_time); |
| 64 | |
| 65 | while (true) { |
| 66 | autonomous::can_grabber_control.MakeWithBuilder() |
| 67 | .can_grabber_voltage(voltage) |
| 68 | .Send(); |
| 69 | |
| 70 | // Poll the running bit and auto done bits. |
| 71 | if (ShouldExitAuto()) { |
| 72 | return; |
| 73 | } |
| 74 | if (::aos::time::Time::Now() > end_time) { |
| 75 | LOG(INFO, "Done grabbing\n"); |
| 76 | return; |
| 77 | } |
| 78 | ::aos::time::PhasedLoopXMS(5, 2500); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Auto methods. |
| 83 | void CanGrabberAuto() { |
| 84 | ResetDrivetrain(); |
| 85 | |
| 86 | // Launch can grabbers. |
| 87 | GrabberForTime(12.0, 0.26); |
| 88 | if (ShouldExitAuto()) return; |
| 89 | InitializeEncoders(); |
| 90 | ResetDrivetrain(); |
| 91 | if (ShouldExitAuto()) return; |
| 92 | |
| 93 | // Drive backwards, and pulse the can grabbers again to tip the cans. |
| 94 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
| 95 | .control_loop_driving(true) |
| 96 | .steering(0.0) |
| 97 | .throttle(0.0) |
| 98 | .left_goal(left_initial_position + 1.75) |
| 99 | .left_velocity_goal(0) |
| 100 | .right_goal(right_initial_position + 1.75) |
| 101 | .right_velocity_goal(0) |
| 102 | .Send(); |
| 103 | GrabberForTime(12.0, 0.02); |
| 104 | if (ShouldExitAuto()) return; |
| 105 | |
| 106 | // We shouldn't need as much power at this point, so lower the can grabber |
| 107 | // voltages to avoid damaging the motors due to stalling. |
| 108 | GrabberForTime(4.0, 0.75); |
| 109 | if (ShouldExitAuto()) return; |
| 110 | GrabberForTime(-3.0, 0.25); |
| 111 | if (ShouldExitAuto()) return; |
| 112 | GrabberForTime(-12.0, 1.0); |
| 113 | if (ShouldExitAuto()) return; |
| 114 | GrabberForTime(-3.0, 12.0); |
| 115 | } |
| 116 | |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 117 | void HandleAuto() { |
| 118 | ::aos::time::Time start_time = ::aos::time::Time::Now(); |
| 119 | LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds()); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 120 | |
Comran Morshed | 34f891d | 2015-09-15 22:04:43 +0000 | [diff] [blame] | 121 | // TODO(comran): Add various options for different autos down below. |
| 122 | CanGrabberAuto(); |
Comran Morshed | 0d6cf9b | 2015-06-17 19:29:57 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } // namespace autonomous |
| 126 | } // namespace bot3 |