Makeblock-library-for-Arduino  V3.2.4
It's a library for arduino application developers
MeCompass.h
Go to the documentation of this file.
1 
47 /* Define to prevent recursive inclusion -------------------------------------*/
48 #ifndef MECOMPASS_H
49 #define MECOMPASS_H
50 
51 /* Includes ------------------------------------------------------------------*/
52 #include <stdint.h>
53 #include <stdbool.h>
54 #include <Arduino.h>
55 #include "MeConfig.h"
56 
57 #ifdef ME_PORT_DEFINED
58 #include "MePort.h"
59 #endif /* ME_PORT_DEFINED */
60 
61 /* Exported macro ------------------------------------------------------------*/
62 //#define COMPASS_SERIAL_DEBUG
63 
64 #define I2C_ERROR (-1)
65 
66 // Me Compass only has one address
67 #define COMPASS_DEFAULT_ADDRESS (0x1E)
68 
69 //Me Compass Register Address
70 #define COMPASS_RA_CONFIG_A (0x00)
71 #define COMPASS_RA_CONFIG_B (0x01)
72 #define COMPASS_RA_MODE (0x02)
73 #define COMPASS_RA_DATAX_H (0x03)
74 #define COMPASS_RA_DATAX_L (0x04)
75 #define COMPASS_RA_DATAZ_H (0x05)
76 #define COMPASS_RA_DATAZ_L (0x06)
77 #define COMPASS_RA_DATAY_H (0x07)
78 #define COMPASS_RA_DATAY_L (0x08)
79 #define COMPASS_RA_STATUS (0x09)
80 #define COMPASS_RA_ID_A (0x0A)
81 #define COMPASS_RA_ID_B (0x0B)
82 #define COMPASS_RA_ID_C (0x0C)
83 
84 //define number of samples averaged per measurement
85 #define COMPASS_AVERAGING_1 (0x00)
86 #define COMPASS_AVERAGING_2 (0x20)
87 #define COMPASS_AVERAGING_4 (0x40)
88 #define COMPASS_AVERAGING_8 (0x60)
89 
90 //define data output rate value (Hz)
91 #define COMPASS_RATE_0P75 (0x00) // 0.75 (Hz)
92 #define COMPASS_RATE_1P5 (0x40) // 1.5 (Hz)
93 #define COMPASS_RATE_3 (0x08) // 3 (Hz)
94 #define COMPASS_RATE_7P5 (0x0C) // 7.5 (Hz)
95 #define COMPASS_RATE_15 (0x10) // 15 (Hz)
96 #define COMPASS_RATE_30 (0x14) // 30 (Hz)
97 #define COMPASS_RATE_75 (0x18) // 75 (Hz)
98 
99 //define measurement bias value
100 #define COMPASS_BIAS_NORMAL (0x00)
101 #define COMPASS_BIAS_POSITIVE (0x01)
102 #define COMPASS_BIAS_NEGATIVE (0x02)
103 
104 //define magnetic field gain value
105 /* -+-------------+-----------------
106  * | Field Range | Gain (LSB/Gauss)
107  * -+-------------+-----------------
108  * | +/- 0.88 Ga | 1370
109  * | +/- 1.3 Ga | 1090 (Default)
110  * | +/- 1.9 Ga | 820
111  * | +/- 2.5 Ga | 660
112  * | +/- 4.0 Ga | 440
113  * | +/- 4.7 Ga | 390
114  * | +/- 5.6 Ga | 330
115  * | +/- 8.1 Ga | 230
116  * -+-------------+-----------------*/
117 #define COMPASS_GAIN_1370 (0x00)
118 #define COMPASS_GAIN_1090 (0x20)
119 #define COMPASS_GAIN_820 (0x40)
120 #define COMPASS_GAIN_660 (0x60)
121 #define COMPASS_GAIN_440 (0x80)
122 #define COMPASS_GAIN_390 (0xA0)
123 #define COMPASS_GAIN_330 (0xC0)
124 #define COMPASS_GAIN_220 (0xE0)
125 
126 //define measurement mode
127 #define COMPASS_MODE_CONTINUOUS (0x00)
128 #define COMPASS_MODE_SINGLE (0x01)
129 #define COMPASS_MODE_IDLE (0x02)
130 
131 //define others parameter
132 #define COMPASS_PI 3.14159265F
133 #define START_ADDRESS_OF_EEPROM_BUFFER (int16_t)(0x00)
134 
135 /* define a struct to save calibration parameters------------------------------*/
137 {
138  float X_excursion;
139  float Y_excursion;
140  float Z_excursion;
141  float X_gain;
142  float Y_gain;
143  float Z_gain;
144  uint8_t Rotation_Axis; //1:X_Axis 2:Y_Axis 3:Z_Axis
145 
146  uint8_t verify_flag;
147 };
148 
154 #ifndef ME_PORT_DEFINED
155 class MeCompass
156 #else // !ME_PORT_DEFINED
157 class MeCompass : public MePort
158 #endif // !ME_PORT_DEFINED
159 {
160 public:
161 #ifdef ME_PORT_DEFINED
162 
166  MeCompass();
167 
174  MeCompass(uint8_t port);
175 
185  MeCompass(uint8_t port, uint8_t address);
186 #else
187 
195  MeCompass(uint8_t keyPin, uint8_t ledPin);
196 
207  MeCompass(uint8_t keyPin, uint8_t ledPin, uint8_t address);
208 #endif // ME_PORT_DEFINED
209 
225  void setpin(uint8_t keyPin, uint8_t ledPin);
226 
241  void begin(void);
242 
257  bool testConnection(void);
258 
273  double getAngle(void);
274 
289  int16_t getHeadingX(void);
290 
305  int16_t getHeadingY(void);
306 
321  int16_t getHeadingZ(void);
322 
341  int16_t getHeading(int16_t *x, int16_t *y, int16_t *z);
342 private:
343  static volatile uint8_t _keyPin;
344  static volatile uint8_t _ledPin;
345  bool Calibration_Flag;
346  uint8_t buffer[6];
347  uint8_t Device_Address;
348  uint8_t Measurement_Mode;
349  struct Compass_Calibration_Parameter Cal_parameter;
350 
374  int8_t writeReg(int16_t reg, uint8_t data);
375 
401  int8_t writeData(uint8_t start, const uint8_t *pData, uint8_t size);
402 
428  int8_t readData(uint8_t start, uint8_t *buffer, uint8_t size);
429 
446  void deviceCalibration(void);
447 
463  void read_EEPROM_Buffer(void);
464 
480  void write_EEPROM_Buffer(struct Compass_Calibration_Parameter *parameter_pointer);
481 };
482 #endif // MECOMPASS_H
double getAngle(void)
Definition: MeCompass.cpp:231
Definition: MeCompass.h:136
float Z_excursion
Definition: MeCompass.h:140
Configuration file of library.
Port Mapping for RJ25.
Definition: MePort.h:118
float Z_gain
Definition: MeCompass.h:143
int16_t getHeadingZ(void)
Definition: MeCompass.cpp:351
int16_t getHeading(int16_t *x, int16_t *y, int16_t *z)
Definition: MeCompass.cpp:385
void setpin(uint8_t keyPin, uint8_t ledPin)
Definition: MeCompass.cpp:146
void begin(void)
Definition: MeCompass.cpp:171
float X_gain
Definition: MeCompass.h:141
uint8_t verify_flag
Definition: MeCompass.h:146
uint8_t Rotation_Axis
Definition: MeCompass.h:144
MeCompass()
Definition: MeCompass.cpp:62
Driver for MeCompass module.
Definition: MeCompass.h:157
float Y_excursion
Definition: MeCompass.h:139
float Y_gain
Definition: MeCompass.h:142
float X_excursion
Definition: MeCompass.h:138
bool testConnection(void)
Definition: MeCompass.cpp:208
Header for MePort.cpp module.
int16_t getHeadingY(void)
Definition: MeCompass.cpp:321
int16_t getHeadingX(void)
Definition: MeCompass.cpp:291