Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [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" |
| 12 | |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 13 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
| 14 | #include "bot3/control_loops/drivetrain/drivetrain_constants.h" |
| 15 | #include "bot3/control_loops/rollers/rollers.q.h" |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 16 | #include "frc971/autonomous/auto.q.h" |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 17 | #include "frc971/queues/other_sensors.q.h" |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 18 | |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 19 | using ::bot3::control_loops::drivetrain; |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [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 | |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 26 | namespace bot3 { |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 27 | namespace input { |
| 28 | namespace joysticks { |
| 29 | |
| 30 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 31 | kDriveControlLoopEnable2(1, 11); |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 32 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(4, 2); |
| 33 | const ButtonLocation kShiftHigh(4, 1), kShiftLow(4, 3); |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 34 | const ButtonLocation kQuickTurn(1, 5); |
| 35 | |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 36 | const ButtonLocation kFrontRollersIn(3, 5); |
| 37 | const ButtonLocation kBackRollersIn(3, 3); |
| 38 | const ButtonLocation kFrontRollersOut(3, 12); |
| 39 | const ButtonLocation kBackRollersOut(3, 8); |
| 40 | const ButtonLocation kHumanPlayer(3, 11); |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 41 | |
| 42 | class Reader : public ::aos::input::JoystickInput { |
| 43 | public: |
| 44 | Reader() |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 45 | : is_high_gear_(false) {} |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 46 | |
| 47 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
| 48 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 49 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 50 | LOG(INFO, "Starting auto mode\n"); |
| 51 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 52 | .run_auto(true) |
| 53 | .Send(); |
| 54 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 55 | LOG(INFO, "Stopping auto mode\n"); |
| 56 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
| 57 | .run_auto(false) |
| 58 | .Send(); |
| 59 | } else if (!data.GetControlBit(ControlBit::kEnabled)) { |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 60 | auto goal = drivetrain.goal.MakeMessage(); |
| 61 | goal->Zero(); |
| 62 | goal->control_loop_driving = false; |
| 63 | goal->left_goal = goal->right_goal = 0; |
| 64 | goal->left_velocity_goal = goal->right_velocity_goal = 0; |
| 65 | if (!goal.Send()) { |
| 66 | LOG(WARNING, "sending 0 drivetrain goal failed\n"); |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } else { |
| 70 | HandleTeleop(data); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 75 | bool is_control_loop_driving = false; |
| 76 | double left_goal = 0.0; |
| 77 | double right_goal = 0.0; |
| 78 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 79 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 80 | const double kThrottleGain = 1.0 / 2.5; |
| 81 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 82 | data.IsPressed(kDriveControlLoopEnable2))) { |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 83 | static double distance = 0.0; |
| 84 | static double angle = 0.0; |
| 85 | static double filtered_goal_distance = 0.0; |
| 86 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 87 | data.PosEdge(kDriveControlLoopEnable2)) { |
| 88 | if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) { |
| 89 | distance = (drivetrain.position->left_encoder + |
| 90 | drivetrain.position->right_encoder) / |
| 91 | 2.0 - |
| 92 | throttle * kThrottleGain / 2.0; |
| 93 | angle = gyro_reading->angle; |
| 94 | filtered_goal_distance = distance; |
| 95 | } |
| 96 | } |
| 97 | is_control_loop_driving = true; |
| 98 | |
| 99 | // const double gyro_angle = Gyro.View().angle; |
| 100 | const double goal_theta = angle - wheel * 0.27; |
| 101 | const double goal_distance = distance + throttle * kThrottleGain; |
| 102 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 103 | const double kMaxVelocity = 0.6; |
| 104 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 105 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 106 | } else if (goal_distance < |
| 107 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 108 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 109 | } else { |
| 110 | filtered_goal_distance = goal_distance; |
| 111 | } |
| 112 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 113 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 114 | is_high_gear_ = false; |
| 115 | |
| 116 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 117 | } |
| 118 | if (!drivetrain.goal.MakeWithBuilder() |
| 119 | .steering(wheel) |
| 120 | .throttle(throttle) |
| 121 | .highgear(is_high_gear_) |
| 122 | .quickturn(data.IsPressed(kQuickTurn)) |
| 123 | .control_loop_driving(is_control_loop_driving) |
| 124 | .left_goal(left_goal) |
| 125 | .right_goal(right_goal) |
| 126 | .left_velocity_goal(0) |
| 127 | .right_velocity_goal(0) |
| 128 | .Send()) { |
| 129 | LOG(WARNING, "sending stick values failed\n"); |
| 130 | } |
| 131 | if (data.PosEdge(kShiftHigh)) { |
| 132 | is_high_gear_ = false; |
| 133 | } |
| 134 | if (data.PosEdge(kShiftLow)) { |
| 135 | is_high_gear_ = true; |
| 136 | } |
| 137 | } |
| 138 | |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 139 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 140 | HandleDrivetrain(data); |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 141 | |
| 142 | // Rollers. |
| 143 | auto rollers_goal = control_loops::rollers.goal.MakeMessage(); |
| 144 | if (data.IsPressed(kFrontRollersIn)) { |
| 145 | rollers_goal->intake = 1; |
| 146 | } else if (data.IsPressed(kFrontRollersOut)) { |
| 147 | rollers_goal->low_spit = 1; |
| 148 | } else if (data.IsPressed(kBackRollersIn)) { |
| 149 | rollers_goal->intake = -1; |
| 150 | } else if (data.IsPressed(kBackRollersOut)) { |
| 151 | rollers_goal->low_spit = -1; |
| 152 | } else if (data.IsPressed(kHumanPlayer)) { |
| 153 | rollers_goal->human_player = true; |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 154 | } |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 155 | if (!rollers_goal.Send()) { |
| 156 | LOG(WARNING, "Sending rollers values failed.\n"); |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 157 | } |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
| 161 | bool is_high_gear_; |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | } // namespace joysticks |
| 165 | } // namespace input |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 166 | } // namespace bot3 |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 167 | |
| 168 | int main() { |
| 169 | ::aos::Init(); |
Daniel Petti | 2a85f71 | 2014-11-14 12:44:29 -0800 | [diff] [blame] | 170 | ::bot3::input::joysticks::Reader reader; |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 171 | reader.Run(); |
| 172 | ::aos::Cleanup(); |
| 173 | } |