Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 1 | #include "y2016/autonomous/auto.h" |
| 2 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <memory> |
| 6 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 7 | #include "aos/common/actions/actions.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 8 | #include "aos/common/logging/logging.h" |
| 9 | #include "aos/common/logging/queue_logging.h" |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 10 | #include "aos/common/time.h" |
| 11 | #include "aos/common/util/phased_loop.h" |
| 12 | #include "aos/common/util/trapezoid_profile.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 13 | |
| 14 | #include "frc971/autonomous/auto.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 16 | #include "y2016/actors/drivetrain_actor.h" |
| 17 | #include "y2016/constants.h" |
| 18 | #include "y2016/queues/profile_params.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 19 | |
| 20 | using ::aos::time::Time; |
| 21 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 22 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 23 | namespace autonomous { |
| 24 | |
| 25 | namespace time = ::aos::time; |
| 26 | |
| 27 | static double left_initial_position, right_initial_position; |
| 28 | |
| 29 | bool ShouldExitAuto() { |
| 30 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 31 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 32 | if (ans) { |
| 33 | LOG(INFO, "Time to exit auto mode\n"); |
| 34 | } |
| 35 | return ans; |
| 36 | } |
| 37 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 38 | void ResetDrivetrain() { |
| 39 | LOG(INFO, "resetting the drivetrain\n"); |
| 40 | ::frc971::control_loops::drivetrain_queue.goal.MakeWithBuilder() |
| 41 | .control_loop_driving(false) |
| 42 | .highgear(true) |
| 43 | .steering(0.0) |
| 44 | .throttle(0.0) |
| 45 | .left_goal(left_initial_position) |
| 46 | .left_velocity_goal(0) |
| 47 | .right_goal(right_initial_position) |
| 48 | .right_velocity_goal(0) |
| 49 | .Send(); |
| 50 | } |
| 51 | |
| 52 | void WaitUntilDoneOrCanceled( |
| 53 | ::std::unique_ptr<aos::common::actions::Action> action) { |
| 54 | if (!action) { |
| 55 | LOG(ERROR, "No action, not waiting\n"); |
| 56 | return; |
| 57 | } |
| 58 | while (true) { |
| 59 | // Poll the running bit and auto done bits. |
| 60 | ::aos::time::PhasedLoopXMS(10, 5000); |
| 61 | if (!action->Running() || ShouldExitAuto()) { |
| 62 | return; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 67 | const ProfileParams kFastDrive = {3.0, 2.5}; |
| 68 | const ProfileParams kSlowDrive = {2.5, 2.5}; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 69 | const ProfileParams kFastTurn = {3.0, 10.0}; |
| 70 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 71 | ::std::unique_ptr<::y2016::actors::DrivetrainAction> SetDriveGoal( |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 72 | double distance, const ProfileParams drive_params, double theta = 0, |
| 73 | const ProfileParams &turn_params = kFastTurn) { |
| 74 | LOG(INFO, "Driving to %f\n", distance); |
| 75 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 76 | ::y2016::actors::DrivetrainActionParams params; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 77 | params.left_initial_position = left_initial_position; |
| 78 | params.right_initial_position = right_initial_position; |
| 79 | params.y_offset = distance; |
| 80 | params.theta_offset = theta; |
| 81 | params.maximum_turn_acceleration = turn_params.acceleration; |
| 82 | params.maximum_turn_velocity = turn_params.velocity; |
| 83 | params.maximum_velocity = drive_params.velocity; |
| 84 | params.maximum_acceleration = drive_params.acceleration; |
| 85 | auto drivetrain_action = actors::MakeDrivetrainAction(params); |
| 86 | drivetrain_action->Start(); |
| 87 | left_initial_position += |
| 88 | distance - theta * constants::GetValues().turn_width / 2.0; |
| 89 | right_initial_position += |
| 90 | distance + theta * constants::GetValues().turn_width / 2.0; |
| 91 | return ::std::move(drivetrain_action); |
| 92 | } |
| 93 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 94 | void InitializeEncoders() { |
| 95 | ::frc971::control_loops::drivetrain_queue.status.FetchAnother(); |
| 96 | left_initial_position = |
| 97 | ::frc971::control_loops::drivetrain_queue.status->filtered_left_position; |
| 98 | right_initial_position = |
| 99 | ::frc971::control_loops::drivetrain_queue.status->filtered_right_position; |
| 100 | } |
| 101 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 102 | void HandleAuto() { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 103 | LOG(INFO, "Handling auto mode\n"); |
| 104 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 105 | ResetDrivetrain(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 106 | InitializeEncoders(); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace autonomous |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame^] | 110 | } // namespace y2016 |