Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | #include "aos/common/util/phased_loop.h" |
| 6 | #include "aos/common/time.h" |
| 7 | #include "aos/common/util/trapezoid_profile.h" |
| 8 | #include "aos/common/logging/logging.h" |
| 9 | #include "aos/common/logging/queue_logging.h" |
| 10 | |
| 11 | #include "bot3/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/autonomous/auto.q.h" |
| 13 | #include "frc971/queues/other_sensors.q.h" |
| 14 | |
| 15 | using ::aos::time::Time; |
| 16 | |
| 17 | namespace bot3 { |
| 18 | namespace autonomous { |
| 19 | |
| 20 | namespace time = ::aos::time; |
| 21 | |
| 22 | static double left_initial_position, right_initial_position; |
| 23 | |
| 24 | bool ShouldExitAuto() { |
| 25 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 26 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 27 | if (ans) { |
| 28 | LOG(INFO, "Time to exit auto mode\n"); |
| 29 | } |
| 30 | return ans; |
| 31 | } |
| 32 | |
| 33 | void StopDrivetrain() { |
| 34 | LOG(INFO, "Stopping the drivetrain\n"); |
| 35 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 36 | .control_loop_driving(true) |
| 37 | .left_goal(left_initial_position) |
| 38 | .left_velocity_goal(0) |
| 39 | .right_goal(right_initial_position) |
| 40 | .right_velocity_goal(0) |
| 41 | .quickturn(false) |
| 42 | .Send(); |
| 43 | } |
| 44 | |
| 45 | void ResetDrivetrain() { |
| 46 | LOG(INFO, "resetting the drivetrain\n"); |
| 47 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 48 | .control_loop_driving(false) |
| 49 | .highgear(false) |
| 50 | .steering(0.0) |
| 51 | .throttle(0.0) |
| 52 | .left_goal(left_initial_position) |
| 53 | .left_velocity_goal(0) |
| 54 | .right_goal(right_initial_position) |
| 55 | .right_velocity_goal(0) |
| 56 | .Send(); |
| 57 | } |
| 58 | |
| 59 | void DriveSpin(double radians) { |
| 60 | LOG(INFO, "going to spin %f\n", radians); |
| 61 | |
| 62 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 63 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 64 | const double goal_velocity = 0.0; |
| 65 | const double epsilon = 0.01; |
| 66 | // in drivetrain "meters" |
| 67 | const double kRobotWidth = 0.4544; |
| 68 | |
| 69 | profile.set_maximum_acceleration(1.5); |
| 70 | profile.set_maximum_velocity(0.8); |
| 71 | |
| 72 | const double side_offset = kRobotWidth * radians / 2.0; |
| 73 | |
| 74 | while (true) { |
| 75 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
| 76 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 77 | |
| 78 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 79 | if (ShouldExitAuto()) return; |
| 80 | |
| 81 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
| 82 | left_initial_position - driveTrainState(0, 0), |
| 83 | right_initial_position + driveTrainState(0, 0)); |
| 84 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 85 | .control_loop_driving(true) |
| 86 | .highgear(false) |
| 87 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 88 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 89 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 90 | .right_velocity_goal(driveTrainState(1, 0)) |
| 91 | .Send(); |
| 92 | } |
| 93 | left_initial_position -= side_offset; |
| 94 | right_initial_position += side_offset; |
| 95 | LOG(INFO, "Done moving\n"); |
| 96 | } |
| 97 | |
| 98 | void InitializeEncoders() { |
| 99 | control_loops::drivetrain.status.FetchLatest(); |
| 100 | while (!control_loops::drivetrain.status.get()) { |
| 101 | LOG(WARNING, |
| 102 | "No previous drivetrain position packet, trying to fetch again\n"); |
| 103 | control_loops::drivetrain.status.FetchNextBlocking(); |
| 104 | } |
| 105 | left_initial_position = |
| 106 | control_loops::drivetrain.status->filtered_left_position; |
| 107 | right_initial_position = |
| 108 | control_loops::drivetrain.status->filtered_right_position; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | void HandleAuto() { |
| 113 | //TODO (danielp): Implement this. |
| 114 | } |
| 115 | |
| 116 | } // namespace autonomous |
| 117 | } // namespace bot3 |