-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathF_AK8975.hpp
More file actions
123 lines (102 loc) · 3.81 KB
/
F_AK8975.hpp
File metadata and controls
123 lines (102 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once
#ifndef _F_AK8975_H_
#define _F_AK8975_H_
#include "IMUBase.hpp"
/*
AK8975 REGISTERS
*/
//Magnetometer Registers
#define AK8975_ADDRESS 0x0C
#define AK8975_WHO_AM_I 0x00 // should return 0x48
#define AK8975_WHOAMI_DEFAULT_VALUE 0x48
#define AK8975_INFO 0x01
#define AK8975_ST1 0x02 // data ready status bit 0
#define AK8975_XOUT_L 0x03 // data
#define AK8975_XOUT_H 0x04
#define AK8975_YOUT_L 0x05
#define AK8975_YOUT_H 0x06
#define AK8975_ZOUT_L 0x07
#define AK8975_ZOUT_H 0x08
#define AK8975_ST2 0x09 // Data overflow bit 3 and data read error status bit 2
#define AK8975_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0
#define AK8975_ASTC 0x0C // Self test control
#define AK8975_I2CDIS 0x0F // I2C disable
#define AK8975_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value
#define AK8975_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value
#define AK8975_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value
class AK8975 : public IMUBase {
public:
AK8975() {};
// Inherited via IMUBase
int init(calData cal, uint8_t address) override;
void update() override;
void getAccel(AccelData* out) override;
void getGyro(GyroData* out) override;
void getMag(MagData* out) override;
void getQuat(Quaternion* out) override {};
float getTemp() override { return 0.f; };
int setGyroRange(int range) override;
int setAccelRange(int range) override;
int setIMUGeometry(int index) override { geometryIndex = index; return 0; };
void calibrateAccelGyro(calData* cal) override;
void calibrateMag(calData* cal) override;
bool hasMagnetometer() override {
return true;
}
bool hasTemperature() override {
return false;
}
bool hasQuatOutput() override {
return false;
}
String IMUName() override {
return "AK8975";
}
String IMUType() override {
return "AK8975";
}
String IMUManufacturer() override {
return "AsahiKASEI";
}
private:
float mRes = 10. * 1229. / 8196.0; //mres value for full range (1229uT) readings
int geometryIndex = 0;
float temperature = 0.f;
MagData mag = { 0, 0, 0 };
calData calibration;
uint8_t IMUAddress;
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.write(data); // Put data in Tx buffer
Wire.endTransmission(); // Send the Tx buffer
}
uint8_t readByte(uint8_t address, uint8_t subAddress)
{
uint8_t data; // `data` will store the register data
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
Wire.requestFrom(address, (uint8_t)1); // Read one byte from slave register address
data = Wire.read(); // Fill Rx buffer with result
return data; // Return data read from slave register
}
void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t* dest)
{
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
uint8_t i = 0;
Wire.requestFrom(address, count); // Read bytes from slave register address
while (Wire.available()) {
dest[i++] = Wire.read();
} // Put read results in the Rx buffer
}
float factoryMagCal[3] = { 0, 0, 0 };
bool dataAvailable()
{
return (readByte(AK8975_ADDRESS, AK8975_ST1) & 0x01);
}
};
#endif /* _F_AK8975_H_ */