blob: 2875cbdaeb4022745c781548a9679a821f5be059 [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/util/phased_loop.h"
6#include "aos/logging/logging.h"
Comran Morshed2f7b4672016-01-23 14:27:34 +00007#include "y2016/actors/superstructure_actor.h"
8#include "y2016/control_loops/superstructure/superstructure.q.h"
9
10namespace y2016 {
11namespace actors {
12
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)
Comran Morshed2f7b4672016-01-23 14:27:34 +000016 : aos::common::actions::ActorBase<actors::SuperstructureActionQueueGroup>(
Austin Schuh9481d0d2019-06-29 21:56:17 -070017 event_loop, ".y2016.actors.superstructure_action"),
18 superstructure_goal_sender_(
19 event_loop
20 ->MakeSender<::y2016::control_loops::SuperstructureQueue::Goal>(
21 ".y2016.control_loops.superstructure_queue.goal")),
22 superstructure_status_fetcher_(
23 event_loop->MakeFetcher<
24 ::y2016::control_loops::SuperstructureQueue::Status>(
25 ".y2016.control_loops.superstructure_queue.status")) {}
Comran Morshed2f7b4672016-01-23 14:27:34 +000026
27bool SuperstructureActor::RunAction(
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070028 const actors::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
Austin Schuhfef64ac2016-04-24 19:08:01 -070031 MoveSuperstructure(params.partial_angle, params.shooter_angle, false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070032 WaitForSuperstructure();
33 if (ShouldCancel()) return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -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>(
36 chrono::duration<double>(params.delay_time))))
37 return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -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
Austin Schuh9481d0d2019-06-29 21:56:17 -070048 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070049
50 new_superstructure_goal->angle_intake = 0;
51 new_superstructure_goal->angle_shoulder = shoulder;
Austin Schuhfef64ac2016-04-24 19:08:01 -070052 new_superstructure_goal->angle_wrist = shooter;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070053
54 new_superstructure_goal->max_angular_velocity_intake = 7.0;
55 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
56 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
57
58 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
59 new_superstructure_goal->max_angular_acceleration_shoulder = 5.0;
60 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
61
62 new_superstructure_goal->voltage_top_rollers = 0;
63 new_superstructure_goal->voltage_bottom_rollers = 0;
64
65 new_superstructure_goal->traverse_unlatched = true;
66 new_superstructure_goal->traverse_down = false;
67 new_superstructure_goal->voltage_climber = 0.0;
68 new_superstructure_goal->unfold_climber = unfold_climber;
69
70 if (!new_superstructure_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070071 AOS_LOG(ERROR, "Sending superstructure move failed.\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070072 }
73}
74
75bool SuperstructureActor::SuperstructureProfileDone() {
76 constexpr double kProfileError = 1e-2;
Austin Schuh9481d0d2019-06-29 21:56:17 -070077 return ::std::abs(superstructure_status_fetcher_->intake.goal_angle -
78 superstructure_goal_.intake) < kProfileError &&
79 ::std::abs(superstructure_status_fetcher_->shoulder.goal_angle -
80 superstructure_goal_.shoulder) < kProfileError &&
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070081 ::std::abs(
Austin Schuh9481d0d2019-06-29 21:56:17 -070082 superstructure_status_fetcher_->intake.goal_angular_velocity) <
83 kProfileError &&
84 ::std::abs(
85 superstructure_status_fetcher_->shoulder.goal_angular_velocity) <
86 kProfileError;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070087}
88
89bool SuperstructureActor::SuperstructureDone() {
Austin Schuh9481d0d2019-06-29 21:56:17 -070090 superstructure_status_fetcher_.Fetch();
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070091
92 // We are no longer running if we are in the zeroing states (below 12), or
93 // estopped.
Austin Schuh9481d0d2019-06-29 21:56:17 -070094 if (superstructure_status_fetcher_->state < 12 ||
95 superstructure_status_fetcher_->state == 16) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070096 AOS_LOG(ERROR, "Superstructure no longer running, aborting action\n");
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070097 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +000098 }
99
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700100 if (SuperstructureProfileDone()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700101 AOS_LOG(DEBUG, "Profile done.\n");
102 return true;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -0700103 }
104 return false;
105}
106
107void SuperstructureActor::WaitForSuperstructure() {
Austin Schuh9481d0d2019-06-29 21:56:17 -0700108 WaitUntil(::std::bind(&SuperstructureActor::SuperstructureDone, this));
Comran Morshed2f7b4672016-01-23 14:27:34 +0000109}
110
Comran Morshed2f7b4672016-01-23 14:27:34 +0000111} // namespace actors
112} // namespace y2016