Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include "y2020/actors/autonomous_actor.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include "aos/logging/logging.h" |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 9 | #include "aos/util/math.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 11 | #include "y2020/actors/auto_splines.h" |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame^] | 12 | #include "y2020/control_loops/drivetrain/drivetrain_base.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 13 | |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 14 | DEFINE_bool(spline_auto, true, "If true, define a spline autonomous mode"); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 15 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 16 | namespace y2020 { |
| 17 | namespace actors { |
| 18 | |
| 19 | using ::aos::monotonic_clock; |
| 20 | using ::frc971::ProfileParametersT; |
| 21 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 22 | namespace chrono = ::std::chrono; |
| 23 | |
| 24 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 25 | : frc971::autonomous::BaseAutonomousActor( |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 26 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame^] | 27 | localizer_control_sender_( |
| 28 | event_loop->MakeSender< |
| 29 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
| 30 | "/drivetrain")), |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 31 | joystick_state_fetcher_( |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame^] | 32 | event_loop->MakeFetcher<aos::JoystickState>("/aos")), |
| 33 | auto_splines_() { |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 34 | set_max_drivetrain_voltage(2.0); |
| 35 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 36 | |
| 37 | void AutonomousActor::Reset() { |
| 38 | InitializeEncoders(); |
| 39 | ResetDrivetrain(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 40 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 41 | joystick_state_fetcher_.Fetch(); |
| 42 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 43 | << "Expect at least one JoystickState message before running auto..."; |
| 44 | alliance_ = joystick_state_fetcher_->alliance(); |
| 45 | // Set up the starting position for the blue alliance. |
| 46 | // Currently just arbitrarily chosen for testing purposes, such that the |
| 47 | // robot starts on the side of the field nearest where it would score |
| 48 | // (although I do not know if this is actually a legal starting position). |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 49 | Eigen::Vector2d starting_position(0.0, 0.0); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 50 | double starting_heading = 0.0; |
| 51 | if (alliance_ == aos::Alliance::kRed) { |
| 52 | starting_position *= -1.0; |
| 53 | starting_heading = aos::math::NormalizeAngle(starting_heading + M_PI); |
| 54 | } |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 55 | if (FLAGS_spline_auto) { |
| 56 | // TODO(james): Resetting the localizer breaks the left/right statespace |
| 57 | // controller. That is a bug, but we can fix that later by not resetting. |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 58 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 59 | |
| 60 | LocalizerControl::Builder localizer_control_builder = |
| 61 | builder.MakeBuilder<LocalizerControl>(); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 62 | localizer_control_builder.add_x(starting_position.x()); |
| 63 | localizer_control_builder.add_y(starting_position.y()); |
| 64 | localizer_control_builder.add_theta(starting_heading); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 65 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 66 | if (!builder.Send(localizer_control_builder.Finish())) { |
| 67 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 68 | } |
| 69 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool AutonomousActor::RunAction( |
Austin Schuh | 6fb0a6d | 2021-01-23 15:43:17 -0800 | [diff] [blame] | 73 | const ::frc971::autonomous::AutonomousActionParams *params) { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 74 | Reset(); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 75 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame] | 76 | if (alliance_ == aos::Alliance::kInvalid) { |
| 77 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 78 | return false; |
| 79 | } |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 80 | if (FLAGS_spline_auto) { |
| 81 | SplineAuto(); |
| 82 | } else { |
| 83 | return DriveFwd(); |
| 84 | } |
| 85 | return true; |
| 86 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 87 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 88 | void AutonomousActor::SplineAuto() { |
Ravago Jones | c2a0802 | 2021-02-06 17:40:54 -0800 | [diff] [blame^] | 89 | SplineHandle spline1 = PlanSpline( |
| 90 | [this](aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 91 | *builder) { return auto_splines_.TestSpline(builder); }, |
| 92 | SplineDirection::kForward); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 93 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 94 | if (!spline1.WaitForPlan()) return; |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 95 | spline1.Start(); |
| 96 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 97 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 98 | } |
| 99 | |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 100 | ProfileParametersT MakeProfileParametersT(const float max_velocity, |
| 101 | const float max_acceleration) { |
| 102 | ProfileParametersT params; |
| 103 | params.max_velocity = max_velocity; |
| 104 | params.max_acceleration = max_acceleration; |
| 105 | return params; |
| 106 | } |
| 107 | |
| 108 | bool AutonomousActor::DriveFwd() { |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 109 | const ProfileParametersT kDrive = MakeProfileParametersT(0.3f, 1.0f); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 110 | const ProfileParametersT kTurn = MakeProfileParametersT(5.0f, 15.0f); |
Austin Schuh | fd1715f | 2021-01-30 16:58:24 -0800 | [diff] [blame] | 111 | StartDrive(1.0, 0.0, kDrive, kTurn); |
milind upadhyay | 47a0ab3 | 2020-11-25 19:34:41 -0800 | [diff] [blame] | 112 | return WaitForDriveDone(); |
| 113 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 114 | } // namespace actors |
| 115 | } // namespace y2020 |