Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/events/event_loop.h" |
| 4 | |
| 5 | namespace y2020 { |
| 6 | namespace control_loops { |
| 7 | namespace superstructure { |
| 8 | |
| 9 | using frc971::control_loops::AbsoluteEncoderProfiledJointStatus; |
| 10 | using frc971::control_loops::PotAndAbsoluteEncoderProfiledJointStatus; |
| 11 | |
| 12 | Superstructure::Superstructure(::aos::EventLoop *event_loop, |
| 13 | const ::std::string &name) |
| 14 | : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 15 | name), |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 16 | hood_(constants::GetValues().hood), |
| 17 | intake_joint_(constants::GetValues().intake) { |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 18 | event_loop->SetRuntimeRealtimePriority(30); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 21 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 22 | const Position *position, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 23 | aos::Sender<Output>::Builder *output, |
| 24 | aos::Sender<Status>::Builder *status) { |
| 25 | if (WasReset()) { |
| 26 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 27 | hood_.Reset(); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 28 | intake_joint_.Reset(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 31 | OutputT output_struct; |
| 32 | |
| 33 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset = |
| 34 | hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr, |
| 35 | position->hood(), |
| 36 | output != nullptr ? &(output_struct.hood_voltage) : nullptr, |
| 37 | status->fbb()); |
| 38 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 39 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> intake_status_offset = |
| 40 | intake_joint_.Iterate( |
| 41 | unsafe_goal != nullptr ? unsafe_goal->intake() : nullptr, |
| 42 | position->intake_joint(), |
| 43 | output != nullptr ? &(output_struct.intake_joint_voltage) : nullptr, |
| 44 | status->fbb()); |
| 45 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 46 | bool zeroed; |
| 47 | bool estopped; |
| 48 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 49 | { |
| 50 | AbsoluteEncoderProfiledJointStatus *hood_status = |
| 51 | GetMutableTemporaryPointer(*status->fbb(), hood_status_offset); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 52 | |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 53 | AbsoluteEncoderProfiledJointStatus *intake_status = |
| 54 | GetMutableTemporaryPointer(*status->fbb(), intake_status_offset); |
| 55 | |
| 56 | zeroed = hood_status->zeroed() && intake_status->zeroed(); |
| 57 | estopped = hood_status->estopped() || intake_status->estopped(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 61 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 62 | status_builder.add_zeroed(zeroed); |
| 63 | status_builder.add_estopped(estopped); |
| 64 | |
| 65 | status_builder.add_hood(hood_status_offset); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 66 | status_builder.add_intake(intake_status_offset); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 67 | |
| 68 | status->Send(status_builder.Finish()); |
Sabina Davis | 0f2d38c | 2020-02-08 17:01:21 -0800 | [diff] [blame^] | 69 | |
| 70 | if (output != nullptr) { |
| 71 | if (unsafe_goal) { |
| 72 | output_struct.intake_roller_voltage = unsafe_goal->roller_voltage(); |
| 73 | } else { |
| 74 | output_struct.intake_roller_voltage = 0.0; |
| 75 | } |
| 76 | output->Send(Output::Pack(*output->fbb(), &output_struct)); |
| 77 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame] | 80 | } // namespace superstructure |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 81 | } // namespace control_loops |
| 82 | } // namespace y2020 |