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/logging/logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include "aos/util/phased_loop.h" |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 7 | #include "y2016/actors/superstructure_actor.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "y2016/control_loops/superstructure/superstructure_goal_generated.h" |
| 9 | #include "y2016/control_loops/superstructure/superstructure_status_generated.h" |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 10 | |
| 11 | namespace y2016 { |
| 12 | namespace actors { |
| 13 | |
Austin Schuh | 02bd83e | 2016-11-25 20:13:54 -0800 | [diff] [blame] | 14 | namespace chrono = ::std::chrono; |
| 15 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 16 | SuperstructureActor::SuperstructureActor(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | : aos::common::actions::ActorBase<superstructure_action::Goal>( |
| 18 | event_loop, "/superstructure_action"), |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 19 | superstructure_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | event_loop->MakeSender<::y2016::control_loops::superstructure::Goal>( |
| 21 | "/superstructure")), |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 22 | superstructure_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | event_loop |
| 24 | ->MakeFetcher<::y2016::control_loops::superstructure::Status>( |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame^] | 25 | "/superstructure")) { |
| 26 | event_loop->SetRuntimeRealtimePriority(29); |
| 27 | } |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 28 | |
| 29 | bool SuperstructureActor::RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | const superstructure_action::SuperstructureActionParams *params) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 31 | AOS_LOG(INFO, "Starting superstructure action\n"); |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 32 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | MoveSuperstructure(params->partial_angle(), params->shooter_angle(), false); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 34 | WaitForSuperstructure(); |
| 35 | if (ShouldCancel()) return true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | MoveSuperstructure(params->partial_angle(), params->shooter_angle(), true); |
Austin Schuh | 02bd83e | 2016-11-25 20:13:54 -0800 | [diff] [blame] | 37 | if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | chrono::duration<double>(params->delay_time())))) |
Austin Schuh | 02bd83e | 2016-11-25 20:13:54 -0800 | [diff] [blame] | 39 | return true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | MoveSuperstructure(params->full_angle(), params->shooter_angle(), true); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 41 | WaitForSuperstructure(); |
| 42 | if (ShouldCancel()) return true; |
| 43 | return true; |
| 44 | } |
| 45 | |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 46 | void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter, |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 47 | bool unfold_climber) { |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 48 | superstructure_goal_ = {0, shoulder, shooter}; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 49 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 51 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 52 | control_loops::superstructure::Goal::Builder superstructure_goal_builder = |
| 53 | builder.MakeBuilder<control_loops::superstructure::Goal>(); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 54 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | superstructure_goal_builder.add_angle_intake(0); |
| 56 | superstructure_goal_builder.add_angle_shoulder(shoulder); |
| 57 | superstructure_goal_builder.add_angle_wrist(shooter); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 58 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 59 | superstructure_goal_builder.add_max_angular_velocity_intake(7.0); |
| 60 | superstructure_goal_builder.add_max_angular_velocity_shoulder(4.0); |
| 61 | superstructure_goal_builder.add_max_angular_velocity_wrist(10.0); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 62 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | superstructure_goal_builder.add_max_angular_acceleration_intake(40.0); |
| 64 | superstructure_goal_builder.add_max_angular_acceleration_shoulder(5.0); |
| 65 | superstructure_goal_builder.add_max_angular_acceleration_wrist(25.0); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 66 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | superstructure_goal_builder.add_voltage_top_rollers(0); |
| 68 | superstructure_goal_builder.add_voltage_bottom_rollers(0); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 69 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | superstructure_goal_builder.add_traverse_unlatched(true); |
| 71 | superstructure_goal_builder.add_traverse_down(false); |
| 72 | superstructure_goal_builder.add_voltage_climber(0.0); |
| 73 | superstructure_goal_builder.add_unfold_climber(unfold_climber); |
| 74 | |
| 75 | if (!builder.Send(superstructure_goal_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 76 | AOS_LOG(ERROR, "Sending superstructure move failed.\n"); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | bool SuperstructureActor::SuperstructureProfileDone() { |
| 81 | constexpr double kProfileError = 1e-2; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | return ::std::abs(superstructure_status_fetcher_->intake()->goal_angle() - |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 83 | superstructure_goal_.intake) < kProfileError && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | ::std::abs(superstructure_status_fetcher_->shoulder()->goal_angle() - |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 85 | superstructure_goal_.shoulder) < kProfileError && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 86 | ::std::abs(superstructure_status_fetcher_->intake() |
| 87 | ->goal_angular_velocity()) < kProfileError && |
| 88 | ::std::abs(superstructure_status_fetcher_->shoulder() |
| 89 | ->goal_angular_velocity()) < kProfileError; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool SuperstructureActor::SuperstructureDone() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 93 | superstructure_status_fetcher_.Fetch(); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 94 | |
| 95 | // We are no longer running if we are in the zeroing states (below 12), or |
| 96 | // estopped. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | if (superstructure_status_fetcher_->state() < 12 || |
| 98 | superstructure_status_fetcher_->state() == 16) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 99 | AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n"); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 100 | return true; |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 103 | if (SuperstructureProfileDone()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 104 | AOS_LOG(DEBUG, "Profile done.\n"); |
| 105 | return true; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | void SuperstructureActor::WaitForSuperstructure() { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 111 | WaitUntil(::std::bind(&SuperstructureActor::SuperstructureDone, this)); |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Comran Morshed | 2f7b467 | 2016-01-23 14:27:34 +0000 | [diff] [blame] | 114 | } // namespace actors |
| 115 | } // namespace y2016 |