blob: 7144e57f16316af19f9b18ba4557198fdd18a006 [file] [log] [blame]
milind-u37385182023-02-20 15:07:28 -08001#include "y2023/control_loops/superstructure/arm/arm.h"
2
milind-u18a901d2023-02-17 21:51:55 -08003#include "y2023/control_loops/superstructure/roll/integral_hybrid_roll_plant.h"
4#include "y2023/control_loops/superstructure/roll/integral_roll_plant.h"
5
milind-u37385182023-02-20 15:07:28 -08006namespace y2023 {
7namespace control_loops {
8namespace superstructure {
9namespace arm {
10namespace {
11
12namespace chrono = ::std::chrono;
13using ::aos::monotonic_clock;
14
Austin Schuhf0db08e2023-04-05 19:48:42 -070015constexpr int kMaxBrownoutCount = 20;
milind-u37385182023-02-20 15:07:28 -080016
17} // namespace
18
Maxwell Hendersonb392b742023-03-05 07:53:51 -080019Arm::Arm(std::shared_ptr<const constants::Values> values,
20 const ArmTrajectories &arm_trajectories)
milind-u37385182023-02-20 15:07:28 -080021 : values_(values),
22 state_(ArmState::UNINITIALIZED),
23 proximal_zeroing_estimator_(values_->arm_proximal.zeroing),
24 distal_zeroing_estimator_(values_->arm_distal.zeroing),
milind-u18a901d2023-02-17 21:51:55 -080025 roll_joint_zeroing_estimator_(values_->roll_joint.zeroing),
milind-u37385182023-02-20 15:07:28 -080026 proximal_offset_(0.0),
27 distal_offset_(0.0),
milind-u18a901d2023-02-17 21:51:55 -080028 roll_joint_offset_(0.0),
29 alpha_unitizer_((::Eigen::DiagonalMatrix<double, 3>().diagonal()
Maxwell Henderson5c47a462023-02-25 14:40:44 -080030 << (1.0 / constants::Values::kArmAlpha0Max()),
31 (1.0 / constants::Values::kArmAlpha1Max()),
32 (1.0 / constants::Values::kArmAlpha2Max()))
milind-u37385182023-02-20 15:07:28 -080033 .finished()),
34 dynamics_(kArmConstants),
milind-u37385182023-02-20 15:07:28 -080035 close_enough_for_full_power_(false),
36 brownout_count_(0),
milind-u18a901d2023-02-17 21:51:55 -080037 roll_joint_loop_(roll::MakeIntegralRollLoop()),
38 hybrid_roll_joint_loop_(roll::MakeIntegralHybridRollLoop()),
milind-u37385182023-02-20 15:07:28 -080039 arm_ekf_(&dynamics_),
Maxwell Hendersonb392b742023-03-05 07:53:51 -080040 search_graph_(GetSearchGraph(arm_trajectories)),
milind-u37385182023-02-20 15:07:28 -080041 // Go to the start of the first trajectory.
Austin Schuh9a11ebd2023-02-26 14:16:31 -080042 follower_(&dynamics_, &hybrid_roll_joint_loop_, NeutralPoint()),
milind-u37385182023-02-20 15:07:28 -080043 points_(PointList()),
44 current_node_(0) {
Maxwell Hendersonb392b742023-03-05 07:53:51 -080045 // Creating trajectories from fbs
46 for (const auto *trajectory : *arm_trajectories.trajectories()) {
47 trajectories_.emplace_back(&dynamics_, &hybrid_roll_joint_loop_.plant(),
48 *trajectory);
49 }
50
milind-u37385182023-02-20 15:07:28 -080051 int i = 0;
52 for (const auto &trajectory : trajectories_) {
53 AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i,
54 trajectory.trajectory.path().length());
55 ++i;
56 }
57}
58
59void Arm::Reset() { state_ = ArmState::UNINITIALIZED; }
60
milind-u3b91b752023-02-25 15:21:06 -080061namespace {
milind-u3b91b752023-02-25 15:21:06 -080062// Proximal joint center in xy space
63constexpr std::pair<double, double> kJointCenter = {-0.203, 0.787};
64
65std::tuple<double, double, int> ArmThetasToXY(double theta_proximal,
66 double theta_distal) {
67 double theta_proximal_shifted = M_PI / 2.0 - theta_proximal;
68 double theta_distal_shifted = M_PI / 2.0 - theta_distal;
69
70 double x = std::cos(theta_proximal_shifted) * kArmConstants.l0 +
71 std::cos(theta_distal_shifted) * kArmConstants.l1 +
72 kJointCenter.first;
73 double y = std::sin(theta_proximal_shifted) * kArmConstants.l0 +
74 std::sin(theta_distal_shifted) * kArmConstants.l1 +
75 kJointCenter.second;
76
77 int circular_index =
78 std::floor((theta_distal_shifted - theta_proximal_shifted) / M_PI);
79
80 return std::make_tuple(x, y, circular_index);
81}
82
83} // namespace
84
milind-u37385182023-02-20 15:07:28 -080085flatbuffers::Offset<superstructure::ArmStatus> Arm::Iterate(
86 const ::aos::monotonic_clock::time_point /*monotonic_now*/,
87 const uint32_t *unsafe_goal, const superstructure::ArmPosition *position,
88 bool trajectory_override, double *proximal_output, double *distal_output,
Maxwell Henderson5938a832023-02-23 09:33:15 -080089 double *roll_joint_output, flatbuffers::FlatBufferBuilder *fbb) {
90 ::Eigen::Matrix<double, 2, 1> Y;
milind-u37385182023-02-20 15:07:28 -080091 const bool outputs_disabled =
milind-u18a901d2023-02-17 21:51:55 -080092 ((proximal_output == nullptr) || (distal_output == nullptr) ||
93 (roll_joint_output == nullptr));
milind-u37385182023-02-20 15:07:28 -080094 if (outputs_disabled) {
95 ++brownout_count_;
96 } else {
97 brownout_count_ = 0;
98 }
99
milind-u18a901d2023-02-17 21:51:55 -0800100 // TODO(milind): should we default to the closest position?
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800101 uint32_t filtered_goal = arm::NeutralIndex();
milind-u37385182023-02-20 15:07:28 -0800102 if (unsafe_goal != nullptr) {
103 filtered_goal = *unsafe_goal;
104 }
105
milind-u18a901d2023-02-17 21:51:55 -0800106 ::Eigen::Matrix<double, 2, 1> Y_arm;
107 Y_arm << position->proximal()->encoder() + proximal_offset_,
milind-u37385182023-02-20 15:07:28 -0800108 position->distal()->encoder() + distal_offset_;
milind-u18a901d2023-02-17 21:51:55 -0800109 ::Eigen::Matrix<double, 1, 1> Y_roll_joint;
110 Y_roll_joint << position->roll_joint()->encoder() + roll_joint_offset_;
milind-u37385182023-02-20 15:07:28 -0800111
112 proximal_zeroing_estimator_.UpdateEstimate(*position->proximal());
113 distal_zeroing_estimator_.UpdateEstimate(*position->distal());
milind-u18a901d2023-02-17 21:51:55 -0800114 roll_joint_zeroing_estimator_.UpdateEstimate(*position->roll_joint());
milind-u37385182023-02-20 15:07:28 -0800115
116 if (proximal_output != nullptr) {
117 *proximal_output = 0.0;
118 }
119 if (distal_output != nullptr) {
120 *distal_output = 0.0;
121 }
milind-u18a901d2023-02-17 21:51:55 -0800122 if (roll_joint_output != nullptr) {
123 *roll_joint_output = 0.0;
124 }
milind-u37385182023-02-20 15:07:28 -0800125
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800126 arm_ekf_.Correct(Y_arm, constants::Values::kArmDt());
milind-u18a901d2023-02-17 21:51:55 -0800127 roll_joint_loop_.Correct(Y_roll_joint);
milind-u37385182023-02-20 15:07:28 -0800128
129 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 &&
milind-u18a901d2023-02-17 21:51:55 -0800130 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05 &&
131 ::std::abs(roll_joint_loop_.X_hat(0) - follower_.theta(2)) <= 0.05) {
milind-u37385182023-02-20 15:07:28 -0800132 close_enough_for_full_power_ = true;
133 }
134 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 ||
milind-u18a901d2023-02-17 21:51:55 -0800135 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10 ||
136 ::std::abs(roll_joint_loop_.X_hat(0) - follower_.theta(2)) >= 0.50) {
milind-u37385182023-02-20 15:07:28 -0800137 close_enough_for_full_power_ = false;
138 }
139
140 switch (state_) {
141 case ArmState::UNINITIALIZED:
142 // Wait in the uninitialized state until the intake is initialized.
143 AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n");
144 state_ = ArmState::ZEROING;
145 proximal_zeroing_estimator_.Reset();
146 distal_zeroing_estimator_.Reset();
milind-u18a901d2023-02-17 21:51:55 -0800147 roll_joint_zeroing_estimator_.Reset();
milind-u37385182023-02-20 15:07:28 -0800148 break;
149
150 case ArmState::ZEROING:
151 // Zero by not moving.
milind-u18a901d2023-02-17 21:51:55 -0800152 if (zeroed()) {
milind-u37385182023-02-20 15:07:28 -0800153 state_ = ArmState::DISABLED;
154
155 proximal_offset_ = proximal_zeroing_estimator_.offset();
156 distal_offset_ = distal_zeroing_estimator_.offset();
milind-u18a901d2023-02-17 21:51:55 -0800157 roll_joint_offset_ = roll_joint_zeroing_estimator_.offset();
milind-u37385182023-02-20 15:07:28 -0800158
milind-u18a901d2023-02-17 21:51:55 -0800159 Y_arm << position->proximal()->encoder() + proximal_offset_,
milind-u37385182023-02-20 15:07:28 -0800160 position->distal()->encoder() + distal_offset_;
milind-u18a901d2023-02-17 21:51:55 -0800161 Y_roll_joint << position->roll_joint()->encoder() + roll_joint_offset_;
milind-u37385182023-02-20 15:07:28 -0800162
163 // TODO(austin): Offset ekf rather than reset it. Since we aren't
164 // moving at this point, it's pretty safe to do this.
milind-u18a901d2023-02-17 21:51:55 -0800165 ::Eigen::Matrix<double, 4, 1> X_arm;
166 X_arm << Y_arm(0), 0.0, Y_arm(1), 0.0;
167 arm_ekf_.Reset(X_arm);
168
169 ::Eigen::Matrix<double, 3, 1> X_roll_joint;
170 X_roll_joint << Y_roll_joint(0), 0.0, 0.0;
171 roll_joint_loop_.mutable_X_hat() = X_roll_joint;
milind-u37385182023-02-20 15:07:28 -0800172 } else {
173 break;
174 }
175 [[fallthrough]];
176
177 case ArmState::DISABLED: {
178 follower_.SwitchTrajectory(nullptr);
179 close_enough_for_full_power_ = false;
180
milind-u18a901d2023-02-17 21:51:55 -0800181 const ::Eigen::Matrix<double, 3, 1> current_theta =
182 (::Eigen::Matrix<double, 3, 1>() << arm_ekf_.X_hat(0),
183 arm_ekf_.X_hat(2), roll_joint_loop_.X_hat(0))
milind-u37385182023-02-20 15:07:28 -0800184 .finished();
185 uint32_t best_index = 0;
186 double best_distance = (points_[0] - current_theta).norm();
187 uint32_t current_index = 0;
milind-u18a901d2023-02-17 21:51:55 -0800188 for (const ::Eigen::Matrix<double, 3, 1> &point : points_) {
milind-u37385182023-02-20 15:07:28 -0800189 const double new_distance = (point - current_theta).norm();
190 if (new_distance < best_distance) {
191 best_distance = new_distance;
192 best_index = current_index;
193 }
194 ++current_index;
195 }
196 follower_.set_theta(points_[best_index]);
197 current_node_ = best_index;
198
199 if (!outputs_disabled) {
200 state_ = ArmState::GOTO_PATH;
201 } else {
202 break;
203 }
204 }
205 [[fallthrough]];
206
207 case ArmState::GOTO_PATH:
208 if (outputs_disabled) {
209 state_ = ArmState::DISABLED;
210 } else if (trajectory_override) {
211 follower_.SwitchTrajectory(nullptr);
212 current_node_ = filtered_goal;
213 follower_.set_theta(points_[current_node_]);
214 state_ = ArmState::GOTO_PATH;
215 } else if (close_enough_for_full_power_) {
216 state_ = ArmState::RUNNING;
217 }
218 break;
219
220 case ArmState::RUNNING:
221 // ESTOP if we hit the hard limits.
222 // TODO(austin): Pick some sane limits.
223 if (proximal_zeroing_estimator_.error() ||
milind-u18a901d2023-02-17 21:51:55 -0800224 distal_zeroing_estimator_.error() ||
225 roll_joint_zeroing_estimator_.error()) {
milind-u37385182023-02-20 15:07:28 -0800226 AOS_LOG(ERROR, "Zeroing error ESTOP\n");
227 state_ = ArmState::ESTOP;
228 } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) {
229 state_ = ArmState::DISABLED;
230 } else if (trajectory_override) {
231 follower_.SwitchTrajectory(nullptr);
232 current_node_ = filtered_goal;
233 follower_.set_theta(points_[current_node_]);
234 state_ = ArmState::GOTO_PATH;
235 }
236 break;
237
238 case ArmState::ESTOP:
239 AOS_LOG(ERROR, "Estop\n");
240 break;
241 }
242
243 const bool disable = outputs_disabled || (state_ != ArmState::RUNNING &&
244 state_ != ArmState::GOTO_PATH);
245 if (disable) {
246 close_enough_for_full_power_ = false;
247 }
248
249 if (state_ == ArmState::RUNNING && unsafe_goal != nullptr) {
250 if (current_node_ != filtered_goal) {
milind-u37385182023-02-20 15:07:28 -0800251 if (filtered_goal >= search_graph_.num_vertexes()) {
252 AOS_LOG(ERROR, "goal node out of range ESTOP\n");
253 state_ = ArmState::ESTOP;
254 } else if (follower_.path_distance_to_go() > 1e-3) {
255 // Still on the old path segment. Can't change yet.
256 } else {
257 search_graph_.SetGoal(filtered_goal);
258
259 size_t min_edge = 0;
260 double min_cost = ::std::numeric_limits<double>::infinity();
261 for (const SearchGraph::HalfEdge &edge :
262 search_graph_.Neighbors(current_node_)) {
263 const double cost = search_graph_.GetCostToGoal(edge.dest);
264 if (cost < min_cost) {
265 min_edge = edge.edge_id;
266 min_cost = cost;
267 }
268 }
269 // Ok, now we know which edge we are on. Figure out the path and
270 // trajectory.
271 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
272 AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n",
273 static_cast<int>(current_node_),
274 static_cast<int>(next_edge.end), static_cast<int>(min_edge));
275 vmax_ = trajectories_[min_edge].vmax;
276 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
277 current_node_ = next_edge.end;
278 }
279 }
280 }
281
282 const double max_operating_voltage =
283 close_enough_for_full_power_
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800284 ? constants::Values::kArmOperatingVoltage()
285 : (state_ == ArmState::GOTO_PATH
286 ? constants::Values::kArmGotoPathVMax()
287 : constants::Values::kArmPathlessVMax());
milind-u18a901d2023-02-17 21:51:55 -0800288 ::Eigen::Matrix<double, 9, 1> X_hat;
289 X_hat.block<6, 1>(0, 0) = arm_ekf_.X_hat();
290 X_hat.block<3, 1>(6, 0) = roll_joint_loop_.X_hat();
291
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800292 follower_.Update(X_hat, disable, constants::Values::kArmDt(), vmax_,
293 max_operating_voltage);
milind-u37385182023-02-20 15:07:28 -0800294
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800295 arm_ekf_.Predict(follower_.U().head<2>(), constants::Values::kArmDt());
296 roll_joint_loop_.UpdateObserver(follower_.U().tail<1>(),
297 constants::Values::kArmDtDuration());
milind-u18a901d2023-02-17 21:51:55 -0800298
milind-u37385182023-02-20 15:07:28 -0800299 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
300 proximal_estimator_state_offset =
301 proximal_zeroing_estimator_.GetEstimatorState(fbb);
302 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
303 distal_estimator_state_offset =
304 distal_zeroing_estimator_.GetEstimatorState(fbb);
milind-u18a901d2023-02-17 21:51:55 -0800305 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
306 roll_joint_estimator_state_offset =
307 roll_joint_zeroing_estimator_.GetEstimatorState(fbb);
milind-u37385182023-02-20 15:07:28 -0800308
milind-u3b91b752023-02-25 15:21:06 -0800309 const auto [arm_x, arm_y, arm_circular_index] =
310 ArmThetasToXY(arm_ekf_.X_hat(0), arm_ekf_.X_hat(2));
311
milind-u37385182023-02-20 15:07:28 -0800312 superstructure::ArmStatus::Builder status_builder(*fbb);
313 status_builder.add_proximal_estimator_state(proximal_estimator_state_offset);
314 status_builder.add_distal_estimator_state(distal_estimator_state_offset);
milind-u18a901d2023-02-17 21:51:55 -0800315 status_builder.add_roll_joint_estimator_state(
316 roll_joint_estimator_state_offset);
milind-u37385182023-02-20 15:07:28 -0800317
318 status_builder.add_goal_theta0(follower_.theta(0));
319 status_builder.add_goal_theta1(follower_.theta(1));
milind-u18a901d2023-02-17 21:51:55 -0800320 status_builder.add_goal_theta2(follower_.theta(2));
milind-u37385182023-02-20 15:07:28 -0800321 status_builder.add_goal_omega0(follower_.omega(0));
322 status_builder.add_goal_omega1(follower_.omega(1));
milind-u18a901d2023-02-17 21:51:55 -0800323 status_builder.add_goal_omega2(follower_.omega(2));
milind-u37385182023-02-20 15:07:28 -0800324
325 status_builder.add_theta0(arm_ekf_.X_hat(0));
326 status_builder.add_theta1(arm_ekf_.X_hat(2));
milind-u18a901d2023-02-17 21:51:55 -0800327 status_builder.add_theta2(roll_joint_loop_.X_hat(0));
milind-u37385182023-02-20 15:07:28 -0800328 status_builder.add_omega0(arm_ekf_.X_hat(1));
329 status_builder.add_omega1(arm_ekf_.X_hat(3));
milind-u18a901d2023-02-17 21:51:55 -0800330 status_builder.add_omega2(roll_joint_loop_.X_hat(1));
milind-u37385182023-02-20 15:07:28 -0800331 status_builder.add_voltage_error0(arm_ekf_.X_hat(4));
332 status_builder.add_voltage_error1(arm_ekf_.X_hat(5));
milind-u18a901d2023-02-17 21:51:55 -0800333 status_builder.add_voltage_error2(roll_joint_loop_.X_hat(2));
milind-u37385182023-02-20 15:07:28 -0800334
milind-u3b91b752023-02-25 15:21:06 -0800335 status_builder.add_arm_x(arm_x);
336 status_builder.add_arm_y(arm_y);
337 status_builder.add_arm_circular_index(arm_circular_index);
338
milind-u37385182023-02-20 15:07:28 -0800339 if (!disable) {
340 *proximal_output = ::std::max(
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800341 -constants::Values::kArmOperatingVoltage(),
342 ::std::min(constants::Values::kArmOperatingVoltage(), follower_.U(0)));
milind-u37385182023-02-20 15:07:28 -0800343 *distal_output = ::std::max(
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800344 -constants::Values::kArmOperatingVoltage(),
345 ::std::min(constants::Values::kArmOperatingVoltage(), follower_.U(1)));
milind-u18a901d2023-02-17 21:51:55 -0800346 *roll_joint_output = ::std::max(
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800347 -constants::Values::kArmOperatingVoltage(),
348 ::std::min(constants::Values::kArmOperatingVoltage(), follower_.U(2)));
milind-u37385182023-02-20 15:07:28 -0800349 }
350
351 status_builder.add_path_distance_to_go(follower_.path_distance_to_go());
352 status_builder.add_current_node(current_node_);
353
354 status_builder.add_zeroed(zeroed());
355 status_builder.add_estopped(estopped());
356 status_builder.add_state(state_);
357 status_builder.add_failed_solutions(follower_.failed_solutions());
358
milind-u37385182023-02-20 15:07:28 -0800359 return status_builder.Finish();
360}
361
362} // namespace arm
363} // namespace superstructure
364} // namespace control_loops
365} // namespace y2023