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