Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/controls/control_loops.q.h" |
| 4 | #include "frc971/control_loops/control_loops.q.h" |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 5 | #include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h" |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 6 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 7 | #include "y2019/status_light.q.h" |
| 8 | |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 9 | namespace y2019 { |
| 10 | namespace control_loops { |
| 11 | namespace superstructure { |
| 12 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 13 | namespace { |
| 14 | |
| 15 | void SendColors(float red, float green, float blue) { |
| 16 | auto new_status_light = status_light.MakeMessage(); |
| 17 | new_status_light->red = red; |
| 18 | new_status_light->green = green; |
| 19 | new_status_light->blue = blue; |
| 20 | |
| 21 | if (!new_status_light.Send()) { |
| 22 | LOG(ERROR, "Failed to send lights.\n"); |
| 23 | } |
| 24 | } |
| 25 | } // namespace |
| 26 | |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 27 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 28 | const ::std::string &name) |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 29 | : aos::controls::ControlLoop<SuperstructureQueue>(event_loop, name), |
| 30 | elevator_(constants::GetValues().elevator.subsystem_params), |
| 31 | wrist_(constants::GetValues().wrist.subsystem_params), |
| 32 | intake_(constants::GetValues().intake), |
| 33 | stilts_(constants::GetValues().stilts.subsystem_params) {} |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 34 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 35 | void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal, |
| 36 | const SuperstructureQueue::Position *position, |
| 37 | SuperstructureQueue::Output *output, |
| 38 | SuperstructureQueue::Status *status) { |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 39 | if (WasReset()) { |
| 40 | LOG(ERROR, "WPILib reset, restarting\n"); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 41 | elevator_.Reset(); |
| 42 | wrist_.Reset(); |
| 43 | intake_.Reset(); |
| 44 | stilts_.Reset(); |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 45 | } |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 46 | |
| 47 | elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr, |
| 48 | &(position->elevator), |
| 49 | output != nullptr ? &(output->elevator_voltage) : nullptr, |
| 50 | &(status->elevator)); |
| 51 | |
| 52 | wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr, |
| 53 | &(position->wrist), |
| 54 | output != nullptr ? &(output->wrist_voltage) : nullptr, |
| 55 | &(status->wrist)); |
| 56 | |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 57 | intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr, |
| 58 | &(position->intake_joint), |
| 59 | output != nullptr ? &(output->intake_joint_voltage) : nullptr, |
| 60 | &(status->intake)); |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 61 | |
| 62 | stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr, |
| 63 | &(position->stilts), |
| 64 | output != nullptr ? &(output->stilts_voltage) : nullptr, |
| 65 | &(status->stilts)); |
| 66 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 67 | vacuum_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr, |
| 68 | position->suction_pressure, output, &(status->has_piece), |
| 69 | event_loop()); |
Austin Schuh | e835475 | 2019-02-17 14:59:05 -0800 | [diff] [blame] | 70 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 71 | status->zeroed = status->elevator.zeroed && status->wrist.zeroed && |
| 72 | status->intake.zeroed && status->stilts.zeroed; |
| 73 | |
| 74 | status->estopped = status->elevator.estopped || status->wrist.estopped || |
| 75 | status->intake.estopped || status->stilts.estopped; |
| 76 | |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 77 | if (output) { |
Austin Schuh | 85e2e91 | 2019-02-17 15:04:29 -0800 | [diff] [blame] | 78 | if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) { |
| 79 | output->intake_roller_voltage = unsafe_goal->roller_voltage; |
Theo Bafrali | 09517b9 | 2019-02-16 15:59:17 -0800 | [diff] [blame] | 80 | } else { |
| 81 | output->intake_roller_voltage = 0.0; |
| 82 | } |
| 83 | } |
| 84 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 85 | // TODO(theo) move these up when Iterate() is split |
| 86 | // update the goals |
| 87 | collision_avoidance_.UpdateGoal(status, unsafe_goal); |
| 88 | |
| 89 | elevator_.set_min_position(collision_avoidance_.min_elevator_goal()); |
| 90 | wrist_.set_min_position(collision_avoidance_.min_wrist_goal()); |
| 91 | wrist_.set_max_position(collision_avoidance_.max_wrist_goal()); |
| 92 | intake_.set_min_position(collision_avoidance_.min_intake_goal()); |
| 93 | intake_.set_max_position(collision_avoidance_.max_intake_goal()); |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame^] | 94 | |
| 95 | if (status && unsafe_goal) { |
| 96 | // Light Logic |
| 97 | if (status->estopped) { |
| 98 | // Estop is red |
| 99 | SendColors(0.5, 0.0, 0.0); |
| 100 | } else if (unsafe_goal->suction.gamepiece_mode == 0) { |
| 101 | // Ball mode is blue |
| 102 | SendColors(0.0, 0.0, 0.5); |
| 103 | } else if (unsafe_goal->suction.gamepiece_mode == 1) { |
| 104 | // Disk mode is yellow |
| 105 | SendColors(0.5, 0.5, 0.0); |
| 106 | } else { |
| 107 | SendColors(0.0, 0.0, 0.0); |
| 108 | } |
| 109 | } |
Tyler Chatow | e51334a | 2019-01-20 16:58:16 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace superstructure |
| 113 | } // namespace control_loops |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 114 | } // namespace y2019 |