###### tags: `程式組教程` # Introduction to Kinematics and The Chassis Speeds Class # What is kinematics? The brand new kinematics suite contains classes for differential drive, swerve drive, and mecanum drive kinematics and odometry. The kinematics classes help convert between a universal **ChassisSpeeds** object, containing linear and angular velocities for a robot to usable speeds for each individual type of drivetrain i.e. left and right wheel speeds for a differential drive, four wheel speeds for a mecanum drive, or ==individual module states (speed and angle) for a swerve drive. :::spoiler Chinese > kinematics 運動學 > suite 套件 > odometry 里程計 > convert 轉換 > universal 普遍的 > Chassis Speed 物件 > linear and angular velocities 線和角速度 > usable 可用的 > individual 單獨的 > i.e. 舉例來說 > individual module states 單得的模塊狀態 ::: # What is odometry? Odometry involves using sensors on the robot to create an estimate of the position of the robot on the field. In FRC, these sensors are typically several encoders (the exact number depends on the drive type) and a gyroscope to measure robot angle. The odometry classes utilize the kinematics classes along with periodic user inputs about speeds (and angles in the case of swerve) to create an estimate of the robot’s location on the field. :::spoiler Chinese > odometry 里程計 > involves 涉及 > estimate 估計 > typically 通常(是) > exact number 確切的速度 > gyroscope 陀螺儀 > utilize 利用 > along with 隨著 > periodic 週期性(規律性的) > estimate 估計 ::: # The Chassis Speeds Class The **ChassisSpeeds** object is essential to the new WPILib kinematics and odometry suite. The **ChassisSpeeds** object represents the speeds of a robot chassis. This struct has three components: * ****vx**:** The velocity of the robot in the x(forward)direction. * **vy:** The velocity of the robot in the y(sideways)direction. ➨(Positive values mean the robot is moving to the left) * **omega:** The angular velocity of the robot in radians per second.(degrees per second) :::info **Note:** A non-holonomic drivetrain (i.e. a drivetrain that cannot move sideways, ex: a differential drive) will have a **vy** component of zero because of its inability to move sideways. ::: :::spoiler Chinese > essential 基本的 > represents 代表 > chassis 底盤 > struct 結構 > components 部分 > angular velocity 角速度 > radians 弧度 > non-holonomic 非完整系統 > i.e 舉例說明 > vy component of zero Y分量(力)為零 > inability 無能力的 ::: # Constructing a ChassisSpeeds object The constructor for the **ChassisSpeeds** object is very straightforward, accepting three arguments for **vx**, **vy**, and **omega**. In Java, **vx** and **vy** must be in meters per second. In C++, the units library may be used to provide a linear velocity using any linear velocity unit. :::spoiler Chinese > constructor 構造函數 > straightforward 直觀的;直截了當 > arguments 參數 > linear velocity 線速度 ::: ==**API**== ``` Constructor: ChassisSpeeds( double vxMetersPerSecond, double vyMetersPerSecond, double omegaRadiansPerSecond ) Description: Constructs a ChassisSpeeds object. ``` ==**JAVA**== ```java= // The robot is moving at: // 3 meters per second forward, // 2 meters per second to the right, // rotating at half a rotation persecond counterclockwise. var speeds = new ChassisSpeeds(3.0, -2.0, Math.PI); ``` ==**C++**== ```cpp= // The robot is moving at: // 3 meters per second forward, // 2 meters per second to the right, // rotating at half a rotation per second counterclockwise. frc::ChassisSpeeds speeds{ 3.0_mps, -2.0_mps, units::radians_per_second_t(wpi::math::pi) }; ``` # Creating a ChassisSpeeds Object from Field-Relative Speeds A **ChassisSpeeds** object can also be created from a set of field-relative speeds when the robot angle is given. This converts a set of desired velocities relative to the field (for example, toward the opposite alliance station and toward the right field boundary) to a **ChassisSpeeds** object which represents speeds that are relative to the robot frame. This is useful for implementing field-oriented controls for a swerve or mecanum drive robot. The static **ChassisSpeeds.fromFieldRelativeSpeeds** (Java) / **ChassisSpeeds::FromFieldRelativeSpeeds** (C++) method can be used to generate the **ChassisSpeeds** object from field-relative speeds. This method accepts the **vx** (relative to the field), **vy** (relative to the field), **omega**, and the robot angle. :::spoiler Chinese > a set of 一套 > field-relative 相對於場地的;和場地有關的 > converts 轉換 > alliance 聯盟 > boundary 邊界 > represents 代表 > frame 機殼;框架 > implementing 實施 > field-oriented controls 場地面相控制 > static 靜止的;靜態 > generate 產生 > field-relative 相對於場地的;和場地有關的 ::: ==**API**== ``` Modifier and Type: static ChassisSpeeds Method: fromFieldRelativeSpeeds( double vxMetersPerSecond, double vyMetersPerSecond, double omegaRadiansPerSecond, Rotation2d robotAngle ) Description: Converts a user provided field-relative set of speeds into a robot-relative ChassisSpeeds object. ``` ==**JAVA**== ```java= // The desired field relative speed here is 2 meters per second // toward the opponent's alliance station wall // , and 2 meters per second toward the left field boundary. // The desired rotation is a quarter of a rotation // per second counterclockwise. // The current robot angle is 45 degrees. ChassisSpeeds speeds = ChassisSpeeds.fromFieldRelativeSpeeds( 2.0, 2.0, Math.PI / 2.0, Rotation2d.fromDegrees(45.0) ); ``` ==**C++**== ```cpp= // The desired field relative speed here is 2 meters per second // toward the opponent's alliance station wall, and 2 meters per // second toward the left field boundary. The desired rotation // is a quarter of a rotation per second counterclockwise. The current // robot angle is 45 degrees. frc::ChassisSpeeds speeds = frc::ChassisSpeeds::FromFieldRelativeSpeeds( 2_mps, 2_mps, units::radians_per_second_t(wpi::math::pi / 2.0), Rotation2d(45_deg) ); ``` # Original link and Reference website * [FRC Official - Introduction to Kinematics and The Chassis Speeds Class](https://docs.wpilib.org/en/stable/docs/software/kinematics-and-odometry/intro-and-chassis-speeds.html#what-is-kinematics) * [API - Rotation2d](https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/math/geometry/Rotation2d.html) * [API - ChassisSpeeds](https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/math/kinematics/ChassisSpeeds.html)