Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [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/prime/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" |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 12 | #include "aos/common/actions/actions.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 13 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/claw/claw.q.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 16 | #include "frc971/control_loops/fridge/fridge.q.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 17 | #include "frc971/constants.h" |
| 18 | #include "frc971/queues/gyro.q.h" |
| 19 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 20 | #include "frc971/actors/pickup_actor.h" |
| 21 | #include "frc971/actors/stack_actor.h" |
| 22 | #include "frc971/actors/lift_actor.h" |
Austin Schuh | 994d42c | 2015-03-01 00:02:17 -0800 | [diff] [blame] | 23 | #include "frc971/actors/can_pickup_actor.h" |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 24 | #include "frc971/actors/horizontal_can_pickup_actor.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 25 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 26 | using ::frc971::control_loops::claw_queue; |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 27 | using ::frc971::control_loops::drivetrain_queue; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 28 | using ::frc971::control_loops::fridge_queue; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 29 | using ::frc971::sensors::gyro_reading; |
| 30 | |
| 31 | using ::aos::input::driver_station::ButtonLocation; |
| 32 | using ::aos::input::driver_station::JoystickAxis; |
| 33 | using ::aos::input::driver_station::ControlBit; |
| 34 | |
| 35 | namespace frc971 { |
| 36 | namespace input { |
| 37 | namespace joysticks { |
| 38 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 39 | // preset motion limits |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame^] | 40 | constexpr actors::ProfileParams kArmMove{1.00, 1.0}; |
| 41 | constexpr actors::ProfileParams kElevatorMove{1.00, 3.2}; |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 42 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 43 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 44 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 45 | const ButtonLocation kQuickTurn(1, 5); |
| 46 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 47 | // TODO(danielp): Real buttons for all of these. |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 48 | const ButtonLocation kElevatorUp(3, 10); |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 49 | const ButtonLocation kElevatorCanUp(1, 1); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 50 | const ButtonLocation kElevatorDown(3, 3); |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 51 | const ButtonLocation kArmUp(3, 8); |
Austin Schuh | 29dabb6 | 2015-03-01 00:13:40 -0800 | [diff] [blame] | 52 | const ButtonLocation kArmHighUp(1, 4); |
Austin Schuh | 994d42c | 2015-03-01 00:02:17 -0800 | [diff] [blame] | 53 | const ButtonLocation kCanPickup(3, 8); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 54 | const ButtonLocation kArmDown(2, 6); |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 55 | const ButtonLocation kClawUp(3, 7); |
| 56 | const ButtonLocation kClawDown(3, 6); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 57 | const ButtonLocation kClawOpen(3, 11); |
| 58 | const ButtonLocation kClawClosed(3, 5); |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 59 | const ButtonLocation kFridgeClosed(3, 1); |
| 60 | const ButtonLocation kFridgeOpen(2, 11); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 61 | const ButtonLocation kRollersIn(3, 4); |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 62 | const ButtonLocation kRollersOut(1, 9); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 63 | const ButtonLocation kClawMiddle(3, 2); |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 64 | const ButtonLocation kPickup(2, 10); |
| 65 | const ButtonLocation kZero(2, 7); |
| 66 | |
| 67 | const ButtonLocation kStack(3, 9); |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 68 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 69 | // TODO(ben): Real buttons for all of these. |
| 70 | const ButtonLocation kArmPresetOne(99, 99); |
| 71 | const ButtonLocation kArmPresetTwo(99, 99); |
| 72 | const ButtonLocation kElevatorPresetOne(99, 99); |
| 73 | const ButtonLocation kElevatorPresetTwo(99, 99); |
| 74 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 75 | // Testing mode. |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 76 | const double kElevatorVelocity = 0.5; |
| 77 | const double kArmVelocity = 0.5; |
| 78 | const double kClawVelocity = 2.0; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 79 | // TODO(danielp): Verify. |
| 80 | const double kJoystickDt = 0.01; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 81 | |
| 82 | class Reader : public ::aos::input::JoystickInput { |
| 83 | public: |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 84 | Reader() : was_running_(false) {} |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 85 | |
| 86 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 87 | bool last_auto_running = auto_running_; |
| 88 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
Brian Silverman | e1103e6 | 2015-02-15 02:03:38 -0500 | [diff] [blame] | 89 | data.GetControlBit(ControlBit::kEnabled); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 90 | if (auto_running_ != last_auto_running) { |
| 91 | if (auto_running_) { |
| 92 | StartAuto(); |
| 93 | } else { |
| 94 | StopAuto(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 95 | } |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 99 | HandleDrivetrain(data); |
| 100 | if (data.GetControlBit(ControlBit::kTestMode)) { |
| 101 | HandleTest(data); |
| 102 | } else { |
| 103 | HandleTeleop(data); |
| 104 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 109 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 110 | const double throttle = -data.GetAxis(kDriveThrottle); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 111 | |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 112 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 113 | .steering(wheel) |
| 114 | .throttle(throttle) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 115 | .quickturn(data.IsPressed(kQuickTurn)) |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 116 | .control_loop_driving(false) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 117 | .Send()) { |
| 118 | LOG(WARNING, "sending stick values failed\n"); |
| 119 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 123 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 124 | action_queue_.CancelAllActions(); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 125 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 128 | if (data.PosEdge(kElevatorUp)) { |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 129 | actors::LiftParams params; |
| 130 | params.lift_height = 0.45; |
| 131 | params.lift_arm = 0.2; |
| 132 | action_queue_.EnqueueAction(actors::MakeLiftAction(params)); |
| 133 | |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 134 | claw_goal_ = 0.0; |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 135 | if (!claw_queue.goal.MakeWithBuilder() |
| 136 | .angle(claw_goal_) |
| 137 | .rollers_closed(claw_rollers_closed_) |
| 138 | .intake(0.0) |
| 139 | .Send()) { |
| 140 | LOG(ERROR, "Sending claw goal failed.\n"); |
| 141 | } |
| 142 | } |
| 143 | if (data.PosEdge(kStack)) { |
| 144 | actors::StackParams params; |
| 145 | params.claw_out_angle = 0.6; |
| 146 | action_queue_.EnqueueAction(actors::MakeStackAction(params)); |
| 147 | } |
Austin Schuh | 994d42c | 2015-03-01 00:02:17 -0800 | [diff] [blame] | 148 | |
| 149 | if (data.PosEdge(kCanPickup)) { |
| 150 | actors::CanPickupParams params; |
| 151 | params.pickup_angle = -0.93; |
| 152 | params.pickup_height = 0.265; |
| 153 | params.lift_height = 0.65; |
| 154 | params.end_height = 0.4; |
| 155 | params.end_angle = 0.0; |
| 156 | action_queue_.EnqueueAction(actors::MakeCanPickupAction(params)); |
| 157 | } |
| 158 | |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 159 | if (data.PosEdge(kPickup)) { |
| 160 | actors::PickupParams params; |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 161 | // Lift to here initially. |
| 162 | params.pickup_angle = 0.9; |
| 163 | // Start sucking here |
| 164 | params.suck_angle = 0.8; |
| 165 | // Go back down to here to finish sucking. |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 166 | params.suck_angle_finish = 0.4; |
Austin Schuh | e4e59ef | 2015-03-01 00:05:37 -0800 | [diff] [blame] | 167 | // Pack the box back in here. |
| 168 | params.pickup_finish_angle = 0.95; |
| 169 | params.intake_time = 0.8; |
| 170 | params.intake_voltage = 9.0; |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 171 | action_queue_.EnqueueAction(actors::MakePickupAction(params)); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 172 | } |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 173 | |
| 174 | if (data.PosEdge(kElevatorCanUp)) { |
| 175 | actors::HorizontalCanPickupParams params; |
| 176 | params.elevator_height = 0.3; |
| 177 | params.pickup_angle = 0.54; |
| 178 | params.suck_time = 0.05; |
| 179 | params.suck_power = 8.0; |
| 180 | |
| 181 | params.claw_settle_time = 0.05; |
| 182 | params.claw_settle_power = 5.0; |
| 183 | params.claw_full_lift_angle = 1.35; |
| 184 | params.claw_end_angle = 0.5; |
| 185 | action_queue_.EnqueueAction( |
| 186 | actors::MakeHorizontalCanPickupAction(params)); |
| 187 | } |
| 188 | |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 189 | if (data.PosEdge(kElevatorDown)) { |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 190 | claw_goal_ = 0.0; |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame^] | 191 | arm_goal_ = 0.0; |
| 192 | elevator_goal_ = 0.035; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if (data.PosEdge(kClawMiddle)) { |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 196 | claw_goal_ = 0.8; |
| 197 | } |
| 198 | |
Austin Schuh | 29dabb6 | 2015-03-01 00:13:40 -0800 | [diff] [blame] | 199 | if (data.PosEdge(kArmHighUp)) { |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 200 | claw_goal_ = 1.3; |
Austin Schuh | 29dabb6 | 2015-03-01 00:13:40 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 203 | if (data.PosEdge(kZero)) { |
| 204 | elevator_goal_ = 0.0; |
| 205 | arm_goal_ = 0.0; |
| 206 | claw_goal_ = 0.0; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 207 | } |
Austin Schuh | 29dabb6 | 2015-03-01 00:13:40 -0800 | [diff] [blame] | 208 | if (data.PosEdge(kClawUp)) { |
| 209 | claw_goal_ = 0.6; |
| 210 | } |
| 211 | if (data.PosEdge(kClawDown)) { |
| 212 | claw_goal_ = 0.0; |
| 213 | } |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 214 | |
| 215 | if (data.PosEdge(kClawClosed)) { |
| 216 | claw_rollers_closed_ = true; |
| 217 | } |
| 218 | if (data.PosEdge(kClawOpen)) { |
| 219 | claw_rollers_closed_ = false; |
| 220 | } |
| 221 | |
| 222 | if (data.PosEdge(kFridgeClosed)) { |
| 223 | fridge_closed_ = true; |
| 224 | } |
| 225 | if (data.PosEdge(kFridgeOpen)) { |
| 226 | fridge_closed_ = false; |
| 227 | } |
| 228 | |
| 229 | if (data.PosEdge(ControlBit::kEnabled)) { |
| 230 | // If we got enabled, wait for everything to zero. |
| 231 | LOG(INFO, "Waiting for zero.\n"); |
| 232 | waiting_for_zero_ = true; |
| 233 | } |
| 234 | |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 235 | claw_queue.status.FetchLatest(); |
| 236 | fridge_queue.status.FetchLatest(); |
| 237 | if (!claw_queue.status.get()) { |
| 238 | LOG(ERROR, "Got no claw status packet.\n"); |
| 239 | // Not safe to continue. |
| 240 | return; |
| 241 | } |
| 242 | if (!fridge_queue.status.get()) { |
| 243 | LOG(ERROR, "Got no fridge status packet.\n"); |
| 244 | return; |
| 245 | } |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 246 | |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 247 | if (claw_queue.status->zeroed && fridge_queue.status->zeroed) { |
| 248 | if (waiting_for_zero_) { |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 249 | LOG(INFO, "Zeroed! Starting teleop mode.\n"); |
| 250 | waiting_for_zero_ = false; |
| 251 | |
| 252 | // Set the initial goals to where we are now. |
| 253 | elevator_goal_ = fridge_queue.status->goal_height; |
| 254 | arm_goal_ = fridge_queue.status->goal_angle; |
| 255 | claw_goal_ = claw_queue.status->angle; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 256 | } |
| 257 | } else { |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 258 | waiting_for_zero_ = true; |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | if (!waiting_for_zero_) { |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 263 | if (!action_queue_.Running()) { |
| 264 | auto new_fridge_goal = fridge_queue.goal.MakeMessage(); |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame^] | 265 | new_fridge_goal->max_velocity = elevator_params_.velocity; |
| 266 | new_fridge_goal->max_acceleration = elevator_params_.acceleration; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 267 | new_fridge_goal->height = elevator_goal_; |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame^] | 268 | new_fridge_goal->velocity = 0.0; |
| 269 | new_fridge_goal->max_angular_velocity = arm_params_.velocity; |
| 270 | new_fridge_goal->max_angular_acceleration = arm_params_.acceleration; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 271 | new_fridge_goal->angle = arm_goal_; |
| 272 | new_fridge_goal->angular_velocity = 0.0; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 273 | new_fridge_goal->grabbers.top_front = fridge_closed_; |
| 274 | new_fridge_goal->grabbers.top_back = fridge_closed_; |
| 275 | new_fridge_goal->grabbers.bottom_front = fridge_closed_; |
| 276 | new_fridge_goal->grabbers.bottom_back = fridge_closed_; |
| 277 | |
| 278 | if (!new_fridge_goal.Send()) { |
| 279 | LOG(ERROR, "Sending fridge goal failed.\n"); |
| 280 | } else { |
| 281 | LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_, |
| 282 | arm_goal_); |
| 283 | } |
| 284 | if (!claw_queue.goal.MakeWithBuilder() |
| 285 | .angle(claw_goal_) |
| 286 | .rollers_closed(claw_rollers_closed_) |
Austin Schuh | 700b922 | 2015-03-01 03:03:15 -0800 | [diff] [blame] | 287 | .intake(data.IsPressed(kRollersIn) |
| 288 | ? 12.0 |
| 289 | : (data.IsPressed(kRollersOut) ? -12.0 : 0.0)) |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 290 | .Send()) { |
| 291 | LOG(ERROR, "Sending claw goal failed.\n"); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Austin Schuh | d8b2a24 | 2015-02-22 21:46:53 -0800 | [diff] [blame] | 296 | if (action_queue_.Running()) { |
| 297 | // If we are running an action, update our goals to the current goals. |
| 298 | control_loops::fridge_queue.status.FetchLatest(); |
| 299 | if (control_loops::fridge_queue.status.get()) { |
| 300 | arm_goal_ = control_loops::fridge_queue.status->goal_angle; |
| 301 | elevator_goal_ = control_loops::fridge_queue.status->goal_height; |
| 302 | } else { |
| 303 | LOG(ERROR, "No fridge status!\n"); |
| 304 | } |
| 305 | |
| 306 | // If we are running an action, update our goals to the current goals. |
| 307 | control_loops::claw_queue.status.FetchLatest(); |
| 308 | if (control_loops::claw_queue.status.get()) { |
| 309 | claw_goal_ = control_loops::claw_queue.status->goal_angle; |
| 310 | } else { |
| 311 | LOG(ERROR, "No fridge status!\n"); |
| 312 | } |
| 313 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 314 | action_queue_.Tick(); |
| 315 | was_running_ = action_queue_.Running(); |
| 316 | } |
| 317 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 318 | void HandleTest(const ::aos::input::driver_station::Data &data) { |
| 319 | if (action_queue_.Running()) { |
| 320 | // We don't really want any actions running. |
| 321 | LOG(DEBUG, "Cancelling actions for test mode.\n"); |
| 322 | action_queue_.CancelAllActions(); |
| 323 | } |
| 324 | |
| 325 | if (data.GetControlBit(ControlBit::kEnabled)) { |
| 326 | if (data.PosEdge(ControlBit::kEnabled)) { |
| 327 | // If we got enabled, wait for everything to zero. |
| 328 | LOG(INFO, "Waiting for zero.\n"); |
| 329 | waiting_for_zero_ = true; |
| 330 | } |
| 331 | if (waiting_for_zero_) { |
| 332 | claw_queue.status.FetchLatest(); |
| 333 | fridge_queue.status.FetchLatest(); |
| 334 | if (!claw_queue.status.get()) { |
| 335 | LOG(ERROR, "Got no claw status packet.\n"); |
| 336 | // Not safe to continue. |
| 337 | return; |
| 338 | } |
| 339 | if (!fridge_queue.status.get()) { |
| 340 | LOG(ERROR, "Got no fridge status packet.\n"); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if (claw_queue.status->zeroed && fridge_queue.status->zeroed) { |
| 345 | LOG(INFO, "Zeroed! Starting test mode.\n"); |
| 346 | waiting_for_zero_ = false; |
| 347 | |
| 348 | // Set the initial goals to where we are now. |
| 349 | elevator_goal_ = fridge_queue.status->height; |
| 350 | arm_goal_ = fridge_queue.status->angle; |
| 351 | claw_goal_ = claw_queue.status->angle; |
| 352 | } else { |
| 353 | return; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // These buttons move a subsystem up or down for as long as they are |
| 358 | // pressed, at low velocity. |
| 359 | if (data.IsPressed(kElevatorUp)) { |
| 360 | elevator_goal_ += kElevatorVelocity * kJoystickDt; |
| 361 | } |
| 362 | if (data.IsPressed(kElevatorDown)) { |
| 363 | elevator_goal_ -= kElevatorVelocity * kJoystickDt; |
| 364 | } |
| 365 | if (data.IsPressed(kArmUp)) { |
| 366 | arm_goal_ += kArmVelocity * kJoystickDt; |
| 367 | } |
| 368 | if (data.IsPressed(kArmDown)) { |
| 369 | arm_goal_ -= kArmVelocity * kJoystickDt; |
| 370 | } |
| 371 | if (data.IsPressed(kClawUp)) { |
| 372 | claw_goal_ += kClawVelocity * kJoystickDt; |
| 373 | } |
| 374 | if (data.IsPressed(kClawDown)) { |
| 375 | claw_goal_ -= kClawVelocity * kJoystickDt; |
| 376 | } |
| 377 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 378 | if (!action_queue_.Running()) { |
| 379 | if (!fridge_queue.goal.MakeWithBuilder() |
| 380 | .height(elevator_goal_) |
| 381 | .angle(arm_goal_) |
| 382 | .Send()) { |
| 383 | LOG(ERROR, "Sending fridge goal failed.\n"); |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 384 | } else { |
| 385 | LOG(DEBUG, "sending goals: elevator: %f, arm: %f\n", elevator_goal_, |
| 386 | arm_goal_); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 387 | } |
| 388 | if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) { |
| 389 | LOG(ERROR, "Sending claw goal failed.\n"); |
| 390 | } |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 391 | } |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 392 | /* |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 393 | if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) { |
| 394 | actors::FridgeProfileParams fridge_params; |
| 395 | fridge_params.arm_max_velocity = kArmDebugVelocity; |
| 396 | fridge_params.arm_max_acceleration = kArmDebugAcceleration; |
| 397 | if (data.IsPressed(kArmPresetOne)) { |
| 398 | LOG(INFO, "Preset asked for test arm position one position.\n"); |
| 399 | fridge_params.arm_angle = M_PI / 4.0; |
| 400 | fridge_params.top_front_grabber = false; |
| 401 | fridge_params.top_back_grabber = false; |
| 402 | fridge_params.bottom_front_grabber = false; |
| 403 | fridge_params.bottom_back_grabber = false; |
| 404 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 405 | } else if (data.IsPressed(kArmPresetTwo)) { |
| 406 | LOG(INFO, "Preset asked for test arm position two position.\n"); |
| 407 | fridge_params.arm_angle = -M_PI / 4.0; |
| 408 | fridge_params.top_front_grabber = true; |
| 409 | fridge_params.top_back_grabber = true; |
| 410 | fridge_params.bottom_front_grabber = true; |
| 411 | fridge_params.bottom_back_grabber = true; |
| 412 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 413 | } |
| 414 | } else if (data.IsPressed(kElevatorPresetOne) || |
| 415 | data.IsPressed(kElevatorPresetOne)) { |
| 416 | actors::FridgeProfileParams fridge_params; |
| 417 | fridge_params.elevator_max_velocity = kElevatorDebugVelocity; |
| 418 | fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration; |
| 419 | if (data.IsPressed(kElevatorPresetOne)) { |
| 420 | LOG(INFO, "Preset asked for test elevator position one position.\n"); |
| 421 | fridge_params.elevator_height = 0.5; |
| 422 | fridge_params.top_front_grabber = false; |
| 423 | fridge_params.top_back_grabber = false; |
| 424 | fridge_params.bottom_front_grabber = false; |
| 425 | fridge_params.bottom_back_grabber = false; |
| 426 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 427 | } else if (data.IsPressed(kElevatorPresetTwo)) { |
| 428 | LOG(INFO, "Preset asked for test elevator position two position.\n"); |
| 429 | fridge_params.elevator_height = 1.2; |
| 430 | fridge_params.top_front_grabber = true; |
| 431 | fridge_params.top_back_grabber = true; |
| 432 | fridge_params.bottom_front_grabber = true; |
| 433 | fridge_params.bottom_back_grabber = true; |
| 434 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 435 | } |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 436 | } |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 437 | */ |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 441 | private: |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 442 | void StartAuto() { |
| 443 | LOG(INFO, "Starting auto mode\n"); |
| 444 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 445 | } |
| 446 | |
| 447 | void StopAuto() { |
| 448 | LOG(INFO, "Stopping auto mode\n"); |
| 449 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 450 | } |
| 451 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 452 | bool was_running_; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 453 | |
| 454 | // Previous goals for systems. |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 455 | double elevator_goal_ = 0.2; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 456 | double arm_goal_ = 0.0; |
| 457 | double claw_goal_ = 0.0; |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 458 | bool claw_rollers_closed_ = false; |
| 459 | bool fridge_closed_ = false; |
Austin Schuh | 6e242ac | 2015-03-07 17:08:21 -0800 | [diff] [blame^] | 460 | actors::ProfileParams arm_params_ = kArmMove; |
| 461 | actors::ProfileParams elevator_params_ = kElevatorMove; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 462 | |
| 463 | // If we're waiting for the subsystems to zero. |
Austin Schuh | cde7ffe | 2015-02-20 22:10:41 -0800 | [diff] [blame] | 464 | bool waiting_for_zero_ = true; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 465 | |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 466 | bool auto_running_ = false; |
| 467 | |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 468 | ::aos::common::actions::ActionQueue action_queue_; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 469 | |
| 470 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 471 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 472 | "no drivetrain status"); |
| 473 | }; |
| 474 | |
| 475 | } // namespace joysticks |
| 476 | } // namespace input |
| 477 | } // namespace frc971 |
| 478 | |
| 479 | int main() { |
| 480 | ::aos::Init(); |
| 481 | ::frc971::input::joysticks::Reader reader; |
| 482 | reader.Run(); |
| 483 | ::aos::Cleanup(); |
| 484 | } |