blob: 83779e78fbfc5554837c2bfd06c8b206bcbbaf28 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
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
21/**
22 * Luminary Micro Jaguar Speed Control
23 */
24class CANJaguar : public MotorSafety,
25 public SpeedController,
26 public ErrorBase,
27 public LiveWindowSendable,
28 public ITableListener
29{
30public:
31 // The internal PID control loop in the Jaguar runs at 1kHz.
32 static const INT32 kControllerRate = 1000;
33 static const 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 deviceNumber, ControlMode controlMode = kPercentVbus);
44 virtual ~CANJaguar();
45
46 // SpeedController interface
47 virtual float Get();
48 virtual void Set(float value, UINT8 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 GetFaults();
76 bool GetPowerCycled();
77 void SetVoltageRampRate(double rampRate);
78 virtual UINT32 GetFirmwareVersion();
79 UINT8 GetHardwareVersion();
80 void ConfigNeutralMode(NeutralMode mode);
81 void ConfigEncoderCodesPerRev(UINT16 codesPerRev);
82 void ConfigPotentiometerTurns(UINT16 turns);
83 void ConfigSoftPositionLimits(double forwardLimitPosition, double reverseLimitPosition);
84 void DisableSoftPositionLimits();
85 void ConfigMaxOutputVoltage(double voltage);
86 void ConfigFaultTime(float faultTime);
87
88 static void UpdateSyncGroup(UINT8 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
98protected:
99 UINT8 packPercentage(UINT8 *buffer, double value);
100 UINT8 packFXP8_8(UINT8 *buffer, double value);
101 UINT8 packFXP16_16(UINT8 *buffer, double value);
102 UINT8 packINT16(UINT8 *buffer, INT16 value);
103 UINT8 packINT32(UINT8 *buffer, INT32 value);
104 double unpackPercentage(UINT8 *buffer);
105 double unpackFXP8_8(UINT8 *buffer);
106 double unpackFXP16_16(UINT8 *buffer);
107 INT16 unpackINT16(UINT8 *buffer);
108 INT32 unpackINT32(UINT8 *buffer);
109 virtual void setTransaction(UINT32 messageID, const UINT8 *data, UINT8 dataSize);
110 virtual void getTransaction(UINT32 messageID, UINT8 *data, UINT8 *dataSize);
111
112 static INT32 sendMessage(UINT32 messageID, const UINT8 *data, UINT8 dataSize);
113 static INT32 receiveMessage(UINT32 *messageID, UINT8 *data, UINT8 *dataSize, float timeout = 0.02);
114
115 UINT8 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
132private:
133 void InitCANJaguar();
134};
135#endif
136