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