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