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