blob: d33b1253fd271e5cadddd04715f1ef979fcff0e3 [file] [log] [blame]
Comran Morshed2f7b4672016-01-23 14:27:34 +00001#include "y2016/actors/superstructure_actor.h"
2
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -07003#include <cmath>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/util/phased_loop.h"
Comran Morshed2f7b4672016-01-23 14:27:34 +00007#include "y2016/actors/superstructure_actor.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "y2016/control_loops/superstructure/superstructure_goal_generated.h"
9#include "y2016/control_loops/superstructure/superstructure_status_generated.h"
Comran Morshed2f7b4672016-01-23 14:27:34 +000010
11namespace y2016 {
12namespace actors {
13
Austin Schuh02bd83e2016-11-25 20:13:54 -080014namespace chrono = ::std::chrono;
15
Austin Schuh1bf8a212019-05-26 22:13:14 -070016SuperstructureActor::SuperstructureActor(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -070017 : aos::common::actions::ActorBase<superstructure_action::Goal>(
18 event_loop, "/superstructure_action"),
Austin Schuh9481d0d2019-06-29 21:56:17 -070019 superstructure_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 event_loop->MakeSender<::y2016::control_loops::superstructure::Goal>(
21 "/superstructure")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070022 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 event_loop
24 ->MakeFetcher<::y2016::control_loops::superstructure::Status>(
Austin Schuh094d09b2020-11-20 23:26:52 -080025 "/superstructure")) {
26 event_loop->SetRuntimeRealtimePriority(29);
27}
Comran Morshed2f7b4672016-01-23 14:27:34 +000028
29bool SuperstructureActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 const superstructure_action::SuperstructureActionParams *params) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070031 AOS_LOG(INFO, "Starting superstructure action\n");
Comran Morshed2f7b4672016-01-23 14:27:34 +000032
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 MoveSuperstructure(params->partial_angle(), params->shooter_angle(), false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070034 WaitForSuperstructure();
35 if (ShouldCancel()) return true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 MoveSuperstructure(params->partial_angle(), params->shooter_angle(), true);
Austin Schuh02bd83e2016-11-25 20:13:54 -080037 if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 chrono::duration<double>(params->delay_time()))))
Austin Schuh02bd83e2016-11-25 20:13:54 -080039 return true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 MoveSuperstructure(params->full_angle(), params->shooter_angle(), true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070041 WaitForSuperstructure();
42 if (ShouldCancel()) return true;
43 return true;
44}
45
Austin Schuhfef64ac2016-04-24 19:08:01 -070046void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter,
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070047 bool unfold_climber) {
Austin Schuhfef64ac2016-04-24 19:08:01 -070048 superstructure_goal_ = {0, shoulder, shooter};
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070049
Alex Perrycb7da4b2019-08-28 19:35:56 -070050 auto builder = superstructure_goal_sender_.MakeBuilder();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070051
Alex Perrycb7da4b2019-08-28 19:35:56 -070052 control_loops::superstructure::Goal::Builder superstructure_goal_builder =
53 builder.MakeBuilder<control_loops::superstructure::Goal>();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070054
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 superstructure_goal_builder.add_angle_intake(0);
56 superstructure_goal_builder.add_angle_shoulder(shoulder);
57 superstructure_goal_builder.add_angle_wrist(shooter);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070058
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 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 Vandenberg9cc9ab62016-04-20 21:27:47 -070062
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 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 Vandenberg9cc9ab62016-04-20 21:27:47 -070066
Alex Perrycb7da4b2019-08-28 19:35:56 -070067 superstructure_goal_builder.add_voltage_top_rollers(0);
68 superstructure_goal_builder.add_voltage_bottom_rollers(0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070069
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 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
milind1f1dca32021-07-03 13:50:07 -070075 if (builder.Send(superstructure_goal_builder.Finish()) !=
76 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070077 AOS_LOG(ERROR, "Sending superstructure move failed.\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070078 }
79}
80
81bool SuperstructureActor::SuperstructureProfileDone() {
82 constexpr double kProfileError = 1e-2;
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 return ::std::abs(superstructure_status_fetcher_->intake()->goal_angle() -
Austin Schuh9481d0d2019-06-29 21:56:17 -070084 superstructure_goal_.intake) < kProfileError &&
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 ::std::abs(superstructure_status_fetcher_->shoulder()->goal_angle() -
Austin Schuh9481d0d2019-06-29 21:56:17 -070086 superstructure_goal_.shoulder) < kProfileError &&
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 ::std::abs(superstructure_status_fetcher_->intake()
88 ->goal_angular_velocity()) < kProfileError &&
89 ::std::abs(superstructure_status_fetcher_->shoulder()
90 ->goal_angular_velocity()) < kProfileError;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070091}
92
93bool SuperstructureActor::SuperstructureDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -070094 superstructure_status_fetcher_.Fetch();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070095
96 // We are no longer running if we are in the zeroing states (below 12), or
97 // estopped.
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 if (superstructure_status_fetcher_->state() < 12 ||
99 superstructure_status_fetcher_->state() == 16) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700100 AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700101 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +0000102 }
103
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700104 if (SuperstructureProfileDone()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700105 AOS_LOG(DEBUG, "Profile done.\n");
106 return true;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700107 }
108 return false;
109}
110
111void SuperstructureActor::WaitForSuperstructure() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700112 WaitUntil(::std::bind(&SuperstructureActor::SuperstructureDone, this));
Comran Morshed2f7b4672016-01-23 14:27:34 +0000113}
114
Comran Morshed2f7b4672016-01-23 14:27:34 +0000115} // namespace actors
116} // namespace y2016