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_action.q.h" |
| 21 | #include "frc971/actors/fridge_profile_actor.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 22 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 23 | using ::frc971::control_loops::claw_queue; |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 24 | using ::frc971::control_loops::drivetrain_queue; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 25 | using ::frc971::control_loops::fridge_queue; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 26 | using ::frc971::sensors::gyro_reading; |
| 27 | |
| 28 | using ::aos::input::driver_station::ButtonLocation; |
| 29 | using ::aos::input::driver_station::JoystickAxis; |
| 30 | using ::aos::input::driver_station::ControlBit; |
| 31 | |
| 32 | namespace frc971 { |
| 33 | namespace input { |
| 34 | namespace joysticks { |
| 35 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 36 | // preset motion limits |
| 37 | static const double kArmDebugVelocity = 0.17; |
| 38 | static const double kArmDebugAcceleration = 0.8; |
| 39 | static const double kElevatorDebugVelocity = 0.2; |
| 40 | static const double kElevatorDebugAcceleration = 2.2; |
| 41 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 42 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 43 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 44 | const ButtonLocation kQuickTurn(1, 5); |
| 45 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 46 | // TODO(danielp): Real buttons for all of these. |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 47 | const ButtonLocation kElevatorUp(3, 10); |
| 48 | const ButtonLocation kElevatorDown(2, 6); |
| 49 | const ButtonLocation kArmUp(3, 8); |
| 50 | const ButtonLocation kArmDown(3, 3); |
| 51 | const ButtonLocation kClawUp(3, 7); |
| 52 | const ButtonLocation kClawDown(3, 6); |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 53 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 54 | // TODO(ben): Real buttons for all of these. |
| 55 | const ButtonLocation kArmPresetOne(99, 99); |
| 56 | const ButtonLocation kArmPresetTwo(99, 99); |
| 57 | const ButtonLocation kElevatorPresetOne(99, 99); |
| 58 | const ButtonLocation kElevatorPresetTwo(99, 99); |
| 59 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 60 | // Testing mode. |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 61 | const double kElevatorVelocity = 0.5; |
| 62 | const double kArmVelocity = 0.5; |
| 63 | const double kClawVelocity = 2.0; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 64 | // TODO(danielp): Verify. |
| 65 | const double kJoystickDt = 0.01; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 66 | |
| 67 | class Reader : public ::aos::input::JoystickInput { |
| 68 | public: |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 69 | Reader() : was_running_(false) {} |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 70 | |
| 71 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 72 | bool last_auto_running = auto_running_; |
| 73 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
Brian Silverman | e1103e6 | 2015-02-15 02:03:38 -0500 | [diff] [blame] | 74 | data.GetControlBit(ControlBit::kEnabled); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 75 | if (auto_running_ != last_auto_running) { |
| 76 | if (auto_running_) { |
| 77 | StartAuto(); |
| 78 | } else { |
| 79 | StopAuto(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 80 | } |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 84 | HandleDrivetrain(data); |
| 85 | if (data.GetControlBit(ControlBit::kTestMode)) { |
| 86 | HandleTest(data); |
| 87 | } else { |
| 88 | HandleTeleop(data); |
| 89 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 94 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 95 | const double throttle = -data.GetAxis(kDriveThrottle); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 96 | |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 97 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 98 | .steering(wheel) |
| 99 | .throttle(throttle) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 100 | .quickturn(data.IsPressed(kQuickTurn)) |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 101 | .control_loop_driving(false) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 102 | .Send()) { |
| 103 | LOG(WARNING, "sending stick values failed\n"); |
| 104 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 108 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 109 | action_queue_.CancelAllActions(); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 110 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | action_queue_.Tick(); |
| 114 | was_running_ = action_queue_.Running(); |
| 115 | } |
| 116 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 117 | void HandleTest(const ::aos::input::driver_station::Data &data) { |
| 118 | if (action_queue_.Running()) { |
| 119 | // We don't really want any actions running. |
| 120 | LOG(DEBUG, "Cancelling actions for test mode.\n"); |
| 121 | action_queue_.CancelAllActions(); |
| 122 | } |
| 123 | |
| 124 | if (data.GetControlBit(ControlBit::kEnabled)) { |
| 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; |
| 129 | } |
| 130 | if (waiting_for_zero_) { |
| 131 | claw_queue.status.FetchLatest(); |
| 132 | fridge_queue.status.FetchLatest(); |
| 133 | if (!claw_queue.status.get()) { |
| 134 | LOG(ERROR, "Got no claw status packet.\n"); |
| 135 | // Not safe to continue. |
| 136 | return; |
| 137 | } |
| 138 | if (!fridge_queue.status.get()) { |
| 139 | LOG(ERROR, "Got no fridge status packet.\n"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | if (claw_queue.status->zeroed && fridge_queue.status->zeroed) { |
| 144 | LOG(INFO, "Zeroed! Starting test mode.\n"); |
| 145 | waiting_for_zero_ = false; |
| 146 | |
| 147 | // Set the initial goals to where we are now. |
| 148 | elevator_goal_ = fridge_queue.status->height; |
| 149 | arm_goal_ = fridge_queue.status->angle; |
| 150 | claw_goal_ = claw_queue.status->angle; |
| 151 | } else { |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // These buttons move a subsystem up or down for as long as they are |
| 157 | // pressed, at low velocity. |
| 158 | if (data.IsPressed(kElevatorUp)) { |
| 159 | elevator_goal_ += kElevatorVelocity * kJoystickDt; |
| 160 | } |
| 161 | if (data.IsPressed(kElevatorDown)) { |
| 162 | elevator_goal_ -= kElevatorVelocity * kJoystickDt; |
| 163 | } |
| 164 | if (data.IsPressed(kArmUp)) { |
| 165 | arm_goal_ += kArmVelocity * kJoystickDt; |
| 166 | } |
| 167 | if (data.IsPressed(kArmDown)) { |
| 168 | arm_goal_ -= kArmVelocity * kJoystickDt; |
| 169 | } |
| 170 | if (data.IsPressed(kClawUp)) { |
| 171 | claw_goal_ += kClawVelocity * kJoystickDt; |
| 172 | } |
| 173 | if (data.IsPressed(kClawDown)) { |
| 174 | claw_goal_ -= kClawVelocity * kJoystickDt; |
| 175 | } |
| 176 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 177 | if (!action_queue_.Running()) { |
| 178 | if (!fridge_queue.goal.MakeWithBuilder() |
| 179 | .height(elevator_goal_) |
| 180 | .angle(arm_goal_) |
| 181 | .Send()) { |
| 182 | LOG(ERROR, "Sending fridge goal failed.\n"); |
| 183 | } |
| 184 | if (!claw_queue.goal.MakeWithBuilder().angle(claw_goal_).Send()) { |
| 185 | LOG(ERROR, "Sending claw goal failed.\n"); |
| 186 | } |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 187 | } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame^] | 188 | if (data.IsPressed(kArmPresetOne) || data.IsPressed(kArmPresetTwo)) { |
| 189 | actors::FridgeProfileParams fridge_params; |
| 190 | fridge_params.arm_max_velocity = kArmDebugVelocity; |
| 191 | fridge_params.arm_max_acceleration = kArmDebugAcceleration; |
| 192 | if (data.IsPressed(kArmPresetOne)) { |
| 193 | LOG(INFO, "Preset asked for test arm position one position.\n"); |
| 194 | fridge_params.arm_angle = M_PI / 4.0; |
| 195 | fridge_params.top_front_grabber = false; |
| 196 | fridge_params.top_back_grabber = false; |
| 197 | fridge_params.bottom_front_grabber = false; |
| 198 | fridge_params.bottom_back_grabber = false; |
| 199 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 200 | } else if (data.IsPressed(kArmPresetTwo)) { |
| 201 | LOG(INFO, "Preset asked for test arm position two position.\n"); |
| 202 | fridge_params.arm_angle = -M_PI / 4.0; |
| 203 | fridge_params.top_front_grabber = true; |
| 204 | fridge_params.top_back_grabber = true; |
| 205 | fridge_params.bottom_front_grabber = true; |
| 206 | fridge_params.bottom_back_grabber = true; |
| 207 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 208 | } |
| 209 | } else if (data.IsPressed(kElevatorPresetOne) || |
| 210 | data.IsPressed(kElevatorPresetOne)) { |
| 211 | actors::FridgeProfileParams fridge_params; |
| 212 | fridge_params.elevator_max_velocity = kElevatorDebugVelocity; |
| 213 | fridge_params.elevator_max_acceleration = kElevatorDebugAcceleration; |
| 214 | if (data.IsPressed(kElevatorPresetOne)) { |
| 215 | LOG(INFO, "Preset asked for test elevator position one position.\n"); |
| 216 | fridge_params.elevator_height = 0.5; |
| 217 | fridge_params.top_front_grabber = false; |
| 218 | fridge_params.top_back_grabber = false; |
| 219 | fridge_params.bottom_front_grabber = false; |
| 220 | fridge_params.bottom_back_grabber = false; |
| 221 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 222 | } else if (data.IsPressed(kElevatorPresetTwo)) { |
| 223 | LOG(INFO, "Preset asked for test elevator position two position.\n"); |
| 224 | fridge_params.elevator_height = 1.2; |
| 225 | fridge_params.top_front_grabber = true; |
| 226 | fridge_params.top_back_grabber = true; |
| 227 | fridge_params.bottom_front_grabber = true; |
| 228 | fridge_params.bottom_back_grabber = true; |
| 229 | action_queue_.EnqueueAction(MakeFridgeProfileAction(fridge_params)); |
| 230 | } |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 235 | private: |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 236 | void StartAuto() { |
| 237 | LOG(INFO, "Starting auto mode\n"); |
| 238 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 239 | } |
| 240 | |
| 241 | void StopAuto() { |
| 242 | LOG(INFO, "Stopping auto mode\n"); |
| 243 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 244 | } |
| 245 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 246 | bool was_running_; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 247 | |
| 248 | // Previous goals for systems. |
| 249 | double elevator_goal_ = 0.0; |
| 250 | double arm_goal_ = 0.0; |
| 251 | double claw_goal_ = 0.0; |
| 252 | |
| 253 | // If we're waiting for the subsystems to zero. |
| 254 | bool waiting_for_zero_ = false; |
| 255 | |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 256 | bool auto_running_ = false; |
| 257 | |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 258 | ::aos::common::actions::ActionQueue action_queue_; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 259 | |
| 260 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 261 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 262 | "no drivetrain status"); |
| 263 | }; |
| 264 | |
| 265 | } // namespace joysticks |
| 266 | } // namespace input |
| 267 | } // namespace frc971 |
| 268 | |
| 269 | int main() { |
| 270 | ::aos::Init(); |
| 271 | ::frc971::input::joysticks::Reader reader; |
| 272 | reader.Run(); |
| 273 | ::aos::Cleanup(); |
| 274 | } |