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