jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 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 "HiTechnicCompass.h"
|
| 8 | #include "DigitalModule.h"
|
| 9 | #include "I2C.h"
|
| 10 | #include "NetworkCommunication/UsageReporting.h"
|
| 11 | #include "WPIErrors.h"
|
| 12 | #include "LiveWindow/LiveWindow.h"
|
| 13 |
|
| 14 | const UINT8 HiTechnicCompass::kAddress;
|
| 15 | const UINT8 HiTechnicCompass::kManufacturerBaseRegister;
|
| 16 | const UINT8 HiTechnicCompass::kManufacturerSize;
|
| 17 | const UINT8 HiTechnicCompass::kSensorTypeBaseRegister;
|
| 18 | const UINT8 HiTechnicCompass::kSensorTypeSize;
|
| 19 | const UINT8 HiTechnicCompass::kHeadingRegister;
|
| 20 |
|
| 21 | /**
|
| 22 | * Constructor.
|
| 23 | *
|
| 24 | * @param moduleNumber The digital module that the sensor is plugged into (1 or 2).
|
| 25 | */
|
| 26 | HiTechnicCompass::HiTechnicCompass(UINT8 moduleNumber)
|
| 27 | : m_i2c (NULL)
|
| 28 | {
|
jerrym | 37afdca | 2013-03-03 01:17:57 +0000 | [diff] [blame^] | 29 | m_table = NULL;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 30 | DigitalModule *module = DigitalModule::GetInstance(moduleNumber);
|
| 31 | if (module)
|
| 32 | {
|
| 33 | m_i2c = module->GetI2C(kAddress);
|
| 34 |
|
| 35 | // Verify Sensor
|
| 36 | const UINT8 kExpectedManufacturer[] = "HiTechnc";
|
| 37 | const UINT8 kExpectedSensorType[] = "Compass ";
|
| 38 | if ( ! m_i2c->VerifySensor(kManufacturerBaseRegister, kManufacturerSize, kExpectedManufacturer) )
|
| 39 | {
|
| 40 | wpi_setWPIError(CompassManufacturerError);
|
| 41 | return;
|
| 42 | }
|
| 43 | if ( ! m_i2c->VerifySensor(kSensorTypeBaseRegister, kSensorTypeSize, kExpectedSensorType) )
|
| 44 | {
|
| 45 | wpi_setWPIError(CompassTypeError);
|
| 46 | }
|
| 47 |
|
| 48 | nUsageReporting::report(nUsageReporting::kResourceType_HiTechnicCompass, moduleNumber - 1);
|
| 49 | LiveWindow::GetInstance()->AddSensor("HiTechnicCompass", moduleNumber, 0, this);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Destructor.
|
| 55 | */
|
| 56 | HiTechnicCompass::~HiTechnicCompass()
|
| 57 | {
|
| 58 | delete m_i2c;
|
| 59 | m_i2c = NULL;
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * Get the compass angle in degrees.
|
| 64 | *
|
| 65 | * The resolution of this reading is 1 degree.
|
| 66 | *
|
| 67 | * @return Angle of the compass in degrees.
|
| 68 | */
|
| 69 | float HiTechnicCompass::GetAngle()
|
| 70 | {
|
| 71 | UINT16 heading = 0;
|
| 72 | if (m_i2c)
|
| 73 | {
|
| 74 | m_i2c->Read(kHeadingRegister, sizeof(heading), (UINT8 *)&heading);
|
| 75 |
|
| 76 | // Sensor is little endian... swap bytes
|
| 77 | heading = (heading >> 8) | (heading << 8);
|
| 78 | }
|
| 79 | return (float)heading;
|
| 80 | }
|
| 81 |
|
| 82 | void HiTechnicCompass::UpdateTable() {
|
| 83 | if (m_table != NULL) {
|
| 84 | m_table->PutNumber("Value", GetAngle());
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | void HiTechnicCompass::StartLiveWindowMode() {
|
| 89 |
|
| 90 | }
|
| 91 |
|
| 92 | void HiTechnicCompass::StopLiveWindowMode() {
|
| 93 |
|
| 94 | }
|
| 95 |
|
| 96 | std::string HiTechnicCompass::GetSmartDashboardType() {
|
| 97 | return "HiTechnicCompass";
|
| 98 | }
|
| 99 |
|
| 100 | void HiTechnicCompass::InitTable(ITable *subTable) {
|
| 101 | m_table = subTable;
|
| 102 | UpdateTable();
|
| 103 | }
|
| 104 |
|
| 105 | ITable * HiTechnicCompass::GetTable() {
|
| 106 | return m_table;
|
| 107 | }
|
| 108 |
|