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+
129#include < Wire.h>
230#include " SparkFun_ISM330DHCX.h"
331
4- // 8 bit addressing for Sensor Hub
32+ // 8 bit addressing is required for the 6DoF
33+ // to communicate with its' external sensors.
534#define MAG_ADDR_READ 0x61 // (0x30 << 1) | 1)
635#define MAG_ADDR_WRITE 0x60 // (0x30 << 1)
736
837#define MAG_READ_REG 0x00 // Read from 0x00
938#define MAG_READ_LEN 0x07 // Read seven times consecutively
10- #define MAG_WRITE_REG 0x09 // INT_CTRL0 - register to initiate measurements on Magnetometer
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
1143#define MAG_WRITE_DATA 0x01 // Value to write to INT_CTRL0
1244
1345SparkFun_ISM330DHCX myISM;
1446
47+ // Structs for X,Y,Z data
1548sfe_ism_data_t accelData;
1649sfe_ism_data_t gyroData;
1750
18- // Settings for sensor hub
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.
1954sfe_hub_sensor_settings_t readMMC, writeMMC;
2055
56+ // Magnetometer data fields.
2157uint8_t shRawData[MAG_READ_LEN] = {};
2258unsigned int magXVal;
2359unsigned int magYVal;
@@ -30,10 +66,12 @@ double normalizedZ;
3066void setup (){
3167
3268
69+ // Sensor hub settings for writing to the magnetometer.
3370 writeMMC.address = MAG_ADDR_WRITE;
3471 writeMMC.subAddress = MAG_WRITE_REG;
3572 writeMMC.lenData = MAG_WRITE_DATA;
3673
74+ // Sensor hub settings for reading from the magnetometer.
3775 readMMC.address = MAG_ADDR_READ;
3876 readMMC.subAddress = MAG_READ_REG;
3977 readMMC.lenData = MAG_READ_LEN;
@@ -47,9 +85,12 @@ void setup(){
4785 while (1 );
4886 }
4987
88+ // Reset the device and the sensor hub to default settings.
89+ // This is helpful if you're doing multiple uploads testing different settings.
5090 myISM.deviceReset ();
5191 myISM.resetSensorHub ();
5292
93+ // Wait for it to finish reseting
5394 while ( !myISM.getDeviceReset () ){
5495 delay (1 );
5596 }
@@ -104,12 +145,16 @@ void loop(){
104145 myISM.getAccel (&accelData);
105146 myISM.getGyro (&gyroData);
106147
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).
107150 if ( myISM.getExternalSensorNack (0 ) )
108151 Serial.println (" MMC Nacked..." );
109152
153+ // CHeck if the sensor hub is finished.
110154 if ( myISM.getHubStatus () )
111155 {
112156
157+ // Get the data stored in the 6DoF's registers.
113158 myISM.readPeripheralSensor (shRawData, (uint8_t )MAG_READ_LEN);
114159
115160 // Shift raw data
@@ -146,6 +191,7 @@ void loop(){
146191 myISM.setHubSensorRead (0 , &readMMC);
147192 myISM.enableSensorI2C (true );
148193
194+ // Turn the accelerometer and gyrocope back on.
149195 myISM.setAccelDataRate (ISM_XL_ODR_104Hz);
150196 myISM.setGyroDataRate (ISM_GY_ODR_104Hz);
151197
@@ -197,7 +243,6 @@ bool writeControlBit(sfe_hub_sensor_settings_t toWrite)
197243
198244 // Wait for write to complete
199245 while ( !myISM.getHubStatus () ){
200- // Serial.print(".");
201246 delay (1 );
202247 }
203248
0 commit comments