Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 6 | #include "aos/common/actions/actions.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 7 | #include "aos/common/input/driver_station_data.h" |
| 8 | #include "aos/common/logging/logging.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 9 | #include "aos/common/time.h" |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/input/joystick_input.h" |
| 12 | #include "aos/linux_code/init.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 13 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 14 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 15 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 16 | #include "y2017/constants.h" |
| 17 | #include "y2017/control_loops/superstructure/superstructure.q.h" |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 18 | |
| 19 | using ::frc971::control_loops::drivetrain_queue; |
| 20 | using ::y2017::control_loops::superstructure_queue; |
| 21 | |
| 22 | using ::aos::input::driver_station::ButtonLocation; |
| 23 | using ::aos::input::driver_station::ControlBit; |
| 24 | using ::aos::input::driver_station::JoystickAxis; |
| 25 | using ::aos::input::driver_station::POVLocation; |
| 26 | |
| 27 | namespace y2017 { |
| 28 | namespace input { |
| 29 | namespace joysticks { |
| 30 | |
| 31 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 32 | const ButtonLocation kQuickTurn(1, 5); |
| 33 | |
| 34 | const ButtonLocation kTurn1(1, 7); |
| 35 | const ButtonLocation kTurn2(1, 11); |
| 36 | |
| 37 | const ButtonLocation kIntakeDown(3, 9); |
| 38 | const ButtonLocation kIntakeIn(3, 12); |
| 39 | const ButtonLocation kIntakeOut(3, 8); |
| 40 | const POVLocation kHang(3, 90); |
| 41 | const ButtonLocation kFire(3, 3); |
| 42 | const ButtonLocation kCloseShot(3, 7); |
| 43 | const ButtonLocation kMiddleShot(3, 6); |
| 44 | const POVLocation kFarShot(3, 270); |
| 45 | |
| 46 | const ButtonLocation kVisionAlign(3, 5); |
| 47 | |
| 48 | const ButtonLocation kReverseIndexer(3, 4); |
| 49 | const ButtonLocation kExtra1(3, 11); |
| 50 | const ButtonLocation kExtra2(3, 10); |
| 51 | const ButtonLocation kExtra3(3, 2); |
| 52 | |
| 53 | class Reader : public ::aos::input::JoystickInput { |
| 54 | public: |
| 55 | Reader() {} |
| 56 | |
| 57 | void RunIteration(const ::aos::input::driver_station::Data &data) override { |
| 58 | bool last_auto_running = auto_running_; |
| 59 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 60 | data.GetControlBit(ControlBit::kEnabled); |
| 61 | if (auto_running_ != last_auto_running) { |
| 62 | if (auto_running_) { |
| 63 | StartAuto(); |
| 64 | } else { |
| 65 | StopAuto(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | vision_valid_ = false; |
| 70 | |
| 71 | if (!auto_running_) { |
| 72 | HandleDrivetrain(data); |
| 73 | HandleTeleop(data); |
| 74 | } |
| 75 | |
| 76 | // Process any pending actions. |
| 77 | action_queue_.Tick(); |
| 78 | was_running_ = action_queue_.Running(); |
| 79 | } |
| 80 | |
| 81 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 82 | bool is_control_loop_driving = false; |
| 83 | |
| 84 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 85 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 86 | drivetrain_queue.status.FetchLatest(); |
| 87 | |
| 88 | if (data.PosEdge(kTurn1) || data.PosEdge(kTurn2)) { |
| 89 | if (drivetrain_queue.status.get()) { |
| 90 | left_goal_ = drivetrain_queue.status->estimated_left_position; |
| 91 | right_goal_ = drivetrain_queue.status->estimated_right_position; |
| 92 | } |
| 93 | } |
| 94 | if (data.IsPressed(kTurn1) || data.IsPressed(kTurn2)) { |
| 95 | is_control_loop_driving = true; |
| 96 | } |
| 97 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 98 | .steering(wheel) |
| 99 | .throttle(throttle) |
| 100 | .quickturn(data.IsPressed(kQuickTurn)) |
| 101 | .control_loop_driving(is_control_loop_driving) |
| 102 | .left_goal(left_goal_ - wheel * 0.5 + throttle * 0.3) |
| 103 | .right_goal(right_goal_ + wheel * 0.5 + throttle * 0.3) |
| 104 | .left_velocity_goal(0) |
| 105 | .right_velocity_goal(0) |
| 106 | .Send()) { |
| 107 | LOG(WARNING, "sending stick values failed\n"); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 112 | // Default the intake to in. |
| 113 | intake_goal_ = constants::Values::kIntakeRange.lower; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame^] | 114 | bool lights_on = false; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 115 | |
| 116 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 117 | action_queue_.CancelAllActions(); |
| 118 | LOG(DEBUG, "Canceling\n"); |
| 119 | } |
| 120 | |
| 121 | superstructure_queue.status.FetchLatest(); |
| 122 | if (!superstructure_queue.status.get()) { |
| 123 | LOG(ERROR, "Got no superstructure status packet.\n"); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (data.IsPressed(kIntakeDown)) { |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 128 | intake_goal_ = 0.223; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (data.IsPressed(kVisionAlign)) { |
| 132 | // Align shot using vision |
| 133 | // TODO(campbell): Add vision aligning. |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame^] | 134 | lights_on = true; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 135 | shooter_velocity_ = 100.0; |
| 136 | } else if (data.IsPressed(kCloseShot)) { |
| 137 | // Close shot |
| 138 | hood_goal_ = 0.5; |
| 139 | shooter_velocity_ = 350.0; |
| 140 | } else if (data.IsPressed(kMiddleShot)) { |
| 141 | // Medium distance shot |
| 142 | hood_goal_ = 0.4; |
| 143 | shooter_velocity_ = 350.0; |
| 144 | } else if (data.IsPressed(kFarShot)) { |
| 145 | // Far shot |
| 146 | hood_goal_ = 0.6; |
| 147 | shooter_velocity_ = 250.0; |
| 148 | } else { |
| 149 | hood_goal_ = 0.15; |
| 150 | shooter_velocity_ = 0.0; |
| 151 | } |
| 152 | |
| 153 | if (data.IsPressed(kExtra1)) { |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 154 | turret_goal_ = -M_PI * 3.0 / 4.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 155 | } |
| 156 | if (data.IsPressed(kExtra2)) { |
| 157 | turret_goal_ = 0.0; |
| 158 | } |
| 159 | if (data.IsPressed(kExtra3)) { |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 160 | turret_goal_ = M_PI * 3.0 / 4.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | fire_ = data.IsPressed(kFire) && shooter_velocity_ != 0.0; |
| 164 | |
| 165 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 166 | new_superstructure_goal->intake.distance = intake_goal_; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 167 | new_superstructure_goal->intake.disable_intake = false; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 168 | new_superstructure_goal->turret.angle = turret_goal_; |
| 169 | new_superstructure_goal->hood.angle = hood_goal_; |
| 170 | new_superstructure_goal->shooter.angular_velocity = shooter_velocity_; |
| 171 | |
| 172 | new_superstructure_goal->intake.profile_params.max_velocity = 0.50; |
| 173 | new_superstructure_goal->turret.profile_params.max_velocity = 6.0; |
| 174 | new_superstructure_goal->hood.profile_params.max_velocity = 5.0; |
| 175 | |
| 176 | new_superstructure_goal->intake.profile_params.max_acceleration = 5.0; |
| 177 | new_superstructure_goal->turret.profile_params.max_acceleration = 15.0; |
| 178 | new_superstructure_goal->hood.profile_params.max_acceleration = 25.0; |
| 179 | |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 180 | new_superstructure_goal->intake.voltage_rollers = 0.0; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame^] | 181 | new_superstructure_goal->lights_on = lights_on; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 182 | |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 183 | if (data.IsPressed(kHang)) { |
| 184 | new_superstructure_goal->intake.voltage_rollers = -12.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 185 | new_superstructure_goal->intake.disable_intake = true; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 186 | } else if (data.IsPressed(kIntakeIn)) { |
| 187 | new_superstructure_goal->intake.voltage_rollers = 12.0; |
| 188 | } else if (data.IsPressed(kIntakeOut)) { |
| 189 | new_superstructure_goal->intake.voltage_rollers = -8.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 190 | } |
| 191 | if (intake_goal_ < 0.1) { |
| 192 | new_superstructure_goal->intake.voltage_rollers = |
| 193 | ::std::min(8.0, new_superstructure_goal->intake.voltage_rollers); |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if (data.IsPressed(kReverseIndexer)) { |
| 197 | new_superstructure_goal->indexer.voltage_rollers = -4.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 198 | new_superstructure_goal->indexer.angular_velocity = 4.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 199 | new_superstructure_goal->indexer.angular_velocity = 1.0; |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 200 | } else if (fire_) { |
| 201 | new_superstructure_goal->indexer.angular_velocity = -3.0 * M_PI; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 202 | } else { |
| 203 | new_superstructure_goal->indexer.voltage_rollers = 0.0; |
| 204 | new_superstructure_goal->indexer.angular_velocity = 0.0; |
| 205 | } |
| 206 | |
| 207 | LOG_STRUCT(DEBUG, "sending goal", *new_superstructure_goal); |
| 208 | if (!new_superstructure_goal.Send()) { |
| 209 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private: |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 214 | void StartAuto() { |
| 215 | LOG(INFO, "Starting auto mode\n"); |
| 216 | |
| 217 | ::frc971::autonomous::AutonomousActionParams params; |
| 218 | ::frc971::autonomous::auto_mode.FetchLatest(); |
| 219 | if (::frc971::autonomous::auto_mode.get() != nullptr) { |
| 220 | params.mode = ::frc971::autonomous::auto_mode->mode; |
| 221 | } else { |
| 222 | LOG(WARNING, "no auto mode values\n"); |
| 223 | params.mode = 0; |
| 224 | } |
| 225 | action_queue_.EnqueueAction( |
| 226 | ::frc971::autonomous::MakeAutonomousAction(params)); |
| 227 | } |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 228 | |
| 229 | void StopAuto() { |
| 230 | LOG(INFO, "Stopping auto mode\n"); |
| 231 | action_queue_.CancelAllActions(); |
| 232 | } |
| 233 | |
| 234 | // Current goals to send to the robot. |
| 235 | double intake_goal_ = 0.0; |
| 236 | double turret_goal_ = 0.0; |
| 237 | double hood_goal_ = 0.3; |
| 238 | double shooter_velocity_ = 0.0; |
| 239 | |
| 240 | // Goals to send to the drivetrain in closed loop mode. |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 241 | double left_goal_ = 0.0; |
| 242 | double right_goal_ = 0.0; |
Campbell Crowley | 71b5f13 | 2017-02-18 13:16:08 -0800 | [diff] [blame] | 243 | |
| 244 | bool was_running_ = false; |
| 245 | bool auto_running_ = false; |
| 246 | |
| 247 | bool vision_valid_ = false; |
| 248 | |
| 249 | bool fire_ = false; |
| 250 | |
| 251 | ::aos::common::actions::ActionQueue action_queue_; |
| 252 | }; |
| 253 | |
| 254 | } // namespace joysticks |
| 255 | } // namespace input |
| 256 | } // namespace y2017 |
| 257 | |
| 258 | int main() { |
| 259 | ::aos::Init(-1); |
| 260 | ::y2017::input::joysticks::Reader reader; |
| 261 | reader.Run(); |
| 262 | ::aos::Cleanup(); |
| 263 | } |