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