2014-11-17 16:02:41 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2014. 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 "CANSpeedController.h"
|
|
|
|
|
#include "PIDOutput.h"
|
2015-06-15 13:48:53 -04:00
|
|
|
#include "PIDSource.h"
|
|
|
|
|
#include "PIDInterface.h"
|
2015-07-28 13:55:53 -04:00
|
|
|
#include "HAL/CanTalonSRX.h"
|
2014-11-17 16:02:41 -05:00
|
|
|
#include "MotorSafetyHelper.h"
|
2015-06-17 14:38:16 -04:00
|
|
|
#include "LiveWindow/LiveWindowSendable.h"
|
|
|
|
|
#include "tables/ITable.h"
|
2014-11-17 16:02:41 -05:00
|
|
|
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
#include <memory>
|
|
|
|
|
|
2014-11-17 16:02:41 -05:00
|
|
|
/**
|
2014-12-05 09:19:27 -08:00
|
|
|
* CTRE Talon SRX Speed Controller with CAN Control
|
2014-11-17 16:02:41 -05:00
|
|
|
*/
|
|
|
|
|
class CANTalon : public MotorSafety,
|
|
|
|
|
public CANSpeedController,
|
2015-06-17 14:38:16 -04:00
|
|
|
public ErrorBase,
|
|
|
|
|
public LiveWindowSendable,
|
2015-06-15 13:48:53 -04:00
|
|
|
public ITableListener,
|
|
|
|
|
public PIDSource,
|
2015-06-25 15:07:55 -04:00
|
|
|
public PIDInterface {
|
|
|
|
|
public:
|
2014-12-01 14:05:26 -05:00
|
|
|
enum FeedbackDevice {
|
2015-06-25 15:07:55 -04:00
|
|
|
QuadEncoder = 0,
|
|
|
|
|
AnalogPot = 2,
|
|
|
|
|
AnalogEncoder = 3,
|
|
|
|
|
EncRising = 4,
|
|
|
|
|
EncFalling = 5
|
2014-12-15 02:21:54 -05:00
|
|
|
};
|
|
|
|
|
enum StatusFrameRate {
|
2015-06-25 15:07:55 -04:00
|
|
|
StatusFrameRateGeneral = 0,
|
|
|
|
|
StatusFrameRateFeedback = 1,
|
|
|
|
|
StatusFrameRateQuadEncoder = 2,
|
|
|
|
|
StatusFrameRateAnalogTempVbat = 3,
|
2014-12-01 14:05:26 -05:00
|
|
|
};
|
2015-06-25 15:07:55 -04:00
|
|
|
explicit CANTalon(int deviceNumber);
|
|
|
|
|
explicit CANTalon(int deviceNumber, int controlPeriodMs);
|
2015-07-24 09:11:15 -04:00
|
|
|
DEFAULT_MOVE_CONSTRUCTOR(CANTalon);
|
|
|
|
|
virtual ~CANTalon();
|
2014-11-17 16:02:41 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// PIDOutput interface
|
|
|
|
|
virtual void PIDWrite(float output) override;
|
2014-11-17 16:02:41 -05:00
|
|
|
|
2015-06-15 13:48:53 -04:00
|
|
|
// PIDSource interface
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
virtual double PIDGet() override;
|
2015-06-15 13:48:53 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// MotorSafety interface
|
|
|
|
|
virtual void SetExpiration(float timeout) override;
|
|
|
|
|
virtual float GetExpiration() const override;
|
|
|
|
|
virtual bool IsAlive() const override;
|
|
|
|
|
virtual void StopMotor() override;
|
|
|
|
|
virtual void SetSafetyEnabled(bool enabled) override;
|
|
|
|
|
virtual bool IsSafetyEnabled() const override;
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
virtual void GetDescription(std::ostringstream& desc) const override;
|
2014-11-17 16:02:41 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// CANSpeedController interface
|
|
|
|
|
virtual float Get() const override;
|
|
|
|
|
virtual void Set(float value, uint8_t syncGroup = 0) override;
|
2015-06-15 13:48:53 -04:00
|
|
|
virtual void Reset() override;
|
|
|
|
|
virtual void SetSetpoint(float value) override;
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual void Disable() override;
|
2014-11-17 16:02:41 -05:00
|
|
|
virtual void EnableControl();
|
2015-06-23 04:49:51 -07:00
|
|
|
virtual void Enable() override;
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual void SetP(double p) override;
|
|
|
|
|
virtual void SetI(double i) override;
|
|
|
|
|
virtual void SetD(double d) override;
|
|
|
|
|
void SetF(double f);
|
|
|
|
|
void SetIzone(unsigned iz);
|
|
|
|
|
virtual void SetPID(double p, double i, double d) override;
|
|
|
|
|
virtual void SetPID(double p, double i, double d, double f);
|
|
|
|
|
virtual double GetP() const override;
|
|
|
|
|
virtual double GetI() const override;
|
|
|
|
|
virtual double GetD() const override;
|
|
|
|
|
virtual double GetF() const;
|
|
|
|
|
virtual float GetBusVoltage() const override;
|
|
|
|
|
virtual float GetOutputVoltage() const override;
|
|
|
|
|
virtual float GetOutputCurrent() const override;
|
|
|
|
|
virtual float GetTemperature() const override;
|
|
|
|
|
void SetPosition(double pos);
|
|
|
|
|
virtual double GetPosition() const override;
|
|
|
|
|
virtual double GetSpeed() const override;
|
2015-06-19 17:23:54 -07:00
|
|
|
virtual int GetClosedLoopError() const;
|
|
|
|
|
virtual int GetAnalogIn() const;
|
|
|
|
|
virtual int GetAnalogInRaw() const;
|
|
|
|
|
virtual int GetAnalogInVel() const;
|
|
|
|
|
virtual int GetEncPosition() const;
|
|
|
|
|
virtual int GetEncVel() const;
|
2015-06-25 15:07:55 -04:00
|
|
|
int GetPinStateQuadA() const;
|
|
|
|
|
int GetPinStateQuadB() const;
|
|
|
|
|
int GetPinStateQuadIdx() const;
|
|
|
|
|
int IsFwdLimitSwitchClosed() const;
|
|
|
|
|
int IsRevLimitSwitchClosed() const;
|
|
|
|
|
int GetNumberOfQuadIdxRises() const;
|
|
|
|
|
void SetNumberOfQuadIdxRises(int rises);
|
|
|
|
|
virtual bool GetForwardLimitOK() const override;
|
|
|
|
|
virtual bool GetReverseLimitOK() const override;
|
|
|
|
|
virtual uint16_t GetFaults() const override;
|
|
|
|
|
uint16_t GetStickyFaults() const;
|
|
|
|
|
void ClearStickyFaults();
|
|
|
|
|
virtual void SetVoltageRampRate(double rampRate) override;
|
|
|
|
|
virtual uint32_t GetFirmwareVersion() const override;
|
|
|
|
|
virtual void ConfigNeutralMode(NeutralMode mode) override;
|
|
|
|
|
virtual void ConfigEncoderCodesPerRev(uint16_t codesPerRev) override;
|
|
|
|
|
virtual void ConfigPotentiometerTurns(uint16_t turns) override;
|
|
|
|
|
virtual void ConfigSoftPositionLimits(double forwardLimitPosition,
|
|
|
|
|
double reverseLimitPosition) override;
|
|
|
|
|
virtual void DisableSoftPositionLimits() override;
|
|
|
|
|
virtual void ConfigLimitMode(LimitMode mode) override;
|
|
|
|
|
virtual void ConfigForwardLimit(double forwardLimitPosition) override;
|
|
|
|
|
virtual void ConfigReverseLimit(double reverseLimitPosition) override;
|
|
|
|
|
/**
|
|
|
|
|
* Change the fwd limit switch setting to normally open or closed.
|
|
|
|
|
* Talon will disable momentarilly if the Talon's current setting
|
|
|
|
|
* is dissimilar to the caller's requested setting.
|
|
|
|
|
*
|
|
|
|
|
* Since Talon saves setting to flash this should only affect
|
|
|
|
|
* a given Talon initially during robot install.
|
|
|
|
|
*
|
|
|
|
|
* @param normallyOpen true for normally open. false for normally closed.
|
|
|
|
|
*/
|
|
|
|
|
void ConfigFwdLimitSwitchNormallyOpen(bool normallyOpen);
|
|
|
|
|
/**
|
|
|
|
|
* Change the rev limit switch setting to normally open or closed.
|
|
|
|
|
* Talon will disable momentarilly if the Talon's current setting
|
|
|
|
|
* is dissimilar to the caller's requested setting.
|
|
|
|
|
*
|
|
|
|
|
* Since Talon saves setting to flash this should only affect
|
|
|
|
|
* a given Talon initially during robot install.
|
|
|
|
|
*
|
|
|
|
|
* @param normallyOpen true for normally open. false for normally closed.
|
|
|
|
|
*/
|
|
|
|
|
void ConfigRevLimitSwitchNormallyOpen(bool normallyOpen);
|
|
|
|
|
virtual void ConfigMaxOutputVoltage(double voltage) override;
|
|
|
|
|
virtual void ConfigFaultTime(float faultTime) override;
|
|
|
|
|
virtual void SetControlMode(ControlMode mode);
|
2014-12-01 14:05:26 -05:00
|
|
|
void SetFeedbackDevice(FeedbackDevice device);
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetStatusFrameRateMs(StatusFrameRate stateFrame, int periodMs);
|
|
|
|
|
virtual ControlMode GetControlMode() const;
|
|
|
|
|
void SetSensorDirection(bool reverseSensor);
|
|
|
|
|
void SetCloseLoopRampRate(double rampRate);
|
|
|
|
|
void SelectProfileSlot(int slotIdx);
|
|
|
|
|
int GetIzone() const;
|
|
|
|
|
int GetIaccum() const;
|
|
|
|
|
void ClearIaccum();
|
|
|
|
|
int GetBrakeEnableDuringNeutral() const;
|
2014-12-08 15:22:15 -05:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
bool IsControlEnabled() const;
|
2015-06-15 13:48:53 -04:00
|
|
|
bool IsEnabled() const override;
|
|
|
|
|
double GetSetpoint() const override;
|
2015-06-17 14:38:16 -04:00
|
|
|
|
|
|
|
|
// LiveWindow stuff.
|
2015-08-13 23:17:19 -07:00
|
|
|
void ValueChanged(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value, bool isNew) override;
|
2015-06-25 15:07:55 -04:00
|
|
|
void UpdateTable() override;
|
|
|
|
|
void StartLiveWindowMode() override;
|
|
|
|
|
void StopLiveWindowMode() override;
|
|
|
|
|
std::string GetSmartDashboardType() const override;
|
2015-07-29 16:48:04 -04:00
|
|
|
void InitTable(std::shared_ptr<ITable> subTable) override;
|
|
|
|
|
std::shared_ptr<ITable> GetTable() const override;
|
2015-06-15 13:48:53 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// SpeedController overrides
|
2015-03-24 15:01:17 -04:00
|
|
|
virtual void SetInverted(bool isInverted) override;
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual bool GetInverted() const override;
|
|
|
|
|
|
|
|
|
|
private:
|
2014-11-17 16:02:41 -05:00
|
|
|
// Values for various modes as is sent in the CAN packets for the Talon.
|
|
|
|
|
enum TalonControlMode {
|
2015-06-25 15:07:55 -04:00
|
|
|
kThrottle = 0,
|
|
|
|
|
kFollowerMode = 5,
|
|
|
|
|
kVoltageMode = 4,
|
|
|
|
|
kPositionMode = 1,
|
|
|
|
|
kSpeedMode = 2,
|
|
|
|
|
kCurrentMode = 3,
|
|
|
|
|
kDisabled = 15
|
2014-11-17 16:02:41 -05:00
|
|
|
};
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int m_deviceNumber;
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
std::unique_ptr<CanTalonSRX> m_impl;
|
|
|
|
|
std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
|
2015-06-24 01:06:29 -07:00
|
|
|
int m_profile = 0; // Profile from CANTalon to use. Set to zero until we can
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
// actually test this.
|
2014-11-17 16:02:41 -05:00
|
|
|
|
2015-06-24 01:06:29 -07:00
|
|
|
bool m_controlEnabled = true;
|
|
|
|
|
ControlMode m_controlMode = kPercentVbus;
|
2014-12-06 16:06:15 -05:00
|
|
|
TalonControlMode m_sendMode;
|
2014-12-08 15:22:15 -05:00
|
|
|
|
2015-06-24 01:06:29 -07:00
|
|
|
double m_setPoint = 0;
|
2014-12-14 17:39:35 -05:00
|
|
|
static const unsigned int kDelayForSolicitedSignalsUs = 4000;
|
2014-12-16 21:37:20 -05:00
|
|
|
/**
|
|
|
|
|
* Fixup the sendMode so Set() serializes the correct demand value.
|
|
|
|
|
* Also fills the modeSelecet in the control frame to disabled.
|
|
|
|
|
* @param mode Control mode to ultimately enter once user calls Set().
|
|
|
|
|
* @see Set()
|
|
|
|
|
*/
|
|
|
|
|
void ApplyControlMode(CANSpeedController::ControlMode mode);
|
2015-06-17 14:38:16 -04:00
|
|
|
|
|
|
|
|
// LiveWindow stuff.
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<ITable> m_table;
|
2015-03-24 15:01:17 -04:00
|
|
|
bool m_isInverted;
|
2015-07-24 09:11:15 -04:00
|
|
|
|
|
|
|
|
HasBeenMoved m_hasBeenMoved;
|
2014-11-17 16:02:41 -05:00
|
|
|
};
|