mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[wpimath] Add ChassisAccelerations and drivetrain accelerations classes and add forward and inverse kinematics for accelerations to the interface (#8185)
ChassisAccelerations and the drivetrain acceleration types are added in both Java and C++. `ChassisAccelerations` is basically just `ChassisSpeeds` but for accelerations! `DifferentialDriveWheelAccelerations`, `MecanumDriveWheelAccelerations`, and `SwerveModuleAccelerations` are the acceleration equivalent of the drivetrain speeds types. In Java, the `Kinematics` interface now has an additional generic parameter `A` which represents the accelerations, and `toChassisAccelerations` and `toWheelAccelerations` methods, which are implemented the same way as `toChassisSpeeds` and `toWheelSpeeds`. Protobuf and struct classes were also added for all four classes in Java and C++. --------- Signed-off-by: Zach Harel <zach@zharel.me> Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com> Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
@@ -87,3 +87,54 @@ void MecanumDriveKinematics::SetInverseKinematics(Translation2d fl,
|
||||
{1, 1, (rl.X() - rl.Y()).value()},
|
||||
{1, -1, (-(rr.X() + rr.Y())).value()}};
|
||||
}
|
||||
|
||||
ChassisAccelerations MecanumDriveKinematics::ToChassisAccelerations(
|
||||
const MecanumDriveWheelAccelerations& wheelAccelerations) const {
|
||||
Eigen::Vector4d wheelAccelerationsVector{
|
||||
wheelAccelerations.frontLeft.value(),
|
||||
wheelAccelerations.frontRight.value(),
|
||||
wheelAccelerations.rearLeft.value(),
|
||||
wheelAccelerations.rearRight.value()};
|
||||
|
||||
Eigen::Vector3d chassisAccelerationsVector =
|
||||
m_forwardKinematics.solve(wheelAccelerationsVector);
|
||||
|
||||
return {
|
||||
wpi::units::meters_per_second_squared_t{chassisAccelerationsVector(0)},
|
||||
wpi::units::meters_per_second_squared_t{chassisAccelerationsVector(1)},
|
||||
wpi::units::radians_per_second_squared_t{chassisAccelerationsVector(2)}};
|
||||
}
|
||||
|
||||
MecanumDriveWheelAccelerations MecanumDriveKinematics::ToWheelAccelerations(
|
||||
const ChassisAccelerations& chassisAccelerations,
|
||||
const Translation2d& centerOfRotation) const {
|
||||
// We have a new center of rotation. We need to compute the matrix again.
|
||||
if (centerOfRotation != m_previousCoR) {
|
||||
auto fl = m_frontLeftWheel - centerOfRotation;
|
||||
auto fr = m_frontRightWheel - centerOfRotation;
|
||||
auto rl = m_rearLeftWheel - centerOfRotation;
|
||||
auto rr = m_rearRightWheel - centerOfRotation;
|
||||
|
||||
SetInverseKinematics(fl, fr, rl, rr);
|
||||
|
||||
m_previousCoR = centerOfRotation;
|
||||
}
|
||||
|
||||
Eigen::Vector3d chassisAccelerationsVector{
|
||||
chassisAccelerations.ax.value(), chassisAccelerations.ay.value(),
|
||||
chassisAccelerations.alpha.value()};
|
||||
|
||||
Eigen::Vector4d wheelsVector =
|
||||
m_inverseKinematics * chassisAccelerationsVector;
|
||||
|
||||
MecanumDriveWheelAccelerations wheelAccelerations;
|
||||
wheelAccelerations.frontLeft =
|
||||
wpi::units::meters_per_second_squared_t{wheelsVector(0)};
|
||||
wheelAccelerations.frontRight =
|
||||
wpi::units::meters_per_second_squared_t{wheelsVector(1)};
|
||||
wheelAccelerations.rearLeft =
|
||||
wpi::units::meters_per_second_squared_t{wheelsVector(2)};
|
||||
wheelAccelerations.rearRight =
|
||||
wpi::units::meters_per_second_squared_t{wheelsVector(3)};
|
||||
return wheelAccelerations;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/proto/ChassisAccelerationsProto.hpp"
|
||||
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
std::optional<wpi::math::ChassisAccelerations> wpi::util::Protobuf<
|
||||
wpi::math::ChassisAccelerations>::Unpack(InputStream& stream) {
|
||||
wpi_proto_ProtobufChassisAccelerations msg;
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return wpi::math::ChassisAccelerations{
|
||||
units::meters_per_second_squared_t{msg.ax},
|
||||
units::meters_per_second_squared_t{msg.ay},
|
||||
units::radians_per_second_squared_t{msg.alpha},
|
||||
};
|
||||
}
|
||||
|
||||
bool wpi::util::Protobuf<wpi::math::ChassisAccelerations>::Pack(
|
||||
OutputStream& stream, const wpi::math::ChassisAccelerations& value) {
|
||||
wpi_proto_ProtobufChassisAccelerations msg{
|
||||
.ax = value.ax.value(),
|
||||
.ay = value.ay.value(),
|
||||
.alpha = value.alpha.value(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/proto/DifferentialDriveWheelAccelerationsProto.hpp"
|
||||
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
std::optional<wpi::math::DifferentialDriveWheelAccelerations>
|
||||
wpi::util::Protobuf<wpi::math::DifferentialDriveWheelAccelerations>::Unpack(
|
||||
InputStream& stream) {
|
||||
wpi_proto_ProtobufDifferentialDriveWheelAccelerations msg;
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return wpi::math::DifferentialDriveWheelAccelerations{
|
||||
units::meters_per_second_squared_t{msg.left},
|
||||
units::meters_per_second_squared_t{msg.right},
|
||||
};
|
||||
}
|
||||
|
||||
bool wpi::util::Protobuf<wpi::math::DifferentialDriveWheelAccelerations>::Pack(
|
||||
OutputStream& stream,
|
||||
const wpi::math::DifferentialDriveWheelAccelerations& value) {
|
||||
wpi_proto_ProtobufDifferentialDriveWheelAccelerations msg{
|
||||
.left = value.left.value(),
|
||||
.right = value.right.value(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/proto/MecanumDriveWheelAccelerationsProto.hpp"
|
||||
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
std::optional<wpi::math::MecanumDriveWheelAccelerations> wpi::util::Protobuf<
|
||||
wpi::math::MecanumDriveWheelAccelerations>::Unpack(InputStream& stream) {
|
||||
wpi_proto_ProtobufMecanumDriveWheelAccelerations msg;
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return wpi::math::MecanumDriveWheelAccelerations{
|
||||
units::meters_per_second_squared_t{msg.front_left},
|
||||
units::meters_per_second_squared_t{msg.front_right},
|
||||
units::meters_per_second_squared_t{msg.rear_left},
|
||||
units::meters_per_second_squared_t{msg.rear_right},
|
||||
};
|
||||
}
|
||||
|
||||
bool wpi::util::Protobuf<wpi::math::MecanumDriveWheelAccelerations>::Pack(
|
||||
OutputStream& stream,
|
||||
const wpi::math::MecanumDriveWheelAccelerations& value) {
|
||||
wpi_proto_ProtobufMecanumDriveWheelAccelerations msg{
|
||||
.front_left = value.frontLeft.value(),
|
||||
.front_right = value.frontRight.value(),
|
||||
.rear_left = value.rearLeft.value(),
|
||||
.rear_right = value.rearRight.value(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/proto/SwerveModuleAccelerationProto.hpp"
|
||||
|
||||
#include "wpi/util/protobuf/ProtobufCallbacks.hpp"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
std::optional<wpi::math::SwerveModuleAcceleration> wpi::util::Protobuf<
|
||||
wpi::math::SwerveModuleAcceleration>::Unpack(InputStream& stream) {
|
||||
wpi::util::UnpackCallback<wpi::math::Rotation2d> angle;
|
||||
wpi_proto_ProtobufSwerveModuleAcceleration msg{
|
||||
.acceleration = 0,
|
||||
.angle = angle.Callback(),
|
||||
};
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto iangle = angle.Items();
|
||||
|
||||
if (iangle.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return wpi::math::SwerveModuleAcceleration{
|
||||
units::meters_per_second_squared_t{msg.acceleration},
|
||||
iangle[0],
|
||||
};
|
||||
}
|
||||
|
||||
bool wpi::util::Protobuf<wpi::math::SwerveModuleAcceleration>::Pack(
|
||||
OutputStream& stream, const wpi::math::SwerveModuleAcceleration& value) {
|
||||
wpi::util::PackCallback angle{&value.angle};
|
||||
wpi_proto_ProtobufSwerveModuleAcceleration msg{
|
||||
.acceleration = value.acceleration.value(),
|
||||
.angle = angle.Callback(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/struct/ChassisAccelerationsStruct.hpp"
|
||||
|
||||
#include "wpi/math/kinematics/ChassisAccelerations.hpp"
|
||||
#include "wpi/util/struct/Struct.hpp"
|
||||
|
||||
wpi::math::ChassisAccelerations wpi::util::Struct<
|
||||
wpi::math::ChassisAccelerations>::Unpack(std::span<const uint8_t> data) {
|
||||
constexpr size_t kAxOff = 0;
|
||||
constexpr size_t kAyOff = kAxOff + 8;
|
||||
constexpr size_t kAlphaOff = kAyOff + 8;
|
||||
return wpi::math::ChassisAccelerations{
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kAxOff>(data)},
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kAyOff>(data)},
|
||||
units::radians_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kAlphaOff>(data)},
|
||||
};
|
||||
}
|
||||
|
||||
void wpi::util::Struct<wpi::math::ChassisAccelerations>::Pack(
|
||||
std::span<uint8_t> data, const wpi::math::ChassisAccelerations& value) {
|
||||
constexpr size_t kAxOff = 0;
|
||||
constexpr size_t kAyOff = kAxOff + 8;
|
||||
constexpr size_t kAlphaOff = kAyOff + 8;
|
||||
wpi::util::PackStruct<kAxOff>(data, value.ax.value());
|
||||
wpi::util::PackStruct<kAyOff>(data, value.ay.value());
|
||||
wpi::util::PackStruct<kAlphaOff>(data, value.alpha.value());
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/struct/DifferentialDriveWheelAccelerationsStruct.hpp"
|
||||
|
||||
#include "wpi/math/kinematics/DifferentialDriveWheelAccelerations.hpp"
|
||||
#include "wpi/util/struct/Struct.hpp"
|
||||
|
||||
wpi::math::DifferentialDriveWheelAccelerations
|
||||
wpi::util::Struct<wpi::math::DifferentialDriveWheelAccelerations>::Unpack(
|
||||
std::span<const uint8_t> data) {
|
||||
constexpr size_t kLeftOff = 0;
|
||||
constexpr size_t kRightOff = kLeftOff + 8;
|
||||
return wpi::math::DifferentialDriveWheelAccelerations{
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kLeftOff>(data)},
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kRightOff>(data)},
|
||||
};
|
||||
}
|
||||
|
||||
void wpi::util::Struct<wpi::math::DifferentialDriveWheelAccelerations>::Pack(
|
||||
std::span<uint8_t> data,
|
||||
const wpi::math::DifferentialDriveWheelAccelerations& value) {
|
||||
constexpr size_t kLeftOff = 0;
|
||||
constexpr size_t kRightOff = kLeftOff + 8;
|
||||
wpi::util::PackStruct<kLeftOff>(data, value.left.value());
|
||||
wpi::util::PackStruct<kRightOff>(data, value.right.value());
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/struct/MecanumDriveWheelAccelerationsStruct.hpp"
|
||||
|
||||
#include "wpi/math/kinematics/MecanumDriveWheelAccelerations.hpp"
|
||||
#include "wpi/util/struct/Struct.hpp"
|
||||
|
||||
wpi::math::MecanumDriveWheelAccelerations
|
||||
wpi::util::Struct<wpi::math::MecanumDriveWheelAccelerations>::Unpack(
|
||||
std::span<const uint8_t> data) {
|
||||
constexpr size_t kFrontLeftOff = 0;
|
||||
constexpr size_t kFrontRightOff = kFrontLeftOff + 8;
|
||||
constexpr size_t kRearLeftOff = kFrontRightOff + 8;
|
||||
constexpr size_t kRearRightOff = kRearLeftOff + 8;
|
||||
return wpi::math::MecanumDriveWheelAccelerations{
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kFrontLeftOff>(data)},
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kFrontRightOff>(data)},
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kRearLeftOff>(data)},
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kRearRightOff>(data)},
|
||||
};
|
||||
}
|
||||
|
||||
void wpi::util::Struct<wpi::math::MecanumDriveWheelAccelerations>::Pack(
|
||||
std::span<uint8_t> data,
|
||||
const wpi::math::MecanumDriveWheelAccelerations& value) {
|
||||
constexpr size_t kFrontLeftOff = 0;
|
||||
constexpr size_t kFrontRightOff = kFrontLeftOff + 8;
|
||||
constexpr size_t kRearLeftOff = kFrontRightOff + 8;
|
||||
constexpr size_t kRearRightOff = kRearLeftOff + 8;
|
||||
wpi::util::PackStruct<kFrontLeftOff>(data, value.frontLeft.value());
|
||||
wpi::util::PackStruct<kFrontRightOff>(data, value.frontRight.value());
|
||||
wpi::util::PackStruct<kRearLeftOff>(data, value.rearLeft.value());
|
||||
wpi::util::PackStruct<kRearRightOff>(data, value.rearRight.value());
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "wpi/math/kinematics/struct/SwerveModuleAccelerationStruct.hpp"
|
||||
|
||||
#include "wpi/math/kinematics/SwerveModuleAcceleration.hpp"
|
||||
#include "wpi/util/struct/Struct.hpp"
|
||||
|
||||
wpi::math::SwerveModuleAcceleration
|
||||
wpi::util::Struct<wpi::math::SwerveModuleAcceleration>::Unpack(
|
||||
std::span<const uint8_t> data) {
|
||||
constexpr size_t kAccelerationOff = 0;
|
||||
constexpr size_t kAngleOff = kAccelerationOff + 8;
|
||||
return wpi::math::SwerveModuleAcceleration{
|
||||
units::meters_per_second_squared_t{
|
||||
wpi::util::UnpackStruct<double, kAccelerationOff>(data)},
|
||||
wpi::util::UnpackStruct<wpi::math::Rotation2d, kAngleOff>(data)};
|
||||
}
|
||||
|
||||
void wpi::util::Struct<wpi::math::SwerveModuleAcceleration>::Pack(
|
||||
std::span<uint8_t> data, const wpi::math::SwerveModuleAcceleration& value) {
|
||||
constexpr size_t kAccelerationOff = 0;
|
||||
constexpr size_t kAngleOff = kAccelerationOff + 8;
|
||||
wpi::util::PackStruct<kAccelerationOff>(data, value.acceleration.value());
|
||||
wpi::util::PackStruct<kAngleOff>(data, value.angle);
|
||||
}
|
||||
Reference in New Issue
Block a user