blob: 3b0c25818b99ca69d3aafec83aa38be3b66bf242 [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
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070022 MoveSuperstructure(params.partial_angle, false);
23 WaitForSuperstructure();
24 if (ShouldCancel()) return true;
25 MoveSuperstructure(params.partial_angle, true);
26 if (!WaitOrCancel(::aos::time::Time::InSeconds(params.delay_time))) return true;
27 MoveSuperstructure(params.full_angle, true);
28 WaitForSuperstructure();
29 if (ShouldCancel()) return true;
30 return true;
31}
32
33void SuperstructureActor::MoveSuperstructure(double shoulder,
34 bool unfold_climber) {
35 superstructure_goal_ = {0, shoulder, 0};
36
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;
42 new_superstructure_goal->angle_wrist = 0;
43
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 &&
73 ::std::abs(
74 control_loops::superstructure_queue.status->wrist.goal_angle -
75 superstructure_goal_.wrist) < kProfileError &&
76 ::std::abs(control_loops::superstructure_queue.status->intake
77 .goal_angular_velocity) < kProfileError &&
78 ::std::abs(control_loops::superstructure_queue.status->shoulder
79 .goal_angular_velocity) < kProfileError &&
80 ::std::abs(control_loops::superstructure_queue.status->wrist
81 .goal_angular_velocity) < kProfileError;
82}
83
84bool SuperstructureActor::SuperstructureDone() {
85 control_loops::superstructure_queue.status.FetchAnother();
86
87 // We are no longer running if we are in the zeroing states (below 12), or
88 // estopped.
89 if (control_loops::superstructure_queue.status->state < 12 ||
90 control_loops::superstructure_queue.status->state == 16) {
91 LOG(ERROR, "Superstructure no longer running, aborting action\n");
92 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +000093 }
94
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070095 if (SuperstructureProfileDone()) {
96 LOG(DEBUG, "Profile done.\n");
97 return true;
98 }
99 return false;
100}
101
102void SuperstructureActor::WaitForSuperstructure() {
103 while (true) {
104 if (ShouldCancel()) return;
105 if (SuperstructureDone()) return;
106 }
Comran Morshed2f7b4672016-01-23 14:27:34 +0000107}
108
109::std::unique_ptr<SuperstructureAction> MakeSuperstructureAction(
110 const ::y2016::actors::SuperstructureActionParams& params) {
111 return ::std::unique_ptr<SuperstructureAction>(new SuperstructureAction(
112 &::y2016::actors::superstructure_action, params));
113}
114
115} // namespace actors
116} // namespace y2016