5 #include <linux/i2c-dev.h>
11 #include <libraries/math_neon/math_neon.h>
16 #define GYR_X_LSB 0x34
17 #define GYR_X_MSB 0x33
18 #define GYR_Y_LSB 0x36
19 #define GYR_Y_MSB 0x35
20 #define GYR_Z_LSB 0x38
21 #define GYR_Z_MSB 0x37
23 #define ACC_X_LSB 0x2E
24 #define ACC_X_MSB 0x2D
25 #define ACC_Y_LSB 0x30
26 #define ACC_Y_MSB 0x2F
27 #define ACC_Z_LSB 0x32
28 #define ACC_Z_MSB 0x31
33 readFullSensorState();
46 static inline float vectorLength(
const float a,
const float b,
const float c) {
47 return sqrtf_neon(((a*a)+(b*b)+(c*c))/3.0f);
54 snprintf(namebuf,
sizeof(namebuf),
"/dev/i2c-%d", I2CBus);
58 if ((file = open(namebuf, O_RDWR)) < 0){
59 cerr <<
"Failed to open ICM20948 Sensor on " << namebuf <<
" I2C Bus" << endl;
63 if (ioctl(file, I2C_SLAVE, I2CAddress) < 0){
64 cerr <<
"I2C_SLAVE address " << I2CAddress <<
" failed..." << endl;
72 char buf[1] = { 0x00 };
73 if(write(file, buf, 1) !=1){
74 cerr <<
"Failed to Reset Address in readFullSensorState() " << endl;
79 int bytesRead = read(file, this->dataBuffer, numberBytes);
82 cerr <<
"Failure to read Byte Stream in readFullSensorState()" << endl;
87 if (this->dataBuffer[0]!=0xEA){
96 gyroAbs = vectorLength(gyroX, gyroY, gyroZ);
102 accAbs = vectorLength(accX, accY, accZ);
107 float ICM20948::convertGyro(
int msb_reg_addr,
int lsb_reg_addr){
108 short temp = dataBuffer[msb_reg_addr];
109 temp = (temp<<8) | dataBuffer[lsb_reg_addr];
110 return (
float) temp / 32767.0f;
113 float ICM20948::convertAcc(
int msb_reg_addr,
int lsb_reg_addr){
114 short temp = dataBuffer[msb_reg_addr];
115 temp = (temp<<8) | dataBuffer[lsb_reg_addr];
116 const float scale_fac = 32767.0f;
117 return (
float) temp/scale_fac;
121 for(
int i=0; i<iterations; i++){
122 this->readFullSensorState();
128 char temp = 0b00000001;
129 if(this->writeI2CDeviceByte(6, temp)!=0){
130 cerr <<
"Failure to Wake chip" << endl;
134 if(this->writeI2CDeviceByte(5, temp)!=0){
135 cerr <<
"Failure to set Gyro" << endl;
140 char Bank0 = 0b00000000;
141 char Bank2 = 0b00100000;
142 this->writeI2CDeviceByte(127, Bank2);
143 this->readFullSensorState();
146 this->writeI2CDeviceByte(127, Bank0);
150 char Bank0 = 0b00000000;
151 char Bank2 = 0b00100000;
152 this->writeI2CDeviceByte(127, Bank2);
153 this->writeI2CDeviceByte(1, 0b00000111);
154 this->readFullSensorState();
159 this->writeI2CDeviceByte(127, Bank0);
160 this->readFullSensorState();
167 char Bank0 = 0b00000000;
168 char Bank2 = 0b00100000;
169 this->writeI2CDeviceByte(127, Bank2);
170 this->writeI2CDeviceByte(1, 0b00000001);
171 this->readFullSensorState();
176 this->writeI2CDeviceByte(127, Bank0);
177 this->readFullSensorState();
184 char Bank0 = 0b00000000;
185 char Bank2 = 0b00100000;
186 this->writeI2CDeviceByte(127, Bank2);
187 this->writeI2CDeviceByte(6, 0b10000000);
188 this->writeI2CDeviceByte(127, Bank0);
189 this->readFullSensorState();
194 int ICM20948::writeI2CDeviceByte(
char address,
char value){
198 snprintf(namebuf,
sizeof(namebuf),
"/dev/i2c-%d", I2CBus);
200 if ((file = open(namebuf, O_RDWR)) < 0){
201 cerr <<
"Failed to open ICM20948 Sensor on " << namebuf <<
" I2C Bus" << endl;
204 if (ioctl(file, I2C_SLAVE, I2CAddress) < 0){
205 cerr <<
"I2C_SLAVE address " << I2CAddress <<
" failed..." << endl;
212 if ( write(file, buffer, 2) != 2) {
213 cerr <<
"Failure to write values to I2C Device address." << endl;