[hal, wpilib] PWM Rewrite (#7845)

The HAL will only contain the output period and the raw microseconds. Higher level things such as SimDevice can handle everything else.
This commit is contained in:
Thad House
2025-03-20 19:23:22 -07:00
committed by GitHub
parent 2e21a41f87
commit e2cc9e0059
96 changed files with 1037 additions and 2453 deletions

View File

@@ -0,0 +1,26 @@
// 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 "frc/simulation/PWMMotorControllerSim.h"
#include <hal/SimDevice.h>
#include <units/length.h>
#include "frc/simulation/SimDeviceSim.h"
using namespace frc;
using namespace frc::sim;
PWMMotorControllerSim::PWMMotorControllerSim(
const PWMMotorController& motorctrl)
: PWMMotorControllerSim(motorctrl.GetChannel()) {}
PWMMotorControllerSim::PWMMotorControllerSim(int channel) {
frc::sim::SimDeviceSim deviceSim{"PWMMotorController", channel};
m_simSpeed = deviceSim.GetDouble("Speed");
}
double PWMMotorControllerSim::GetSpeed() const {
return m_simSpeed.Get();
}

View File

@@ -16,9 +16,6 @@ using namespace frc::sim;
PWMSim::PWMSim(const PWM& pwm) : m_index{pwm.GetChannel()} {}
PWMSim::PWMSim(const PWMMotorController& motorctrl)
: m_index{motorctrl.GetChannel()} {}
PWMSim::PWMSim(int channel) : m_index{channel} {}
std::unique_ptr<CallbackStore> PWMSim::RegisterInitializedCallback(
@@ -55,72 +52,21 @@ void PWMSim::SetPulseMicrosecond(int32_t microsecondPulseTime) {
HALSIM_SetPWMPulseMicrosecond(m_index, microsecondPulseTime);
}
std::unique_ptr<CallbackStore> PWMSim::RegisterSpeedCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(m_index, -1, callback,
&HALSIM_CancelPWMSpeedCallback);
store->SetUid(HALSIM_RegisterPWMSpeedCallback(m_index, &CallbackStoreThunk,
store.get(), initialNotify));
return store;
}
double PWMSim::GetSpeed() const {
return HALSIM_GetPWMSpeed(m_index);
}
void PWMSim::SetSpeed(double speed) {
HALSIM_SetPWMSpeed(m_index, speed);
}
std::unique_ptr<CallbackStore> PWMSim::RegisterPositionCallback(
std::unique_ptr<CallbackStore> PWMSim::RegisterOutputPeriodCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelPWMPositionCallback);
store->SetUid(HALSIM_RegisterPWMPositionCallback(m_index, &CallbackStoreThunk,
store.get(), initialNotify));
return store;
}
double PWMSim::GetPosition() const {
return HALSIM_GetPWMPosition(m_index);
}
void PWMSim::SetPosition(double position) {
HALSIM_SetPWMPosition(m_index, position);
}
std::unique_ptr<CallbackStore> PWMSim::RegisterPeriodScaleCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelPWMPeriodScaleCallback);
store->SetUid(HALSIM_RegisterPWMPeriodScaleCallback(
m_index, -1, callback, &HALSIM_CancelPWMOutputPeriodCallback);
store->SetUid(HALSIM_RegisterPWMOutputPeriodCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
int PWMSim::GetPeriodScale() const {
return HALSIM_GetPWMPeriodScale(m_index);
int PWMSim::GetOutputPeriod() const {
return HALSIM_GetPWMOutputPeriod(m_index);
}
void PWMSim::SetPeriodScale(int periodScale) {
HALSIM_SetPWMPeriodScale(m_index, periodScale);
}
std::unique_ptr<CallbackStore> PWMSim::RegisterZeroLatchCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelPWMZeroLatchCallback);
store->SetUid(HALSIM_RegisterPWMZeroLatchCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
bool PWMSim::GetZeroLatch() const {
return HALSIM_GetPWMZeroLatch(m_index);
}
void PWMSim::SetZeroLatch(bool zeroLatch) {
HALSIM_SetPWMZeroLatch(m_index, zeroLatch);
void PWMSim::SetOutputPeriod(int period) {
HALSIM_SetPWMOutputPeriod(m_index, period);
}
void PWMSim::ResetData() {

View File

@@ -0,0 +1,28 @@
// 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 "frc/simulation/ServoSim.h"
#include <hal/SimDevice.h>
#include <units/length.h>
#include "frc/simulation/SimDeviceSim.h"
using namespace frc;
using namespace frc::sim;
ServoSim::ServoSim(const Servo& servo) : ServoSim(servo.GetChannel()) {}
ServoSim::ServoSim(int channel) {
frc::sim::SimDeviceSim deviceSim{"Servo", channel};
m_simPosition = deviceSim.GetDouble("Position");
}
double ServoSim::GetPosition() const {
return m_simPosition.Get();
}
double ServoSim::GetAngle() const {
return GetPosition() * Servo::GetServoAngleRange() + Servo::kMinServoAngle;
}