Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include "aos/linux_code/init.h" |
| 7 | #include "aos/input/joystick_input.h" |
| 8 | #include "aos/common/input/driver_station_data.h" |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/common/time.h" |
| 12 | #include "aos/common/actions/actions.h" |
| 13 | |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 15 | #include "y2016/control_loops/shooter/shooter.q.h" |
| 16 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
| 17 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 18 | #include "y2016/constants.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 19 | #include "frc971/queues/gyro.q.h" |
| 20 | #include "frc971/autonomous/auto.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 21 | |
| 22 | using ::frc971::control_loops::drivetrain_queue; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 23 | using ::y2016::control_loops::shooter::shooter_queue; |
| 24 | using ::y2016::control_loops::superstructure_queue; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 25 | |
| 26 | using ::aos::input::driver_station::ButtonLocation; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 27 | using ::aos::input::driver_station::ControlBit; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 28 | using ::aos::input::driver_station::JoystickAxis; |
| 29 | using ::aos::input::driver_station::POVLocation; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 30 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 31 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 35 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 36 | const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 37 | const ButtonLocation kQuickTurn(1, 5); |
| 38 | |
Austin Schuh | 781cdcc | 2016-03-12 13:03:12 -0800 | [diff] [blame] | 39 | const ButtonLocation kTurn1(1, 7); |
| 40 | const ButtonLocation kTurn2(1, 11); |
| 41 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 42 | // Buttons on the lexan driver station to get things running on bring-up day. |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 43 | const ButtonLocation kIntakeDown(3, 11); |
| 44 | const POVLocation kFrontLong(3, 180); |
| 45 | const POVLocation kBackLong(3, 0); |
| 46 | const ButtonLocation kTest3(3, 7); |
| 47 | const ButtonLocation kIntakeIn(3, 12); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 48 | const ButtonLocation kTest5(3, 8); |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 49 | const ButtonLocation kFire(3, 3); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 50 | const ButtonLocation kTest7(3, 5); |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 51 | const ButtonLocation kIntakeOut(3, 9); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 52 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 53 | class Reader : public ::aos::input::JoystickInput { |
| 54 | public: |
| 55 | Reader() |
| 56 | : is_high_gear_(false), |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 57 | intake_goal_(0.0), |
| 58 | shoulder_goal_(M_PI / 2.0), |
| 59 | wrist_goal_(0.0) {} |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 60 | |
| 61 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 62 | bool last_auto_running = auto_running_; |
| 63 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 64 | data.GetControlBit(ControlBit::kEnabled); |
| 65 | if (auto_running_ != last_auto_running) { |
| 66 | if (auto_running_) { |
| 67 | StartAuto(); |
| 68 | } else { |
| 69 | StopAuto(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
| 74 | HandleDrivetrain(data); |
| 75 | HandleTeleop(data); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 80 | bool is_control_loop_driving = false; |
Austin Schuh | 781cdcc | 2016-03-12 13:03:12 -0800 | [diff] [blame] | 81 | static double left_goal = 0.0; |
| 82 | static double right_goal = 0.0; |
| 83 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 84 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 85 | const double throttle = -data.GetAxis(kDriveThrottle); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 86 | |
Austin Schuh | 781cdcc | 2016-03-12 13:03:12 -0800 | [diff] [blame] | 87 | if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) { |
| 88 | drivetrain_queue.status.FetchLatest(); |
| 89 | if (drivetrain_queue.status.get()) { |
| 90 | const double delta = data.PosEdge(kTurn2) ? 0.1 : -0.1; |
| 91 | left_goal = drivetrain_queue.status->estimated_left_position + delta; |
| 92 | right_goal = drivetrain_queue.status->estimated_right_position - delta; |
| 93 | } |
| 94 | } |
| 95 | if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) { |
| 96 | is_control_loop_driving = true; |
| 97 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 98 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 99 | .steering(wheel) |
| 100 | .throttle(throttle) |
| 101 | .highgear(is_high_gear_) |
| 102 | .quickturn(data.IsPressed(kQuickTurn)) |
| 103 | .control_loop_driving(is_control_loop_driving) |
Austin Schuh | 781cdcc | 2016-03-12 13:03:12 -0800 | [diff] [blame] | 104 | .left_goal(left_goal + wheel * 0.5 + throttle * 0.3) |
| 105 | .right_goal(right_goal - wheel * 0.5 + throttle * 0.3) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 106 | .left_velocity_goal(0) |
| 107 | .right_velocity_goal(0) |
| 108 | .Send()) { |
| 109 | LOG(WARNING, "sending stick values failed\n"); |
| 110 | } |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 111 | |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 112 | if (data.PosEdge(kShiftLow)) { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 113 | is_high_gear_ = false; |
| 114 | } |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 115 | |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 116 | if (data.PosEdge(kShiftHigh) || data.PosEdge(kShiftHigh2)) { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 117 | is_high_gear_ = true; |
| 118 | } |
| 119 | } |
| 120 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 121 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 122 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 123 | action_queue_.CancelAllActions(); |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 124 | LOG(DEBUG, "Canceling\n"); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 127 | if (data.PosEdge(ControlBit::kEnabled)) { |
| 128 | // If we got enabled, wait for everything to zero. |
| 129 | LOG(INFO, "Waiting for zero.\n"); |
| 130 | waiting_for_zero_ = true; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 131 | is_high_gear_ = true; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | superstructure_queue.status.FetchLatest(); |
| 135 | if (!superstructure_queue.status.get()) { |
| 136 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 137 | } |
| 138 | |
| 139 | if (superstructure_queue.status.get() && |
| 140 | superstructure_queue.status->zeroed) { |
| 141 | if (waiting_for_zero_) { |
| 142 | LOG(INFO, "Zeroed! Starting teleop mode.\n"); |
| 143 | waiting_for_zero_ = false; |
| 144 | } |
| 145 | } else { |
| 146 | waiting_for_zero_ = true; |
| 147 | } |
| 148 | |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 149 | if (data.IsPressed(kIntakeDown)) { |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 150 | intake_goal_ = 0.1; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 151 | } else { |
| 152 | intake_goal_ = 1.6; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 155 | if (data.IsPressed(kFrontLong)) { |
| 156 | // Forwards shot |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 157 | shoulder_goal_ = M_PI / 2.0 - 0.2; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 158 | wrist_goal_ = M_PI + 0.42; |
| 159 | shooter_velocity_ = 640.0; |
| 160 | } else if (data.IsPressed(kBackLong)) { |
| 161 | // Backwards shot |
| 162 | shoulder_goal_ = M_PI / 2.0 - 0.2; |
| 163 | wrist_goal_ = -0.59; |
| 164 | shooter_velocity_ = 640.0; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 165 | } else { |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 166 | wrist_goal_ = 0.0; |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 167 | shoulder_goal_ = -0.010; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 168 | shooter_velocity_ = 0.0; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 171 | if (data.IsPressed(kTest3)) { |
| 172 | wrist_goal_ = 0.0; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 175 | is_intaking_ = data.IsPressed(kIntakeIn); |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 176 | |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 177 | if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) { |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 178 | fire_ = true; |
| 179 | } else { |
| 180 | fire_ = false; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | if (data.PosEdge(kTest7)) { |
| 184 | } |
| 185 | |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 186 | is_outtaking_ = data.IsPressed(kIntakeOut); |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 187 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 188 | if (!waiting_for_zero_) { |
| 189 | if (!action_queue_.Running()) { |
| 190 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 191 | new_superstructure_goal->angle_intake = intake_goal_; |
| 192 | new_superstructure_goal->angle_shoulder = shoulder_goal_; |
| 193 | new_superstructure_goal->angle_wrist = wrist_goal_; |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 194 | |
| 195 | new_superstructure_goal->max_angular_velocity_intake = 7.0; |
| 196 | new_superstructure_goal->max_angular_velocity_shoulder = 4.0; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 197 | new_superstructure_goal->max_angular_velocity_wrist = 10.0; |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 198 | new_superstructure_goal->max_angular_acceleration_intake = 40.0; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 199 | new_superstructure_goal->max_angular_acceleration_shoulder = 10.0; |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 200 | new_superstructure_goal->max_angular_acceleration_wrist = 25.0; |
| 201 | |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 202 | // Granny mode |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 203 | /* |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 204 | new_superstructure_goal->max_angular_velocity_intake = 0.2; |
| 205 | new_superstructure_goal->max_angular_velocity_shoulder = 0.2; |
| 206 | new_superstructure_goal->max_angular_velocity_wrist = 0.2; |
| 207 | new_superstructure_goal->max_angular_acceleration_intake = 1.0; |
| 208 | new_superstructure_goal->max_angular_acceleration_shoulder = 1.0; |
| 209 | new_superstructure_goal->max_angular_acceleration_wrist = 1.0; |
| 210 | */ |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 211 | if (is_intaking_) { |
| 212 | new_superstructure_goal->voltage_top_rollers = 12.0; |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 213 | new_superstructure_goal->voltage_bottom_rollers = 12.0; |
| 214 | } else if (is_outtaking_) { |
| 215 | new_superstructure_goal->voltage_top_rollers = -12.0; |
| 216 | new_superstructure_goal->voltage_bottom_rollers = -7.0; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 217 | } else { |
| 218 | new_superstructure_goal->voltage_top_rollers = 0.0; |
| 219 | new_superstructure_goal->voltage_bottom_rollers = 0.0; |
| 220 | } |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 221 | |
| 222 | if (!new_superstructure_goal.Send()) { |
| 223 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 224 | } else { |
| 225 | LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n", |
| 226 | intake_goal_, shoulder_goal_, wrist_goal_); |
| 227 | } |
| 228 | |
| 229 | if (!shooter_queue.goal.MakeWithBuilder() |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 230 | .angular_velocity(shooter_velocity_) |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 231 | .clamp_open(is_intaking_ || is_outtaking_) |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 232 | .push_to_shooter(fire_) |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 233 | .Send()) { |
| 234 | LOG(ERROR, "Sending shooter goal failed.\n"); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 239 | was_running_ = action_queue_.Running(); |
| 240 | } |
| 241 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 242 | private: |
| 243 | void StartAuto() { |
| 244 | LOG(INFO, "Starting auto mode\n"); |
| 245 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 246 | } |
| 247 | |
| 248 | void StopAuto() { |
| 249 | LOG(INFO, "Stopping auto mode\n"); |
| 250 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 251 | } |
| 252 | |
| 253 | bool is_high_gear_; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 254 | // Whatever these are set to are our default goals to send out after zeroing. |
| 255 | double intake_goal_; |
| 256 | double shoulder_goal_; |
| 257 | double wrist_goal_; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 258 | double shooter_velocity_ = 0.0; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 259 | |
| 260 | bool was_running_ = false; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 261 | bool auto_running_ = false; |
| 262 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 263 | // If we're waiting for the subsystems to zero. |
| 264 | bool waiting_for_zero_ = true; |
| 265 | |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 266 | bool is_intaking_ = false; |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 267 | bool is_outtaking_ = false; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 268 | bool fire_ = false; |
| 269 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 270 | ::aos::common::actions::ActionQueue action_queue_; |
| 271 | |
| 272 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 273 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 274 | "no drivetrain status"); |
| 275 | }; |
| 276 | |
| 277 | } // namespace joysticks |
| 278 | } // namespace input |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 279 | } // namespace y2016 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 280 | |
| 281 | int main() { |
| 282 | ::aos::Init(-1); |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 283 | ::y2016::input::joysticks::Reader reader; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 284 | reader.Run(); |
| 285 | ::aos::Cleanup(); |
| 286 | } |