Now you can download a copy of these docs so you can use them offline! Download now
Gyro.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. 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 #include "Gyro.h" 00008 #include "AnalogChannel.h" 00009 #include "AnalogModule.h" 00010 #include "Timer.h" 00011 #include "WPIErrors.h" 00012 00013 const UINT32 Gyro::kOversampleBits; 00014 const UINT32 Gyro::kAverageBits; 00015 const float Gyro::kSamplesPerSecond; 00016 const float Gyro::kCalibrationSampleTime; 00017 const float Gyro::kDefaultVoltsPerDegreePerSecond; 00018 00027 void Gyro::InitGyro() 00028 { 00029 if (!m_analog->IsAccumulatorChannel()) 00030 { 00031 wpi_setWPIErrorWithContext(ParameterOutOfRange, 00032 "moduleNumber and/or channel (must be accumulator channel)"); 00033 if (m_channelAllocated) 00034 { 00035 delete m_analog; 00036 m_analog = NULL; 00037 } 00038 return; 00039 } 00040 00041 m_voltsPerDegreePerSecond = kDefaultVoltsPerDegreePerSecond; 00042 m_analog->SetAverageBits(kAverageBits); 00043 m_analog->SetOversampleBits(kOversampleBits); 00044 float sampleRate = kSamplesPerSecond * 00045 (1 << (kAverageBits + kOversampleBits)); 00046 m_analog->GetModule()->SetSampleRate(sampleRate); 00047 Wait(1.0); 00048 00049 m_analog->InitAccumulator(); 00050 Wait(kCalibrationSampleTime); 00051 00052 INT64 value; 00053 UINT32 count; 00054 m_analog->GetAccumulatorOutput(&value, &count); 00055 00056 UINT32 center = (UINT32)((float)value / (float)count + .5); 00057 00058 m_offset = ((float)value / (float)count) - (float)center; 00059 00060 m_analog->SetAccumulatorCenter(center); 00061 m_analog->SetAccumulatorDeadband(0); 00062 m_analog->ResetAccumulator(); 00063 } 00064 00071 Gyro::Gyro(UINT8 moduleNumber, UINT32 channel) 00072 { 00073 m_analog = new AnalogChannel(moduleNumber, channel); 00074 m_channelAllocated = true; 00075 InitGyro(); 00076 } 00077 00085 Gyro::Gyro(UINT32 channel) 00086 { 00087 m_analog = new AnalogChannel(channel); 00088 m_channelAllocated = true; 00089 InitGyro(); 00090 } 00091 00098 Gyro::Gyro(AnalogChannel *channel) 00099 { 00100 m_analog = channel; 00101 m_channelAllocated = false; 00102 if (channel == NULL) 00103 { 00104 wpi_setWPIError(NullParameter); 00105 } 00106 else 00107 { 00108 InitGyro(); 00109 } 00110 } 00111 00112 Gyro::Gyro(AnalogChannel &channel) 00113 { 00114 m_analog = &channel; 00115 m_channelAllocated = false; 00116 InitGyro(); 00117 } 00118 00124 void Gyro::Reset() 00125 { 00126 m_analog->ResetAccumulator(); 00127 } 00128 00132 Gyro::~Gyro() 00133 { 00134 if (m_channelAllocated) 00135 delete m_analog; 00136 } 00137 00149 float Gyro::GetAngle( void ) 00150 { 00151 INT64 rawValue; 00152 UINT32 count; 00153 m_analog->GetAccumulatorOutput(&rawValue, &count); 00154 00155 INT64 value = rawValue - (INT64)((float)count * m_offset); 00156 00157 double scaledValue = value * 1e-9 * (double)m_analog->GetLSBWeight() * (double)(1 << m_analog->GetAverageBits()) / 00158 (m_analog->GetModule()->GetSampleRate() * m_voltsPerDegreePerSecond); 00159 00160 return (float)scaledValue; 00161 } 00162 00163 00171 void Gyro::SetSensitivity( float voltsPerDegreePerSecond ) 00172 { 00173 m_voltsPerDegreePerSecond = voltsPerDegreePerSecond; 00174 } 00175 00181 double Gyro::PIDGet() 00182 { 00183 return GetAngle(); 00184 }
Generated on Thu Jan 12 2012 22:35:20 for WPILibC++ by
1.7.1