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