blob: ae816d84e8f0564c4d51b3c851df7bd7d36329eb [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();
39 new_fridge_goal->max_velocity = elevator_parameters.velocity;
40 new_fridge_goal->max_acceleration = elevator_parameters.acceleration;
41 new_fridge_goal->height = height;
42 new_fridge_goal->velocity = 0.0;
43 new_fridge_goal->max_angular_velocity = arm_parameters.velocity;
44 new_fridge_goal->max_angular_acceleration = arm_parameters.acceleration;
45 new_fridge_goal->angle = angle;
46 new_fridge_goal->angular_velocity = 0.0;
47 new_fridge_goal->grabbers.top_front = top_grabbers;
48 new_fridge_goal->grabbers.top_back = top_grabbers;
49 new_fridge_goal->grabbers.bottom_front = front_grabbers;
50 new_fridge_goal->grabbers.bottom_back = back_grabbers;
Austin Schuhfa65bfd2015-03-14 21:10:44 -070051 LOG(INFO, "Starting profile to %f, %f\n", height, angle);
Austin Schuh6e242ac2015-03-07 17:08:21 -080052
53 if (!new_fridge_goal.Send()) {
54 LOG(ERROR, "Failed to send fridge goal\n");
Austin Schuhfa65bfd2015-03-14 21:10:44 -070055 return false;
56 }
57 return true;
58 }
59
60 enum ProfileStatus { RUNNING, DONE, CANCELING, CANCELED };
61
62 ProfileStatus IterateProfile(double height, double angle,
63 ProfileParams elevator_parameters,
64 ProfileParams arm_parameters, bool top_grabbers,
65 bool front_grabbers, bool back_grabbers) {
66 bool should_cancel = false;
67 if (this->ShouldCancel()) {
68 should_cancel = true;
69 LOG(INFO, "Canceling fridge movement\n");
70 auto new_fridge_goal = control_loops::fridge_queue.goal.MakeMessage();
71 new_fridge_goal->max_velocity = elevator_parameters.velocity;
72 new_fridge_goal->max_acceleration = elevator_parameters.acceleration;
73 new_fridge_goal->height =
74 control_loops::fridge_queue.status->height +
75 (control_loops::fridge_queue.status->goal_velocity *
76 ::std::abs(control_loops::fridge_queue.status->goal_velocity)) /
77 (2.0 * new_fridge_goal->max_acceleration);
78 new_fridge_goal->velocity = 0.0;
79 new_fridge_goal->max_angular_velocity = arm_parameters.velocity;
80 new_fridge_goal->max_angular_acceleration = arm_parameters.acceleration;
81 new_fridge_goal->angle =
82 control_loops::fridge_queue.status->angle +
83 (control_loops::fridge_queue.status->goal_angular_velocity *
84 ::std::abs(
85 control_loops::fridge_queue.status->goal_angular_velocity)) /
86 (2.0 * new_fridge_goal->max_angular_acceleration);
87 new_fridge_goal->angular_velocity = 0.0;
88 new_fridge_goal->grabbers.top_front = top_grabbers;
89 new_fridge_goal->grabbers.top_back = top_grabbers;
90 new_fridge_goal->grabbers.bottom_front = front_grabbers;
91 new_fridge_goal->grabbers.bottom_back = back_grabbers;
92
93 if (!new_fridge_goal.Send()) {
94 LOG(ERROR, "Failed to send fridge goal\n");
95 return CANCELED;
96 }
97 }
98 control_loops::fridge_queue.status.FetchAnother();
99
100 constexpr double kProfileError = 1e-5;
101 constexpr double kAngleEpsilon = 0.02, kHeightEpsilon = 0.015;
102
103 if (control_loops::fridge_queue.status->state != 4) {
104 LOG(ERROR, "Fridge no longer running, aborting action\n");
105 return CANCELED;
106 }
107
108 if (::std::abs(control_loops::fridge_queue.status->goal_angle - angle) <
109 kProfileError &&
110 ::std::abs(control_loops::fridge_queue.status->goal_height - height) <
111 kProfileError &&
112 ::std::abs(control_loops::fridge_queue.status->goal_angular_velocity) <
113 kProfileError &&
114 ::std::abs(control_loops::fridge_queue.status->goal_velocity) <
115 kProfileError) {
116 LOG(INFO, "Profile done.\n");
117 if (should_cancel) {
118 LOG(INFO, "Canceling.\n");
119 return CANCELED;
120 } else if (::std::abs(control_loops::fridge_queue.status->angle - angle) <
121 kAngleEpsilon &&
122 ::std::abs(control_loops::fridge_queue.status->height -
123 height) < kHeightEpsilon) {
124 LOG(INFO, "Near goal, done.\n");
125 return DONE;
126 }
127 }
128
129 if (should_cancel) {
130 return CANCELING;
131 } else {
132 return RUNNING;
133 }
134 }
135
136 void DoFridgeProfile(double height, double angle,
137 ProfileParams elevator_parameters,
138 ProfileParams arm_parameters, bool top_grabbers,
139 bool front_grabbers, bool back_grabbers) {
140 if (!StartFridgeProfile(height, angle, elevator_parameters, arm_parameters,
141 top_grabbers, front_grabbers, back_grabbers)) {
Austin Schuh6e242ac2015-03-07 17:08:21 -0800142 return;
143 }
144
145 while (true) {
Austin Schuhfa65bfd2015-03-14 21:10:44 -0700146 ProfileStatus status =
147 IterateProfile(height, angle, elevator_parameters, arm_parameters,
148 top_grabbers, front_grabbers, back_grabbers);
149 if (status == DONE || status == CANCELED) {
Austin Schuh6e242ac2015-03-07 17:08:21 -0800150 return;
151 }
Austin Schuh6e242ac2015-03-07 17:08:21 -0800152 }
153 }
Austin Schuh813b9af2015-03-08 18:46:58 -0700154
155 bool WaitOrCancel(::aos::time::Time duration) {
156 ::aos::time::Time end_time = ::aos::time::Time::Now() + duration;
157 while (::aos::time::Time::Now() <= end_time) {
158 ::aos::time::PhasedLoopXMS(::aos::controls::kLoopFrequency.ToMSec(),
159 2500);
160 if (this->ShouldCancel()) return false;
161 }
162 return true;
163 }
Austin Schuh6e242ac2015-03-07 17:08:21 -0800164};
165
166} // namespace actors
167} // namespace frc971
168
169#endif // FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_