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 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 8 | namespace frc971 { |
| 9 | namespace control_loops { |
| 10 | namespace drivetrain { |
| 11 | |
| 12 | SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config) |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 13 | : dt_config_(dt_config), new_goal_(&mutex_) { |
| 14 | worker_thread_ = std::thread(&SplineDrivetrain::ComputeTrajectory, this); |
| 15 | } |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 16 | |
| 17 | void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) { |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 18 | output_was_capped_ = |
| 19 | ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 20 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 21 | if (output_was_capped_) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 22 | *U *= 12.0 / U->lpNorm<Eigen::Infinity>(); |
| 23 | } |
| 24 | } |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 25 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 26 | // TODO(alex): work on setting priority. |
| 27 | void SplineDrivetrain::ComputeTrajectory() { |
| 28 | ::aos::MutexLocker locker(&mutex_); |
| 29 | while (run_) { |
| 30 | while (goal_.spline.spline_idx == future_spline_idx_) { |
| 31 | CHECK(!new_goal_.Wait()); |
| 32 | if (!run_) { |
| 33 | return; |
| 34 | } |
| 35 | } |
| 36 | past_distance_spline_.reset(); |
| 37 | past_trajectory_.reset(); |
| 38 | |
| 39 | plan_state_ = PlanState::kBuildingTrajectory; |
| 40 | const ::frc971::MultiSpline &multispline = goal_.spline; |
| 41 | future_spline_idx_ = multispline.spline_idx; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 42 | auto x = multispline.spline_x; |
| 43 | auto y = multispline.spline_y; |
| 44 | ::std::vector<Spline> splines = ::std::vector<Spline>(); |
| 45 | for (int i = 0; i < multispline.spline_count; ++i) { |
| 46 | ::Eigen::Matrix<double, 2, 6> points = |
| 47 | ::Eigen::Matrix<double, 2, 6>::Zero(); |
| 48 | for (int j = 0; j < 6; ++j) { |
| 49 | points(0, j) = x[i * 5 + j]; |
| 50 | points(1, j) = y[i * 5 + j]; |
| 51 | } |
| 52 | splines.emplace_back(Spline(points)); |
| 53 | } |
| 54 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 55 | future_distance_spline_ = ::std::unique_ptr<DistanceSpline>( |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 56 | new DistanceSpline(::std::move(splines))); |
| 57 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 58 | future_trajectory_ = ::std::unique_ptr<Trajectory>( |
| 59 | new Trajectory(future_distance_spline_.get(), dt_config_)); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 60 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 61 | for (size_t i = 0; i < multispline.constraints.size(); ++i) { |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 62 | const ::frc971::Constraint &constraint = multispline.constraints[i]; |
| 63 | switch (constraint.constraint_type) { |
| 64 | case 0: |
| 65 | break; |
| 66 | case 1: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 67 | future_trajectory_->set_longitudal_acceleration(constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 68 | break; |
| 69 | case 2: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 70 | future_trajectory_->set_lateral_acceleration(constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 71 | break; |
| 72 | case 3: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 73 | future_trajectory_->set_voltage_limit(constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 74 | break; |
| 75 | case 4: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 76 | future_trajectory_->LimitVelocity(constraint.start_distance, |
| 77 | constraint.end_distance, |
| 78 | constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 82 | plan_state_ = PlanState::kPlanningTrajectory; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 83 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 84 | future_trajectory_->Plan(); |
| 85 | plan_state_ = PlanState::kPlannedTrajectory; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void SplineDrivetrain::SetGoal( |
| 90 | const ::frc971::control_loops::DrivetrainQueue::Goal &goal) { |
| 91 | current_spline_handle_ = goal.spline_handle; |
| 92 | // If told to stop, set the executing spline to an invalid index. |
| 93 | if (current_spline_handle_ == 0) { |
| 94 | current_spline_idx_ = -1; |
| 95 | } |
| 96 | |
| 97 | ::aos::Mutex::State mutex_state = mutex_.TryLock(); |
| 98 | if (mutex_state == ::aos::Mutex::State::kLocked) { |
| 99 | if (goal.spline.spline_idx && future_spline_idx_ != goal.spline.spline_idx) { |
| 100 | goal_ = goal; |
| 101 | new_goal_.Broadcast(); |
| 102 | } |
| 103 | if (future_trajectory_ && |
| 104 | (!current_trajectory_ || |
| 105 | current_trajectory_->is_at_end(current_xva_.block<2, 1>(0, 0)) || |
| 106 | current_spline_idx_ == -1)) { |
| 107 | // Move current data to other variables to be cleared by worker. |
| 108 | past_trajectory_ = std::move(current_trajectory_); |
| 109 | past_distance_spline_ = std::move(current_distance_spline_); |
| 110 | |
| 111 | // Move the computed data to be executed. |
| 112 | current_trajectory_ = std::move(future_trajectory_); |
| 113 | current_distance_spline_ = std::move(future_distance_spline_); |
| 114 | current_spline_idx_ = future_spline_idx_; |
| 115 | |
| 116 | // Reset internal state to a trajectory start position. |
| 117 | current_xva_ = current_trajectory_->FFAcceleration(0); |
| 118 | current_xva_(1) = 0.0; |
| 119 | } |
| 120 | mutex_.Unlock(); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 124 | // TODO(alex): Hold position when done following the spline. |
| 125 | // TODO(Austin): Compensate for voltage error. |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 126 | void SplineDrivetrain::Update(bool enable, const ::Eigen::Matrix<double, 5, 1> &state) { |
| 127 | next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero(); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 128 | enable_ = enable; |
| 129 | if (enable && current_trajectory_) { |
| 130 | ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero(); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 131 | if (!IsAtEnd() && |
| 132 | current_spline_handle_ == current_spline_idx_) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 133 | // TODO(alex): It takes about a cycle for the outputs to propagate to the |
| 134 | // motors. Consider delaying the output by a cycle. |
| 135 | U_ff = current_trajectory_->FFVoltage(current_xva_(0)); |
| 136 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 137 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 138 | ::Eigen::Matrix<double, 2, 5> K = |
| 139 | current_trajectory_->KForState(state, dt_config_.dt, Q, R); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 140 | ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState(); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 141 | ::Eigen::Matrix<double, 5, 1> state_error = goal_state - state; |
| 142 | ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 143 | |
| 144 | ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2,1>(0,0); |
| 145 | next_xva_ = current_trajectory_->GetNextXVA(dt_config_.dt, &xv_state); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 146 | next_U_ = U_ff + U_fb; |
| 147 | uncapped_U_ = next_U_; |
| 148 | ScaleCapU(&next_U_); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 152 | void SplineDrivetrain::SetOutput( |
| 153 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 154 | if (!output) { |
| 155 | return; |
| 156 | } |
| 157 | if (current_spline_handle_ == current_spline_idx_) { |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 158 | if (!IsAtEnd()) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 159 | output->left_voltage = next_U_(0); |
| 160 | output->right_voltage = next_U_(1); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 161 | current_xva_ = next_xva_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 164 | output->left_voltage = next_U_(0); |
| 165 | output->right_voltage = next_U_(1); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 168 | void SplineDrivetrain::PopulateStatus( |
| 169 | ::frc971::control_loops::DrivetrainQueue::Status *status) const { |
| 170 | if (status && enable_) { |
| 171 | status->uncapped_left_voltage = uncapped_U_(0); |
| 172 | status->uncapped_right_voltage = uncapped_U_(1); |
| 173 | status->robot_speed = current_xva_(1); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame^] | 174 | status->output_was_capped = output_was_capped_; |
| 175 | } |
| 176 | |
| 177 | if (status) { |
| 178 | if (current_distance_spline_) { |
| 179 | ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState(); |
| 180 | status->trajectory_logging.x = goal_state(0); |
| 181 | status->trajectory_logging.y = goal_state(1); |
| 182 | status->trajectory_logging.theta = goal_state(2); |
| 183 | status->trajectory_logging.left_velocity = goal_state(3); |
| 184 | status->trajectory_logging.right_velocity = goal_state(4); |
| 185 | } |
| 186 | status->trajectory_logging.planning_state = static_cast<int8_t>(plan_state_.load()); |
| 187 | status->trajectory_logging.is_executing = !IsAtEnd(); |
| 188 | status->trajectory_logging.current_spline_handle = current_spline_handle_; |
| 189 | status->trajectory_logging.current_spline_idx = current_spline_idx_; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 193 | } // namespace drivetrain |
| 194 | } // namespace control_loops |
| 195 | } // namespace frc971 |