From 2441b571560409ae826f4a7562fa0412d596e710 Mon Sep 17 00:00:00 2001 From: sciencewhiz Date: Sat, 23 Dec 2023 23:47:38 -0800 Subject: [PATCH] [wpilib] Add PWMSparkFlex MotorController (#6089) --- .../native/cpp/motorcontrol/PWMSparkFlex.cpp | 20 +++++++++ .../include/frc/motorcontrol/PWMSparkFlex.h | 41 +++++++++++++++++ .../wpilibj/motorcontrol/PWMSparkFlex.java | 45 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 wpilibc/src/main/native/cpp/motorcontrol/PWMSparkFlex.cpp create mode 100644 wpilibc/src/main/native/include/frc/motorcontrol/PWMSparkFlex.h create mode 100644 wpilibj/src/main/java/edu/wpi/first/wpilibj/motorcontrol/PWMSparkFlex.java diff --git a/wpilibc/src/main/native/cpp/motorcontrol/PWMSparkFlex.cpp b/wpilibc/src/main/native/cpp/motorcontrol/PWMSparkFlex.cpp new file mode 100644 index 0000000000..945a70e22e --- /dev/null +++ b/wpilibc/src/main/native/cpp/motorcontrol/PWMSparkFlex.cpp @@ -0,0 +1,20 @@ +// 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/motorcontrol/PWMSparkFlex.h" + +#include + +using namespace frc; + +PWMSparkFlex::PWMSparkFlex(int channel) + : PWMMotorController("PWMSparkFlex", channel) { + m_pwm.SetBounds(2.003_ms, 1.55_ms, 1.50_ms, 1.46_ms, 0.999_ms); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); + + HAL_Report(HALUsageReporting::kResourceType_RevSparkFlexPWM, + GetChannel() + 1); +} diff --git a/wpilibc/src/main/native/include/frc/motorcontrol/PWMSparkFlex.h b/wpilibc/src/main/native/include/frc/motorcontrol/PWMSparkFlex.h new file mode 100644 index 0000000000..3106921d13 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/motorcontrol/PWMSparkFlex.h @@ -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. + +#pragma once + +#include "frc/motorcontrol/PWMMotorController.h" + +namespace frc { + +/** + * REV Robotics SPARK Flex Motor %Controller. + * + * Note that the SPARK Flex uses the following bounds for PWM values. These + * values should work reasonably well for most controllers, but if users + * experience issues such as asymmetric behavior around the deadband or + * inability to saturate the controller in either direction, calibration is + * recommended. The calibration procedure can be found in the SPARK Flex User + * Manual available from REV Robotics. + * + * \li 2.003ms = full "forward" + * \li 1.550ms = the "high end" of the deadband range + * \li 1.500ms = center of the deadband range (off) + * \li 1.460ms = the "low end" of the deadband range + * \li 0.999ms = full "reverse" + */ +class PWMSparkFlex : public PWMMotorController { + public: + /** + * Constructor for a SPARK Flex. + * + * @param channel The PWM channel that the SPARK Flex is attached to. 0-9 are + * on-board, 10-19 are on the MXP port + */ + explicit PWMSparkFlex(int channel); + + PWMSparkFlex(PWMSparkFlex&&) = default; + PWMSparkFlex& operator=(PWMSparkFlex&&) = default; +}; + +} // namespace frc diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/motorcontrol/PWMSparkFlex.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/motorcontrol/PWMSparkFlex.java new file mode 100644 index 0000000000..059b18af1d --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/motorcontrol/PWMSparkFlex.java @@ -0,0 +1,45 @@ +// 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. + +package edu.wpi.first.wpilibj.motorcontrol; + +import edu.wpi.first.hal.FRCNetComm.tResourceType; +import edu.wpi.first.hal.HAL; +import edu.wpi.first.wpilibj.PWM; + +/** + * REV Robotics SPARK Flex Motor Controller with PWM control. + * + *

Note that the SPARK Flex uses the following bounds for PWM values. These values should work + * reasonably well for most controllers, but if users experience issues such as asymmetric behavior + * around the deadband or inability to saturate the controller in either direction, calibration is + * recommended. The calibration procedure can be found in the SPARK Flex User Manual available from + * REV Robotics. + * + *

+ */ +public class PWMSparkFlex extends PWMMotorController { + /** + * Common initialization code called by all constructors. + * + * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port + */ + @SuppressWarnings("this-escape") + public PWMSparkFlex(final int channel) { + super("PWMSparkFlex", channel); + + m_pwm.setBoundsMicroseconds(2003, 1550, 1500, 1460, 999); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); + + HAL.report(tResourceType.kResourceType_RevSparkFlexPWM, getChannel() + 1); + } +}