blob: 734512a133f678b1a65dbad161b99a2512f68c44 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace y2016::actors {
Comran Morshed2f7b4672016-01-23 14:27:34 +000012
Austin Schuh02bd83e2016-11-25 20:13:54 -080013namespace chrono = ::std::chrono;
14
Austin Schuh1bf8a212019-05-26 22:13:14 -070015SuperstructureActor::SuperstructureActor(::aos::EventLoop *event_loop)
Alex Perrycb7da4b2019-08-28 19:35:56 -070016 : aos::common::actions::ActorBase<superstructure_action::Goal>(
17 event_loop, "/superstructure_action"),
Austin Schuh9481d0d2019-06-29 21:56:17 -070018 superstructure_goal_sender_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070019 event_loop->MakeSender<::y2016::control_loops::superstructure::Goal>(
20 "/superstructure")),
Austin Schuh9481d0d2019-06-29 21:56:17 -070021 superstructure_status_fetcher_(
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 event_loop
23 ->MakeFetcher<::y2016::control_loops::superstructure::Status>(
Austin Schuh094d09b2020-11-20 23:26:52 -080024 "/superstructure")) {
25 event_loop->SetRuntimeRealtimePriority(29);
26}
Comran Morshed2f7b4672016-01-23 14:27:34 +000027
28bool SuperstructureActor::RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 const superstructure_action::SuperstructureActionParams *params) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070030 AOS_LOG(INFO, "Starting superstructure action\n");
Comran Morshed2f7b4672016-01-23 14:27:34 +000031
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 MoveSuperstructure(params->partial_angle(), params->shooter_angle(), false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070033 WaitForSuperstructure();
34 if (ShouldCancel()) return true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070035 MoveSuperstructure(params->partial_angle(), params->shooter_angle(), true);
Austin Schuh02bd83e2016-11-25 20:13:54 -080036 if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>(
Alex Perrycb7da4b2019-08-28 19:35:56 -070037 chrono::duration<double>(params->delay_time()))))
Austin Schuh02bd83e2016-11-25 20:13:54 -080038 return true;
Alex Perrycb7da4b2019-08-28 19:35:56 -070039 MoveSuperstructure(params->full_angle(), params->shooter_angle(), true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070040 WaitForSuperstructure();
41 if (ShouldCancel()) return true;
42 return true;
43}
44
Austin Schuhfef64ac2016-04-24 19:08:01 -070045void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter,
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070046 bool unfold_climber) {
Austin Schuhfef64ac2016-04-24 19:08:01 -070047 superstructure_goal_ = {0, shoulder, shooter};
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070048
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 auto builder = superstructure_goal_sender_.MakeBuilder();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070050
Alex Perrycb7da4b2019-08-28 19:35:56 -070051 control_loops::superstructure::Goal::Builder superstructure_goal_builder =
52 builder.MakeBuilder<control_loops::superstructure::Goal>();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070053
Alex Perrycb7da4b2019-08-28 19:35:56 -070054 superstructure_goal_builder.add_angle_intake(0);
55 superstructure_goal_builder.add_angle_shoulder(shoulder);
56 superstructure_goal_builder.add_angle_wrist(shooter);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070057
Alex Perrycb7da4b2019-08-28 19:35:56 -070058 superstructure_goal_builder.add_max_angular_velocity_intake(7.0);
59 superstructure_goal_builder.add_max_angular_velocity_shoulder(4.0);
60 superstructure_goal_builder.add_max_angular_velocity_wrist(10.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070061
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 superstructure_goal_builder.add_max_angular_acceleration_intake(40.0);
63 superstructure_goal_builder.add_max_angular_acceleration_shoulder(5.0);
64 superstructure_goal_builder.add_max_angular_acceleration_wrist(25.0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070065
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 superstructure_goal_builder.add_voltage_top_rollers(0);
67 superstructure_goal_builder.add_voltage_bottom_rollers(0);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070068
Alex Perrycb7da4b2019-08-28 19:35:56 -070069 superstructure_goal_builder.add_traverse_unlatched(true);
70 superstructure_goal_builder.add_traverse_down(false);
71 superstructure_goal_builder.add_voltage_climber(0.0);
72 superstructure_goal_builder.add_unfold_climber(unfold_climber);
73
milind1f1dca32021-07-03 13:50:07 -070074 if (builder.Send(superstructure_goal_builder.Finish()) !=
75 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070076 AOS_LOG(ERROR, "Sending superstructure move failed.\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070077 }
78}
79
80bool SuperstructureActor::SuperstructureProfileDone() {
81 constexpr double kProfileError = 1e-2;
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 return ::std::abs(superstructure_status_fetcher_->intake()->goal_angle() -
Austin Schuh9481d0d2019-06-29 21:56:17 -070083 superstructure_goal_.intake) < kProfileError &&
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 ::std::abs(superstructure_status_fetcher_->shoulder()->goal_angle() -
Austin Schuh9481d0d2019-06-29 21:56:17 -070085 superstructure_goal_.shoulder) < kProfileError &&
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 ::std::abs(superstructure_status_fetcher_->intake()
87 ->goal_angular_velocity()) < kProfileError &&
88 ::std::abs(superstructure_status_fetcher_->shoulder()
89 ->goal_angular_velocity()) < kProfileError;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070090}
91
92bool SuperstructureActor::SuperstructureDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -070093 superstructure_status_fetcher_.Fetch();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070094
95 // We are no longer running if we are in the zeroing states (below 12), or
96 // estopped.
Alex Perrycb7da4b2019-08-28 19:35:56 -070097 if (superstructure_status_fetcher_->state() < 12 ||
98 superstructure_status_fetcher_->state() == 16) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070099 AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700100 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +0000101 }
102
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700103 if (SuperstructureProfileDone()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700104 AOS_LOG(DEBUG, "Profile done.\n");
105 return true;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700106 }
107 return false;
108}
109
110void SuperstructureActor::WaitForSuperstructure() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700111 WaitUntil(::std::bind(&SuperstructureActor::SuperstructureDone, this));
Comran Morshed2f7b4672016-01-23 14:27:34 +0000112}
113
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800114} // namespace y2016::actors