blob: a86881d34a6c2e91c5ad8f09b980ce0b1435bc2f [file] [log] [blame]
Austin Schuh6e242ac2015-03-07 17:08:21 -08001#ifndef FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_
2#define FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_
3
4#include <cmath>
5
Austin Schuh813b9af2015-03-08 18:46:58 -07006#include "aos/common/controls/control_loop.h"
Austin Schuh6e242ac2015-03-07 17:08:21 -08007#include "aos/common/actions/actor.h"
Austin Schuh813b9af2015-03-08 18:46:58 -07008#include "aos/common/util/phased_loop.h"
Austin Schuh6e242ac2015-03-07 17:08:21 -08009#include "frc971/control_loops/fridge/fridge.q.h"
10
11namespace frc971 {
12namespace actors {
13
14struct ProfileParams {
15 double velocity;
16 double acceleration;
17};
18
19// Base class to provide helper utilities to all Actors who want to control the
20// fridge.
21template <typename T>
22class FridgeActorBase : public aos::common::actions::ActorBase<T> {
23 public:
24 FridgeActorBase(T *queues) : aos::common::actions::ActorBase<T>(queues) {}
25
26 protected:
27 void DoFridgeProfile(double height, double angle,
28 ProfileParams elevator_parameters,
29 ProfileParams arm_parameters, bool grabbers) {
30 DoFridgeProfile(height, angle, elevator_parameters, arm_parameters,
31 grabbers, grabbers, grabbers);
32 }
33
Austin Schuhfa65bfd2015-03-14 21:10:44 -070034 bool StartFridgeProfile(double height, double angle,
35 ProfileParams elevator_parameters,
36 ProfileParams arm_parameters, bool top_grabbers,
37 bool front_grabbers, bool back_grabbers) {
Austin Schuh6e242ac2015-03-07 17:08:21 -080038 auto new_fridge_goal = control_loops::fridge_queue.goal.MakeMessage();
Austin Schuh1d44bd42015-03-15 16:40:45 -070039 new_fridge_goal->profiling_type = 0;
Austin Schuh6e242ac2015-03-07 17:08:21 -080040 new_fridge_goal->max_velocity = elevator_parameters.velocity;
41 new_fridge_goal->max_acceleration = elevator_parameters.acceleration;
42 new_fridge_goal->height = height;
43 new_fridge_goal->velocity = 0.0;
44 new_fridge_goal->max_angular_velocity = arm_parameters.velocity;
45 new_fridge_goal->max_angular_acceleration = arm_parameters.acceleration;
46 new_fridge_goal->angle = angle;
47 new_fridge_goal->angular_velocity = 0.0;
48 new_fridge_goal->grabbers.top_front = top_grabbers;
49 new_fridge_goal->grabbers.top_back = top_grabbers;
50 new_fridge_goal->grabbers.bottom_front = front_grabbers;
51 new_fridge_goal->grabbers.bottom_back = back_grabbers;
Austin Schuhfa65bfd2015-03-14 21:10:44 -070052 LOG(INFO, "Starting profile to %f, %f\n", height, angle);
Austin Schuh6e242ac2015-03-07 17:08:21 -080053
54 if (!new_fridge_goal.Send()) {
55 LOG(ERROR, "Failed to send fridge goal\n");
Austin Schuhfa65bfd2015-03-14 21:10:44 -070056 return false;
57 }
58 return true;
59 }
60
61 enum ProfileStatus { RUNNING, DONE, CANCELING, CANCELED };
62
63 ProfileStatus IterateProfile(double height, double angle,
64 ProfileParams elevator_parameters,
65 ProfileParams arm_parameters, bool top_grabbers,
66 bool front_grabbers, bool back_grabbers) {
67 bool should_cancel = false;
68 if (this->ShouldCancel()) {
69 should_cancel = true;
70 LOG(INFO, "Canceling fridge movement\n");
71 auto new_fridge_goal = control_loops::fridge_queue.goal.MakeMessage();
Austin Schuh1d44bd42015-03-15 16:40:45 -070072 new_fridge_goal->profiling_type = 0;
Austin Schuhfa65bfd2015-03-14 21:10:44 -070073 new_fridge_goal->max_velocity = elevator_parameters.velocity;
74 new_fridge_goal->max_acceleration = elevator_parameters.acceleration;
75 new_fridge_goal->height =
76 control_loops::fridge_queue.status->height +
77 (control_loops::fridge_queue.status->goal_velocity *
78 ::std::abs(control_loops::fridge_queue.status->goal_velocity)) /
79 (2.0 * new_fridge_goal->max_acceleration);
80 new_fridge_goal->velocity = 0.0;
81 new_fridge_goal->max_angular_velocity = arm_parameters.velocity;
82 new_fridge_goal->max_angular_acceleration = arm_parameters.acceleration;
83 new_fridge_goal->angle =
84 control_loops::fridge_queue.status->angle +
85 (control_loops::fridge_queue.status->goal_angular_velocity *
86 ::std::abs(
87 control_loops::fridge_queue.status->goal_angular_velocity)) /
88 (2.0 * new_fridge_goal->max_angular_acceleration);
89 new_fridge_goal->angular_velocity = 0.0;
90 new_fridge_goal->grabbers.top_front = top_grabbers;
91 new_fridge_goal->grabbers.top_back = top_grabbers;
92 new_fridge_goal->grabbers.bottom_front = front_grabbers;
93 new_fridge_goal->grabbers.bottom_back = back_grabbers;
94
95 if (!new_fridge_goal.Send()) {
96 LOG(ERROR, "Failed to send fridge goal\n");
97 return CANCELED;
98 }
99 }
100 control_loops::fridge_queue.status.FetchAnother();
101
102 constexpr double kProfileError = 1e-5;
103 constexpr double kAngleEpsilon = 0.02, kHeightEpsilon = 0.015;
104
105 if (control_loops::fridge_queue.status->state != 4) {
106 LOG(ERROR, "Fridge no longer running, aborting action\n");
107 return CANCELED;
108 }
109
110 if (::std::abs(control_loops::fridge_queue.status->goal_angle - angle) <
111 kProfileError &&
112 ::std::abs(control_loops::fridge_queue.status->goal_height - height) <
113 kProfileError &&
114 ::std::abs(control_loops::fridge_queue.status->goal_angular_velocity) <
115 kProfileError &&
116 ::std::abs(control_loops::fridge_queue.status->goal_velocity) <
117 kProfileError) {
118 LOG(INFO, "Profile done.\n");
119 if (should_cancel) {
120 LOG(INFO, "Canceling.\n");
121 return CANCELED;
122 } else if (::std::abs(control_loops::fridge_queue.status->angle - angle) <
123 kAngleEpsilon &&
124 ::std::abs(control_loops::fridge_queue.status->height -
125 height) < kHeightEpsilon) {
126 LOG(INFO, "Near goal, done.\n");
127 return DONE;
128 }
129 }
130
131 if (should_cancel) {
132 return CANCELING;
133 } else {
134 return RUNNING;
135 }
136 }
137
138 void DoFridgeProfile(double height, double angle,
139 ProfileParams elevator_parameters,
140 ProfileParams arm_parameters, bool top_grabbers,
141 bool front_grabbers, bool back_grabbers) {
142 if (!StartFridgeProfile(height, angle, elevator_parameters, arm_parameters,
143 top_grabbers, front_grabbers, back_grabbers)) {
Austin Schuh6e242ac2015-03-07 17:08:21 -0800144 return;
145 }
146
147 while (true) {
Austin Schuhfa65bfd2015-03-14 21:10:44 -0700148 ProfileStatus status =
149 IterateProfile(height, angle, elevator_parameters, arm_parameters,
150 top_grabbers, front_grabbers, back_grabbers);
151 if (status == DONE || status == CANCELED) {
Austin Schuh6e242ac2015-03-07 17:08:21 -0800152 return;
153 }
Austin Schuh6e242ac2015-03-07 17:08:21 -0800154 }
155 }
Austin Schuh813b9af2015-03-08 18:46:58 -0700156
157 bool WaitOrCancel(::aos::time::Time duration) {
158 ::aos::time::Time end_time = ::aos::time::Time::Now() + duration;
159 while (::aos::time::Time::Now() <= end_time) {
160 ::aos::time::PhasedLoopXMS(::aos::controls::kLoopFrequency.ToMSec(),
161 2500);
162 if (this->ShouldCancel()) return false;
163 }
164 return true;
165 }
Austin Schuh6e242ac2015-03-07 17:08:21 -0800166};
167
168} // namespace actors
169} // namespace frc971
170
171#endif // FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_