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 | |
| 14 | namespace y2020 { |
| 15 | namespace actors { |
| 16 | |
| 17 | using ::aos::monotonic_clock; |
| 18 | using ::frc971::ProfileParametersT; |
| 19 | using frc971::control_loops::drivetrain::LocalizerControl; |
| 20 | namespace chrono = ::std::chrono; |
| 21 | |
| 22 | AutonomousActor::AutonomousActor(::aos::EventLoop *event_loop) |
| 23 | : frc971::autonomous::BaseAutonomousActor( |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 24 | event_loop, control_loops::drivetrain::GetDrivetrainConfig()), |
| 25 | localizer_control_sender_(event_loop->MakeSender< |
| 26 | ::frc971::control_loops::drivetrain::LocalizerControl>( |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame^] | 27 | "/drivetrain")), |
| 28 | joystick_state_fetcher_( |
| 29 | event_loop->MakeFetcher<aos::JoystickState>("/aos")) {} |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 30 | |
| 31 | void AutonomousActor::Reset() { |
| 32 | InitializeEncoders(); |
| 33 | ResetDrivetrain(); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 34 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame^] | 35 | joystick_state_fetcher_.Fetch(); |
| 36 | CHECK(joystick_state_fetcher_.get() != nullptr) |
| 37 | << "Expect at least one JoystickState message before running auto..."; |
| 38 | alliance_ = joystick_state_fetcher_->alliance(); |
| 39 | // Set up the starting position for the blue alliance. |
| 40 | // Currently just arbitrarily chosen for testing purposes, such that the |
| 41 | // robot starts on the side of the field nearest where it would score |
| 42 | // (although I do not know if this is actually a legal starting position). |
| 43 | Eigen::Vector2d starting_position(3.3, -0.7); |
| 44 | double starting_heading = 0.0; |
| 45 | if (alliance_ == aos::Alliance::kRed) { |
| 46 | starting_position *= -1.0; |
| 47 | starting_heading = aos::math::NormalizeAngle(starting_heading + M_PI); |
| 48 | } |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 49 | { |
| 50 | auto builder = localizer_control_sender_.MakeBuilder(); |
| 51 | |
| 52 | LocalizerControl::Builder localizer_control_builder = |
| 53 | builder.MakeBuilder<LocalizerControl>(); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame^] | 54 | localizer_control_builder.add_x(starting_position.x()); |
| 55 | localizer_control_builder.add_y(starting_position.y()); |
| 56 | localizer_control_builder.add_theta(starting_heading); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 57 | localizer_control_builder.add_theta_uncertainty(0.00001); |
| 58 | if (!builder.Send(localizer_control_builder.Finish())) { |
| 59 | AOS_LOG(ERROR, "Failed to reset localizer.\n"); |
| 60 | } |
| 61 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool AutonomousActor::RunAction( |
| 65 | const ::frc971::autonomous::AutonomousActionParams *params) { |
| 66 | Reset(); |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame^] | 67 | if (alliance_ == aos::Alliance::kInvalid) { |
| 68 | AOS_LOG(INFO, "Aborting autonomous due to invalid alliance selection."); |
| 69 | return false; |
| 70 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 71 | |
James Kuszmaul | ddd2ba6 | 2020-03-08 22:17:13 -0700 | [diff] [blame^] | 72 | SplineHandle spline1 = PlanSpline(std::bind(AutonomousSplines::BasicSSpline, |
| 73 | std::placeholders::_1, alliance_), |
| 74 | SplineDirection::kForward); |
James Kuszmaul | 5f6d1d4 | 2020-03-01 18:10:07 -0800 | [diff] [blame] | 75 | |
| 76 | if (!spline1.WaitForPlan()) return true; |
| 77 | spline1.Start(); |
| 78 | |
| 79 | if (!spline1.WaitForSplineDistanceRemaining(0.02)) return true; |
| 80 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 81 | AOS_LOG(INFO, "Params are %d\n", params->mode()); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | } // namespace actors |
| 86 | } // namespace y2020 |