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" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 20 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 21 | using ::frc971::control_loops::claw_queue; |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 22 | using ::frc971::control_loops::drivetrain_queue; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 23 | using ::frc971::control_loops::fridge_queue; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 24 | using ::frc971::sensors::gyro_reading; |
| 25 | |
| 26 | using ::aos::input::driver_station::ButtonLocation; |
| 27 | using ::aos::input::driver_station::JoystickAxis; |
| 28 | using ::aos::input::driver_station::ControlBit; |
| 29 | |
| 30 | namespace frc971 { |
| 31 | namespace input { |
| 32 | namespace joysticks { |
| 33 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 34 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 35 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 36 | const ButtonLocation kQuickTurn(1, 5); |
| 37 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 38 | // TODO(danielp): Real buttons for all of these. |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame^] | 39 | const ButtonLocation kElevatorUp(3, 10); |
| 40 | const ButtonLocation kElevatorDown(2, 6); |
| 41 | const ButtonLocation kArmUp(3, 8); |
| 42 | const ButtonLocation kArmDown(3, 3); |
| 43 | const ButtonLocation kClawUp(3, 7); |
| 44 | const ButtonLocation kClawDown(3, 6); |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 45 | |
| 46 | // Testing mode. |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame^] | 47 | const double kElevatorVelocity = 0.5; |
| 48 | const double kArmVelocity = 0.5; |
| 49 | const double kClawVelocity = 2.0; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 50 | // TODO(danielp): Verify. |
| 51 | const double kJoystickDt = 0.01; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 52 | |
| 53 | class Reader : public ::aos::input::JoystickInput { |
| 54 | public: |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 55 | Reader() : was_running_(false) {} |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 56 | |
| 57 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 58 | bool last_auto_running = auto_running_; |
| 59 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
Brian Silverman | e1103e6 | 2015-02-15 02:03:38 -0500 | [diff] [blame] | 60 | data.GetControlBit(ControlBit::kEnabled); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 61 | if (auto_running_ != last_auto_running) { |
| 62 | if (auto_running_) { |
| 63 | StartAuto(); |
| 64 | } else { |
| 65 | StopAuto(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 66 | } |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 70 | HandleDrivetrain(data); |
| 71 | if (data.GetControlBit(ControlBit::kTestMode)) { |
| 72 | HandleTest(data); |
| 73 | } else { |
| 74 | HandleTeleop(data); |
| 75 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 80 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 81 | const double throttle = -data.GetAxis(kDriveThrottle); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 82 | |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 83 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 84 | .steering(wheel) |
| 85 | .throttle(throttle) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 86 | .quickturn(data.IsPressed(kQuickTurn)) |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 87 | .control_loop_driving(false) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 88 | .Send()) { |
| 89 | LOG(WARNING, "sending stick values failed\n"); |
| 90 | } |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 94 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 95 | action_queue_.CancelAllActions(); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 96 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | action_queue_.Tick(); |
| 100 | was_running_ = action_queue_.Running(); |
| 101 | } |
| 102 | |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 103 | void HandleTest(const ::aos::input::driver_station::Data &data) { |
| 104 | if (action_queue_.Running()) { |
| 105 | // We don't really want any actions running. |
| 106 | LOG(DEBUG, "Cancelling actions for test mode.\n"); |
| 107 | action_queue_.CancelAllActions(); |
| 108 | } |
| 109 | |
| 110 | if (data.GetControlBit(ControlBit::kEnabled)) { |
| 111 | if (data.PosEdge(ControlBit::kEnabled)) { |
| 112 | // If we got enabled, wait for everything to zero. |
| 113 | LOG(INFO, "Waiting for zero.\n"); |
| 114 | waiting_for_zero_ = true; |
| 115 | } |
| 116 | if (waiting_for_zero_) { |
| 117 | claw_queue.status.FetchLatest(); |
| 118 | fridge_queue.status.FetchLatest(); |
| 119 | if (!claw_queue.status.get()) { |
| 120 | LOG(ERROR, "Got no claw status packet.\n"); |
| 121 | // Not safe to continue. |
| 122 | return; |
| 123 | } |
| 124 | if (!fridge_queue.status.get()) { |
| 125 | LOG(ERROR, "Got no fridge status packet.\n"); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (claw_queue.status->zeroed && fridge_queue.status->zeroed) { |
| 130 | LOG(INFO, "Zeroed! Starting test mode.\n"); |
| 131 | waiting_for_zero_ = false; |
| 132 | |
| 133 | // Set the initial goals to where we are now. |
| 134 | elevator_goal_ = fridge_queue.status->height; |
| 135 | arm_goal_ = fridge_queue.status->angle; |
| 136 | claw_goal_ = claw_queue.status->angle; |
| 137 | } else { |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // These buttons move a subsystem up or down for as long as they are |
| 143 | // pressed, at low velocity. |
| 144 | if (data.IsPressed(kElevatorUp)) { |
| 145 | elevator_goal_ += kElevatorVelocity * kJoystickDt; |
| 146 | } |
| 147 | if (data.IsPressed(kElevatorDown)) { |
| 148 | elevator_goal_ -= kElevatorVelocity * kJoystickDt; |
| 149 | } |
| 150 | if (data.IsPressed(kArmUp)) { |
| 151 | arm_goal_ += kArmVelocity * kJoystickDt; |
| 152 | } |
| 153 | if (data.IsPressed(kArmDown)) { |
| 154 | arm_goal_ -= kArmVelocity * kJoystickDt; |
| 155 | } |
| 156 | if (data.IsPressed(kClawUp)) { |
| 157 | claw_goal_ += kClawVelocity * kJoystickDt; |
| 158 | } |
| 159 | if (data.IsPressed(kClawDown)) { |
| 160 | claw_goal_ -= kClawVelocity * kJoystickDt; |
| 161 | } |
| 162 | |
| 163 | if (!fridge_queue.goal.MakeWithBuilder() |
| 164 | .height(elevator_goal_) |
| 165 | .angle(arm_goal_) |
| 166 | .Send()) { |
| 167 | LOG(ERROR, "Sending fridge goal failed.\n"); |
| 168 | } |
| 169 | if (!claw_queue.goal.MakeWithBuilder() |
| 170 | .angle(claw_goal_) |
| 171 | .Send()) { |
| 172 | LOG(ERROR, "Sending claw goal failed.\n"); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 177 | private: |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 178 | void StartAuto() { |
| 179 | LOG(INFO, "Starting auto mode\n"); |
| 180 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 181 | } |
| 182 | |
| 183 | void StopAuto() { |
| 184 | LOG(INFO, "Stopping auto mode\n"); |
| 185 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 186 | } |
| 187 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 188 | bool was_running_; |
Daniel Petti | 6189652 | 2015-02-15 18:01:43 -0800 | [diff] [blame] | 189 | |
| 190 | // Previous goals for systems. |
| 191 | double elevator_goal_ = 0.0; |
| 192 | double arm_goal_ = 0.0; |
| 193 | double claw_goal_ = 0.0; |
| 194 | |
| 195 | // If we're waiting for the subsystems to zero. |
| 196 | bool waiting_for_zero_ = false; |
| 197 | |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 198 | bool auto_running_ = false; |
| 199 | |
Austin Schuh | 331e13d | 2015-02-15 00:16:51 -0800 | [diff] [blame] | 200 | ::aos::common::actions::ActionQueue action_queue_; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 201 | |
| 202 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 203 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 204 | "no drivetrain status"); |
| 205 | }; |
| 206 | |
| 207 | } // namespace joysticks |
| 208 | } // namespace input |
| 209 | } // namespace frc971 |
| 210 | |
| 211 | int main() { |
| 212 | ::aos::Init(); |
| 213 | ::frc971::input::joysticks::Reader reader; |
| 214 | reader.Run(); |
| 215 | ::aos::Cleanup(); |
| 216 | } |