STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
example3_sensor_hub.ino
Go to the documentation of this file.
1/*
2 example3-sensor_hub
3
4 This example demonstrates the "sensor hub" feature of the ISM330DHCX.
5 The ISM330DHCX acts as a controller for external sensors connected to this
6 alternate bus (SDX/SCX). In this example, the ISM330DHCX is connected to the
7 MMC5983MA Magnetometer. You might notice that we have a 9DoF with these two
8 parts but not in this configuration. The reason is that the magnetometer requires
9 an initiate measurement bit to be flipped before every reading, while this is
10 possible (it's demonstrated below) it's also not ideal for this setup. A more
11 ideal setup would be a sensor that is just turned on and data is pulled
12 periodically.
13
14 Written by Elias Santistevan @ SparkFun Electronics, August 2022
15
16 Product:
17
18 6DoF: https://www.sparkfun.com/products/19764
19 9DoF: https://www.sparkfun.com/products/19895
20
21 Repository:
22
23 https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library
24
25 SparkFun code, firmware, and software is released under the MIT
26 License (http://opensource.org/licenses/MIT).
27*/
28
29#include <Wire.h>
30#include "STAR_ISM330DH.h"
31
32// 8 bit addressing is required for the 6DoF
33// to communicate with its' external sensors.
34#define MAG_ADDR_READ 0x61 // (0x30 << 1) | 1)
35#define MAG_ADDR_WRITE 0x60 // (0x30 << 1)
36
37#define MAG_READ_REG 0x00 // Read from 0x00
38#define MAG_READ_LEN 0x07 // Read seven times consecutively
39
40// INT_CTRL0 (0x09) - contains the bit to initiate measurement.
41// It must be written before each read and is cleared automatically.
42#define MAG_WRITE_REG 0x09
43#define MAG_WRITE_DATA 0x01 // Value to write to INT_CTRL0
44
46
47// Structs for X,Y,Z data
50
51// The settings for the sensor hub have three specific fields.
52// In addition there are different settings for reads and writes as
53// indicated above.
55
56// Magnetometer data fields.
57uint8_t shRawData[MAG_READ_LEN] = {};
58unsigned int magXVal;
59unsigned int magYVal;
60unsigned int magZVal;
61
65
66void setup(){
67
68
69 // Sensor hub settings for writing to the magnetometer.
70 writeMMC.address = MAG_ADDR_WRITE;
71 writeMMC.subAddress = MAG_WRITE_REG;
72 writeMMC.lenData = MAG_WRITE_DATA;
73
74 // Sensor hub settings for reading from the magnetometer.
75 readMMC.address = MAG_ADDR_READ;
76 readMMC.subAddress = MAG_READ_REG;
77 readMMC.lenData = MAG_READ_LEN;
78
79 Wire.begin();
80
81 Serial.begin(115200);
82
83 if( !myISM.begin() ){
84 Serial.println("Did not begin.");
85 while(1);
86 }
87
88 // Reset the device and the sensor hub to default settings.
89 // This is helpful if you're doing multiple uploads testing different settings.
90 myISM.deviceReset();
91 myISM.resetSensorHub();
92
93 // Wait for it to finish reseting
94 while( !myISM.getDeviceReset() ){
95 delay(1);
96 }
97
98 Serial.println("Reset.");
99 Serial.println("Applying settings.");
100 delay(100);
101
102 myISM.setDeviceConfig();
103 myISM.setBlockDataUpdate();
104
105
106 // Set the number of peripheral sensor to be read by the 6DoF
107 myISM.setNumberHubSensors(0);
108 // Enable pullup resistors on SDX/SCX
109 myISM.setHubPullUps();
110 // Output data rate for the external sensor
111 myISM.setHubODR(ISM_SH_ODR_104Hz);
112
113
114 // Send control bit to Magnetometer
116
117 // Set read settings for external sensor "zero" (0-3)
118 myISM.setHubSensorRead(0, &readMMC);
119
120 // Enable the 6DoF as a controller I2C
121 // All configurations to the sensor hub must occur while the controller I2C
122 // bus is powered down.
123 myISM.enableSensorI2C(true);
124
125 // Apply acceleromter settings
126 myISM.setAccelFullScale(ISM_4g);
127 myISM.setAccelFilterLP2();
128 myISM.setAccelSlopeFilter(ISM_LP_ODR_DIV_100);
129
130 // Apply gyroscope settings
131 myISM.setGyroFullScale(ISM_500dps);
132 myISM.setGyroFilterLP1();
133 myISM.setGyroLP1Bandwidth(ISM_MEDIUM);
134
135 // Enable Sensors
136 myISM.setAccelDataRate(ISM_XL_ODR_104Hz);
137 myISM.setGyroDataRate(ISM_GY_ODR_104Hz);
138
139}
140
141void loop(){
142
143 if( myISM.checkStatus() ){
144
145 myISM.getAccel(&accelData);
146 myISM.getGyro(&gyroData);
147
148 // If you've given the 6DoF the wrong address for the external sensor, this
149 // bit will tell you. The zero argument is the external sensor to check (0-3).
150 if( myISM.getExternalSensorNack(0) )
151 Serial.println("MMC Nacked...");
152
153 // CHeck if the sensor hub is finished.
154 if( myISM.getHubStatus() )
155 {
156
157 // Get the data stored in the 6DoF's registers.
158 myISM.readPeripheralSensor(shRawData, (uint8_t)MAG_READ_LEN);
159
160 // Shift raw data
161 magXVal = (((uint32_t)shRawData[0]) << 10) | (((uint32_t)shRawData[1]) << 2) | ((((uint32_t)shRawData[6]) >> 6) & 0x03);
162 magYVal = (((uint32_t)shRawData[2]) << 10) | (((uint32_t)shRawData [3]) << 2) | ((((uint32_t)shRawData[6]) >> 4) & 0x03);
163 magZVal = (((uint32_t)shRawData[4]) << 10) | (((uint32_t)shRawData [5]) << 2) | ((((uint32_t)shRawData[6]) >> 2) & 0x03);
164
165 // Convert raw data
166 normalizedX = (double)magXVal - 131072.0;
167 normalizedX = (normalizedX/131072.0) * 8;
168
169 normalizedY = (double)magYVal - 131072.0;
170 normalizedY = (normalizedY/131072.0) * 8;
171
172 normalizedZ = (double)magZVal - 131072.0;
173 normalizedZ = (normalizedZ/131072.0) * 8;
174
175 // Z axis between magnetometer and acclerometer are opposite.
176 normalizedZ = normalizedZ * (-1);
177
178 Serial.print("Magnetometer: ");
179 Serial.print("X: ");
180 Serial.print(normalizedX);
181 Serial.print(" ");
182 Serial.print("Y: ");
183 Serial.print(normalizedY);
184 Serial.print(" ");
185 Serial.print("Z: ");
186 Serial.print(normalizedZ);
187 Serial.println("\n");
188
189 // Send another bit to take more measurements
191 myISM.setHubSensorRead(0, &readMMC);
192 myISM.enableSensorI2C(true);
193
194 // Turn the accelerometer and gyrocope back on.
195 myISM.setAccelDataRate(ISM_XL_ODR_104Hz);
196 myISM.setGyroDataRate(ISM_GY_ODR_104Hz);
197
198 }
199
200
201 Serial.print("Accelerometer: ");
202 Serial.print("X: ");
203 Serial.print(accelData.xData);
204 Serial.print(" ");
205 Serial.print("Y: ");
206 Serial.print(accelData.yData);
207 Serial.print(" ");
208 Serial.print("Z: ");
209 Serial.print(accelData.zData);
210 Serial.println(" ");
211 Serial.print("Gyroscope: ");
212 Serial.print("X: ");
213 Serial.print(gyroData.xData);
214 Serial.print(" ");
215 Serial.print("Y: ");
216 Serial.print(gyroData.yData);
217 Serial.print(" ");
218 Serial.print("Z: ");
219 Serial.print(gyroData.zData);
220 Serial.println(" ");
221 }
222
223 delay(100);
224}
225
226
228{
229
230 // Sensors must be off for sensor hub configuration.
231 myISM.setAccelDataRate(ISM_XL_ODR_OFF);
232 myISM.setGyroDataRate(ISM_GY_ODR_OFF);
233 myISM.enableSensorI2C(false);
234
235 // 300us until 6DoF I2C is powered down
237
238 // Configure a write
239 myISM.setHubSensorWrite(&toWrite);
240 // Re-enable sensor for write
241 myISM.setAccelDataRate(ISM_XL_ODR_104Hz);
242 myISM.enableSensorI2C(true);
243
244 // Wait for write to complete
245 while( !myISM.getHubStatus() ){
246 delay(1);
247 }
248
249
250 // Turn off sensor
251 myISM.enableSensorI2C(false);
253
254 return true;
255
256}
static SerialStub Serial
Definition Arduino.h:128
void delayMicroseconds(unsigned int)
Definition Arduino.h:58
void delay(int ms)
STAR_ISM330DH — improved fork of SparkFun's SparkFun_6DoF_ISM330DHCX_Arduino_Library.
ISM330DH myISM
#define MAG_WRITE_REG
#define MAG_WRITE_DATA
unsigned int magXVal
#define MAG_ADDR_WRITE
#define MAG_READ_REG
double normalizedX
unsigned int magYVal
unsigned int magZVal
void setup()
bool writeControlBit(sfe_hub_sensor_settings_t toWrite)
#define MAG_ADDR_READ
uint8_t shRawData[MAG_READ_LEN]
sfe_hub_sensor_settings_t writeMMC
double normalizedY
#define MAG_READ_LEN
double normalizedZ
sfe_hub_sensor_settings_t readMMC
void loop()
static sfe_ism_data_t gyroData
Definition main.cpp:10
static sfe_ism_data_t accelData
Definition main.cpp:9
#define ISM_GY_ODR_104Hz
#define ISM_XL_ODR_OFF
#define ISM_MEDIUM
#define ISM_LP_ODR_DIV_100
#define ISM_4g
#define ISM_500dps
#define ISM_SH_ODR_104Hz
#define ISM_GY_ODR_OFF
#define ISM_XL_ODR_104Hz