diff --git a/wpilibc/wpilibC++Devices/include/CANTalon.h b/wpilibc/wpilibC++Devices/include/CANTalon.h index c720ea0cc7..a9c710828a 100644 --- a/wpilibc/wpilibC++Devices/include/CANTalon.h +++ b/wpilibc/wpilibC++Devices/include/CANTalon.h @@ -9,6 +9,8 @@ #include "CANSpeedController.h" #include "PIDOutput.h" #include "MotorSafetyHelper.h" +#include "LiveWindow/LiveWindowSendable.h" +#include "tables/ITable.h" class CanTalonSRX; @@ -17,7 +19,9 @@ class CanTalonSRX; */ class CANTalon : public MotorSafety, public CANSpeedController, - public ErrorBase + public ErrorBase, + public LiveWindowSendable, + public ITableListener { public: enum FeedbackDevice { @@ -138,6 +142,15 @@ public: bool IsControlEnabled(); double GetSetpoint(); + + // LiveWindow stuff. + void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew); + void UpdateTable(); + void StartLiveWindowMode(); + void StopLiveWindowMode(); + std::string GetSmartDashboardType(); + void InitTable(ITable *subTable); + ITable * GetTable(); private: // Values for various modes as is sent in the CAN packets for the Talon. enum TalonControlMode { @@ -168,4 +181,7 @@ private: * @see Set() */ void ApplyControlMode(CANSpeedController::ControlMode mode); + + // LiveWindow stuff. + ITable *m_table; }; diff --git a/wpilibc/wpilibC++Devices/src/CANTalon.cpp b/wpilibc/wpilibC++Devices/src/CANTalon.cpp index 5d075276ab..423a2927e6 100644 --- a/wpilibc/wpilibC++Devices/src/CANTalon.cpp +++ b/wpilibc/wpilibC++Devices/src/CANTalon.cpp @@ -1262,3 +1262,48 @@ void CANTalon::StopMotor() { Disable(); } + +void CANTalon::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) +{ + Set(value.f); +} + +void CANTalon::UpdateTable() +{ + if (m_table != NULL) + { + m_table->PutNumber("Value", Get()); + } +} + +void CANTalon::StartLiveWindowMode() +{ + if (m_table != NULL) + { + m_table->AddTableListener("Value", this, true); + } +} + +void CANTalon::StopLiveWindowMode() +{ + if (m_table != NULL) + { + m_table->RemoveTableListener(this); + } +} + +std::string CANTalon::GetSmartDashboardType() +{ + return "Speed Controller"; +} + +void CANTalon::InitTable(ITable *subTable) +{ + m_table = subTable; + UpdateTable(); +} + +ITable * CANTalon::GetTable() +{ + return m_table; +} diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java index 24e0e35db3..c987885368 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/CANTalon.java @@ -16,8 +16,11 @@ import edu.wpi.first.wpilibj.hal.SWIGTYPE_p_double; import edu.wpi.first.wpilibj.hal.SWIGTYPE_p_int; import edu.wpi.first.wpilibj.hal.SWIGTYPE_p_uint32_t; import edu.wpi.first.wpilibj.hal.SWIGTYPE_p_CTR_Code; +import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable; +import edu.wpi.first.wpilibj.tables.ITable; +import edu.wpi.first.wpilibj.tables.ITableListener; -public class CANTalon implements MotorSafety, PIDOutput, SpeedController { +public class CANTalon implements MotorSafety, PIDOutput, SpeedController, LiveWindowSendable { private MotorSafetyHelper m_safetyHelper; public enum ControlMode { PercentVbus(0), Follower(5), Voltage(4), Position(1), Speed(2), Current(3), Disabled(15); @@ -451,14 +454,14 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController { * Also fills the modeSelecet in the control frame to disabled. * @param controlMode Control mode to ultimately enter once user calls set(). * @see #set - */ + */ private void applyControlMode(ControlMode controlMode) { m_controlMode = controlMode; if (controlMode == ControlMode.Disabled) m_controlEnabled = false; // Disable until set() is called. m_impl.SetModeSelect(ControlMode.Disabled.value); - + UsageReporting.report(tResourceType.kResourceType_CANTalonSRX, m_deviceNumber + 1, controlMode.value); } public void changeControlMode(ControlMode controlMode) { @@ -995,6 +998,69 @@ public class CANTalon implements MotorSafety, PIDOutput, SpeedController { @Override public String getDescription() { - return "CANJaguar ID "+m_deviceNumber; + return "CANTalon ID "+m_deviceNumber; } + + /* + * Live Window code, only does anything if live window is activated. + */ + @Override + public String getSmartDashboardType() { + return "Speed Controller"; + } + private ITable m_table = null; + private ITableListener m_table_listener = null; + + /** + * {@inheritDoc} + */ + @Override + public void initTable(ITable subtable) { + m_table = subtable; + updateTable(); + } + + /** + * {@inheritDoc} + */ + @Override + public void updateTable() { + if (m_table != null) { + m_table.putNumber("Value", get()); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ITable getTable() { + return m_table; + } + + /** + * {@inheritDoc} + */ + @Override + public void startLiveWindowMode() { + set(0); // Stop for safety + m_table_listener = new ITableListener() { + @Override + public void valueChanged(ITable itable, String key, Object value, boolean bln) { + set(((Double) value).doubleValue()); + } + }; + m_table.addTableListener("Value", m_table_listener, true); + } + + /** + * {@inheritDoc} + */ + @Override + public void stopLiveWindowMode() { + set(0); // Stop for safety + // TODO: Broken, should only remove the listener from "Value" only. + m_table.removeTableListener(m_table_listener); + } + }