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), |
| 16 | hood_(constants::GetValues().hood) { |
| 17 | event_loop->SetRuntimeRealtimePriority(30); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 18 | } |
| 19 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame^] | 20 | void Superstructure::RunIteration(const Goal *unsafe_goal, |
| 21 | const Position *position, |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 22 | aos::Sender<Output>::Builder *output, |
| 23 | aos::Sender<Status>::Builder *status) { |
| 24 | if (WasReset()) { |
| 25 | AOS_LOG(ERROR, "WPILib reset, restarting\n"); |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame^] | 26 | hood_.Reset(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame^] | 29 | OutputT output_struct; |
| 30 | |
| 31 | flatbuffers::Offset<AbsoluteEncoderProfiledJointStatus> hood_status_offset = |
| 32 | hood_.Iterate(unsafe_goal != nullptr ? unsafe_goal->hood() : nullptr, |
| 33 | position->hood(), |
| 34 | output != nullptr ? &(output_struct.hood_voltage) : nullptr, |
| 35 | status->fbb()); |
| 36 | |
| 37 | bool zeroed; |
| 38 | bool estopped; |
| 39 | |
| 40 | const AbsoluteEncoderProfiledJointStatus *hood_status = |
| 41 | GetMutableTemporaryPointer(*status->fbb(), hood_status_offset); |
| 42 | zeroed = hood_status->zeroed(); |
| 43 | estopped = hood_status->estopped(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 44 | |
| 45 | if (output != nullptr) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 46 | output->Send(Output::Pack(*output->fbb(), &output_struct)); |
| 47 | } |
| 48 | |
| 49 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 50 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame^] | 51 | status_builder.add_zeroed(zeroed); |
| 52 | status_builder.add_estopped(estopped); |
| 53 | |
| 54 | status_builder.add_hood(hood_status_offset); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 55 | |
| 56 | status->Send(status_builder.Finish()); |
| 57 | } |
| 58 | |
Sabina Davis | cf08b15 | 2020-01-31 22:12:09 -0800 | [diff] [blame^] | 59 | } // namespace superstructure |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 60 | } // namespace control_loops |
| 61 | } // namespace y2020 |