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 |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 33 | const ButtonLocation kElevatorUp(4, 4); |
| 34 | const ButtonLocation kIntakeOut(3, 3); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 35 | const ButtonLocation kElevatorDown(0, 0); |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 36 | const ButtonLocation kElevator1(4, 1); |
| 37 | const ButtonLocation kElevator2(4, 11); |
| 38 | const ButtonLocation kElevator3(4, 9); |
| 39 | const ButtonLocation kElevator4(4, 7); |
| 40 | const ButtonLocation kElevator5(4, 5); |
| 41 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 42 | const ButtonLocation kDiskLoad(0, 0); |
| 43 | const ButtonLocation kDiskRocketMiddle(0, 0); |
| 44 | const ButtonLocation kDiskRocketTop(0, 0); |
| 45 | const ButtonLocation kCargoLoad(0, 0); |
| 46 | const ButtonLocation kCargoBay(0, 0); |
| 47 | const ButtonLocation kCargoRocketBase(0, 0); |
| 48 | const ButtonLocation kCargoRocketMiddle(0, 0); |
| 49 | const ButtonLocation kCargoRocketTop(0, 0); |
| 50 | const ButtonLocation kStow(0, 0); |
| 51 | const ButtonLocation kIntakeExtend(0, 0); |
| 52 | const ButtonLocation kIntake(0, 0); |
| 53 | const ButtonLocation kSpit(0, 0); |
| 54 | const ButtonLocation kCargoSuction(0, 0); |
| 55 | const ButtonLocation kDiskSuction(0, 0); |
| 56 | const ButtonLocation kSuctionOut(0, 0); |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 57 | const ButtonLocation kDeployStilt(3, 8); |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 58 | const ButtonLocation kRetractStilt(0, 0); |
| 59 | const ButtonLocation kBackwards(0, 0); |
| 60 | |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 61 | const ButtonLocation kWristDown(3, 1); |
| 62 | |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 63 | class Reader : public ::aos::input::ActionJoystickInput { |
| 64 | public: |
| 65 | Reader(::aos::EventLoop *event_loop) |
| 66 | : ::aos::input::ActionJoystickInput( |
| 67 | event_loop, |
| 68 | ::y2019::control_loops::drivetrain::GetDrivetrainConfig()) {} |
| 69 | |
| 70 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 71 | superstructure_queue.position.FetchLatest(); |
| 72 | superstructure_queue.status.FetchLatest(); |
| 73 | if (!superstructure_queue.status.get() || |
| 74 | !superstructure_queue.position.get()) { |
| 75 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 80 | |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 81 | /* |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 82 | if (data.IsPressed(kElevatorUp)) { |
| 83 | elevator_height_ += 0.1; |
| 84 | } else if (data.IsPressed(kElevatorDown)) { |
| 85 | elevator_height_ -= 0.1; |
| 86 | } else if (data.IsPressed(kDiskLoad)) { |
| 87 | elevator_height_ = 0.48; |
| 88 | wrist_angle_ = M_PI; |
| 89 | } else if (data.IsPressed(kDiskRocketMiddle)) { |
| 90 | elevator_height_ = 1.19; |
| 91 | wrist_angle_ = M_PI; |
| 92 | } else if (data.IsPressed(kDiskRocketTop)) { |
| 93 | elevator_height_ = 1.90; |
| 94 | wrist_angle_ = M_PI; |
| 95 | } |
| 96 | |
| 97 | // TODO(sabina): do we need an angle here? |
| 98 | else if (data.IsPressed(kCargoLoad)) { |
| 99 | elevator_height_ = 1.12; |
| 100 | wrist_angle_ = M_PI; |
| 101 | } else if (data.IsPressed(kCargoBay)) { |
| 102 | elevator_height_ = 0.0; |
| 103 | wrist_angle_ = M_PI / 3; |
| 104 | } else if (data.IsPressed(kCargoRocketBase)) { |
| 105 | elevator_height_ = 0.7; |
| 106 | wrist_angle_ = M_PI; |
| 107 | } else if (data.IsPressed(kCargoRocketMiddle)) { |
| 108 | elevator_height_ = 1.41; |
| 109 | wrist_angle_ = M_PI; |
| 110 | } else if (data.IsPressed(kCargoRocketTop)) { |
| 111 | elevator_height_ = 2.12; |
| 112 | wrist_angle_ = M_PI; |
| 113 | } else if (data.IsPressed(kStow)) { |
| 114 | elevator_height_ = 0.5; |
| 115 | wrist_angle_ = 0.0; |
| 116 | } else { |
| 117 | } |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 118 | */ |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 119 | |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 120 | /* |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 121 | // TODO(sabina): get accurate angle. |
| 122 | if (data.IsPressed(kIntakeExtend)) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 123 | new_superstructure_goal->intake.unsafe_goal = 0.5; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 124 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 125 | new_superstructure_goal->intake.unsafe_goal = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (data.IsPressed(kIntake)) { |
| 129 | new_superstructure_goal->suction.bottom = true; |
| 130 | if (superstructure_queue.status->has_piece == false) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 131 | new_superstructure_goal->roller_voltage = 12.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 132 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 133 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 134 | } |
| 135 | } else if (data.IsPressed(kSpit)) { |
| 136 | new_superstructure_goal->suction.bottom = false; |
| 137 | if (superstructure_queue.status->has_piece == false) { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 138 | new_superstructure_goal->roller_voltage = 12.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 139 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 140 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 141 | } |
| 142 | } else { |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 143 | new_superstructure_goal->roller_voltage = 0.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 144 | } |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 145 | */ |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 146 | |
| 147 | // TODO(sabina): decide if we should really have disk suction as its own |
| 148 | // button |
| 149 | if (data.IsPressed(kCargoSuction)) { |
| 150 | new_superstructure_goal->suction.top = false; |
| 151 | new_superstructure_goal->suction.bottom = true; |
| 152 | } else if (data.IsPressed(kDiskSuction)) { |
| 153 | new_superstructure_goal->suction.top = true; |
| 154 | new_superstructure_goal->suction.bottom = true; |
| 155 | } else if (data.IsPressed(kSuctionOut)) { |
| 156 | new_superstructure_goal->suction.top = true; |
| 157 | new_superstructure_goal->suction.bottom = true; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // TODO(sabina): max height please? |
| 161 | if (data.IsPressed(kDeployStilt)) { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 162 | new_superstructure_goal->stilts.unsafe_goal = 0.3; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 163 | } else { |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 164 | new_superstructure_goal->stilts.unsafe_goal = 0.01; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Austin Schuh | 2cf16b8 | 2019-02-15 23:23:22 -0800 | [diff] [blame^] | 167 | if (data.IsPressed(kIntakeOut)) { |
| 168 | new_superstructure_goal->intake.unsafe_goal = 0.8; |
| 169 | new_superstructure_goal->roller_voltage = 5.0; |
| 170 | } else { |
| 171 | new_superstructure_goal->intake.unsafe_goal = -1.2; |
| 172 | new_superstructure_goal->roller_voltage = 0.0; |
| 173 | } |
| 174 | |
| 175 | if (data.IsPressed(kElevator1)) { |
| 176 | elevator_height_ = 1.5; |
| 177 | } else if (data.IsPressed(kElevator2)) { |
| 178 | elevator_height_ = 1.2; |
| 179 | } else if (data.IsPressed(kElevator3)) { |
| 180 | elevator_height_ = 0.8; |
| 181 | } else if (data.IsPressed(kElevator4)) { |
| 182 | elevator_height_ = 0.3; |
| 183 | } else if (data.IsPressed(kElevator5)) { |
| 184 | elevator_height_ = 0.01; |
| 185 | } |
| 186 | |
| 187 | if (data.IsPressed(kWristDown)) { |
| 188 | wrist_angle_ = -M_PI / 3.0; |
| 189 | } else { |
| 190 | wrist_angle_ = M_PI / 3.0; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Theo Bafrali | 00e4227 | 2019-02-12 01:07:46 -0800 | [diff] [blame] | 193 | new_superstructure_goal->elevator.unsafe_goal = elevator_height_; |
| 194 | new_superstructure_goal->wrist.unsafe_goal = wrist_angle_; |
Sabina Davis | 91b2360 | 2019-01-21 00:06:01 -0800 | [diff] [blame] | 195 | |
| 196 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 197 | if (!new_superstructure_goal.Send()) { |
| 198 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | // Current goals here. |
| 204 | double elevator_height_ = 0.0; |
| 205 | double wrist_angle_ = 0.0; |
| 206 | }; |
| 207 | |
| 208 | } // namespace joysticks |
| 209 | } // namespace input |
| 210 | } // namespace y2019 |
| 211 | |
| 212 | int main() { |
| 213 | ::aos::Init(-1); |
| 214 | ::aos::ShmEventLoop event_loop; |
| 215 | ::y2019::input::joysticks::Reader reader(&event_loop); |
| 216 | reader.Run(); |
| 217 | ::aos::Cleanup(); |
| 218 | } |