Files
allwpilib/wpilibc/wpilibC++Devices/include/CANJaguar.h
James Kuszmaul 534ea134a4 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-07-20 13:18:29 -04:00

250 lines
9.3 KiB
C++

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2009. 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 "ErrorBase.h"
#include "MotorSafety.h"
#include "Resource.h"
#include "MotorSafetyHelper.h"
#include "PIDOutput.h"
#include "CANSpeedController.h"
#include "HAL/cpp/Semaphore.hpp"
#include "HAL/HAL.hpp"
#include "LiveWindow/LiveWindowSendable.h"
#include "tables/ITable.h"
#include "NetworkCommunication/CANSessionMux.h"
#include "CAN/can_proto.h"
#include <atomic>
#include "HAL/cpp/priority_mutex.h"
#include <memory>
#include <utility>
#include <sstream>
/**
* Luminary Micro / Vex Robotics Jaguar Speed Control
*/
class CANJaguar : public MotorSafety,
public CANSpeedController,
public ErrorBase,
public LiveWindowSendable,
public ITableListener {
public:
// The internal PID control loop in the Jaguar runs at 1kHz.
static const int32_t kControllerRate = 1000;
static constexpr double kApproxBusVoltage = 12.0;
// Control mode tags
/** Sets an encoder as the speed reference only. <br> Passed as the "tag" when
* setting the control mode.*/
static const struct EncoderStruct {
} Encoder;
/** Sets a quadrature encoder as the position and speed reference. <br> Passed
* as the "tag" when setting the control mode.*/
static const struct QuadEncoderStruct {
} QuadEncoder;
/** Sets a potentiometer as the position reference only. <br> Passed as the
* "tag" when setting the control mode. */
static const struct PotentiometerStruct {
} Potentiometer;
explicit CANJaguar(uint8_t deviceNumber);
virtual ~CANJaguar();
uint8_t getDeviceNumber() const;
uint8_t GetHardwareVersion() const;
// PIDOutput interface
virtual void PIDWrite(float output) override;
// Control mode methods
void EnableControl(double encoderInitialPosition = 0.0);
void DisableControl();
void SetPercentMode();
void SetPercentMode(EncoderStruct, uint16_t codesPerRev);
void SetPercentMode(QuadEncoderStruct, uint16_t codesPerRev);
void SetPercentMode(PotentiometerStruct);
void SetCurrentMode(double p, double i, double d);
void SetCurrentMode(EncoderStruct, uint16_t codesPerRev, double p, double i,
double d);
void SetCurrentMode(QuadEncoderStruct, uint16_t codesPerRev, double p,
double i, double d);
void SetCurrentMode(PotentiometerStruct, double p, double i, double d);
void SetSpeedMode(EncoderStruct, uint16_t codesPerRev, double p, double i,
double d);
void SetSpeedMode(QuadEncoderStruct, uint16_t codesPerRev, double p, double i,
double d);
void SetPositionMode(QuadEncoderStruct, uint16_t codesPerRev, double p,
double i, double d);
void SetPositionMode(PotentiometerStruct, double p, double i, double d);
void SetVoltageMode();
void SetVoltageMode(EncoderStruct, uint16_t codesPerRev);
void SetVoltageMode(QuadEncoderStruct, uint16_t codesPerRev);
void SetVoltageMode(PotentiometerStruct);
// CANSpeedController interface
virtual float Get() const override;
virtual void Set(float value, uint8_t syncGroup = 0) override;
virtual void Disable() override;
virtual void SetP(double p) override;
virtual void SetI(double i) override;
virtual void SetD(double d) override;
virtual void SetPID(double p, double i, double d) override;
virtual double GetP() const override;
virtual double GetI() const override;
virtual double GetD() const override;
virtual float GetBusVoltage() const override;
virtual float GetOutputVoltage() const override;
virtual float GetOutputCurrent() const override;
virtual float GetTemperature() const override;
virtual double GetPosition() const override;
virtual double GetSpeed() const override;
virtual bool GetForwardLimitOK() const override;
virtual bool GetReverseLimitOK() const override;
virtual uint16_t GetFaults() const override;
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;
virtual void ConfigMaxOutputVoltage(double voltage) override;
virtual void ConfigFaultTime(float faultTime) override;
virtual void SetControlMode(ControlMode mode);
virtual ControlMode GetControlMode() const;
static void UpdateSyncGroup(uint8_t syncGroup);
void SetExpiration(float timeout) override;
float GetExpiration() const override;
bool IsAlive() const override;
void StopMotor() override;
bool IsSafetyEnabled() const override;
void SetSafetyEnabled(bool enabled) override;
void GetDescription(std::ostringstream& desc) const override;
uint8_t GetDeviceID() const;
// SpeedController overrides
virtual void SetInverted(bool isInverted) override;
virtual bool GetInverted() const override;
protected:
// Control mode helpers
void SetSpeedReference(uint8_t reference);
uint8_t GetSpeedReference() const;
void SetPositionReference(uint8_t reference);
uint8_t GetPositionReference() const;
uint8_t packPercentage(uint8_t *buffer, double value);
uint8_t packFXP8_8(uint8_t *buffer, double value);
uint8_t packFXP16_16(uint8_t *buffer, double value);
uint8_t packint16_t(uint8_t *buffer, int16_t value);
uint8_t packint32_t(uint8_t *buffer, int32_t value);
double unpackPercentage(uint8_t *buffer) const;
double unpackFXP8_8(uint8_t *buffer) const;
double unpackFXP16_16(uint8_t *buffer) const;
int16_t unpackint16_t(uint8_t *buffer) const;
int32_t unpackint32_t(uint8_t *buffer) const;
void sendMessage(uint32_t messageID, const uint8_t *data, uint8_t dataSize,
int32_t period = CAN_SEND_PERIOD_NO_REPEAT);
void requestMessage(uint32_t messageID,
int32_t period = CAN_SEND_PERIOD_NO_REPEAT);
bool getMessage(uint32_t messageID, uint32_t mask, uint8_t *data,
uint8_t *dataSize) const;
void setupPeriodicStatus();
void updatePeriodicStatus() const;
mutable priority_recursive_mutex m_mutex;
uint8_t m_deviceNumber;
float m_value = 0.0f;
// Parameters/configuration
ControlMode m_controlMode;
uint8_t m_speedReference = LM_REF_NONE;
uint8_t m_positionReference = LM_REF_NONE;
double m_p = 0.0;
double m_i = 0.0;
double m_d = 0.0;
NeutralMode m_neutralMode = kNeutralMode_Jumper;
uint16_t m_encoderCodesPerRev = 0;
uint16_t m_potentiometerTurns = 0;
LimitMode m_limitMode = kLimitMode_SwitchInputsOnly;
double m_forwardLimit = 0.0;
double m_reverseLimit = 0.0;
double m_maxOutputVoltage = 30.0;
double m_voltageRampRate = 0.0;
float m_faultTime = 0.0f;
// Which parameters have been verified since they were last set?
bool m_controlModeVerified = false; // Needs to be verified because it's set in the constructor
bool m_speedRefVerified = true;
bool m_posRefVerified = true;
bool m_pVerified = true;
bool m_iVerified = true;
bool m_dVerified = true;
bool m_neutralModeVerified = true;
bool m_encoderCodesPerRevVerified = true;
bool m_potentiometerTurnsVerified = true;
bool m_forwardLimitVerified = true;
bool m_reverseLimitVerified = true;
bool m_limitModeVerified = true;
bool m_maxOutputVoltageVerified = true;
bool m_voltageRampRateVerified = true;
bool m_faultTimeVerified = true;
// Status data
mutable float m_busVoltage = 0.0f;
mutable float m_outputVoltage = 0.0f;
mutable float m_outputCurrent = 0.0f;
mutable float m_temperature = 0.0f;
mutable double m_position = 0.0;
mutable double m_speed = 0.0;
mutable uint8_t m_limits = 0x00;
mutable uint16_t m_faults = 0x0000;
uint32_t m_firmwareVersion = 0;
uint8_t m_hardwareVersion = 0;
// Which periodic status messages have we received at least once?
mutable std::atomic<bool> m_receivedStatusMessage0{false};
mutable std::atomic<bool> m_receivedStatusMessage1{false};
mutable std::atomic<bool> m_receivedStatusMessage2{false};
bool m_controlEnabled;
void verify();
std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
void ValueChanged(::std::shared_ptr<ITable> source, const std::string &key, EntryValue value,
bool isNew) override;
void UpdateTable() override;
void StartLiveWindowMode() override;
void StopLiveWindowMode() override;
std::string GetSmartDashboardType() const override;
void InitTable(::std::shared_ptr<ITable> subTable) override;
::std::shared_ptr<ITable> GetTable() const override;
::std::shared_ptr<ITable> m_table = nullptr;
private:
void InitCANJaguar();
bool m_isInverted = false;
};