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