Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 1 | #ifndef FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_ |
| 2 | #define FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_ |
| 3 | |
| 4 | #include <cmath> |
| 5 | |
| 6 | #include "aos/common/actions/actor.h" |
Austin Schuh | 813b9af | 2015-03-08 18:46:58 -0700 | [diff] [blame] | 7 | #include "aos/common/util/phased_loop.h" |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 8 | #include "frc971/control_loops/fridge/fridge.q.h" |
| 9 | |
| 10 | namespace frc971 { |
| 11 | namespace actors { |
| 12 | |
| 13 | struct ProfileParams { |
| 14 | double velocity; |
| 15 | double acceleration; |
| 16 | }; |
| 17 | |
| 18 | // Base class to provide helper utilities to all Actors who want to control the |
| 19 | // fridge. |
| 20 | template <typename T> |
| 21 | class FridgeActorBase : public aos::common::actions::ActorBase<T> { |
| 22 | public: |
| 23 | FridgeActorBase(T *queues) : aos::common::actions::ActorBase<T>(queues) {} |
| 24 | |
| 25 | protected: |
| 26 | void DoFridgeProfile(double height, double angle, |
| 27 | ProfileParams elevator_parameters, |
| 28 | ProfileParams arm_parameters, bool grabbers) { |
| 29 | DoFridgeProfile(height, angle, elevator_parameters, arm_parameters, |
| 30 | grabbers, grabbers, grabbers); |
| 31 | } |
| 32 | |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 33 | bool StartFridgeProfile(double height, double angle, |
| 34 | ProfileParams elevator_parameters, |
| 35 | ProfileParams arm_parameters, bool top_grabbers, |
| 36 | bool front_grabbers, bool back_grabbers) { |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 37 | auto new_fridge_goal = control_loops::fridge_queue.goal.MakeMessage(); |
Austin Schuh | 1d44bd4 | 2015-03-15 16:40:45 -0700 | [diff] [blame] | 38 | new_fridge_goal->profiling_type = 0; |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 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 Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 51 | LOG(INFO, "Starting profile to %f, %f\n", height, angle); |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 52 | |
| 53 | if (!new_fridge_goal.Send()) { |
| 54 | LOG(ERROR, "Failed to send fridge goal\n"); |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
Brian Silverman | ed86fc1 | 2015-03-19 23:37:35 -0700 | [diff] [blame] | 60 | enum ProfileStatus { RUNNING, DONE, CANCELED }; |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 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) { |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 66 | if (this->ShouldCancel()) { |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 67 | LOG(INFO, "Canceling fridge movement\n"); |
Brian Silverman | f1c5e7d | 2015-03-28 18:25:25 -0400 | [diff] [blame] | 68 | if (!control_loops::fridge_queue.status.get()) { |
| 69 | LOG(WARNING, "no fridge status so can't really cancel\n"); |
| 70 | return CANCELED; |
| 71 | } |
| 72 | |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 73 | auto new_fridge_goal = control_loops::fridge_queue.goal.MakeMessage(); |
Austin Schuh | 1d44bd4 | 2015-03-15 16:40:45 -0700 | [diff] [blame] | 74 | new_fridge_goal->profiling_type = 0; |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 75 | new_fridge_goal->max_velocity = elevator_parameters.velocity; |
| 76 | new_fridge_goal->max_acceleration = elevator_parameters.acceleration; |
| 77 | new_fridge_goal->height = |
| 78 | control_loops::fridge_queue.status->height + |
| 79 | (control_loops::fridge_queue.status->goal_velocity * |
| 80 | ::std::abs(control_loops::fridge_queue.status->goal_velocity)) / |
| 81 | (2.0 * new_fridge_goal->max_acceleration); |
Austin Schuh | 511e865 | 2015-03-15 17:53:31 -0700 | [diff] [blame] | 82 | height = new_fridge_goal->height; |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 83 | new_fridge_goal->velocity = 0.0; |
| 84 | new_fridge_goal->max_angular_velocity = arm_parameters.velocity; |
| 85 | new_fridge_goal->max_angular_acceleration = arm_parameters.acceleration; |
| 86 | new_fridge_goal->angle = |
| 87 | control_loops::fridge_queue.status->angle + |
| 88 | (control_loops::fridge_queue.status->goal_angular_velocity * |
| 89 | ::std::abs( |
| 90 | control_loops::fridge_queue.status->goal_angular_velocity)) / |
| 91 | (2.0 * new_fridge_goal->max_angular_acceleration); |
Austin Schuh | 511e865 | 2015-03-15 17:53:31 -0700 | [diff] [blame] | 92 | angle = new_fridge_goal->angle; |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 93 | new_fridge_goal->angular_velocity = 0.0; |
| 94 | new_fridge_goal->grabbers.top_front = top_grabbers; |
| 95 | new_fridge_goal->grabbers.top_back = top_grabbers; |
| 96 | new_fridge_goal->grabbers.bottom_front = front_grabbers; |
| 97 | new_fridge_goal->grabbers.bottom_back = back_grabbers; |
| 98 | |
| 99 | if (!new_fridge_goal.Send()) { |
| 100 | LOG(ERROR, "Failed to send fridge goal\n"); |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 101 | } |
Brian Silverman | ed86fc1 | 2015-03-19 23:37:35 -0700 | [diff] [blame] | 102 | return CANCELED; |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 103 | } |
| 104 | control_loops::fridge_queue.status.FetchAnother(); |
| 105 | |
| 106 | constexpr double kProfileError = 1e-5; |
| 107 | constexpr double kAngleEpsilon = 0.02, kHeightEpsilon = 0.015; |
| 108 | |
| 109 | if (control_loops::fridge_queue.status->state != 4) { |
| 110 | LOG(ERROR, "Fridge no longer running, aborting action\n"); |
| 111 | return CANCELED; |
| 112 | } |
| 113 | |
| 114 | if (::std::abs(control_loops::fridge_queue.status->goal_angle - angle) < |
| 115 | kProfileError && |
| 116 | ::std::abs(control_loops::fridge_queue.status->goal_height - height) < |
| 117 | kProfileError && |
| 118 | ::std::abs(control_loops::fridge_queue.status->goal_angular_velocity) < |
| 119 | kProfileError && |
| 120 | ::std::abs(control_loops::fridge_queue.status->goal_velocity) < |
| 121 | kProfileError) { |
| 122 | LOG(INFO, "Profile done.\n"); |
Brian Silverman | ed86fc1 | 2015-03-19 23:37:35 -0700 | [diff] [blame] | 123 | if (::std::abs(control_loops::fridge_queue.status->angle - angle) < |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 124 | kAngleEpsilon && |
| 125 | ::std::abs(control_loops::fridge_queue.status->height - |
| 126 | height) < kHeightEpsilon) { |
| 127 | LOG(INFO, "Near goal, done.\n"); |
| 128 | return DONE; |
| 129 | } |
| 130 | } |
| 131 | |
Brian Silverman | ed86fc1 | 2015-03-19 23:37:35 -0700 | [diff] [blame] | 132 | return RUNNING; |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void DoFridgeProfile(double height, double angle, |
| 136 | ProfileParams elevator_parameters, |
| 137 | ProfileParams arm_parameters, bool top_grabbers, |
| 138 | bool front_grabbers, bool back_grabbers) { |
| 139 | if (!StartFridgeProfile(height, angle, elevator_parameters, arm_parameters, |
| 140 | top_grabbers, front_grabbers, back_grabbers)) { |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | while (true) { |
Austin Schuh | fa65bfd | 2015-03-14 21:10:44 -0700 | [diff] [blame] | 145 | ProfileStatus status = |
| 146 | IterateProfile(height, angle, elevator_parameters, arm_parameters, |
| 147 | top_grabbers, front_grabbers, back_grabbers); |
| 148 | if (status == DONE || status == CANCELED) { |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 149 | return; |
| 150 | } |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace actors |
| 156 | } // namespace frc971 |
| 157 | |
| 158 | #endif // FRC971_ACTORS_FRIDGE_PROFILE_LIB_H_ |