Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 1 | #include "y2016/actors/superstructure_actor.h" |
| 2 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 3 | #include <cmath> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/util/phased_loop.h" |
| 6 | #include "aos/logging/logging.h" |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 7 | #include "y2016/actors/superstructure_actor.h" |
| 8 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
| 9 | |
| 10 | namespace y2016 { |
| 11 | namespace actors { |
| 12 | |
Austin Schuh | 02bd83e | 2016-11-25 20:13:54 -0800 | [diff] [blame] | 13 | namespace chrono = ::std::chrono; |
| 14 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 15 | SuperstructureActor::SuperstructureActor(::aos::EventLoop *event_loop) |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 16 | : aos::common::actions::ActorBase<actors::SuperstructureActionQueueGroup>( |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 17 | event_loop, ".y2016.actors.superstructure_action") {} |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 18 | |
| 19 | bool SuperstructureActor::RunAction( |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 20 | const actors::SuperstructureActionParams ¶ms) { |
| 21 | LOG(INFO, "Starting superstructure action\n"); |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 22 | |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 23 | MoveSuperstructure(params.partial_angle, params.shooter_angle, false); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 24 | WaitForSuperstructure(); |
| 25 | if (ShouldCancel()) return true; |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 26 | MoveSuperstructure(params.partial_angle, params.shooter_angle, true); |
Austin Schuh | 02bd83e | 2016-11-25 20:13:54 -0800 | [diff] [blame] | 27 | if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>( |
| 28 | chrono::duration<double>(params.delay_time)))) |
| 29 | return true; |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 30 | MoveSuperstructure(params.full_angle, params.shooter_angle, true); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 31 | WaitForSuperstructure(); |
| 32 | if (ShouldCancel()) return true; |
| 33 | return true; |
| 34 | } |
| 35 | |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 36 | void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter, |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 37 | bool unfold_climber) { |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 38 | superstructure_goal_ = {0, shoulder, shooter}; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 39 | |
| 40 | auto new_superstructure_goal = |
| 41 | ::y2016::control_loops::superstructure_queue.goal.MakeMessage(); |
| 42 | |
| 43 | new_superstructure_goal->angle_intake = 0; |
| 44 | new_superstructure_goal->angle_shoulder = shoulder; |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 45 | new_superstructure_goal->angle_wrist = shooter; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 46 | |
| 47 | new_superstructure_goal->max_angular_velocity_intake = 7.0; |
| 48 | new_superstructure_goal->max_angular_velocity_shoulder = 4.0; |
| 49 | new_superstructure_goal->max_angular_velocity_wrist = 10.0; |
| 50 | |
| 51 | new_superstructure_goal->max_angular_acceleration_intake = 40.0; |
| 52 | new_superstructure_goal->max_angular_acceleration_shoulder = 5.0; |
| 53 | new_superstructure_goal->max_angular_acceleration_wrist = 25.0; |
| 54 | |
| 55 | new_superstructure_goal->voltage_top_rollers = 0; |
| 56 | new_superstructure_goal->voltage_bottom_rollers = 0; |
| 57 | |
| 58 | new_superstructure_goal->traverse_unlatched = true; |
| 59 | new_superstructure_goal->traverse_down = false; |
| 60 | new_superstructure_goal->voltage_climber = 0.0; |
| 61 | new_superstructure_goal->unfold_climber = unfold_climber; |
| 62 | |
| 63 | if (!new_superstructure_goal.Send()) { |
| 64 | LOG(ERROR, "Sending superstructure move failed.\n"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | bool SuperstructureActor::SuperstructureProfileDone() { |
| 69 | constexpr double kProfileError = 1e-2; |
| 70 | return ::std::abs( |
| 71 | control_loops::superstructure_queue.status->intake.goal_angle - |
| 72 | superstructure_goal_.intake) < kProfileError && |
| 73 | ::std::abs( |
| 74 | control_loops::superstructure_queue.status->shoulder.goal_angle - |
| 75 | superstructure_goal_.shoulder) < kProfileError && |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 76 | ::std::abs(control_loops::superstructure_queue.status->intake |
| 77 | .goal_angular_velocity) < kProfileError && |
| 78 | ::std::abs(control_loops::superstructure_queue.status->shoulder |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 79 | .goal_angular_velocity) < kProfileError; |
| 80 | } |
| 81 | |
| 82 | bool SuperstructureActor::SuperstructureDone() { |
| 83 | control_loops::superstructure_queue.status.FetchAnother(); |
| 84 | |
| 85 | // We are no longer running if we are in the zeroing states (below 12), or |
| 86 | // estopped. |
| 87 | if (control_loops::superstructure_queue.status->state < 12 || |
| 88 | control_loops::superstructure_queue.status->state == 16) { |
| 89 | LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
| 90 | return true; |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 93 | if (SuperstructureProfileDone()) { |
| 94 | LOG(DEBUG, "Profile done.\n"); |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | void SuperstructureActor::WaitForSuperstructure() { |
| 101 | while (true) { |
| 102 | if (ShouldCancel()) return; |
| 103 | if (SuperstructureDone()) return; |
| 104 | } |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 107 | } // namespace actors |
| 108 | } // namespace y2016 |