James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 1 | #include "y2020/control_loops/superstructure/turret/aiming.h" |
| 2 | |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 3 | #include "y2020/constants.h" |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 4 | #include "y2020/control_loops/drivetrain/drivetrain_base.h" |
| 5 | |
| 6 | namespace y2020 { |
| 7 | namespace control_loops { |
| 8 | namespace superstructure { |
| 9 | namespace turret { |
| 10 | |
| 11 | using frc971::control_loops::Pose; |
| 12 | |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 13 | // Shooting-on-the-fly concept: |
| 14 | // The current way that we manage shooting-on-the fly endeavors to be reasonably |
| 15 | // simple, until we get a chance to see how the actual dynamics play out. |
| 16 | // Essentially, we assume that the robot's velocity will represent a constant |
| 17 | // offset to the ball's velocity over the entire trajectory to the goal and |
| 18 | // then offset the target that we are pointing at based on that. |
| 19 | // Let us assume that, if the robot shoots while not moving, regardless of shot |
| 20 | // distance, the ball's average speed-over-ground to the target will be a |
| 21 | // constant s_shot (this implies that if the robot is driving straight towards |
| 22 | // the target, the actual ball speed-over-ground will be greater than s_shot). |
| 23 | // We will define things in the robot's coordinate frame. We will be shooting |
| 24 | // at a target that is at position (target_x, target_y) in the robot frame. The |
| 25 | // robot is travelling at (v_robot_x, v_robot_y). In order to shoot the ball, |
| 26 | // we need to generate some virtual target (virtual_x, virtual_y) that we will |
| 27 | // shoot at as if we were standing still. The total time-of-flight to that |
| 28 | // target will be t_shot = norm2(virtual_x, virtual_y) / s_shot. |
| 29 | // we will have virtual_x + v_robot_x * t_shot = target_x, and the same |
| 30 | // for y. This gives us three equations and three unknowns (virtual_x, |
| 31 | // virtual_y, and t_shot), and given appropriate assumptions, can be solved |
| 32 | // analytically. However, doing so is obnoxious and given appropriate functions |
| 33 | // for t_shot may not be feasible. As such, instead of actually solving the |
| 34 | // equation analytically, we will use an iterative solution where we maintain |
| 35 | // a current virtual target estimate. We start with this estimate as if the |
| 36 | // robot is stationary. We then use this estimate to calculate t_shot, and |
| 37 | // calculate the next value for the virtual target. |
| 38 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 39 | namespace { |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 40 | // The overall length and width of the field, in meters. |
| 41 | constexpr double kFieldLength = 15.983; |
| 42 | constexpr double kFieldWidth = 8.212; |
| 43 | // Height of the center of the port(s) above the ground, in meters. |
| 44 | constexpr double kPortHeight = 2.494; |
| 45 | |
| 46 | // Maximum shot angle at which we will attempt to make the shot into the inner |
| 47 | // port, in radians. Zero would imply that we could only shoot if we were |
| 48 | // exactly perpendicular to the target. Larger numbers allow us to aim at the |
| 49 | // inner port more aggressively, at the risk of being more likely to miss the |
| 50 | // outer port entirely. |
| 51 | constexpr double kMaxInnerPortAngle = 20.0 * M_PI / 180.0; |
| 52 | |
| 53 | // Distance (in meters) from the edge of the field to the port. |
| 54 | constexpr double kEdgeOfFieldToPort = 2.404; |
| 55 | |
| 56 | // The amount (in meters) that the inner port is set back from the outer port. |
| 57 | constexpr double kInnerPortBackset = 0.743; |
| 58 | |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 59 | // Average speed-over-ground of the ball on its way to the target. Our current |
| 60 | // model assumes constant ball velocity regardless of shot distance. |
| 61 | // TODO(james): Is this an appropriate model? For the outer port it should be |
| 62 | // good enough that it doesn't really matter, but for the inner port it may be |
| 63 | // more appropriate to do something more dynamic--however, it is not yet clear |
| 64 | // how we would best estimate speed-over-ground given a hood angle + shooter |
| 65 | // speed. Assuming a constant average speed over the course of the trajectory |
| 66 | // should be reasonable, since all we are trying to do here is calculate an |
| 67 | // overall time-of-flight (we don't actually care about the ball speed itself). |
| 68 | constexpr double kBallSpeedOverGround = 15.0; // m/s |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 69 | |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 70 | // Minimum distance that we must be from the inner port in order to attempt the |
| 71 | // shot--this is to account for the fact that if we are too close to the target, |
| 72 | // then we won't have a clear shot on the inner port. |
| 73 | constexpr double kMinimumInnerPortShotDistance = 4.0; |
| 74 | |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 75 | // Amount of buffer, in radians, to leave to help avoid wrapping. I.e., any time |
| 76 | // that we are in kAvoidEdges mode, we will keep ourselves at least |
| 77 | // kAntiWrapBuffer radians away from the hardstops. |
| 78 | constexpr double kAntiWrapBuffer = 0.2; |
| 79 | |
James Kuszmaul | 64c13b7 | 2020-03-01 11:17:31 -0800 | [diff] [blame^] | 80 | // If the turret is at zero, then it will be at this angle relative to pointed |
| 81 | // straight forwards on the robot. |
| 82 | constexpr double kTurretZeroOffset = M_PI; |
| 83 | |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 84 | constexpr double kTurretRange = constants::Values::kTurretRange().range(); |
| 85 | static_assert((kTurretRange - 2.0 * kAntiWrapBuffer) > 2.0 * M_PI, |
| 86 | "kAntiWrap buffer should be small enough that we still have 360 " |
| 87 | "degrees of range."); |
| 88 | |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 89 | Pose ReverseSideOfField(Pose target) { |
| 90 | *target.mutable_pos() *= -1; |
| 91 | target.set_theta(aos::math::NormalizeAngle(target.rel_theta() + M_PI)); |
| 92 | return target; |
| 93 | } |
| 94 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 95 | flatbuffers::DetachedBuffer MakePrefilledGoal() { |
| 96 | flatbuffers::FlatBufferBuilder fbb; |
| 97 | fbb.ForceDefaults(true); |
| 98 | Aimer::Goal::Builder builder(fbb); |
| 99 | builder.add_unsafe_goal(0); |
| 100 | builder.add_goal_velocity(0); |
| 101 | builder.add_ignore_profile(true); |
| 102 | fbb.Finish(builder.Finish()); |
| 103 | return fbb.Release(); |
| 104 | } |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 105 | |
| 106 | // This implements the iteration in the described shooting-on-the-fly algorithm. |
| 107 | // robot_pose: Current robot pose. |
| 108 | // robot_velocity: Current robot velocity, in the absolute field frame. |
| 109 | // target_pose: Absolute goal Pose. |
| 110 | // current_virtual_pose: Current estimate of where we want to shoot at. |
| 111 | Pose IterateVirtualGoal(const Pose &robot_pose, |
| 112 | const Eigen::Vector3d &robot_velocity, |
| 113 | const Pose &target_pose, |
| 114 | const Pose ¤t_virtual_pose) { |
| 115 | const double air_time = |
| 116 | current_virtual_pose.Rebase(&robot_pose).xy_norm() / kBallSpeedOverGround; |
| 117 | const Eigen::Vector3d virtual_target = |
| 118 | target_pose.abs_pos() - air_time * robot_velocity; |
| 119 | return Pose(virtual_target, target_pose.abs_theta()); |
| 120 | } |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 121 | } // namespace |
| 122 | |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 123 | Pose InnerPortPose(aos::Alliance alliance) { |
| 124 | const Pose target({kFieldLength / 2 + kInnerPortBackset, |
| 125 | -kFieldWidth / 2.0 + kEdgeOfFieldToPort, kPortHeight}, |
| 126 | 0.0); |
| 127 | if (alliance == aos::Alliance::kRed) { |
| 128 | return ReverseSideOfField(target); |
| 129 | } |
| 130 | return target; |
| 131 | } |
| 132 | |
| 133 | Pose OuterPortPose(aos::Alliance alliance) { |
| 134 | Pose target( |
| 135 | {kFieldLength / 2, -kFieldWidth / 2.0 + kEdgeOfFieldToPort, kPortHeight}, |
| 136 | 0.0); |
| 137 | if (alliance == aos::Alliance::kRed) { |
| 138 | return ReverseSideOfField(target); |
| 139 | } |
| 140 | return target; |
| 141 | } |
| 142 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 143 | Aimer::Aimer() : goal_(MakePrefilledGoal()) {} |
| 144 | |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 145 | void Aimer::Update(const Status *status, aos::Alliance alliance, |
| 146 | WrapMode wrap_mode, ShotMode shot_mode) { |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 147 | const Pose robot_pose({status->x(), status->y(), 0}, status->theta()); |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 148 | const Pose inner_port = InnerPortPose(alliance); |
| 149 | const Pose outer_port = OuterPortPose(alliance); |
| 150 | const Pose robot_pose_from_inner_port = robot_pose.Rebase(&inner_port); |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 151 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 152 | // TODO(james): This code should probably just be in the localizer and have |
| 153 | // xdot/ydot get populated in the status message directly... that way we don't |
| 154 | // keep duplicating this math. |
| 155 | // Also, this doesn't currently take into account the lateral velocity of the |
| 156 | // robot. All of this would be helped by just doing this work in the Localizer |
| 157 | // itself. |
| 158 | const Eigen::Vector2d linear_angular = |
| 159 | drivetrain::GetDrivetrainConfig().Tlr_to_la() * |
| 160 | Eigen::Vector2d(status->localizer()->left_velocity(), |
| 161 | status->localizer()->right_velocity()); |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 162 | const double xdot = linear_angular(0) * std::cos(status->theta()); |
| 163 | const double ydot = linear_angular(0) * std::sin(status->theta()); |
| 164 | |
| 165 | const double inner_port_angle = robot_pose_from_inner_port.heading(); |
| 166 | const double inner_port_distance = robot_pose_from_inner_port.xy_norm(); |
| 167 | aiming_for_inner_port_ = |
| 168 | (std::abs(inner_port_angle) < kMaxInnerPortAngle) && |
| 169 | (inner_port_distance > kMinimumInnerPortShotDistance); |
| 170 | |
| 171 | // This code manages compensating the goal turret heading for the robot's |
| 172 | // current velocity, to allow for shooting on-the-fly. |
| 173 | // This works by solving for the correct turret angle numerically, since while |
| 174 | // we technically could do it analytically, doing so would both make it hard |
| 175 | // to make small changes (since it would force us to redo the math) and be |
| 176 | // error-prone since it'd be easy to make typos or other minor math errors. |
| 177 | Pose virtual_goal; |
| 178 | { |
| 179 | const Pose goal = aiming_for_inner_port_ ? inner_port : outer_port; |
| 180 | virtual_goal = goal; |
| 181 | if (shot_mode == ShotMode::kShootOnTheFly) { |
| 182 | for (int ii = 0; ii < 3; ++ii) { |
| 183 | virtual_goal = |
| 184 | IterateVirtualGoal(robot_pose, {xdot, ydot, 0}, goal, virtual_goal); |
| 185 | } |
| 186 | VLOG(1) << "Shooting-on-the-fly target position: " |
| 187 | << virtual_goal.abs_pos().transpose(); |
| 188 | } |
| 189 | virtual_goal = virtual_goal.Rebase(&robot_pose); |
| 190 | } |
| 191 | |
| 192 | const double heading_to_goal = virtual_goal.heading(); |
| 193 | CHECK(status->has_localizer()); |
| 194 | distance_ = virtual_goal.xy_norm(); |
| 195 | |
| 196 | // The following code all works to calculate what the rate of turn of the |
| 197 | // turret should be. The code only accounts for the rate of turn if we are |
| 198 | // aiming at a static target, which should be close enough to correct that it |
| 199 | // doesn't matter that it fails to account for the |
| 200 | // shooting-on-the-fly compensation. |
| 201 | const double rel_x = virtual_goal.rel_pos().x(); |
| 202 | const double rel_y = virtual_goal.rel_pos().y(); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 203 | const double squared_norm = rel_x * rel_x + rel_y * rel_y; |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 204 | |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 205 | // If squared_norm gets to be too close to zero, just zero out the relevant |
| 206 | // term to prevent NaNs. Note that this doesn't address the chattering that |
| 207 | // would likely occur if we were to get excessively close to the target. |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 208 | // Note that x and y terms are swapped relative to what you would normally see |
| 209 | // in the derivative of atan because xdot and ydot are the derivatives of |
| 210 | // robot_pos and we are working with the atan of (target_pos - robot_pos). |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 211 | const double atan_diff = (squared_norm < 1e-3) |
| 212 | ? 0.0 |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 213 | : (rel_y * xdot - rel_x * ydot) / squared_norm; |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 214 | // heading = atan2(relative_y, relative_x) - robot_theta |
| 215 | // dheading / dt = (rel_x * rel_y' - rel_y * rel_x') / (rel_x^2 + rel_y^2) - dtheta / dt |
| 216 | const double dheading_dt = atan_diff - linear_angular(1); |
| 217 | |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 218 | double range = kTurretRange; |
James Kuszmaul | 3b393d7 | 2020-02-26 19:43:51 -0800 | [diff] [blame] | 219 | if (wrap_mode == WrapMode::kAvoidEdges) { |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 220 | range -= 2.0 * kAntiWrapBuffer; |
| 221 | } |
| 222 | // Calculate a goal turret heading such that it is within +/- pi of the |
| 223 | // current position (i.e., a goal that would minimize the amount the turret |
| 224 | // would have to travel). |
| 225 | // We then check if this goal would bring us out of range of the valid angles, |
| 226 | // and if it would, we reset to be within +/- pi of zero. |
James Kuszmaul | 64c13b7 | 2020-03-01 11:17:31 -0800 | [diff] [blame^] | 227 | double turret_heading = |
| 228 | goal_.message().unsafe_goal() + |
| 229 | aos::math::NormalizeAngle(heading_to_goal - kTurretZeroOffset - |
| 230 | goal_.message().unsafe_goal()); |
James Kuszmaul | b83d6e1 | 2020-02-22 20:44:48 -0800 | [diff] [blame] | 231 | if (std::abs(turret_heading - constants::Values::kTurretRange().middle()) > |
| 232 | range / 2.0) { |
| 233 | turret_heading = aos::math::NormalizeAngle(turret_heading); |
| 234 | } |
| 235 | |
| 236 | goal_.mutable_message()->mutate_unsafe_goal(turret_heading); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 237 | goal_.mutable_message()->mutate_goal_velocity(dheading_dt); |
| 238 | } |
| 239 | |
| 240 | flatbuffers::Offset<AimerStatus> Aimer::PopulateStatus( |
| 241 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 242 | AimerStatus::Builder builder(*fbb); |
| 243 | builder.add_turret_position(goal_.message().unsafe_goal()); |
| 244 | builder.add_turret_velocity(goal_.message().goal_velocity()); |
James Kuszmaul | a53c3ac | 2020-02-22 19:36:01 -0800 | [diff] [blame] | 245 | builder.add_aiming_for_inner_port(aiming_for_inner_port_); |
James Kuszmaul | b1b2d8e | 2020-02-21 21:11:46 -0800 | [diff] [blame] | 246 | return builder.Finish(); |
| 247 | } |
| 248 | |
| 249 | } // namespace turret |
| 250 | } // namespace superstructure |
| 251 | } // namespace control_loops |
| 252 | } // namespace y2020 |