转向驱动测距法

用户可以使用转向驱动器运动学类来执行:ref: odometry <docs/software/kinematics-and-odometry/intro-and-chassis-speeds:What is odometry?>`。 WPILib包含一个``SwerveDriveOdometry’’类,该类可用于追踪场上的转向驱动机器人的位置。

备注

由于这种方法只使用编码器和陀螺仪,机器人在场上的位置估计会随着时间的推移而漂移,特别是当你的机器人在游戏过程中与其他机器人接触时。不过,在自主期,测距法通常非常准确。

创建测距法对象

The SwerveDriveOdometry<int NumModules> class constructor requires one template argument (only C++), three mandatory arguments, and one optional argument. The template argument (only C++) is an integer representing the number of swerve modules.

The mandatory arguments are:

  • The kinematics object that represents your swerve drive (as a SwerveDriveKinematics instance)

  • The angle reported by your gyroscope (as a Rotation2d)

  • The initial positions of the swerve modules (as an array of SwerveModulePosition). In Java, this must be constructed with each wheel position in meters. In C++, the units library must be used to represent your wheel positions. It is important that the order in which you pass the SwerveModulePosition objects is the same as the order in which you created the kinematics object.

The fourth optional argument is the starting pose of your robot on the field (as a Pose2d). By default, the robot will start at x = 0, y = 0, theta = 0.

备注

0 degrees / radians represents the robot angle when the robot is facing directly toward your opponent’s alliance station. As your robot turns to the left, your gyroscope angle should increase. The Gyro interface supplies getRotation2d/GetRotation2d that you can use for this purpose. See Field Coordinate System for more information about the coordinate system.

// Locations for the swerve drive modules relative to the robot center.
Translation2d m_frontLeftLocation = new Translation2d(0.381, 0.381);
Translation2d m_frontRightLocation = new Translation2d(0.381, -0.381);
Translation2d m_backLeftLocation = new Translation2d(-0.381, 0.381);
Translation2d m_backRightLocation = new Translation2d(-0.381, -0.381);

// Creating my kinematics object using the module locations
SwerveDriveKinematics m_kinematics = new SwerveDriveKinematics(
  m_frontLeftLocation, m_frontRightLocation, m_backLeftLocation, m_backRightLocation
);

// Creating my odometry object from the kinematics object and the initial wheel positions.
// Here, our starting pose is 5 meters along the long end of the field and in the
// center of the field along the short end, facing the opposing alliance wall.
SwerveDriveOdometry m_odometry = new SwerveDriveOdometry(
  m_kinematics, m_gyro.getRotation2d(),
  new SwerveModulePosition[] {
    m_frontLeftModule.getPosition(),
    m_frontRightModule.getPosition(),
    m_backLeftModule.getPosition(),
    m_backRightModule.getPosition()
  }, new Pose2d(5.0, 13.5, new Rotation2d()));

更新机器人姿势

The update method of the odometry class updates the robot position on the field. The update method takes in the gyro angle of the robot, along with an array of SwerveModulePosition objects. It is important that the order in which you pass the SwerveModulePosition objects is the same as the order in which you created the kinematics object.

调用此”update”方法必需为周期性,最好是在Subsystem<docs/software/commandbased/subsystems:Subsystems>的”periodic()”方法中调用。 “update”方法将返回机器人的新的姿势。

@Override
public void periodic() {
  // Get the rotation of the robot from the gyro.
  var gyroAngle = m_gyro.getRotation2d();

  // Update the pose
  m_pose = m_odometry.update(gyroAngle,
    new SwerveModulePosition[] {
      m_frontLeftModule.getPosition(), m_frontRightModule.getPosition(),
      m_backLeftModule.getPosition(), m_backRightModule.getPosition()
    });
}

重置机器人姿势

The robot pose can be reset via the resetPosition method. This method accepts three arguments: the current gyro angle, an array of the current module positions (as in the constructor and update method), and the new field-relative pose.

重要

If at any time, you decide to reset your gyroscope or wheel encoders, the resetPosition method MUST be called with the new gyro angle and wheel encoder positions.

备注

The implementation of getPosition() / GetPosition() above is left to the user. The idea is to get the module position (distance and angle) from each module. For a full example, see here: C++ / Java.

此外,可以使用GetPose(C ++)/ getPoseMeters(Java)方法来检索当前的机器人姿态,而无需进行更新。