Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include "aos/actions/actions.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/input/action_joystick_input.h" |
| 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/input/drivetrain_input.h" |
| 11 | #include "aos/input/joystick_input.h" |
| 12 | #include "aos/logging/logging.h" |
| 13 | #include "aos/logging/logging.h" |
| 14 | #include "aos/util/log_interval.h" |
| 15 | #include "frc971/autonomous/auto.q.h" |
| 16 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 17 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 18 | |
| 19 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
| 20 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
| 21 | |
| 22 | using ::y2019::control_loops::superstructure::superstructure_queue; |
| 23 | using ::aos::input::driver_station::ButtonLocation; |
| 24 | using ::aos::input::driver_station::ControlBit; |
| 25 | using ::aos::input::driver_station::JoystickAxis; |
| 26 | using ::aos::input::driver_station::POVLocation; |
| 27 | |
| 28 | namespace y2019 { |
| 29 | namespace input { |
| 30 | namespace joysticks { |
| 31 | |
| 32 | // TODO(sabina): update button locations when the board is done |
| 33 | const ButtonLocation kElevatorUp(0, 0); |
| 34 | const ButtonLocation kElevatorDown(0, 0); |
| 35 | const ButtonLocation kDiskLoad(0, 0); |
| 36 | const ButtonLocation kDiskRocketMiddle(0, 0); |
| 37 | const ButtonLocation kDiskRocketTop(0, 0); |
| 38 | const ButtonLocation kCargoLoad(0, 0); |
| 39 | const ButtonLocation kCargoBay(0, 0); |
| 40 | const ButtonLocation kCargoRocketBase(0, 0); |
| 41 | const ButtonLocation kCargoRocketMiddle(0, 0); |
| 42 | const ButtonLocation kCargoRocketTop(0, 0); |
| 43 | const ButtonLocation kStow(0, 0); |
| 44 | const ButtonLocation kIntakeExtend(0, 0); |
| 45 | const ButtonLocation kIntake(0, 0); |
| 46 | const ButtonLocation kSpit(0, 0); |
| 47 | const ButtonLocation kCargoSuction(0, 0); |
| 48 | const ButtonLocation kDiskSuction(0, 0); |
| 49 | const ButtonLocation kSuctionOut(0, 0); |
| 50 | const ButtonLocation kDeployStilt(0, 0); |
| 51 | const ButtonLocation kRetractStilt(0, 0); |
| 52 | const ButtonLocation kBackwards(0, 0); |
| 53 | |
| 54 | class Reader : public ::aos::input::ActionJoystickInput { |
| 55 | public: |
| 56 | Reader(::aos::EventLoop *event_loop) |
| 57 | : ::aos::input::ActionJoystickInput( |
| 58 | event_loop, |
| 59 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {} |
| 60 | |
| 61 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 62 | superstructure_queue.position.FetchLatest(); |
| 63 | superstructure_queue.status.FetchLatest(); |
| 64 | if (!superstructure_queue.status.get() || |
| 65 | !superstructure_queue.position.get()) { |
| 66 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 71 | |
| 72 | if (data.IsPressed(kElevatorUp)) { |
| 73 | elevator_height_ += 0.1; |
| 74 | } else if (data.IsPressed(kElevatorDown)) { |
| 75 | elevator_height_ -= 0.1; |
| 76 | } else if (data.IsPressed(kDiskLoad)) { |
| 77 | elevator_height_ = 0.48; |
| 78 | wrist_angle_ = M_PI; |
| 79 | } else if (data.IsPressed(kDiskRocketMiddle)) { |
| 80 | elevator_height_ = 1.19; |
| 81 | wrist_angle_ = M_PI; |
| 82 | } else if (data.IsPressed(kDiskRocketTop)) { |
| 83 | elevator_height_ = 1.90; |
| 84 | wrist_angle_ = M_PI; |
| 85 | } |
| 86 | |
| 87 | // TODO(sabina): do we need an angle here? |
| 88 | else if (data.IsPressed(kCargoLoad)) { |
| 89 | elevator_height_ = 1.12; |
| 90 | wrist_angle_ = M_PI; |
| 91 | } else if (data.IsPressed(kCargoBay)) { |
| 92 | elevator_height_ = 0.0; |
| 93 | wrist_angle_ = M_PI / 3; |
| 94 | } else if (data.IsPressed(kCargoRocketBase)) { |
| 95 | elevator_height_ = 0.7; |
| 96 | wrist_angle_ = M_PI; |
| 97 | } else if (data.IsPressed(kCargoRocketMiddle)) { |
| 98 | elevator_height_ = 1.41; |
| 99 | wrist_angle_ = M_PI; |
| 100 | } else if (data.IsPressed(kCargoRocketTop)) { |
| 101 | elevator_height_ = 2.12; |
| 102 | wrist_angle_ = M_PI; |
| 103 | } else if (data.IsPressed(kStow)) { |
| 104 | elevator_height_ = 0.5; |
| 105 | wrist_angle_ = 0.0; |
| 106 | } else { |
| 107 | } |
| 108 | |
| 109 | // TODO(sabina): get accurate angle. |
| 110 | if (data.IsPressed(kIntakeExtend)) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 111 | new_superstructure_goal->intake.unsafe_goal = 0.5; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 112 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 113 | new_superstructure_goal->intake.unsafe_goal = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (data.IsPressed(kIntake)) { |
| 117 | new_superstructure_goal->suction.bottom = true; |
| 118 | if (superstructure_queue.status->has_piece == false) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 119 | new_superstructure_goal->roller_voltage = 12.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 120 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 121 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 122 | } |
| 123 | } else if (data.IsPressed(kSpit)) { |
| 124 | new_superstructure_goal->suction.bottom = false; |
| 125 | if (superstructure_queue.status->has_piece == false) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 126 | new_superstructure_goal->roller_voltage = 12.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 127 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 128 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 129 | } |
| 130 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 131 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // TODO(sabina): decide if we should really have disk suction as its own |
| 135 | // button |
| 136 | if (data.IsPressed(kCargoSuction)) { |
| 137 | new_superstructure_goal->suction.top = false; |
| 138 | new_superstructure_goal->suction.bottom = true; |
| 139 | } else if (data.IsPressed(kDiskSuction)) { |
| 140 | new_superstructure_goal->suction.top = true; |
| 141 | new_superstructure_goal->suction.bottom = true; |
| 142 | } else if (data.IsPressed(kSuctionOut)) { |
| 143 | new_superstructure_goal->suction.top = true; |
| 144 | new_superstructure_goal->suction.bottom = true; |
| 145 | } else { |
| 146 | } |
| 147 | |
| 148 | // TODO(sabina): max height please? |
| 149 | if (data.IsPressed(kDeployStilt)) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 150 | new_superstructure_goal->stilts.unsafe_goal= 0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 151 | } else if (data.IsPressed(kRetractStilt)) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 152 | new_superstructure_goal->stilts.unsafe_goal = 0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 153 | } else { |
| 154 | } |
| 155 | |
| 156 | if (data.IsPressed(kBackwards)) { |
| 157 | wrist_angle_ = -wrist_angle_; |
| 158 | } |
| 159 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 160 | new_superstructure_goal->elevator.unsafe_goal = elevator_height_; |
| 161 | new_superstructure_goal->wrist.unsafe_goal = wrist_angle_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 162 | |
| 163 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 164 | if (!new_superstructure_goal.Send()) { |
| 165 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | // Current goals here. |
| 171 | double elevator_height_ = 0.0; |
| 172 | double wrist_angle_ = 0.0; |
| 173 | }; |
| 174 | |
| 175 | } // namespace joysticks |
| 176 | } // namespace input |
| 177 | } // namespace y2019 |
| 178 | |
| 179 | int main() { |
| 180 | ::aos::Init(-1); |
| 181 | ::aos::ShmEventLoop event_loop; |
| 182 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 183 | reader.Run(); |
| 184 | ::aos::Cleanup(); |
| 185 | } |