From fa903dd9cebc4bbe725c0c2407a75c3e9cfccccc Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 11 Dec 2015 16:37:53 -0500 Subject: [PATCH] Add SPARK and SD540 motor controllers Change-Id: I33f9c588b6d535b1f274d211563ef146f34439b1 --- wpilibc/Athena/include/SD540.h | 31 +++++ wpilibc/Athena/include/Spark.h | 31 +++++ wpilibc/Athena/include/WPILib.h | 2 + wpilibc/Athena/src/SD540.cpp | 88 +++++++++++++ wpilibc/Athena/src/Spark.cpp | 88 +++++++++++++ .../java/edu/wpi/first/wpilibj/SD540.java | 123 ++++++++++++++++++ .../java/edu/wpi/first/wpilibj/Spark.java | 123 ++++++++++++++++++ 7 files changed, 486 insertions(+) create mode 100644 wpilibc/Athena/include/SD540.h create mode 100644 wpilibc/Athena/include/Spark.h create mode 100644 wpilibc/Athena/src/SD540.cpp create mode 100644 wpilibc/Athena/src/Spark.cpp create mode 100644 wpilibj/src/athena/java/edu/wpi/first/wpilibj/SD540.java create mode 100644 wpilibj/src/athena/java/edu/wpi/first/wpilibj/Spark.java diff --git a/wpilibc/Athena/include/SD540.h b/wpilibc/Athena/include/SD540.h new file mode 100644 index 0000000000..2d88d10649 --- /dev/null +++ b/wpilibc/Athena/include/SD540.h @@ -0,0 +1,31 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008. All Rights Reserved. + */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ +/*----------------------------------------------------------------------------*/ +#pragma once + +#include "SafePWM.h" +#include "SpeedController.h" +#include "PIDOutput.h" + +/** + * Mindsensors SD540 Speed Controller + */ +class SD540 : public SafePWM, public SpeedController { + public: + explicit SD540(uint32_t channel); + virtual ~SD540() = default; + virtual void Set(float value, uint8_t syncGroup = 0) override; + virtual float Get() const override; + virtual void Disable() override; + + virtual void PIDWrite(float output) override; + + virtual void SetInverted(bool isInverted) override; + virtual bool GetInverted() const override; + + private: + bool m_isInverted = false; +}; diff --git a/wpilibc/Athena/include/Spark.h b/wpilibc/Athena/include/Spark.h new file mode 100644 index 0000000000..57fe851908 --- /dev/null +++ b/wpilibc/Athena/include/Spark.h @@ -0,0 +1,31 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008. All Rights Reserved. + */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ +/*----------------------------------------------------------------------------*/ +#pragma once + +#include "SafePWM.h" +#include "SpeedController.h" +#include "PIDOutput.h" + +/** + * REV Robotics Speed Controller + */ +class Spark : public SafePWM, public SpeedController { + public: + explicit Spark(uint32_t channel); + virtual ~Spark() = default; + virtual void Set(float value, uint8_t syncGroup = 0) override; + virtual float Get() const override; + virtual void Disable() override; + + virtual void PIDWrite(float output) override; + + virtual void SetInverted(bool isInverted) override; + virtual bool GetInverted() const override; + + private: + bool m_isInverted = false; +}; diff --git a/wpilibc/Athena/include/WPILib.h b/wpilibc/Athena/include/WPILib.h index 0386b03fcf..1428d2424f 100644 --- a/wpilibc/Athena/include/WPILib.h +++ b/wpilibc/Athena/include/WPILib.h @@ -71,6 +71,7 @@ #include "Resource.h" #include "RobotBase.h" #include "RobotDrive.h" +#include "SD540.h" #include "SensorBase.h" #include "SerialPort.h" #include "Servo.h" @@ -78,6 +79,7 @@ #include "SmartDashboard/SendableChooser.h" #include "SmartDashboard/SmartDashboard.h" #include "Solenoid.h" +#include "Spark.h" #include "SpeedController.h" #include "SPI.h" #include "Talon.h" diff --git a/wpilibc/Athena/src/SD540.cpp b/wpilibc/Athena/src/SD540.cpp new file mode 100644 index 0000000000..ddac020926 --- /dev/null +++ b/wpilibc/Athena/src/SD540.cpp @@ -0,0 +1,88 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008. All Rights Reserved. + */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ +/*----------------------------------------------------------------------------*/ + +#include "SD540.h" + +#include "LiveWindow/LiveWindow.h" + +/** + * Note that the SD540 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 SD540 User Manual available + * from Mindsensors. + * + * 2.05ms = full "forward" + * 1.55ms = the "high end" of the deadband range + * 1.50ms = center of the deadband range (off) + * 1.44ms = the "low end" of the deadband range + * 0.94ms = full "reverse" + */ + +/** + * Constructor for a SD540 + * @param channel The PWM channel that the SD540 is attached to. 0-9 are + * on-board, 10-19 are on the MXP port + */ +SD540::SD540(uint32_t channel) : SafePWM(channel) { + SetBounds(2.05, 1.55, 1.50, 1.44, .94); + SetPeriodMultiplier(kPeriodMultiplier_1X); + SetRaw(m_centerPwm); + SetZeroLatch(); + + HALReport(HALUsageReporting::kResourceType_MindsensorsSD540, GetChannel()); + LiveWindow::GetInstance()->AddActuator("SD540", GetChannel(), this); +} + +/** + * Set the PWM value. + * + * The PWM value is set using a range of -1.0 to 1.0, appropriately + * scaling the value for the FPGA. + * + * @param speed The speed value between -1.0 and 1.0 to set. + * @param syncGroup Unused interface. + */ +void SD540::Set(float speed, uint8_t syncGroup) { + SetSpeed(m_isInverted ? -speed : speed); +} + +/** + * Get the recently set value of the PWM. + * + * @return The most recently set value for the PWM between -1.0 and 1.0. + */ +float SD540::Get() const { return GetSpeed(); } + +/** + * Common interface for inverting direction of a speed controller. + * @param isInverted The state of inversion, true is inverted. + */ +void SD540::SetInverted(bool isInverted) { m_isInverted = isInverted; } + +/** + * Common interface for the inverting direction of a speed controller. + * + * @return isInverted The state of inversion, true is inverted. + * + */ +bool SD540::GetInverted() const { return m_isInverted; } + +/** + * Common interface for disabling a motor. + */ +void SD540::Disable() { SetRaw(kPwmDisabled); } + +/** + * Write out the PID value as seen in the PIDOutput base object. + * + * @param output Write out the PWM value as was found in the PIDController + */ +void SD540::PIDWrite(float output) { Set(output); } diff --git a/wpilibc/Athena/src/Spark.cpp b/wpilibc/Athena/src/Spark.cpp new file mode 100644 index 0000000000..ce08618ca7 --- /dev/null +++ b/wpilibc/Athena/src/Spark.cpp @@ -0,0 +1,88 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008. All Rights Reserved. + */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ +/*----------------------------------------------------------------------------*/ + +#include "Spark.h" + +#include "LiveWindow/LiveWindow.h" + +/** + * Note that the Spark 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 User Manual available + * from REV Robotics. + * + * 2.003ms = full "forward" + * 1.55ms = the "high end" of the deadband range + * 1.50ms = center of the deadband range (off) + * 1.46ms = the "low end" of the deadband range + * 0.999ms = full "reverse" + */ + +/** + * Constructor for a Spark + * @param channel The PWM channel that the Spark is attached to. 0-9 are + * on-board, 10-19 are on the MXP port + */ +Spark::Spark(uint32_t channel) : SafePWM(channel) { + SetBounds(2.003, 1.55, 1.50, 1.46, .999); + SetPeriodMultiplier(kPeriodMultiplier_1X); + SetRaw(m_centerPwm); + SetZeroLatch(); + + HALReport(HALUsageReporting::kResourceType_RevSPARK, GetChannel()); + LiveWindow::GetInstance()->AddActuator("Spark", GetChannel(), this); +} + +/** + * Set the PWM value. + * + * The PWM value is set using a range of -1.0 to 1.0, appropriately + * scaling the value for the FPGA. + * + * @param speed The speed value between -1.0 and 1.0 to set. + * @param syncGroup Unused interface. + */ +void Spark::Set(float speed, uint8_t syncGroup) { + SetSpeed(m_isInverted ? -speed : speed); +} + +/** + * Get the recently set value of the PWM. + * + * @return The most recently set value for the PWM between -1.0 and 1.0. + */ +float Spark::Get() const { return GetSpeed(); } + +/** + * Common interface for inverting direction of a speed controller. + * @param isInverted The state of inversion, true is inverted. + */ +void Spark::SetInverted(bool isInverted) { m_isInverted = isInverted; } + +/** + * Common interface for the inverting direction of a speed controller. + * + * @return isInverted The state of inversion, true is inverted. + * + */ +bool Spark::GetInverted() const { return m_isInverted; } + +/** + * Common interface for disabling a motor. + */ +void Spark::Disable() { SetRaw(kPwmDisabled); } + +/** + * Write out the PID value as seen in the PIDOutput base object. + * + * @param output Write out the PWM value as was found in the PIDController + */ +void Spark::PIDWrite(float output) { Set(output); } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SD540.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SD540.java new file mode 100644 index 0000000000..1b9e78e52b --- /dev/null +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SD540.java @@ -0,0 +1,123 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj; + +import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType; +import edu.wpi.first.wpilibj.communication.UsageReporting; +import edu.wpi.first.wpilibj.livewindow.LiveWindow; + +/** + * Mindsensors SD540 Speed Controller + */ +public class SD540 extends SafePWM implements SpeedController { + private boolean isInverted = false; + + /** + * Common initialization code called by all constructors. + * + * Note that the SD540 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 SD540 User + * Manual available from Mindsensors. + * + * - 2.05ms = full "forward" - 1.55ms = the "high end" of the deadband range + * - 1.50ms = center of the deadband range (off) - 1.44ms = the "low end" of + * the deadband range - .94ms = full "reverse" + */ + protected void initSD540() { + setBounds(2.05, 1.55, 1.50, 1.44, .94); + setPeriodMultiplier(PeriodMultiplier.k1X); + setRaw(m_centerPwm); + setZeroLatch(); + + LiveWindow.addActuator("SD540", getChannel(), this); + UsageReporting.report(tResourceType.kResourceType_MindsensorsSD540, getChannel()); + } + + /** + * Constructor. + * + * @param channel The PWM channel that the SD540 is attached to. 0-9 are + * on-board, 10-19 are on the MXP port + */ + public SD540(final int channel) { + super(channel); + initSD540(); + } + + /** + * Set the PWM value. + * + * @deprecated For compatibility with CANJaguar + * + * The PWM value is set using a range of -1.0 to 1.0, + * appropriately scaling the value for the FPGA. + * + * @param speed The speed to set. Value should be between -1.0 and 1.0. + * @param syncGroup The update group to add this Set() to, pending + * UpdateSyncGroup(). If 0, update immediately. + */ + public void set(double speed, byte syncGroup) { + setSpeed(isInverted ? -speed : speed); + Feed(); + } + + /** + * Set the PWM value. + * + * The PWM value is set using a range of -1.0 to 1.0, appropriately scaling + * the value for the FPGA. + * + * @param speed The speed value between -1.0 and 1.0 to set. + */ + public void set(double speed) { + setSpeed(isInverted ? -speed : speed); + Feed(); + } + + /** + * Common interface for inverting direction of a speed controller + * + * @param isInverted The state of inversion true is inverted + */ + @Override + public void setInverted(boolean isInverted) { + this.isInverted = isInverted; + } + + /** + * Common interface for the inverting direction of a speed controller. + * + * @return isInverted The state of inversion, true is inverted. + * + */ + @Override + public boolean getInverted() { + return this.isInverted; + } + + /** + * Get the recently set value of the PWM. + * + * @return The most recently set value for the PWM between -1.0 and 1.0. + */ + public double get() { + return getSpeed(); + } + + /** + * Write out the PID value as seen in the PIDOutput base object. + * + * @param output Write out the PWM value as was found in the PIDController + */ + public void pidWrite(double output) { + set(output); + } +} diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Spark.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Spark.java new file mode 100644 index 0000000000..552e418718 --- /dev/null +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Spark.java @@ -0,0 +1,123 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj; + +import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType; +import edu.wpi.first.wpilibj.communication.UsageReporting; +import edu.wpi.first.wpilibj.livewindow.LiveWindow; + +/** + * REV Robotics SPARK Speed Controller + */ +public class Spark extends SafePWM implements SpeedController { + private boolean isInverted = false; + + /** + * Common initialization code called by all constructors. + * + * Note that the SPARK 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 User + * Manual available from REV Robotics. + * + * - 2.003ms = full "forward" - 1.55ms = the "high end" of the deadband range + * - 1.50ms = center of the deadband range (off) - 1.46ms = the "low end" of + * the deadband range - .999ms = full "reverse" + */ + protected void initSpark() { + setBounds(2.003, 1.55, 1.50, 1.46, .999); + setPeriodMultiplier(PeriodMultiplier.k1X); + setRaw(m_centerPwm); + setZeroLatch(); + + LiveWindow.addActuator("Spark", getChannel(), this); + UsageReporting.report(tResourceType.kResourceType_RevSPARK, getChannel()); + } + + /** + * Constructor. + * + * @param channel The PWM channel that the SPARK is attached to. 0-9 are + * on-board, 10-19 are on the MXP port + */ + public Spark(final int channel) { + super(channel); + initSpark(); + } + + /** + * Set the PWM value. + * + * @deprecated For compatibility with CANJaguar + * + * The PWM value is set using a range of -1.0 to 1.0, + * appropriately scaling the value for the FPGA. + * + * @param speed The speed to set. Value should be between -1.0 and 1.0. + * @param syncGroup The update group to add this Set() to, pending + * UpdateSyncGroup(). If 0, update immediately. + */ + public void set(double speed, byte syncGroup) { + setSpeed(isInverted ? -speed : speed); + Feed(); + } + + /** + * Set the PWM value. + * + * The PWM value is set using a range of -1.0 to 1.0, appropriately scaling + * the value for the FPGA. + * + * @param speed The speed value between -1.0 and 1.0 to set. + */ + public void set(double speed) { + setSpeed(isInverted ? -speed : speed); + Feed(); + } + + /** + * Common interface for inverting direction of a speed controller + * + * @param isInverted The state of inversion true is inverted + */ + @Override + public void setInverted(boolean isInverted) { + this.isInverted = isInverted; + } + + /** + * Common interface for the inverting direction of a speed controller. + * + * @return isInverted The state of inversion, true is inverted. + * + */ + @Override + public boolean getInverted() { + return this.isInverted; + } + + /** + * Get the recently set value of the PWM. + * + * @return The most recently set value for the PWM between -1.0 and 1.0. + */ + public double get() { + return getSpeed(); + } + + /** + * Write out the PID value as seen in the PIDOutput base object. + * + * @param output Write out the PWM value as was found in the PIDController + */ + public void pidWrite(double output) { + set(output); + } +}