blob: c2d7c4dd49a060bf1b2e2daf61827a2a51cb0756 [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
Comran Morshed2f7b4672016-01-23 14:27:34 +00005#include "aos/common/util/phased_loop.h"
6#include "aos/common/logging/logging.h"
7#include "y2016/actors/superstructure_actor.h"
8#include "y2016/control_loops/superstructure/superstructure.q.h"
9
10namespace y2016 {
11namespace actors {
12
13SuperstructureActor::SuperstructureActor(
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070014 actors::SuperstructureActionQueueGroup *s)
Comran Morshed2f7b4672016-01-23 14:27:34 +000015 : aos::common::actions::ActorBase<actors::SuperstructureActionQueueGroup>(
16 s) {}
17
18bool SuperstructureActor::RunAction(
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070019 const actors::SuperstructureActionParams &params) {
20 LOG(INFO, "Starting superstructure action\n");
Comran Morshed2f7b4672016-01-23 14:27:34 +000021
Austin Schuhfef64ac2016-04-24 19:08:01 -070022 MoveSuperstructure(params.partial_angle, params.shooter_angle, false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070023 WaitForSuperstructure();
24 if (ShouldCancel()) return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -070025 MoveSuperstructure(params.partial_angle, params.shooter_angle, true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070026 if (!WaitOrCancel(::aos::time::Time::InSeconds(params.delay_time))) return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -070027 MoveSuperstructure(params.full_angle, params.shooter_angle, true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070028 WaitForSuperstructure();
29 if (ShouldCancel()) return true;
30 return true;
31}
32
Austin Schuhfef64ac2016-04-24 19:08:01 -070033void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter,
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070034 bool unfold_climber) {
Austin Schuhfef64ac2016-04-24 19:08:01 -070035 superstructure_goal_ = {0, shoulder, shooter};
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070036
37 auto new_superstructure_goal =
38 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
39
40 new_superstructure_goal->angle_intake = 0;
41 new_superstructure_goal->angle_shoulder = shoulder;
Austin Schuhfef64ac2016-04-24 19:08:01 -070042 new_superstructure_goal->angle_wrist = shooter;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070043
44 new_superstructure_goal->max_angular_velocity_intake = 7.0;
45 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
46 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
47
48 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
49 new_superstructure_goal->max_angular_acceleration_shoulder = 5.0;
50 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
51
52 new_superstructure_goal->voltage_top_rollers = 0;
53 new_superstructure_goal->voltage_bottom_rollers = 0;
54
55 new_superstructure_goal->traverse_unlatched = true;
56 new_superstructure_goal->traverse_down = false;
57 new_superstructure_goal->voltage_climber = 0.0;
58 new_superstructure_goal->unfold_climber = unfold_climber;
59
60 if (!new_superstructure_goal.Send()) {
61 LOG(ERROR, "Sending superstructure move failed.\n");
62 }
63}
64
65bool SuperstructureActor::SuperstructureProfileDone() {
66 constexpr double kProfileError = 1e-2;
67 return ::std::abs(
68 control_loops::superstructure_queue.status->intake.goal_angle -
69 superstructure_goal_.intake) < kProfileError &&
70 ::std::abs(
71 control_loops::superstructure_queue.status->shoulder.goal_angle -
72 superstructure_goal_.shoulder) < kProfileError &&
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070073 ::std::abs(control_loops::superstructure_queue.status->intake
74 .goal_angular_velocity) < kProfileError &&
75 ::std::abs(control_loops::superstructure_queue.status->shoulder
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070076 .goal_angular_velocity) < kProfileError;
77}
78
79bool SuperstructureActor::SuperstructureDone() {
80 control_loops::superstructure_queue.status.FetchAnother();
81
82 // We are no longer running if we are in the zeroing states (below 12), or
83 // estopped.
84 if (control_loops::superstructure_queue.status->state < 12 ||
85 control_loops::superstructure_queue.status->state == 16) {
86 LOG(ERROR, "Superstructure no longer running, aborting action\n");
87 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +000088 }
89
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070090 if (SuperstructureProfileDone()) {
91 LOG(DEBUG, "Profile done.\n");
92 return true;
93 }
94 return false;
95}
96
97void SuperstructureActor::WaitForSuperstructure() {
98 while (true) {
99 if (ShouldCancel()) return;
100 if (SuperstructureDone()) return;
101 }
Comran Morshed2f7b4672016-01-23 14:27:34 +0000102}
103
104::std::unique_ptr<SuperstructureAction> MakeSuperstructureAction(
105 const ::y2016::actors::SuperstructureActionParams& params) {
106 return ::std::unique_ptr<SuperstructureAction>(new SuperstructureAction(
107 &::y2016::actors::superstructure_action, params));
108}
109
110} // namespace actors
111} // namespace y2016