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