blob: d8537ce974aecf8ca1320ec39f32e40ee34b22fd [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
Comran Morshed2f7b4672016-01-23 14:27:34 +000015SuperstructureActor::SuperstructureActor(
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070016 actors::SuperstructureActionQueueGroup *s)
Comran Morshed2f7b4672016-01-23 14:27:34 +000017 : aos::common::actions::ActorBase<actors::SuperstructureActionQueueGroup>(
18 s) {}
19
20bool SuperstructureActor::RunAction(
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070021 const actors::SuperstructureActionParams &params) {
22 LOG(INFO, "Starting superstructure action\n");
Comran Morshed2f7b4672016-01-23 14:27:34 +000023
Austin Schuhfef64ac2016-04-24 19:08:01 -070024 MoveSuperstructure(params.partial_angle, params.shooter_angle, false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070025 WaitForSuperstructure();
26 if (ShouldCancel()) return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -070027 MoveSuperstructure(params.partial_angle, params.shooter_angle, true);
Austin Schuh02bd83e2016-11-25 20:13:54 -080028 if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>(
29 chrono::duration<double>(params.delay_time))))
30 return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -070031 MoveSuperstructure(params.full_angle, params.shooter_angle, true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070032 WaitForSuperstructure();
33 if (ShouldCancel()) return true;
34 return true;
35}
36
Austin Schuhfef64ac2016-04-24 19:08:01 -070037void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter,
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070038 bool unfold_climber) {
Austin Schuhfef64ac2016-04-24 19:08:01 -070039 superstructure_goal_ = {0, shoulder, shooter};
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070040
41 auto new_superstructure_goal =
42 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
43
44 new_superstructure_goal->angle_intake = 0;
45 new_superstructure_goal->angle_shoulder = shoulder;
Austin Schuhfef64ac2016-04-24 19:08:01 -070046 new_superstructure_goal->angle_wrist = shooter;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070047
48 new_superstructure_goal->max_angular_velocity_intake = 7.0;
49 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
50 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
51
52 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
53 new_superstructure_goal->max_angular_acceleration_shoulder = 5.0;
54 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
55
56 new_superstructure_goal->voltage_top_rollers = 0;
57 new_superstructure_goal->voltage_bottom_rollers = 0;
58
59 new_superstructure_goal->traverse_unlatched = true;
60 new_superstructure_goal->traverse_down = false;
61 new_superstructure_goal->voltage_climber = 0.0;
62 new_superstructure_goal->unfold_climber = unfold_climber;
63
64 if (!new_superstructure_goal.Send()) {
65 LOG(ERROR, "Sending superstructure move failed.\n");
66 }
67}
68
69bool SuperstructureActor::SuperstructureProfileDone() {
70 constexpr double kProfileError = 1e-2;
71 return ::std::abs(
72 control_loops::superstructure_queue.status->intake.goal_angle -
73 superstructure_goal_.intake) < kProfileError &&
74 ::std::abs(
75 control_loops::superstructure_queue.status->shoulder.goal_angle -
76 superstructure_goal_.shoulder) < kProfileError &&
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070077 ::std::abs(control_loops::superstructure_queue.status->intake
78 .goal_angular_velocity) < kProfileError &&
79 ::std::abs(control_loops::superstructure_queue.status->shoulder
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070080 .goal_angular_velocity) < kProfileError;
81}
82
83bool SuperstructureActor::SuperstructureDone() {
84 control_loops::superstructure_queue.status.FetchAnother();
85
86 // We are no longer running if we are in the zeroing states (below 12), or
87 // estopped.
88 if (control_loops::superstructure_queue.status->state < 12 ||
89 control_loops::superstructure_queue.status->state == 16) {
90 LOG(ERROR, "Superstructure no longer running, aborting action\n");
91 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +000092 }
93
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070094 if (SuperstructureProfileDone()) {
95 LOG(DEBUG, "Profile done.\n");
96 return true;
97 }
98 return false;
99}
100
101void SuperstructureActor::WaitForSuperstructure() {
102 while (true) {
103 if (ShouldCancel()) return;
104 if (SuperstructureDone()) return;
105 }
Comran Morshed2f7b4672016-01-23 14:27:34 +0000106}
107
108::std::unique_ptr<SuperstructureAction> MakeSuperstructureAction(
109 const ::y2016::actors::SuperstructureActionParams& params) {
110 return ::std::unique_ptr<SuperstructureAction>(new SuperstructureAction(
111 &::y2016::actors::superstructure_action, params));
112}
113
114} // namespace actors
115} // namespace y2016