WPILib 2012
WPILibRoboticsLibraryforFRC
|
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2009. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 00008 #ifndef CANJAGUAR_H 00009 #define CANJAGUAR_H 00010 00011 #include "ErrorBase.h" 00012 #include "MotorSafety.h" 00013 #include "MotorSafetyHelper.h" 00014 #include "PIDOutput.h" 00015 #include "SpeedController.h" 00016 #include <semLib.h> 00017 #include <vxWorks.h> 00018 00022 class CANJaguar : public MotorSafety, public PIDOutput, public SpeedController, public ErrorBase 00023 { 00024 public: 00025 // The internal PID control loop in the Jaguar runs at 1kHz. 00026 static const INT32 kControllerRate = 1000; 00027 static const double kApproxBusVoltage = 12.0; 00028 00029 typedef enum {kPercentVbus, kCurrent, kSpeed, kPosition, kVoltage} ControlMode; 00030 typedef enum {kCurrentFault = 1, kTemperatureFault = 2, kBusVoltageFault = 4, kGateDriverFault = 8} Faults; 00031 typedef enum {kForwardLimit = 1, kReverseLimit = 2} Limits; 00032 typedef enum {kPosRef_QuadEncoder = 0, kPosRef_Potentiometer = 1, kPosRef_None = 0xFF} PositionReference; 00033 typedef enum {kSpeedRef_Encoder = 0, kSpeedRef_InvEncoder = 2, kSpeedRef_QuadEncoder = 3, kSpeedRef_None = 0xFF} SpeedReference; 00034 typedef enum {kNeutralMode_Jumper = 0, kNeutralMode_Brake = 1, kNeutralMode_Coast = 2} NeutralMode; 00035 typedef enum {kLimitMode_SwitchInputsOnly = 0, kLimitMode_SoftPositionLimits = 1} LimitMode; 00036 00037 explicit CANJaguar(UINT8 deviceNumber, ControlMode controlMode = kPercentVbus); 00038 virtual ~CANJaguar(); 00039 00040 // SpeedController interface 00041 virtual float Get(); 00042 virtual void Set(float value, UINT8 syncGroup=0); 00043 virtual void Disable(); 00044 00045 // PIDOutput interface 00046 virtual void PIDWrite(float output); 00047 00048 // Other Accessors 00049 void SetSpeedReference(SpeedReference reference); 00050 SpeedReference GetSpeedReference(); 00051 void SetPositionReference(PositionReference reference); 00052 PositionReference GetPositionReference(); 00053 void SetPID(double p, double i, double d); 00054 double GetP(); 00055 double GetI(); 00056 double GetD(); 00057 void EnableControl(double encoderInitialPosition = 0.0); 00058 void DisableControl(); 00059 void ChangeControlMode(ControlMode controlMode); 00060 ControlMode GetControlMode(); 00061 float GetBusVoltage(); 00062 float GetOutputVoltage(); 00063 float GetOutputCurrent(); 00064 float GetTemperature(); 00065 double GetPosition(); 00066 double GetSpeed(); 00067 bool GetForwardLimitOK(); 00068 bool GetReverseLimitOK(); 00069 UINT16 GetFaults(); 00070 bool GetPowerCycled(); 00071 void SetVoltageRampRate(double rampRate); 00072 virtual UINT32 GetFirmwareVersion(); 00073 UINT8 GetHardwareVersion(); 00074 void ConfigNeutralMode(NeutralMode mode); 00075 void ConfigEncoderCodesPerRev(UINT16 codesPerRev); 00076 void ConfigPotentiometerTurns(UINT16 turns); 00077 void ConfigSoftPositionLimits(double forwardLimitPosition, double reverseLimitPosition); 00078 void DisableSoftPositionLimits(); 00079 void ConfigMaxOutputVoltage(double voltage); 00080 void ConfigFaultTime(float faultTime); 00081 00082 static void UpdateSyncGroup(UINT8 syncGroup); 00083 00084 void SetExpiration(float timeout); 00085 float GetExpiration(); 00086 bool IsAlive(); 00087 void StopMotor(); 00088 bool IsSafetyEnabled(); 00089 void SetSafetyEnabled(bool enabled); 00090 void GetDescription(char *desc); 00091 00092 protected: 00093 UINT8 packPercentage(UINT8 *buffer, double value); 00094 UINT8 packFXP8_8(UINT8 *buffer, double value); 00095 UINT8 packFXP16_16(UINT8 *buffer, double value); 00096 UINT8 packINT16(UINT8 *buffer, INT16 value); 00097 UINT8 packINT32(UINT8 *buffer, INT32 value); 00098 double unpackPercentage(UINT8 *buffer); 00099 double unpackFXP8_8(UINT8 *buffer); 00100 double unpackFXP16_16(UINT8 *buffer); 00101 INT16 unpackINT16(UINT8 *buffer); 00102 INT32 unpackINT32(UINT8 *buffer); 00103 virtual void setTransaction(UINT32 messageID, const UINT8 *data, UINT8 dataSize); 00104 virtual void getTransaction(UINT32 messageID, UINT8 *data, UINT8 *dataSize); 00105 00106 static INT32 sendMessage(UINT32 messageID, const UINT8 *data, UINT8 dataSize); 00107 static INT32 receiveMessage(UINT32 *messageID, UINT8 *data, UINT8 *dataSize, float timeout = 0.02); 00108 00109 UINT8 m_deviceNumber; 00110 ControlMode m_controlMode; 00111 SEM_ID m_transactionSemaphore; 00112 double m_maxOutputVoltage; 00113 00114 MotorSafetyHelper *m_safetyHelper; 00115 00116 private: 00117 void InitCANJaguar(); 00118 }; 00119 #endif 00120