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