blob: 27a14505342b0ceac88086dc9cf0959a388e58d1 [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
15constexpr int kMaxBrownoutCount = 4;
16
17} // namespace
18
19Arm::Arm(std::shared_ptr<const constants::Values> values)
20 : values_(values),
21 state_(ArmState::UNINITIALIZED),
22 proximal_zeroing_estimator_(values_->arm_proximal.zeroing),
23 distal_zeroing_estimator_(values_->arm_distal.zeroing),
milind-u18a901d2023-02-17 21:51:55 -080024 roll_joint_zeroing_estimator_(values_->roll_joint.zeroing),
milind-u37385182023-02-20 15:07:28 -080025 proximal_offset_(0.0),
26 distal_offset_(0.0),
milind-u18a901d2023-02-17 21:51:55 -080027 roll_joint_offset_(0.0),
28 alpha_unitizer_((::Eigen::DiagonalMatrix<double, 3>().diagonal()
Maxwell Henderson5c47a462023-02-25 14:40:44 -080029 << (1.0 / constants::Values::kArmAlpha0Max()),
30 (1.0 / constants::Values::kArmAlpha1Max()),
31 (1.0 / constants::Values::kArmAlpha2Max()))
milind-u37385182023-02-20 15:07:28 -080032 .finished()),
33 dynamics_(kArmConstants),
milind-u37385182023-02-20 15:07:28 -080034 close_enough_for_full_power_(false),
35 brownout_count_(0),
milind-u18a901d2023-02-17 21:51:55 -080036 roll_joint_loop_(roll::MakeIntegralRollLoop()),
37 hybrid_roll_joint_loop_(roll::MakeIntegralHybridRollLoop()),
milind-u37385182023-02-20 15:07:28 -080038 arm_ekf_(&dynamics_),
milind-u18a901d2023-02-17 21:51:55 -080039 search_graph_(MakeSearchGraph(&dynamics_, &trajectories_, alpha_unitizer_,
Maxwell Henderson5c47a462023-02-25 14:40:44 -080040 constants::Values::kArmVMax(),
41 &hybrid_roll_joint_loop_)),
milind-u37385182023-02-20 15:07:28 -080042 // Go to the start of the first trajectory.
Austin Schuh9a11ebd2023-02-26 14:16:31 -080043 follower_(&dynamics_, &hybrid_roll_joint_loop_, NeutralPoint()),
milind-u37385182023-02-20 15:07:28 -080044 points_(PointList()),
45 current_node_(0) {
46 int i = 0;
47 for (const auto &trajectory : trajectories_) {
48 AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i,
49 trajectory.trajectory.path().length());
50 ++i;
51 }
52}
53
54void Arm::Reset() { state_ = ArmState::UNINITIALIZED; }
55
milind-u3b91b752023-02-25 15:21:06 -080056namespace {
57
58// Proximal joint center in xy space
59constexpr std::pair<double, double> kJointCenter = {-0.203, 0.787};
60
61std::tuple<double, double, int> ArmThetasToXY(double theta_proximal,
62 double theta_distal) {
63 double theta_proximal_shifted = M_PI / 2.0 - theta_proximal;
64 double theta_distal_shifted = M_PI / 2.0 - theta_distal;
65
66 double x = std::cos(theta_proximal_shifted) * kArmConstants.l0 +
67 std::cos(theta_distal_shifted) * kArmConstants.l1 +
68 kJointCenter.first;
69 double y = std::sin(theta_proximal_shifted) * kArmConstants.l0 +
70 std::sin(theta_distal_shifted) * kArmConstants.l1 +
71 kJointCenter.second;
72
73 int circular_index =
74 std::floor((theta_distal_shifted - theta_proximal_shifted) / M_PI);
75
76 return std::make_tuple(x, y, circular_index);
77}
78
79} // namespace
80
milind-u37385182023-02-20 15:07:28 -080081flatbuffers::Offset<superstructure::ArmStatus> Arm::Iterate(
82 const ::aos::monotonic_clock::time_point /*monotonic_now*/,
83 const uint32_t *unsafe_goal, const superstructure::ArmPosition *position,
84 bool trajectory_override, double *proximal_output, double *distal_output,
Maxwell Henderson5938a832023-02-23 09:33:15 -080085 double *roll_joint_output, flatbuffers::FlatBufferBuilder *fbb) {
86 ::Eigen::Matrix<double, 2, 1> Y;
milind-u37385182023-02-20 15:07:28 -080087 const bool outputs_disabled =
milind-u18a901d2023-02-17 21:51:55 -080088 ((proximal_output == nullptr) || (distal_output == nullptr) ||
89 (roll_joint_output == nullptr));
milind-u37385182023-02-20 15:07:28 -080090 if (outputs_disabled) {
91 ++brownout_count_;
92 } else {
93 brownout_count_ = 0;
94 }
95
milind-u18a901d2023-02-17 21:51:55 -080096 // TODO(milind): should we default to the closest position?
Austin Schuh9a11ebd2023-02-26 14:16:31 -080097 uint32_t filtered_goal = arm::NeutralIndex();
milind-u37385182023-02-20 15:07:28 -080098 if (unsafe_goal != nullptr) {
99 filtered_goal = *unsafe_goal;
100 }
101
milind-u18a901d2023-02-17 21:51:55 -0800102 ::Eigen::Matrix<double, 2, 1> Y_arm;
103 Y_arm << position->proximal()->encoder() + proximal_offset_,
milind-u37385182023-02-20 15:07:28 -0800104 position->distal()->encoder() + distal_offset_;
milind-u18a901d2023-02-17 21:51:55 -0800105 ::Eigen::Matrix<double, 1, 1> Y_roll_joint;
106 Y_roll_joint << position->roll_joint()->encoder() + roll_joint_offset_;
milind-u37385182023-02-20 15:07:28 -0800107
108 proximal_zeroing_estimator_.UpdateEstimate(*position->proximal());
109 distal_zeroing_estimator_.UpdateEstimate(*position->distal());
milind-u18a901d2023-02-17 21:51:55 -0800110 roll_joint_zeroing_estimator_.UpdateEstimate(*position->roll_joint());
milind-u37385182023-02-20 15:07:28 -0800111
112 if (proximal_output != nullptr) {
113 *proximal_output = 0.0;
114 }
115 if (distal_output != nullptr) {
116 *distal_output = 0.0;
117 }
milind-u18a901d2023-02-17 21:51:55 -0800118 if (roll_joint_output != nullptr) {
119 *roll_joint_output = 0.0;
120 }
milind-u37385182023-02-20 15:07:28 -0800121
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800122 arm_ekf_.Correct(Y_arm, constants::Values::kArmDt());
milind-u18a901d2023-02-17 21:51:55 -0800123 roll_joint_loop_.Correct(Y_roll_joint);
milind-u37385182023-02-20 15:07:28 -0800124
125 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 &&
milind-u18a901d2023-02-17 21:51:55 -0800126 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05 &&
127 ::std::abs(roll_joint_loop_.X_hat(0) - follower_.theta(2)) <= 0.05) {
milind-u37385182023-02-20 15:07:28 -0800128 close_enough_for_full_power_ = true;
129 }
130 if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 ||
milind-u18a901d2023-02-17 21:51:55 -0800131 ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10 ||
132 ::std::abs(roll_joint_loop_.X_hat(0) - follower_.theta(2)) >= 0.50) {
milind-u37385182023-02-20 15:07:28 -0800133 close_enough_for_full_power_ = false;
134 }
135
136 switch (state_) {
137 case ArmState::UNINITIALIZED:
138 // Wait in the uninitialized state until the intake is initialized.
139 AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n");
140 state_ = ArmState::ZEROING;
141 proximal_zeroing_estimator_.Reset();
142 distal_zeroing_estimator_.Reset();
milind-u18a901d2023-02-17 21:51:55 -0800143 roll_joint_zeroing_estimator_.Reset();
milind-u37385182023-02-20 15:07:28 -0800144 break;
145
146 case ArmState::ZEROING:
147 // Zero by not moving.
milind-u18a901d2023-02-17 21:51:55 -0800148 if (zeroed()) {
milind-u37385182023-02-20 15:07:28 -0800149 state_ = ArmState::DISABLED;
150
151 proximal_offset_ = proximal_zeroing_estimator_.offset();
152 distal_offset_ = distal_zeroing_estimator_.offset();
milind-u18a901d2023-02-17 21:51:55 -0800153 roll_joint_offset_ = roll_joint_zeroing_estimator_.offset();
milind-u37385182023-02-20 15:07:28 -0800154
milind-u18a901d2023-02-17 21:51:55 -0800155 Y_arm << position->proximal()->encoder() + proximal_offset_,
milind-u37385182023-02-20 15:07:28 -0800156 position->distal()->encoder() + distal_offset_;
milind-u18a901d2023-02-17 21:51:55 -0800157 Y_roll_joint << position->roll_joint()->encoder() + roll_joint_offset_;
milind-u37385182023-02-20 15:07:28 -0800158
159 // TODO(austin): Offset ekf rather than reset it. Since we aren't
160 // moving at this point, it's pretty safe to do this.
milind-u18a901d2023-02-17 21:51:55 -0800161 ::Eigen::Matrix<double, 4, 1> X_arm;
162 X_arm << Y_arm(0), 0.0, Y_arm(1), 0.0;
163 arm_ekf_.Reset(X_arm);
164
165 ::Eigen::Matrix<double, 3, 1> X_roll_joint;
166 X_roll_joint << Y_roll_joint(0), 0.0, 0.0;
167 roll_joint_loop_.mutable_X_hat() = X_roll_joint;
milind-u37385182023-02-20 15:07:28 -0800168 } else {
169 break;
170 }
171 [[fallthrough]];
172
173 case ArmState::DISABLED: {
174 follower_.SwitchTrajectory(nullptr);
175 close_enough_for_full_power_ = false;
176
milind-u18a901d2023-02-17 21:51:55 -0800177 const ::Eigen::Matrix<double, 3, 1> current_theta =
178 (::Eigen::Matrix<double, 3, 1>() << arm_ekf_.X_hat(0),
179 arm_ekf_.X_hat(2), roll_joint_loop_.X_hat(0))
milind-u37385182023-02-20 15:07:28 -0800180 .finished();
181 uint32_t best_index = 0;
182 double best_distance = (points_[0] - current_theta).norm();
183 uint32_t current_index = 0;
milind-u18a901d2023-02-17 21:51:55 -0800184 for (const ::Eigen::Matrix<double, 3, 1> &point : points_) {
milind-u37385182023-02-20 15:07:28 -0800185 const double new_distance = (point - current_theta).norm();
186 if (new_distance < best_distance) {
187 best_distance = new_distance;
188 best_index = current_index;
189 }
190 ++current_index;
191 }
192 follower_.set_theta(points_[best_index]);
193 current_node_ = best_index;
194
195 if (!outputs_disabled) {
196 state_ = ArmState::GOTO_PATH;
197 } else {
198 break;
199 }
200 }
201 [[fallthrough]];
202
203 case ArmState::GOTO_PATH:
204 if (outputs_disabled) {
205 state_ = ArmState::DISABLED;
206 } else if (trajectory_override) {
207 follower_.SwitchTrajectory(nullptr);
208 current_node_ = filtered_goal;
209 follower_.set_theta(points_[current_node_]);
210 state_ = ArmState::GOTO_PATH;
211 } else if (close_enough_for_full_power_) {
212 state_ = ArmState::RUNNING;
213 }
214 break;
215
216 case ArmState::RUNNING:
217 // ESTOP if we hit the hard limits.
218 // TODO(austin): Pick some sane limits.
219 if (proximal_zeroing_estimator_.error() ||
milind-u18a901d2023-02-17 21:51:55 -0800220 distal_zeroing_estimator_.error() ||
221 roll_joint_zeroing_estimator_.error()) {
milind-u37385182023-02-20 15:07:28 -0800222 AOS_LOG(ERROR, "Zeroing error ESTOP\n");
223 state_ = ArmState::ESTOP;
224 } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) {
225 state_ = ArmState::DISABLED;
226 } else if (trajectory_override) {
227 follower_.SwitchTrajectory(nullptr);
228 current_node_ = filtered_goal;
229 follower_.set_theta(points_[current_node_]);
230 state_ = ArmState::GOTO_PATH;
231 }
232 break;
233
234 case ArmState::ESTOP:
235 AOS_LOG(ERROR, "Estop\n");
236 break;
237 }
238
239 const bool disable = outputs_disabled || (state_ != ArmState::RUNNING &&
240 state_ != ArmState::GOTO_PATH);
241 if (disable) {
242 close_enough_for_full_power_ = false;
243 }
244
245 if (state_ == ArmState::RUNNING && unsafe_goal != nullptr) {
246 if (current_node_ != filtered_goal) {
247 AOS_LOG(INFO, "Goal is different\n");
248 if (filtered_goal >= search_graph_.num_vertexes()) {
249 AOS_LOG(ERROR, "goal node out of range ESTOP\n");
250 state_ = ArmState::ESTOP;
251 } else if (follower_.path_distance_to_go() > 1e-3) {
252 // Still on the old path segment. Can't change yet.
253 } else {
254 search_graph_.SetGoal(filtered_goal);
255
256 size_t min_edge = 0;
257 double min_cost = ::std::numeric_limits<double>::infinity();
258 for (const SearchGraph::HalfEdge &edge :
259 search_graph_.Neighbors(current_node_)) {
260 const double cost = search_graph_.GetCostToGoal(edge.dest);
261 if (cost < min_cost) {
262 min_edge = edge.edge_id;
263 min_cost = cost;
264 }
265 }
266 // Ok, now we know which edge we are on. Figure out the path and
267 // trajectory.
268 const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge];
269 AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n",
270 static_cast<int>(current_node_),
271 static_cast<int>(next_edge.end), static_cast<int>(min_edge));
272 vmax_ = trajectories_[min_edge].vmax;
273 follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory);
274 current_node_ = next_edge.end;
275 }
276 }
277 }
278
279 const double max_operating_voltage =
280 close_enough_for_full_power_
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800281 ? constants::Values::kArmOperatingVoltage()
282 : (state_ == ArmState::GOTO_PATH
283 ? constants::Values::kArmGotoPathVMax()
284 : constants::Values::kArmPathlessVMax());
milind-u18a901d2023-02-17 21:51:55 -0800285 ::Eigen::Matrix<double, 9, 1> X_hat;
286 X_hat.block<6, 1>(0, 0) = arm_ekf_.X_hat();
287 X_hat.block<3, 1>(6, 0) = roll_joint_loop_.X_hat();
288
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800289 follower_.Update(X_hat, disable, constants::Values::kArmDt(), vmax_,
290 max_operating_voltage);
291 AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage);
milind-u37385182023-02-20 15:07:28 -0800292
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800293 arm_ekf_.Predict(follower_.U().head<2>(), constants::Values::kArmDt());
294 roll_joint_loop_.UpdateObserver(follower_.U().tail<1>(),
295 constants::Values::kArmDtDuration());
milind-u18a901d2023-02-17 21:51:55 -0800296
milind-u37385182023-02-20 15:07:28 -0800297 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
298 proximal_estimator_state_offset =
299 proximal_zeroing_estimator_.GetEstimatorState(fbb);
300 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
301 distal_estimator_state_offset =
302 distal_zeroing_estimator_.GetEstimatorState(fbb);
milind-u18a901d2023-02-17 21:51:55 -0800303 flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState>
304 roll_joint_estimator_state_offset =
305 roll_joint_zeroing_estimator_.GetEstimatorState(fbb);
milind-u37385182023-02-20 15:07:28 -0800306
milind-u3b91b752023-02-25 15:21:06 -0800307 const auto [arm_x, arm_y, arm_circular_index] =
308 ArmThetasToXY(arm_ekf_.X_hat(0), arm_ekf_.X_hat(2));
309
milind-u37385182023-02-20 15:07:28 -0800310 superstructure::ArmStatus::Builder status_builder(*fbb);
311 status_builder.add_proximal_estimator_state(proximal_estimator_state_offset);
312 status_builder.add_distal_estimator_state(distal_estimator_state_offset);
milind-u18a901d2023-02-17 21:51:55 -0800313 status_builder.add_roll_joint_estimator_state(
314 roll_joint_estimator_state_offset);
milind-u37385182023-02-20 15:07:28 -0800315
316 status_builder.add_goal_theta0(follower_.theta(0));
317 status_builder.add_goal_theta1(follower_.theta(1));
milind-u18a901d2023-02-17 21:51:55 -0800318 status_builder.add_goal_theta2(follower_.theta(2));
milind-u37385182023-02-20 15:07:28 -0800319 status_builder.add_goal_omega0(follower_.omega(0));
320 status_builder.add_goal_omega1(follower_.omega(1));
milind-u18a901d2023-02-17 21:51:55 -0800321 status_builder.add_goal_omega2(follower_.omega(2));
milind-u37385182023-02-20 15:07:28 -0800322
323 status_builder.add_theta0(arm_ekf_.X_hat(0));
324 status_builder.add_theta1(arm_ekf_.X_hat(2));
milind-u18a901d2023-02-17 21:51:55 -0800325 status_builder.add_theta2(roll_joint_loop_.X_hat(0));
milind-u37385182023-02-20 15:07:28 -0800326 status_builder.add_omega0(arm_ekf_.X_hat(1));
327 status_builder.add_omega1(arm_ekf_.X_hat(3));
milind-u18a901d2023-02-17 21:51:55 -0800328 status_builder.add_omega2(roll_joint_loop_.X_hat(1));
milind-u37385182023-02-20 15:07:28 -0800329 status_builder.add_voltage_error0(arm_ekf_.X_hat(4));
330 status_builder.add_voltage_error1(arm_ekf_.X_hat(5));
milind-u18a901d2023-02-17 21:51:55 -0800331 status_builder.add_voltage_error2(roll_joint_loop_.X_hat(2));
milind-u37385182023-02-20 15:07:28 -0800332
milind-u3b91b752023-02-25 15:21:06 -0800333 status_builder.add_arm_x(arm_x);
334 status_builder.add_arm_y(arm_y);
335 status_builder.add_arm_circular_index(arm_circular_index);
336
milind-u37385182023-02-20 15:07:28 -0800337 if (!disable) {
338 *proximal_output = ::std::max(
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800339 -constants::Values::kArmOperatingVoltage(),
340 ::std::min(constants::Values::kArmOperatingVoltage(), follower_.U(0)));
milind-u37385182023-02-20 15:07:28 -0800341 *distal_output = ::std::max(
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800342 -constants::Values::kArmOperatingVoltage(),
343 ::std::min(constants::Values::kArmOperatingVoltage(), follower_.U(1)));
milind-u18a901d2023-02-17 21:51:55 -0800344 *roll_joint_output = ::std::max(
Maxwell Henderson5c47a462023-02-25 14:40:44 -0800345 -constants::Values::kArmOperatingVoltage(),
346 ::std::min(constants::Values::kArmOperatingVoltage(), follower_.U(2)));
milind-u37385182023-02-20 15:07:28 -0800347 }
348
349 status_builder.add_path_distance_to_go(follower_.path_distance_to_go());
350 status_builder.add_current_node(current_node_);
351
352 status_builder.add_zeroed(zeroed());
353 status_builder.add_estopped(estopped());
354 status_builder.add_state(state_);
355 status_builder.add_failed_solutions(follower_.failed_solutions());
356
milind-u37385182023-02-20 15:07:28 -0800357 return status_builder.Finish();
358}
359
360} // namespace arm
361} // namespace superstructure
362} // namespace control_loops
363} // namespace y2023