Switches PWMs to do scaling at the HAL level. (#143)

This commit is contained in:
Thad House
2016-07-08 21:29:29 -07:00
committed by Peter Johnson
parent be2647d44e
commit 5ad28d58ec
30 changed files with 737 additions and 364 deletions

View File

@@ -37,7 +37,7 @@ public class Jaguar extends PWMSpeedController {
*/
setBounds(2.31, 1.55, 1.507, 1.454, .697);
setPeriodMultiplier(PeriodMultiplier.k1X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
UsageReporting.report(tResourceType.kResourceType_Jaguar, getChannel());

View File

@@ -63,40 +63,6 @@ public class PWM extends SensorBase implements LiveWindowSendable {
private int m_channel;
private int m_handle;
/**
* kDefaultPwmPeriod is in ms.
*
* <p>- 20ms periods (50 Hz) are the "safest" setting in that this works for all devices - 20ms
* periods seem to be desirable for Vex Motors - 20ms periods are the specified period for
* HS-322HD servos, but work reliably down to 10.0 ms; starting at about 8.5ms, the servo
* sometimes hums and get hot; by 5.0ms the hum is nearly continuous - 10ms periods work well for
* Victor 884 - 5ms periods allows higher update rates for Luminary Micro Jaguar speed
* controllers. Due to the shipping firmware on the Jaguar, we can't run the update period less
* than 5.05 ms.
*
* <p>kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period scaling is implemented
* as an output squelch to get longer periods for old devices.
*/
protected static final double kDefaultPwmPeriod = 5.05;
/**
* kDefaultPwmCenter is the PWM range center in ms.
*/
protected static final double kDefaultPwmCenter = 1.5;
/**
* kDefaultPWMStepsDown is the number of PWM steps below the centerpoint.
*/
protected static final int kDefaultPwmStepsDown = 1000;
public static final int kPwmDisabled = 0;
private boolean m_eliminateDeadband;
private int m_maxPwm;
private int m_deadbandMaxPwm;
/*
* Intentionally package private
*/
int m_centerPwm;
private int m_deadbandMinPwm;
private int m_minPwm;
/**
* Allocate a PWM given a channel.
*
@@ -108,9 +74,9 @@ public class PWM extends SensorBase implements LiveWindowSendable {
m_handle = PWMJNI.initializePWMPort(DIOJNI.getPort((byte) channel));
PWMJNI.setPWM(m_handle, (short) 0);
setDisabled();
m_eliminateDeadband = false;
PWMJNI.setPWMEliminateDeadband(m_handle, false);
UsageReporting.report(tResourceType.kResourceType_PWM, channel);
}
@@ -124,7 +90,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
if (m_handle == 0) {
return;
}
PWMJNI.setPWM(m_handle, (short) 0);
setDisabled();
PWMJNI.freePWMPort(m_handle);
m_handle = 0;
}
@@ -137,7 +103,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* modifying any values.
*/
public void enableDeadbandElimination(boolean eliminateDeadband) {
m_eliminateDeadband = eliminateDeadband;
PWMJNI.setPWMEliminateDeadband(m_handle, eliminateDeadband);
}
/**
@@ -154,13 +120,9 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* double, double)}
*/
@Deprecated
public void setBounds(final int max, final int deadbandMax, final int center,
public void setRawBounds(final int max, final int deadbandMax, final int center,
final int deadbandMin, final int min) {
m_maxPwm = max;
m_deadbandMaxPwm = deadbandMax;
m_centerPwm = center;
m_deadbandMinPwm = deadbandMin;
m_minPwm = min;
PWMJNI.setPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMax, min);
}
/**
@@ -174,18 +136,18 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @param deadbandMin The low end of the deadband pulse width in ms
* @param min The minimum pulse width in ms
*/
protected void setBounds(double max, double deadbandMax, double center, double deadbandMin,
public void setBounds(double max, double deadbandMax, double center, double deadbandMin,
double min) {
double loopTime =
DIOJNI.getLoopTiming() / (kSystemClockTicksPerMicrosecond * 1e3);
m_maxPwm = (int) ((max - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
m_deadbandMaxPwm =
(int) ((deadbandMax - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
m_centerPwm = (int) ((center - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
m_deadbandMinPwm =
(int) ((deadbandMin - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
m_minPwm = (int) ((min - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
PWMJNI.setPWMConfig(m_handle, max, deadbandMax, center, deadbandMax, min);
}
/**
* Gets the bounds on the PWM pulse widths. This Gets the bounds on the PWM values for a
* particular type of controller. The values determine the upper and lower speeds as well
* as the deadband bracket.
*/
public PWMConfigDataResult getRawBounds() {
return PWMJNI.getPWMConfigRaw(m_handle);
}
/**
@@ -207,19 +169,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @pre SetMinNegativePwm() called.
*/
public void setPosition(double pos) {
if (pos < 0.0) {
pos = 0.0;
} else if (pos > 1.0) {
pos = 1.0;
}
int rawValue;
// note, need to perform the multiplication below as floating point before
// converting to int
rawValue = (int) ((pos * (double) getFullRangeScaleFactor()) + getMinNegativePwm());
// send the computed pwm value to the FPGA
setRaw(rawValue);
PWMJNI.setPWMPosition(m_handle, (float)pos);
}
/**
@@ -232,14 +182,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @pre SetMinNegativePwm() called.
*/
public double getPosition() {
int value = getRaw();
if (value < getMinNegativePwm()) {
return 0.0;
} else if (value > getMaxPositivePwm()) {
return 1.0;
} else {
return (double) (value - getMinNegativePwm()) / (double) getFullRangeScaleFactor();
}
return PWMJNI.getPWMPosition(m_handle);
}
/**
@@ -254,30 +197,8 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @pre SetMaxNegativePwm() called.
* @pre SetMinNegativePwm() called.
*/
final void setSpeed(double speed) {
// clamp speed to be in the range 1.0 >= speed >= -1.0
if (speed < -1.0) {
speed = -1.0;
} else if (speed > 1.0) {
speed = 1.0;
}
// calculate the desired output pwm value by scaling the speed appropriately
int rawValue;
if (speed == 0.0) {
rawValue = getCenterPwm();
} else if (speed > 0.0) {
rawValue =
(int) (speed * ((double) getPositiveScaleFactor())
+ ((double) getMinPositivePwm()) + 0.5);
} else {
rawValue =
(int) (speed * ((double) getNegativeScaleFactor())
+ ((double) getMaxNegativePwm()) + 0.5);
}
// send the computed pwm value to the FPGA
setRaw(rawValue);
public void setSpeed(double speed) {
PWMJNI.setPWMSpeed(m_handle, (float)speed);
}
/**
@@ -292,18 +213,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @pre SetMinNegativePwm() called.
*/
public double getSpeed() {
int value = getRaw();
if (value > getMaxPositivePwm()) {
return 1.0;
} else if (value < getMinNegativePwm()) {
return -1.0;
} else if (value > getMinPositivePwm()) {
return (double) (value - getMinPositivePwm()) / (double) getPositiveScaleFactor();
} else if (value < getMaxNegativePwm()) {
return (double) (value - getMaxNegativePwm()) / (double) getNegativeScaleFactor();
} else {
return 0.0;
}
return PWMJNI.getPWMSpeed(m_handle);
}
/**
@@ -314,7 +224,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @param value Raw PWM value. Range 0 - 255.
*/
public void setRaw(int value) {
PWMJNI.setPWM(m_handle, (short) value);
PWMJNI.setPWMRaw(m_handle, (short) value);
}
/**
@@ -325,7 +235,15 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @return Raw PWM control value. Range: 0 - 255.
*/
public int getRaw() {
return PWMJNI.getPWM(m_handle);
return PWMJNI.getPWMRaw(m_handle);
}
/**
* Temporarily disables the PWM output. The next set call will reenable
* the output.
*/
public void setDisabled() {
PWMJNI.setPWMDisabled(m_handle);
}
/**
@@ -356,38 +274,6 @@ public class PWM extends SensorBase implements LiveWindowSendable {
PWMJNI.latchPWMZero(m_handle);
}
private int getMaxPositivePwm() {
return m_maxPwm;
}
private int getMinPositivePwm() {
return m_eliminateDeadband ? m_deadbandMaxPwm : m_centerPwm + 1;
}
private int getCenterPwm() {
return m_centerPwm;
}
private int getMaxNegativePwm() {
return m_eliminateDeadband ? m_deadbandMinPwm : m_centerPwm - 1;
}
private int getMinNegativePwm() {
return m_minPwm;
}
private int getPositiveScaleFactor() {
return getMaxPositivePwm() - getMinPositivePwm();
} // /< The scale for positive speeds.
private int getNegativeScaleFactor() {
return getMaxNegativePwm() - getMinNegativePwm();
} // /< The scale for negative speeds.
private int getFullRangeScaleFactor() {
return getMaxPositivePwm() - getMinNegativePwm();
} // /< The scale for positions.
/*
* Live Window code, only does anything if live window is activated.
*/

View File

@@ -0,0 +1,52 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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;
/**
* Structure for holding the config data result for PWM.
*/
public class PWMConfigDataResult {
PWMConfigDataResult(int max, int deadbandMax, int center, int deadbandMin, int min) {
this.max = max;
this.deadbandMax = deadbandMax;
this.center = center;
this.deadbandMin = deadbandMin;
this.min = min;
}
/**
* The maximum PWM value.
*/
@SuppressWarnings("MemberName")
public int max;
/**
* The deadband maximum PWM value.
*/
@SuppressWarnings("MemberName")
public int deadbandMax;
/**
* The center PWM value.
*/
@SuppressWarnings("MemberName")
public int center;
/**
* The deadband minimum PWM value.
*/
@SuppressWarnings("MemberName")
public int deadbandMin;
/**
* The minimum PWM value.
*/
@SuppressWarnings("MemberName")
public int min;
}

View File

@@ -32,7 +32,7 @@ public class SD540 extends PWMSpeedController {
protected void initSD540() {
setBounds(2.05, 1.55, 1.50, 1.44, .94);
setPeriodMultiplier(PeriodMultiplier.k1X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
LiveWindow.addActuator("SD540", getChannel(), this);

View File

@@ -90,6 +90,6 @@ public class SafePWM extends PWM implements MotorSafety {
}
public void disable() {
setRaw(kPwmDisabled);
setDisabled();
}
}

View File

@@ -32,7 +32,7 @@ public class Spark extends PWMSpeedController {
protected void initSpark() {
setBounds(2.003, 1.55, 1.50, 1.46, .999);
setPeriodMultiplier(PeriodMultiplier.k1X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
LiveWindow.addActuator("Spark", getChannel(), this);

View File

@@ -37,7 +37,7 @@ public class Talon extends PWMSpeedController {
setBounds(2.037, 1.539, 1.513, 1.487, .989);
setPeriodMultiplier(PeriodMultiplier.k1X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
LiveWindow.addActuator("Talon", getChannel(), this);

View File

@@ -40,7 +40,7 @@ public class TalonSRX extends PWMSpeedController {
setBounds(2.004, 1.52, 1.50, 1.48, .997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
LiveWindow.addActuator("TalonSRX", getChannel(), this);

View File

@@ -39,7 +39,7 @@ public class Victor extends PWMSpeedController {
setBounds(2.027, 1.525, 1.507, 1.49, 1.026);
setPeriodMultiplier(PeriodMultiplier.k2X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
LiveWindow.addActuator("Victor", getChannel(), this);

View File

@@ -37,7 +37,7 @@ public class VictorSP extends PWMSpeedController {
setBounds(2.004, 1.52, 1.50, 1.48, .997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setRaw(m_centerPwm);
setSpeed(0.0);
setZeroLatch();
LiveWindow.addActuator("VictorSP", getChannel(), this);

View File

@@ -7,15 +7,41 @@
package edu.wpi.first.wpilibj.hal;
import edu.wpi.first.wpilibj.PWMConfigDataResult;
@SuppressWarnings("AbbreviationAsWordInName")
public class PWMJNI extends DIOJNI {
public static native int initializePWMPort(int halPortHandle);
public static native void freePWMPort(int pwmPortHandle);
public static native void setPWMConfigRaw(int pwmPortHandle, int maxPwm,
int deadbandMaxPwm, int centerPwm,
int deadbandMinPwm, int minPwm);
public static native void setPWMConfig(int pwmPortHandle, double maxPwm,
double deadbandMaxPwm, double centerPwm,
double deadbandMinPwm, double minPwm);
public static native void setPWM(int pwmPortHandle, short value);
public static native PWMConfigDataResult getPWMConfigRaw(int pwmPortHandle);
public static native short getPWM(int pwmPortHandle);
public static native void setPWMEliminateDeadband(int pwmPortHandle, boolean eliminateDeadband);
public static native boolean getPWMEliminateDeadband(int pwmPortHandle);
public static native void setPWMRaw(int pwmPortHandle, short value);
public static native void setPWMSpeed(int pwmPortHandle, float speed);
public static native void setPWMPosition(int pwmPortHandle, float position);
public static native short getPWMRaw(int pwmPortHandle);
public static native float getPWMSpeed(int pwmPortHandle);
public static native float getPWMPosition(int pwmPortHandle);
public static native void setPWMDisabled(int pwmPortHandle);
public static native void latchPWMZero(int pwmPortHandle);