Now you can download a copy of these docs so you can use them offline! Download now
HiTechnicCompass.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 "HiTechnicCompass.h" 00008 #include "DigitalModule.h" 00009 #include "I2C.h" 00010 #include "WPIErrors.h" 00011 00012 const UINT8 HiTechnicCompass::kAddress; 00013 const UINT8 HiTechnicCompass::kManufacturerBaseRegister; 00014 const UINT8 HiTechnicCompass::kManufacturerSize; 00015 const UINT8 HiTechnicCompass::kSensorTypeBaseRegister; 00016 const UINT8 HiTechnicCompass::kSensorTypeSize; 00017 const UINT8 HiTechnicCompass::kHeadingRegister; 00018 00024 HiTechnicCompass::HiTechnicCompass(UINT8 moduleNumber) 00025 : m_i2c (NULL) 00026 { 00027 DigitalModule *module = DigitalModule::GetInstance(moduleNumber); 00028 if (module) 00029 { 00030 m_i2c = module->GetI2C(kAddress); 00031 00032 // Verify Sensor 00033 const UINT8 kExpectedManufacturer[] = "HiTechnc"; 00034 const UINT8 kExpectedSensorType[] = "Compass "; 00035 if ( ! m_i2c->VerifySensor(kManufacturerBaseRegister, kManufacturerSize, kExpectedManufacturer) ) 00036 { 00037 wpi_setWPIError(CompassManufacturerError); 00038 return; 00039 } 00040 if ( ! m_i2c->VerifySensor(kSensorTypeBaseRegister, kSensorTypeSize, kExpectedSensorType) ) 00041 { 00042 wpi_setWPIError(CompassTypeError); 00043 } 00044 } 00045 } 00046 00050 HiTechnicCompass::~HiTechnicCompass() 00051 { 00052 delete m_i2c; 00053 m_i2c = NULL; 00054 } 00055 00063 float HiTechnicCompass::GetAngle() 00064 { 00065 UINT16 heading = 0; 00066 if (m_i2c) 00067 { 00068 m_i2c->Read(kHeadingRegister, sizeof(heading), (UINT8 *)&heading); 00069 00070 // Sensor is little endian... swap bytes 00071 heading = (heading >> 8) | (heading << 8); 00072 } 00073 return (float)heading; 00074 } 00075
Generated on Thu Jan 12 2012 22:35:20 for WPILibC++ by
1.7.1