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