Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 6 | #include "aos/actions/actions.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/shm_event_loop.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 8 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/input/driver_station_data.h" |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 10 | #include "aos/input/joystick_input.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/logging/logging.h" |
| 12 | #include "aos/time/time.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 13 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 15 | #include "y2012/control_loops/accessories/accessories_generated.h" |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 16 | |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 17 | using ::aos::input::driver_station::ButtonLocation; |
| 18 | using ::aos::input::driver_station::JoystickAxis; |
| 19 | using ::aos::input::driver_station::ControlBit; |
| 20 | |
| 21 | #define OLD_DS 0 |
| 22 | |
| 23 | namespace y2012 { |
| 24 | namespace input { |
| 25 | namespace joysticks { |
| 26 | |
| 27 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 28 | const ButtonLocation kShiftHigh(2, 3), kShiftLow(2, 1); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 29 | const ButtonLocation kQuickTurn(1, 5); |
| 30 | |
| 31 | const ButtonLocation kCatch(3, 10); |
| 32 | |
| 33 | #if OLD_DS |
| 34 | const ButtonLocation kFire(3, 11); |
| 35 | const ButtonLocation kUnload(1, 4); |
| 36 | const ButtonLocation kReload(1, 2); |
| 37 | |
| 38 | const ButtonLocation kRollersOut(3, 12); |
| 39 | const ButtonLocation kRollersIn(3, 7); |
| 40 | |
| 41 | const ButtonLocation kTuck(3, 9); |
| 42 | const ButtonLocation kIntakePosition(3, 8); |
| 43 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 44 | const ButtonLocation kVerticalTuck(3, 1); |
| 45 | const JoystickAxis kFlipRobot(3, 3); |
| 46 | |
| 47 | const ButtonLocation kLongShot(3, 5); |
| 48 | const ButtonLocation kCloseShot(3, 2); |
| 49 | const ButtonLocation kFenderShot(3, 3); |
| 50 | const ButtonLocation kTrussShot(2, 11); |
| 51 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 52 | #else |
| 53 | const ButtonLocation kFire(3, 9); |
| 54 | const ButtonLocation kUnload(1, 4); |
| 55 | const ButtonLocation kReload(1, 2); |
| 56 | |
| 57 | const ButtonLocation kRollersOut(3, 8); |
| 58 | const ButtonLocation kRollersIn(3, 3); |
| 59 | |
| 60 | const ButtonLocation kTuck(3, 4); |
| 61 | const ButtonLocation kIntakePosition(3, 5); |
| 62 | const ButtonLocation kIntakeOpenPosition(3, 11); |
| 63 | const ButtonLocation kVerticalTuck(2, 6); |
| 64 | const JoystickAxis kFlipRobot(3, 3); |
| 65 | |
| 66 | const ButtonLocation kLongShot(3, 7); |
| 67 | const ButtonLocation kCloseShot(3, 6); |
| 68 | const ButtonLocation kFenderShot(3, 2); |
| 69 | const ButtonLocation kTrussShot(2, 11); |
| 70 | const ButtonLocation kHumanPlayerShot(3, 1); |
| 71 | #endif |
| 72 | |
| 73 | const ButtonLocation kUserLeft(2, 7); |
| 74 | const ButtonLocation kUserRight(2, 10); |
| 75 | |
| 76 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 77 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 78 | |
| 79 | class Reader : public ::aos::input::JoystickInput { |
| 80 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 81 | Reader(::aos::EventLoop *event_loop) |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame] | 82 | : ::aos::input::JoystickInput(event_loop), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 83 | drivetrain_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | event_loop->MakeSender<::frc971::control_loops::drivetrain::Goal>( |
| 85 | "/drivetrain")), |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame] | 86 | accessories_goal_sender_( |
| 87 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | ->MakeSender<::y2012::control_loops::accessories::Message>( |
| 89 | "/accessories")), |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame] | 90 | is_high_gear_(false) {} |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 91 | |
| 92 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 93 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 94 | HandleDrivetrain(data); |
| 95 | HandleTeleop(data); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 100 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 101 | const double throttle = -data.GetAxis(kDriveThrottle); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 102 | if (data.PosEdge(kShiftLow)) { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 103 | is_high_gear_ = false; |
| 104 | } |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 105 | if (data.PosEdge(kShiftHigh)) { |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 106 | is_high_gear_ = true; |
| 107 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 108 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
| 109 | frc971::control_loops::drivetrain::Goal::Builder goal_builder = |
| 110 | builder.MakeBuilder<frc971::control_loops::drivetrain::Goal>(); |
| 111 | goal_builder.add_wheel(wheel); |
| 112 | goal_builder.add_throttle(throttle); |
| 113 | goal_builder.add_highgear(is_high_gear_); |
| 114 | goal_builder.add_quickturn(data.IsPressed(kQuickTurn)); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 115 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 116 | if (!builder.Send(goal_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 117 | AOS_LOG(WARNING, "sending stick values failed\n"); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 122 | auto builder = accessories_goal_sender_.MakeBuilder(); |
| 123 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> solenoids_offset = |
| 124 | builder.fbb()->CreateVector<uint8_t>({data.IsPressed(kLongShot), |
| 125 | data.IsPressed(kCloseShot), |
| 126 | data.IsPressed(kFenderShot)}); |
| 127 | |
| 128 | flatbuffers::Offset<flatbuffers::Vector<double>> sticks_offset = |
| 129 | builder.fbb()->CreateVector<double>({data.GetAxis(kAdjustClawGoal), |
| 130 | data.GetAxis(kAdjustClawSeparation)}); |
| 131 | |
| 132 | y2012::control_loops::accessories::Message::Builder message_builder = |
| 133 | builder.MakeBuilder<y2012::control_loops::accessories::Message>(); |
| 134 | message_builder.add_solenoids(solenoids_offset); |
| 135 | message_builder.add_sticks(sticks_offset); |
| 136 | if (!builder.Send(message_builder.Finish())) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 137 | AOS_LOG(WARNING, "sending accessories goal failed\n"); |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 138 | } |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 142 | ::aos::Sender<::frc971::control_loops::drivetrain::Goal> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 143 | drivetrain_goal_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | ::aos::Sender<::y2012::control_loops::accessories::Message> |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame] | 145 | accessories_goal_sender_; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 146 | |
Austin Schuh | e452623 | 2019-06-29 16:51:33 -0700 | [diff] [blame] | 147 | bool is_high_gear_; |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // namespace joysticks |
| 151 | } // namespace input |
| 152 | } // namespace y2012 |
| 153 | |
| 154 | int main() { |
James Kuszmaul | ad8a808 | 2020-02-14 21:21:58 -0800 | [diff] [blame] | 155 | ::aos::InitNRT(); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 156 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 157 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 158 | aos::configuration::ReadConfig("config.json"); |
| 159 | |
| 160 | ::aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 161 | ::y2012::input::joysticks::Reader reader(&event_loop); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 162 | |
| 163 | event_loop.Run(); |
| 164 | |
Brian Silverman | c71537c | 2016-01-01 13:43:14 -0800 | [diff] [blame] | 165 | ::aos::Cleanup(); |
| 166 | } |