blob: bed7b3ad1a407cdd08610f4f7206cdc26eab38d2 [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>(
25 "/superstructure")) {}
Comran Morshed2f7b4672016-01-23 14:27:34 +000026
27bool SuperstructureActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070028 const superstructure_action::SuperstructureActionParams *params) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070029 AOS_LOG(INFO, "Starting superstructure action\n");
Comran Morshed2f7b4672016-01-23 14:27:34 +000030
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 MoveSuperstructure(params->partial_angle(), params->shooter_angle(), false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070032 WaitForSuperstructure();
33 if (ShouldCancel()) return true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 MoveSuperstructure(params->partial_angle(), params->shooter_angle(), true);
Austin Schuh02bd83e2016-11-25 20:13:54 -080035 if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 chrono::duration<double>(params->delay_time()))))
Austin Schuh02bd83e2016-11-25 20:13:54 -080037 return true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 MoveSuperstructure(params->full_angle(), params->shooter_angle(), true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070039 WaitForSuperstructure();
40 if (ShouldCancel()) return true;
41 return true;
42}
43
Austin Schuhfef64ac2016-04-24 19:08:01 -070044void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter,
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070045 bool unfold_climber) {
Austin Schuhfef64ac2016-04-24 19:08:01 -070046 superstructure_goal_ = {0, shoulder, shooter};
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070047
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 auto builder = superstructure_goal_sender_.MakeBuilder();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070049
Alex Perrycb7da4b2019-08-28 19:35:56 -070050 control_loops::superstructure::Goal::Builder superstructure_goal_builder =
51 builder.MakeBuilder<control_loops::superstructure::Goal>();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070052
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 superstructure_goal_builder.add_angle_intake(0);
54 superstructure_goal_builder.add_angle_shoulder(shoulder);
55 superstructure_goal_builder.add_angle_wrist(shooter);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070056
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 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 Vandenberg9cc9ab62016-04-20 21:27:47 -070060
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 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 Vandenberg9cc9ab62016-04-20 21:27:47 -070064
Alex Perrycb7da4b2019-08-28 19:35:56 -070065 superstructure_goal_builder.add_voltage_top_rollers(0);
66 superstructure_goal_builder.add_voltage_bottom_rollers(0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070067
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 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 Schuhf257f3c2019-10-27 21:00:43 -070074 AOS_LOG(ERROR, "Sending superstructure move failed.\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070075 }
76}
77
78bool SuperstructureActor::SuperstructureProfileDone() {
79 constexpr double kProfileError = 1e-2;
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 return ::std::abs(superstructure_status_fetcher_->intake()->goal_angle() -
Austin Schuh9481d0d2019-06-29 21:56:17 -070081 superstructure_goal_.intake) < kProfileError &&
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 ::std::abs(superstructure_status_fetcher_->shoulder()->goal_angle() -
Austin Schuh9481d0d2019-06-29 21:56:17 -070083 superstructure_goal_.shoulder) < kProfileError &&
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 ::std::abs(superstructure_status_fetcher_->intake()
85 ->goal_angular_velocity()) < kProfileError &&
86 ::std::abs(superstructure_status_fetcher_->shoulder()
87 ->goal_angular_velocity()) < kProfileError;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070088}
89
90bool SuperstructureActor::SuperstructureDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -070091 superstructure_status_fetcher_.Fetch();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070092
93 // We are no longer running if we are in the zeroing states (below 12), or
94 // estopped.
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 if (superstructure_status_fetcher_->state() < 12 ||
96 superstructure_status_fetcher_->state() == 16) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070097 AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070098 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +000099 }
100
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700101 if (SuperstructureProfileDone()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700102 AOS_LOG(DEBUG, "Profile done.\n");
103 return true;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700104 }
105 return false;
106}
107
108void SuperstructureActor::WaitForSuperstructure() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700109 WaitUntil(::std::bind(&SuperstructureActor::SuperstructureDone, this));
Comran Morshed2f7b4672016-01-23 14:27:34 +0000110}
111
Comran Morshed2f7b4672016-01-23 14:27:34 +0000112} // namespace actors
113} // namespace y2016