Now you can download a copy of these docs so you can use them offline! Download now
Gyro.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. 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 #include "Gyro.h"
8 #include "AnalogChannel.h"
9 #include "AnalogModule.h"
10 #include "NetworkCommunication/UsageReporting.h"
11 #include "Timer.h"
12 #include "WPIErrors.h"
13 #include "LiveWindow/LiveWindow.h"
14 
15 const uint32_t Gyro::kOversampleBits;
16 const uint32_t Gyro::kAverageBits;
17 constexpr float Gyro::kSamplesPerSecond;
18 constexpr float Gyro::kCalibrationSampleTime;
19 constexpr float Gyro::kDefaultVoltsPerDegreePerSecond;
20 
29 void Gyro::InitGyro()
30 {
31  m_table = NULL;
32  if (!m_analog->IsAccumulatorChannel())
33  {
34  wpi_setWPIErrorWithContext(ParameterOutOfRange,
35  "moduleNumber and/or channel (must be accumulator channel)");
36  if (m_channelAllocated)
37  {
38  delete m_analog;
39  m_analog = NULL;
40  }
41  return;
42  }
43 
44  m_voltsPerDegreePerSecond = kDefaultVoltsPerDegreePerSecond;
45  m_analog->SetAverageBits(kAverageBits);
46  m_analog->SetOversampleBits(kOversampleBits);
47  float sampleRate = kSamplesPerSecond *
48  (1 << (kAverageBits + kOversampleBits));
49  m_analog->GetModule()->SetSampleRate(sampleRate);
50  Wait(1.0);
51 
52  m_analog->InitAccumulator();
53  Wait(kCalibrationSampleTime);
54 
55  INT64 value;
56  uint32_t count;
57  m_analog->GetAccumulatorOutput(&value, &count);
58 
59  m_center = (uint32_t)((float)value / (float)count + .5);
60 
61  m_offset = ((float)value / (float)count) - (float)m_center;
62 
63  m_analog->SetAccumulatorCenter(m_center);
64  m_analog->SetAccumulatorDeadband(0);
65  m_analog->ResetAccumulator();
66 
67  SetPIDSourceParameter(kAngle);
68 
69  nUsageReporting::report(nUsageReporting::kResourceType_Gyro, m_analog->GetChannel(), m_analog->GetModuleNumber() - 1);
70  LiveWindow::GetInstance()->AddSensor("Gyro", m_analog->GetModuleNumber(), m_analog->GetChannel(), this);
71 }
72 
79 Gyro::Gyro(uint8_t moduleNumber, uint32_t channel)
80 {
81  m_analog = new AnalogChannel(moduleNumber, channel);
82  m_channelAllocated = true;
83  InitGyro();
84 }
85 
93 Gyro::Gyro(uint32_t channel)
94 {
95  m_analog = new AnalogChannel(channel);
96  m_channelAllocated = true;
97  InitGyro();
98 }
99 
107 {
108  m_analog = channel;
109  m_channelAllocated = false;
110  if (channel == NULL)
111  {
112  wpi_setWPIError(NullParameter);
113  }
114  else
115  {
116  InitGyro();
117  }
118 }
119 
120 Gyro::Gyro(AnalogChannel &channel)
121 {
122  m_analog = &channel;
123  m_channelAllocated = false;
124  InitGyro();
125 }
126 
133 {
134  m_analog->ResetAccumulator();
135 }
136 
141 {
142  if (m_channelAllocated)
143  delete m_analog;
144 }
145 
157 float Gyro::GetAngle( void )
158 {
159  INT64 rawValue;
160  uint32_t count;
161  m_analog->GetAccumulatorOutput(&rawValue, &count);
162 
163  INT64 value = rawValue - (INT64)((float)count * m_offset);
164 
165  double scaledValue = value * 1e-9 * (double)m_analog->GetLSBWeight() * (double)(1 << m_analog->GetAverageBits()) /
166  (m_analog->GetModule()->GetSampleRate() * m_voltsPerDegreePerSecond);
167 
168  return (float)scaledValue;
169 }
170 
171 
179 double Gyro::GetRate( void )
180 {
181  return (m_analog->GetAverageValue() - ((double)m_center + m_offset)) * 1e-9 * m_analog->GetLSBWeight()
182  / ((1 << m_analog->GetOversampleBits()) * m_voltsPerDegreePerSecond);
183 }
184 
185 
193 void Gyro::SetSensitivity( float voltsPerDegreePerSecond )
194 {
195  m_voltsPerDegreePerSecond = voltsPerDegreePerSecond;
196 }
197 
198 void Gyro::SetPIDSourceParameter(PIDSourceParameter pidSource)
199 {
200  if(pidSource == 0 || pidSource > 2)
201  wpi_setWPIErrorWithContext(ParameterOutOfRange, "Gyro pidSource");
202  m_pidSource = pidSource;
203 }
204 
210 double Gyro::PIDGet()
211 {
212  switch(m_pidSource){
213  case kRate:
214  return GetRate();
215  case kAngle:
216  return GetAngle();
217  default:
218  return 0;
219  }
220 }
221 
223  if (m_table != NULL) {
224  m_table->PutNumber("Value", GetAngle());
225  }
226 }
227 
229 
230 }
231 
233 
234 }
235 
237  return "Gyro";
238 }
239 
240 void Gyro::InitTable(ITable *subTable) {
241  m_table = subTable;
242  UpdateTable();
243 }
244 
246  return m_table;
247 }
248 
int32_t GetAverageValue()
void GetAccumulatorOutput(INT64 *value, uint32_t *count)
uint32_t GetChannel()
virtual void PutNumber(std::string key, double value)=0
void SetAccumulatorDeadband(int32_t deadband)
ITable * GetTable()
Definition: Gyro.cpp:245
Definition: ITable.h:26
virtual ~Gyro()
Definition: Gyro.cpp:140
uint8_t GetModuleNumber()
uint32_t GetOversampleBits()
void SetSampleRate(float samplesPerSecond)
virtual void Reset()
Definition: Gyro.cpp:132
void AddSensor(const char *subsystem, const char *name, LiveWindowSendable *component)
Definition: LiveWindow.cpp:82
AnalogModule * GetModule()
bool IsAccumulatorChannel()
void ResetAccumulator()
virtual float GetAngle()
Definition: Gyro.cpp:157
void InitTable(ITable *subTable)
Definition: Gyro.cpp:240
static LiveWindow * GetInstance()
Definition: LiveWindow.cpp:13
virtual double GetRate()
Definition: Gyro.cpp:179
void InitAccumulator()
uint32_t GetAverageBits()
void StartLiveWindowMode()
Definition: Gyro.cpp:228
void StopLiveWindowMode()
Definition: Gyro.cpp:232
void SetAccumulatorCenter(int32_t center)
Gyro(uint8_t moduleNumber, uint32_t channel)
Definition: Gyro.cpp:79
uint32_t GetLSBWeight()
float GetSampleRate()
void SetAverageBits(uint32_t bits)
void SetSensitivity(float voltsPerDegreePerSecond)
Definition: Gyro.cpp:193
std::string GetSmartDashboardType()
Definition: Gyro.cpp:236
void SetOversampleBits(uint32_t bits)
double PIDGet()
Definition: Gyro.cpp:210
void UpdateTable()
Definition: Gyro.cpp:222

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6