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 | |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 15 | #include "frc971/constants.h" |
| 16 | #include "frc971/queues/gyro.q.h" |
| 17 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 18 | |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 19 | using ::frc971::control_loops::drivetrain_queue; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 20 | using ::frc971::sensors::gyro_reading; |
| 21 | |
| 22 | using ::aos::input::driver_station::ButtonLocation; |
| 23 | using ::aos::input::driver_station::JoystickAxis; |
| 24 | using ::aos::input::driver_station::ControlBit; |
| 25 | |
| 26 | namespace frc971 { |
| 27 | namespace input { |
| 28 | namespace joysticks { |
| 29 | |
| 30 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 31 | kDriveControlLoopEnable2(1, 11); |
| 32 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 33 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 34 | const ButtonLocation kQuickTurn(1, 5); |
| 35 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 36 | |
| 37 | class Reader : public ::aos::input::JoystickInput { |
| 38 | public: |
| 39 | Reader() |
| 40 | : is_high_gear_(false), |
| 41 | was_running_(false) {} |
| 42 | |
| 43 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 44 | bool last_auto_running = auto_running_; |
| 45 | auto_running_ = data.GetControlBit(ControlBit::kAutonomous) && |
| 46 | data.IsPressed(ControlBit::kEnabled); |
| 47 | if (auto_running_ != last_auto_running) { |
| 48 | if (auto_running_) { |
| 49 | StartAuto(); |
| 50 | } else { |
| 51 | StopAuto(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 52 | } |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | if (!data.GetControlBit(ControlBit::kAutonomous)) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 56 | HandleTeleop(data); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 61 | bool is_control_loop_driving = false; |
| 62 | double left_goal = 0.0; |
| 63 | double right_goal = 0.0; |
| 64 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 65 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 66 | const double kThrottleGain = 1.0 / 2.5; |
| 67 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 68 | data.IsPressed(kDriveControlLoopEnable2))) { |
| 69 | // TODO(austin): Static sucks! |
| 70 | static double distance = 0.0; |
| 71 | static double angle = 0.0; |
| 72 | static double filtered_goal_distance = 0.0; |
| 73 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 74 | data.PosEdge(kDriveControlLoopEnable2)) { |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 75 | if (drivetrain_queue.position.FetchLatest() && |
| 76 | gyro_reading.FetchLatest()) { |
| 77 | distance = (drivetrain_queue.position->left_encoder + |
| 78 | drivetrain_queue.position->right_encoder) / |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 79 | 2.0 - |
| 80 | throttle * kThrottleGain / 2.0; |
| 81 | angle = gyro_reading->angle; |
| 82 | filtered_goal_distance = distance; |
| 83 | } |
| 84 | } |
| 85 | is_control_loop_driving = true; |
| 86 | |
| 87 | // const double gyro_angle = Gyro.View().angle; |
| 88 | const double goal_theta = angle - wheel * 0.27; |
| 89 | const double goal_distance = distance + throttle * kThrottleGain; |
| 90 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 91 | const double kMaxVelocity = 0.6; |
| 92 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 93 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 94 | } else if (goal_distance < |
| 95 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 96 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 97 | } else { |
| 98 | filtered_goal_distance = goal_distance; |
| 99 | } |
| 100 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 101 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 102 | is_high_gear_ = false; |
| 103 | |
| 104 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 105 | } |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 106 | if (!drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 107 | .steering(wheel) |
| 108 | .throttle(throttle) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 109 | //.highgear(is_high_gear_) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 110 | .quickturn(data.IsPressed(kQuickTurn)) |
| 111 | .control_loop_driving(is_control_loop_driving) |
| 112 | .left_goal(left_goal) |
| 113 | .right_goal(right_goal) |
| 114 | .left_velocity_goal(0) |
| 115 | .right_velocity_goal(0) |
| 116 | .Send()) { |
| 117 | LOG(WARNING, "sending stick values failed\n"); |
| 118 | } |
| 119 | if (data.PosEdge(kShiftHigh)) { |
| 120 | is_high_gear_ = false; |
| 121 | } |
| 122 | if (data.PosEdge(kShiftLow)) { |
| 123 | is_high_gear_ = true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 128 | HandleDrivetrain(data); |
| 129 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 130 | action_queue_.CancelAllActions(); |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 131 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | action_queue_.Tick(); |
| 135 | was_running_ = action_queue_.Running(); |
| 136 | } |
| 137 | |
| 138 | private: |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 139 | void StartAuto() { |
| 140 | LOG(INFO, "Starting auto mode\n"); |
| 141 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(true).Send(); |
| 142 | } |
| 143 | |
| 144 | void StopAuto() { |
| 145 | LOG(INFO, "Stopping auto mode\n"); |
| 146 | ::frc971::autonomous::autonomous.MakeWithBuilder().run_auto(false).Send(); |
| 147 | } |
| 148 | |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 149 | bool is_high_gear_; |
| 150 | bool was_running_; |
| 151 | |
Austin Schuh | 6182f8d | 2015-02-14 22:15:04 -0800 | [diff] [blame] | 152 | bool auto_running_ = false; |
| 153 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame^] | 154 | aos::common::actions::ActionQueue action_queue_; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 155 | |
| 156 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 157 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 158 | "no drivetrain status"); |
| 159 | }; |
| 160 | |
| 161 | } // namespace joysticks |
| 162 | } // namespace input |
| 163 | } // namespace frc971 |
| 164 | |
| 165 | int main() { |
| 166 | ::aos::Init(); |
| 167 | ::frc971::input::joysticks::Reader reader; |
| 168 | reader.Run(); |
| 169 | ::aos::Cleanup(); |
| 170 | } |