Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame^] | 1 | #include "frc971/control_loops/drivetrain/splinedrivetrain.h" |
| 2 | |
| 3 | #include "Eigen/Dense" |
| 4 | |
| 5 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 6 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 7 | |
| 8 | const int kMaxSplineConstraints = 6; |
| 9 | |
| 10 | namespace frc971 { |
| 11 | namespace control_loops { |
| 12 | namespace drivetrain { |
| 13 | |
| 14 | SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config) |
| 15 | : dt_config_(dt_config) {} |
| 16 | |
| 17 | // TODO(alex): put in another thread to avoid malloc in RT. |
| 18 | void SplineDrivetrain::SetGoal( |
| 19 | const ::frc971::control_loops::DrivetrainQueue::Goal &goal) { |
| 20 | current_spline_handle_ = goal.spline_handle; |
| 21 | const ::frc971::MultiSpline &multispline = goal.spline; |
| 22 | if (multispline.spline_idx) { |
| 23 | current_spline_idx_ = multispline.spline_idx; |
| 24 | auto x = multispline.spline_x; |
| 25 | auto y = multispline.spline_y; |
| 26 | ::std::vector<Spline> splines = ::std::vector<Spline>(); |
| 27 | for (int i = 0; i < multispline.spline_count; ++i) { |
| 28 | ::Eigen::Matrix<double, 2, 6> points = |
| 29 | ::Eigen::Matrix<double, 2, 6>::Zero(); |
| 30 | for (int j = 0; j < 6; ++j) { |
| 31 | points(0, j) = x[i * 5 + j]; |
| 32 | points(1, j) = y[i * 5 + j]; |
| 33 | } |
| 34 | splines.emplace_back(Spline(points)); |
| 35 | } |
| 36 | |
| 37 | distance_spline_ = ::std::unique_ptr<DistanceSpline>( |
| 38 | new DistanceSpline(::std::move(splines))); |
| 39 | |
| 40 | current_trajectory_ = ::std::unique_ptr<Trajectory>( |
| 41 | new Trajectory(distance_spline_.get(), dt_config_)); |
| 42 | |
| 43 | for (int i = 0; i < kMaxSplineConstraints; ++i) { |
| 44 | const ::frc971::Constraint &constraint = multispline.constraints[i]; |
| 45 | switch (constraint.constraint_type) { |
| 46 | case 0: |
| 47 | break; |
| 48 | case 1: |
| 49 | current_trajectory_->set_longitudal_acceleration(constraint.value); |
| 50 | break; |
| 51 | case 2: |
| 52 | current_trajectory_->set_lateral_acceleration(constraint.value); |
| 53 | break; |
| 54 | case 3: |
| 55 | current_trajectory_->set_voltage_limit(constraint.value); |
| 56 | break; |
| 57 | case 4: |
| 58 | current_trajectory_->LimitVelocity(constraint.start_distance, |
| 59 | constraint.end_distance, |
| 60 | constraint.value); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | current_trajectory_->Plan(); |
| 66 | current_xva_plan_ = current_trajectory_->PlanXVA(dt_config_.dt); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // TODO(alex): Handle drift. |
| 71 | void SplineDrivetrain::SetOutput( |
| 72 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 73 | if (!output) { |
| 74 | return; |
| 75 | } |
| 76 | if (current_spline_handle_ == current_spline_idx_) { |
| 77 | if (current_xva_idx_ < current_xva_plan_.size()) { |
| 78 | double current_distance = current_xva_plan_[current_xva_idx_](0); |
| 79 | ::Eigen::Matrix<double, 2, 1> FFVoltage = |
| 80 | current_trajectory_->FFVoltage(current_distance); |
| 81 | output->left_voltage = FFVoltage(0); |
| 82 | output->right_voltage = FFVoltage(1); |
| 83 | ++current_xva_idx_; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | } // namespace drivetrain |
| 89 | } // namespace control_loops |
| 90 | } // namespace frc971 |