[wpilib][sim] Add Onboard IMU Sim (#8855)

This provides the ability to simulate parts of the Onboard IMU at the
HAL level. This allows team to use and simulate the IMU in code, and a
follow up PR could be made to the halsim_gui to add a new widget to view
and modify the data graphically.

Since the C++ IMU uses radians for angles that is what I did for the
simulator.

Partially deals with #8845
This commit is contained in:
PJ Reiniger
2026-05-07 13:01:26 -04:00
committed by GitHub
parent 3d4cabfbc9
commit 4c07aedd91
23 changed files with 713 additions and 15 deletions

View File

@@ -148,7 +148,7 @@ wpi::units::meters_per_second_squared_t OnboardIMU::GetAccelY() {
int32_t status = 0;
HAL_GetIMUAcceleration(&val, &status);
WPILIB_CheckErrorStatus(status, "Onboard IMU");
return wpi::units::meters_per_second_squared_t{val.x};
return wpi::units::meters_per_second_squared_t{val.y};
}
wpi::units::meters_per_second_squared_t OnboardIMU::GetAccelZ() {
@@ -156,5 +156,5 @@ wpi::units::meters_per_second_squared_t OnboardIMU::GetAccelZ() {
int32_t status = 0;
HAL_GetIMUAcceleration(&val, &status);
WPILIB_CheckErrorStatus(status, "Onboard IMU");
return wpi::units::meters_per_second_squared_t{val.x};
return wpi::units::meters_per_second_squared_t{val.z};
}

View File

@@ -0,0 +1,49 @@
// 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/simulation/OnboardIMUSim.hpp"
#include "wpi/hal/simulation/IMUData.h"
namespace wpi::sim {
void OnboardIMUSim::SetAngleX(wpi::units::radian_t angle) {
HALSIM_SetIMUAngleX(angle.to<double>());
}
void OnboardIMUSim::SetAngleY(wpi::units::radian_t angle) {
HALSIM_SetIMUAngleY(angle.to<double>());
}
void OnboardIMUSim::SetAngleZ(wpi::units::radian_t angle) {
HALSIM_SetIMUAngleZ(angle.to<double>());
}
void OnboardIMUSim::SetGyroRateX(wpi::units::radians_per_second_t rate) {
HALSIM_SetIMUGyroRateX(rate.to<double>());
}
void OnboardIMUSim::SetGyroRateY(wpi::units::radians_per_second_t rate) {
HALSIM_SetIMUGyroRateY(rate.to<double>());
}
void OnboardIMUSim::SetGyroRateZ(wpi::units::radians_per_second_t rate) {
HALSIM_SetIMUGyroRateZ(rate.to<double>());
}
void OnboardIMUSim::SetAccelX(wpi::units::meters_per_second_squared_t accel) {
HALSIM_SetIMUAccelX(accel.to<double>());
}
void OnboardIMUSim::SetAccelY(wpi::units::meters_per_second_squared_t accel) {
HALSIM_SetIMUAccelY(accel.to<double>());
}
void OnboardIMUSim::SetAccelZ(wpi::units::meters_per_second_squared_t accel) {
HALSIM_SetIMUAccelZ(accel.to<double>());
}
void OnboardIMUSim::SetYaw(wpi::units::radian_t angle) {
HALSIM_SetIMUYaw(angle.to<double>());
}
} // namespace wpi::sim

View File

@@ -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.
#pragma once
#include "wpi/units/acceleration.hpp"
#include "wpi/units/angle.hpp"
#include "wpi/units/angular_velocity.hpp"
namespace wpi::sim {
class OnboardIMUSim {
public:
void SetAngleX(wpi::units::radian_t angle);
void SetAngleY(wpi::units::radian_t angle);
void SetAngleZ(wpi::units::radian_t angle);
void SetGyroRateX(wpi::units::radians_per_second_t rate);
void SetGyroRateY(wpi::units::radians_per_second_t rate);
void SetGyroRateZ(wpi::units::radians_per_second_t rate);
void SetAccelX(wpi::units::meters_per_second_squared_t accel);
void SetAccelY(wpi::units::meters_per_second_squared_t accel);
void SetAccelZ(wpi::units::meters_per_second_squared_t accel);
void SetYaw(wpi::units::radian_t angle);
};
} // namespace wpi::sim