WPILib  2014.0
WPIRoboticsLibraryforFRC
 All Classes Functions Variables Pages
CANJaguar.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2009. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 
8 #ifndef CANJAGUAR_H
9 #define CANJAGUAR_H
10 
11 #include "ErrorBase.h"
12 #include "MotorSafety.h"
13 #include "MotorSafetyHelper.h"
14 #include "PIDOutput.h"
15 #include "SpeedController.h"
16 #include <semLib.h>
17 #include <vxWorks.h>
18 #include "LiveWindow/LiveWindowSendable.h"
19 #include "tables/ITable.h"
20 
24 class CANJaguar : public MotorSafety,
25  public SpeedController,
26  public ErrorBase,
27  public LiveWindowSendable,
28  public ITableListener
29 {
30 public:
31  // The internal PID control loop in the Jaguar runs at 1kHz.
32  static const int32_t kControllerRate = 1000;
33  static constexpr double kApproxBusVoltage = 12.0;
34 
35  typedef enum {kPercentVbus, kCurrent, kSpeed, kPosition, kVoltage} ControlMode;
36  typedef enum {kCurrentFault = 1, kTemperatureFault = 2, kBusVoltageFault = 4, kGateDriverFault = 8} Faults;
37  typedef enum {kForwardLimit = 1, kReverseLimit = 2} Limits;
38  typedef enum {kPosRef_QuadEncoder = 0, kPosRef_Potentiometer = 1, kPosRef_None = 0xFF} PositionReference;
39  typedef enum {kSpeedRef_Encoder = 0, kSpeedRef_InvEncoder = 2, kSpeedRef_QuadEncoder = 3, kSpeedRef_None = 0xFF} SpeedReference;
40  typedef enum {kNeutralMode_Jumper = 0, kNeutralMode_Brake = 1, kNeutralMode_Coast = 2} NeutralMode;
41  typedef enum {kLimitMode_SwitchInputsOnly = 0, kLimitMode_SoftPositionLimits = 1} LimitMode;
42 
43  explicit CANJaguar(uint8_t deviceNumber, ControlMode controlMode = kPercentVbus);
44  virtual ~CANJaguar();
45 
46  // SpeedController interface
47  virtual float Get();
48  virtual void Set(float value, uint8_t syncGroup=0);
49  virtual void Disable();
50 
51  // PIDOutput interface
52  virtual void PIDWrite(float output);
53 
54  // Other Accessors
55  void SetSpeedReference(SpeedReference reference);
56  SpeedReference GetSpeedReference();
57  void SetPositionReference(PositionReference reference);
58  PositionReference GetPositionReference();
59  void SetPID(double p, double i, double d);
60  double GetP();
61  double GetI();
62  double GetD();
63  void EnableControl(double encoderInitialPosition = 0.0);
64  void DisableControl();
65  void ChangeControlMode(ControlMode controlMode);
66  ControlMode GetControlMode();
67  float GetBusVoltage();
68  float GetOutputVoltage();
69  float GetOutputCurrent();
70  float GetTemperature();
71  double GetPosition();
72  double GetSpeed();
73  bool GetForwardLimitOK();
74  bool GetReverseLimitOK();
75  uint16_t GetFaults();
76  bool GetPowerCycled();
77  void SetVoltageRampRate(double rampRate);
78  virtual uint32_t GetFirmwareVersion();
79  uint8_t GetHardwareVersion();
80  void ConfigNeutralMode(NeutralMode mode);
81  void ConfigEncoderCodesPerRev(uint16_t codesPerRev);
82  void ConfigPotentiometerTurns(uint16_t turns);
83  void ConfigSoftPositionLimits(double forwardLimitPosition, double reverseLimitPosition);
85  void ConfigMaxOutputVoltage(double voltage);
86  void ConfigFaultTime(float faultTime);
87 
88  static void UpdateSyncGroup(uint8_t syncGroup);
89 
90  void SetExpiration(float timeout);
91  float GetExpiration();
92  bool IsAlive();
93  void StopMotor();
94  bool IsSafetyEnabled();
95  void SetSafetyEnabled(bool enabled);
96  void GetDescription(char *desc);
97 
98 protected:
99  uint8_t packPercentage(uint8_t *buffer, double value);
100  uint8_t packFXP8_8(uint8_t *buffer, double value);
101  uint8_t packFXP16_16(uint8_t *buffer, double value);
102  uint8_t packint16_t(uint8_t *buffer, int16_t value);
103  uint8_t packint32_t(uint8_t *buffer, int32_t value);
104  double unpackPercentage(uint8_t *buffer);
105  double unpackFXP8_8(uint8_t *buffer);
106  double unpackFXP16_16(uint8_t *buffer);
107  int16_t unpackint16_t(uint8_t *buffer);
108  int32_t unpackint32_t(uint8_t *buffer);
109  virtual void setTransaction(uint32_t messageID, const uint8_t *data, uint8_t dataSize);
110  virtual void getTransaction(uint32_t messageID, uint8_t *data, uint8_t *dataSize);
111 
112  static int32_t sendMessage(uint32_t messageID, const uint8_t *data, uint8_t dataSize);
113  static int32_t receiveMessage(uint32_t *messageID, uint8_t *data, uint8_t *dataSize, float timeout = 0.02);
114 
115  uint8_t m_deviceNumber;
116  ControlMode m_controlMode;
117  SEM_ID m_transactionSemaphore;
118  double m_maxOutputVoltage;
119 
120  MotorSafetyHelper *m_safetyHelper;
121 
122  void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
123  void UpdateTable();
124  void StartLiveWindowMode();
125  void StopLiveWindowMode();
126  std::string GetSmartDashboardType();
127  void InitTable(ITable *subTable);
128  ITable * GetTable();
129 
130  ITable *m_table;
131 
132 private:
133  void InitCANJaguar();
134 };
135 #endif
136 
void StopMotor()
Definition: CANJaguar.cpp:1236
bool GetReverseLimitOK()
Definition: CANJaguar.cpp:934
SpeedReference GetSpeedReference()
Definition: CANJaguar.cpp:482
Definition: MotorSafety.h:11
virtual void setTransaction(uint32_t messageID, const uint8_t *data, uint8_t dataSize)
Definition: CANJaguar.cpp:395
void DisableControl()
Definition: CANJaguar.cpp:738
virtual void Set(float value, uint8_t syncGroup=0)
Definition: CANJaguar.cpp:113
bool GetPowerCycled()
Definition: CANJaguar.cpp:973
bool GetForwardLimitOK()
Definition: CANJaguar.cpp:916
virtual float Get()
Definition: CANJaguar.cpp:182
static int32_t receiveMessage(uint32_t *messageID, uint8_t *data, uint8_t *dataSize, float timeout=0.02)
Definition: CANJaguar.cpp:377
void SetSpeedReference(SpeedReference reference)
Definition: CANJaguar.cpp:469
double GetPosition()
Definition: CANJaguar.cpp:880
static void UpdateSyncGroup(uint8_t syncGroup)
Definition: CANJaguar.cpp:1191
float GetBusVoltage()
Definition: CANJaguar.cpp:807
void ConfigMaxOutputVoltage(double voltage)
Definition: CANJaguar.cpp:1158
double GetSpeed()
Definition: CANJaguar.cpp:898
void ConfigFaultTime(float faultTime)
Definition: CANJaguar.cpp:1176
virtual void Disable()
Definition: CANJaguar.cpp:233
virtual void PIDWrite(float output)
Definition: CANJaguar.cpp:245
void EnableControl(double encoderInitialPosition=0.0)
Definition: CANJaguar.cpp:707
ControlMode GetControlMode()
Definition: CANJaguar.cpp:789
double GetP()
Definition: CANJaguar.cpp:579
void ConfigEncoderCodesPerRev(uint16_t codesPerRev)
Definition: CANJaguar.cpp:1084
void SetPID(double p, double i, double d)
Definition: CANJaguar.cpp:536
virtual void getTransaction(uint32_t messageID, uint8_t *data, uint8_t *dataSize)
Definition: CANJaguar.cpp:430
Definition: ErrorBase.h:37
void ConfigPotentiometerTurns(uint16_t turns)
Definition: CANJaguar.cpp:1101
double GetD()
Definition: CANJaguar.cpp:661
static int32_t sendMessage(uint32_t messageID, const uint8_t *data, uint8_t dataSize)
Definition: CANJaguar.cpp:334
float GetTemperature()
Definition: CANJaguar.cpp:862
void ConfigNeutralMode(NeutralMode mode)
Definition: CANJaguar.cpp:1071
Definition: SpeedController.h:16
Definition: MotorSafetyHelper.h:16
void SetVoltageRampRate(double rampRate)
Definition: CANJaguar.cpp:1003
void ChangeControlMode(ControlMode controlMode)
Definition: CANJaguar.cpp:771
virtual uint32_t GetFirmwareVersion()
Definition: CANJaguar.cpp:1028
CANJaguar(uint8_t deviceNumber, ControlMode controlMode=kPercentVbus)
Definition: CANJaguar.cpp:82
PositionReference GetPositionReference()
Definition: CANJaguar.cpp:516
uint8_t GetHardwareVersion()
Definition: CANJaguar.cpp:1047
void DisableSoftPositionLimits()
Definition: CANJaguar.cpp:1142
uint16_t GetFaults()
Definition: CANJaguar.cpp:952
void ConfigSoftPositionLimits(double forwardLimitPosition, double reverseLimitPosition)
Definition: CANJaguar.cpp:1120
double GetI()
Definition: CANJaguar.cpp:620
float GetOutputCurrent()
Definition: CANJaguar.cpp:844
float GetOutputVoltage()
Definition: CANJaguar.cpp:825
Definition: CANJaguar.h:24
void SetPositionReference(PositionReference reference)
Definition: CANJaguar.cpp:503