blob: f65d05aa7a2a760a0b3c21196c01056f4c7618af [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 Schuh1bf8a212019-05-26 22:13:14 -070017 event_loop, ".y2016.actors.superstructure_action") {}
Comran Morshed2f7b4672016-01-23 14:27:34 +000018
19bool SuperstructureActor::RunAction(
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070020 const actors::SuperstructureActionParams &params) {
21 LOG(INFO, "Starting superstructure action\n");
Comran Morshed2f7b4672016-01-23 14:27:34 +000022
Austin Schuhfef64ac2016-04-24 19:08:01 -070023 MoveSuperstructure(params.partial_angle, params.shooter_angle, false);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070024 WaitForSuperstructure();
25 if (ShouldCancel()) return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -070026 MoveSuperstructure(params.partial_angle, params.shooter_angle, true);
Austin Schuh02bd83e2016-11-25 20:13:54 -080027 if (!WaitOrCancel(chrono::duration_cast<::aos::monotonic_clock::duration>(
28 chrono::duration<double>(params.delay_time))))
29 return true;
Austin Schuhfef64ac2016-04-24 19:08:01 -070030 MoveSuperstructure(params.full_angle, params.shooter_angle, true);
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070031 WaitForSuperstructure();
32 if (ShouldCancel()) return true;
33 return true;
34}
35
Austin Schuhfef64ac2016-04-24 19:08:01 -070036void SuperstructureActor::MoveSuperstructure(double shoulder, double shooter,
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070037 bool unfold_climber) {
Austin Schuhfef64ac2016-04-24 19:08:01 -070038 superstructure_goal_ = {0, shoulder, shooter};
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070039
40 auto new_superstructure_goal =
41 ::y2016::control_loops::superstructure_queue.goal.MakeMessage();
42
43 new_superstructure_goal->angle_intake = 0;
44 new_superstructure_goal->angle_shoulder = shoulder;
Austin Schuhfef64ac2016-04-24 19:08:01 -070045 new_superstructure_goal->angle_wrist = shooter;
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070046
47 new_superstructure_goal->max_angular_velocity_intake = 7.0;
48 new_superstructure_goal->max_angular_velocity_shoulder = 4.0;
49 new_superstructure_goal->max_angular_velocity_wrist = 10.0;
50
51 new_superstructure_goal->max_angular_acceleration_intake = 40.0;
52 new_superstructure_goal->max_angular_acceleration_shoulder = 5.0;
53 new_superstructure_goal->max_angular_acceleration_wrist = 25.0;
54
55 new_superstructure_goal->voltage_top_rollers = 0;
56 new_superstructure_goal->voltage_bottom_rollers = 0;
57
58 new_superstructure_goal->traverse_unlatched = true;
59 new_superstructure_goal->traverse_down = false;
60 new_superstructure_goal->voltage_climber = 0.0;
61 new_superstructure_goal->unfold_climber = unfold_climber;
62
63 if (!new_superstructure_goal.Send()) {
64 LOG(ERROR, "Sending superstructure move failed.\n");
65 }
66}
67
68bool SuperstructureActor::SuperstructureProfileDone() {
69 constexpr double kProfileError = 1e-2;
70 return ::std::abs(
71 control_loops::superstructure_queue.status->intake.goal_angle -
72 superstructure_goal_.intake) < kProfileError &&
73 ::std::abs(
74 control_loops::superstructure_queue.status->shoulder.goal_angle -
75 superstructure_goal_.shoulder) < kProfileError &&
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070076 ::std::abs(control_loops::superstructure_queue.status->intake
77 .goal_angular_velocity) < kProfileError &&
78 ::std::abs(control_loops::superstructure_queue.status->shoulder
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070079 .goal_angular_velocity) < kProfileError;
80}
81
82bool SuperstructureActor::SuperstructureDone() {
83 control_loops::superstructure_queue.status.FetchAnother();
84
85 // We are no longer running if we are in the zeroing states (below 12), or
86 // estopped.
87 if (control_loops::superstructure_queue.status->state < 12 ||
88 control_loops::superstructure_queue.status->state == 16) {
89 LOG(ERROR, "Superstructure no longer running, aborting action\n");
90 return true;
Comran Morshed2f7b4672016-01-23 14:27:34 +000091 }
92
Diana Vandenberg9cc9ab62016-04-20 21:27:47 -070093 if (SuperstructureProfileDone()) {
94 LOG(DEBUG, "Profile done.\n");
95 return true;
96 }
97 return false;
98}
99
100void SuperstructureActor::WaitForSuperstructure() {
101 while (true) {
102 if (ShouldCancel()) return;
103 if (SuperstructureDone()) return;
104 }
Comran Morshed2f7b4672016-01-23 14:27:34 +0000105}
106
Comran Morshed2f7b4672016-01-23 14:27:34 +0000107} // namespace actors
108} // namespace y2016