Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -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" |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame^] | 8 | #include "aos/common/input/driver_station_data.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
| 10 | |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/queues/gyro_angle.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 13 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/claw/claw.q.h" |
| 15 | #include "frc971/control_loops/shooter/shooter.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 16 | |
| 17 | using ::frc971::control_loops::drivetrain; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 18 | using ::frc971::sensors::gyro; |
| 19 | |
| 20 | using ::aos::input::driver_station::ButtonLocation; |
| 21 | using ::aos::input::driver_station::JoystickAxis; |
| 22 | using ::aos::input::driver_station::ControlBit; |
| 23 | |
| 24 | namespace frc971 { |
| 25 | namespace input { |
| 26 | namespace joysticks { |
| 27 | |
| 28 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 29 | kDriveControlLoopEnable2(1, 11); |
| 30 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 31 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 32 | const ButtonLocation kQuickTurn(1, 5); |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 33 | const ButtonLocation kClawClosed(3, 7); |
| 34 | const ButtonLocation kClawOpen(3, 9); |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame^] | 35 | const ButtonLocation kFire(3, 11); |
| 36 | const ButtonLocation kUnload(3, 12); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 37 | |
| 38 | class Reader : public ::aos::input::JoystickInput { |
| 39 | public: |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 40 | Reader() : closed_(true) {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 41 | |
| 42 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 43 | static bool is_high_gear = false; |
| 44 | |
| 45 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 46 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 47 | LOG(INFO, "Starting auto mode\n"); |
| 48 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 49 | .run_auto(true).Send(); |
| 50 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 51 | LOG(INFO, "Stopping auto mode\n"); |
| 52 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 53 | .run_auto(false).Send(); |
| 54 | } |
| 55 | } else { // teleop |
| 56 | bool is_control_loop_driving = false; |
| 57 | double left_goal = 0.0; |
| 58 | double right_goal = 0.0; |
| 59 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 60 | const double throttle = -data.GetAxis(kDriveThrottle); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 61 | const double kThrottleGain = 1.0 / 2.5; |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 62 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 63 | data.IsPressed(kDriveControlLoopEnable2))) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 64 | static double distance = 0.0; |
| 65 | static double angle = 0.0; |
| 66 | static double filtered_goal_distance = 0.0; |
| 67 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 68 | data.PosEdge(kDriveControlLoopEnable2)) { |
| 69 | if (drivetrain.position.FetchLatest() && gyro.FetchLatest()) { |
| 70 | distance = (drivetrain.position->left_encoder + |
| 71 | drivetrain.position->right_encoder) / 2.0 |
| 72 | - throttle * kThrottleGain / 2.0; |
| 73 | angle = gyro->angle; |
| 74 | filtered_goal_distance = distance; |
| 75 | } |
| 76 | } |
| 77 | is_control_loop_driving = true; |
| 78 | |
| 79 | //const double gyro_angle = Gyro.View().angle; |
| 80 | const double goal_theta = angle - wheel * 0.27; |
| 81 | const double goal_distance = distance + throttle * kThrottleGain; |
| 82 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 83 | const double kMaxVelocity = 0.6; |
| 84 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 85 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 86 | } else if (goal_distance < -kMaxVelocity * 0.02 + |
| 87 | filtered_goal_distance) { |
| 88 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 89 | } else { |
| 90 | filtered_goal_distance = goal_distance; |
| 91 | } |
| 92 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 93 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 94 | is_high_gear = false; |
| 95 | |
| 96 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 97 | } |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 98 | if (!drivetrain.goal.MakeWithBuilder() |
| 99 | .steering(wheel) |
| 100 | .throttle(throttle) |
| 101 | .highgear(is_high_gear).quickturn(data.IsPressed(kQuickTurn)) |
| 102 | .control_loop_driving(is_control_loop_driving) |
| 103 | .left_goal(left_goal).right_goal(right_goal).Send()) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 104 | LOG(WARNING, "sending stick values failed\n"); |
| 105 | } |
| 106 | |
| 107 | if (data.PosEdge(kShiftHigh)) { |
| 108 | is_high_gear = false; |
| 109 | } |
| 110 | if (data.PosEdge(kShiftLow)) { |
| 111 | is_high_gear = true; |
| 112 | } |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 113 | if (data.PosEdge(kClawClosed)) { |
| 114 | closed_ = true; |
| 115 | } |
| 116 | if (data.PosEdge(kClawOpen)) { |
| 117 | closed_ = false; |
| 118 | } |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 119 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 120 | double separation_angle = closed_ ? 0.0 : 0.5; |
| 121 | double goal_angle = closed_ ? 0.0 : -0.2; |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 122 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame^] | 123 | .bottom_angle(goal_angle) |
| 124 | .separation_angle(separation_angle) |
| 125 | .intake(false) |
| 126 | .Send()) { |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 127 | LOG(WARNING, "sending claw goal failed\n"); |
| 128 | } |
| 129 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame^] | 130 | .shot_power(0.25) |
| 131 | .shot_requested(data.IsPressed(kFire)) |
| 132 | .unload_requested(data.IsPressed(kUnload)) |
| 133 | .Send()) { |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 134 | LOG(WARNING, "sending shooter goal failed\n"); |
| 135 | } |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 138 | |
| 139 | private: |
| 140 | bool closed_; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace joysticks |
| 144 | } // namespace input |
| 145 | } // namespace frc971 |
| 146 | |
| 147 | int main() { |
| 148 | ::aos::Init(); |
| 149 | ::frc971::input::joysticks::Reader reader; |
| 150 | reader.Run(); |
| 151 | ::aos::Cleanup(); |
| 152 | } |