diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..45203bc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "array": "cpp", + "string": "cpp", + "string_view": "cpp" + } +} \ No newline at end of file diff --git a/include/gyro.h b/include/gyro.h new file mode 100644 index 0000000..037fd86 --- /dev/null +++ b/include/gyro.h @@ -0,0 +1,36 @@ +#ifndef __gyro__h__ +#define __gyro__h__ +#include +#include + +#define LSM6S3_WHO_AM_I 0x0F + +#define LSM6DS3_OUTX_L_G 0X22 //Angular rate sensor pitch axis (X) angular rate output register (r). The value is expressed as a 16-bit word in two’s complement +//#define LSM6DS3_OUTY_L_G 0x24 //Angular rate sensor roll axis (Y) angular rate output register (r). The value is expressed as a 16-bit word in two’s complement + +#define LSM6DS3_OUTX_L_XL 0x28 //Linear acceleration sensor X-axis output register (r). The value is expressed as a 16-bit word in two’s complement +#define LSM6DS3_OUT_TEMP_L 0x20 //Temperature data output register (r). L and H registers together express a 16-bit word in two’s complement. + +#define SPI_CS PA4 //Need to use this instead of A3 because A3 is mapped to something else +#define SPI_SCLK D13 // OR D13 +#define SPI_MOSI D11 //OR D11 +#define SPI_MISO D12 //OR D12 + +extern SPIClass SPI_gyro; + +class LSM6DS3 { + public: + void readRegisters(uint8_t address, uint8_t* data, size_t length); + + bool whoAmICheck(); + + void readGyroData(float* x, float* y, float* z); + //void readRollData(float* x, float* y, float* z); + + void readAccelData(float* x, float* y, float* z); + void readTempData(float* t); +}; + + + +#endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 345e039..b60e17d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,3 +13,4 @@ platform = ststm32 board = nucleo_l432kc framework = arduino lib_extra_dirs = ./embedded-pio +lib_deps = adafruit/Adafruit LSM6DS@^4.7.4 diff --git a/src/gyro.cpp b/src/gyro.cpp new file mode 100644 index 0000000..4f7700a --- /dev/null +++ b/src/gyro.cpp @@ -0,0 +1,77 @@ +#include "gyro.h" + +SPIClass SPI_gyro(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS); + + +void LSM6DS3::readRegisters(uint8_t address, uint8_t * data, size_t length){ + + SPI_gyro.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3)); + digitalWrite(SPI_CS, LOW); + SPI_gyro.transfer(address | 0x80); + + for (size_t i = 0; i < length; i++){ + data[i] = SPI_gyro.transfer(0x00); // Send dummy bytes to keep SPI happy + } + + digitalWrite(SPI_CS, HIGH); + SPI_gyro.endTransaction(); +} + +bool LSM6DS3::whoAmICheck(){ + uint8_t data[1] = {0}; + Serial.printf("verified: %x \n", data[0]); + readRegisters(LSM6S3_WHO_AM_I, data, sizeof(data)); + + int verified = data[0]; + Serial.printf("verified: %x \n", verified); + + if (verified == 0b01101010) { + return true; + } else { + return false; + } +} + +void LSM6DS3::readGyroData(float* x, float* y, float* z){ + + int16_t data[3]; + readRegisters(LSM6DS3_OUTX_L_G, (uint8_t*)data, sizeof(data)); + + *x = data[0] * 2000.0 / 32768.0; + *y = data[1] * 2000.0 / 32768.0; + *z = data[2] * 2000.0 / 32768.0; + +} + +/* +void LSM6DS3::readRollData(float* x, float* y, float* z){ + + int16_t data[3]; + readRegisters(LSM6DS3_OUTY_L_G, (uint8_t*)data, sizeof(data)); + + *x = data[0] * 2000.0 / 32768.0; + *y = data[1] * 2000.0 / 32768.0; + *z = data[2] * 2000.0 / 32768.0; + +} +*/ + +void LSM6DS3::readAccelData(float* x, float* y, float* z){ + + int16_t data[3]; + readRegisters(LSM6DS3_OUTX_L_XL, (uint8_t*)data, sizeof(data)); + + *x = data[0] * 4.0 / 32768.0; + *y = data[1] * 4.0 / 32768.0; + *z = data[2] * 4.0 / 32768.0; + +} + +void LSM6DS3::readTempData(float* t){ + + int16_t data[1]; + + readRegisters(LSM6DS3_OUT_TEMP_L, (uint8_t*)data, sizeof(data)); + *t = data[0] / 16.0 + 2; + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cb9fbba..d31eca7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,198 @@ -#include +// Basic demo for accelerometer/gyro readings from Adafruit ISM330DHCX -// put function declarations here: -int myFunction(int, int); +#include -void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); +// For SPI mode, we need a CS pin +#define LSM_CS PA4 +// For software-SPI mode we need SCK/MOSI/MISO pins +#define LSM_SCK D13 +#define LSM_MISO D12 +#define LSM_MOSI D11 + +Adafruit_ISM330DHCX ism330dhcx; +void setup(void) { + Serial.begin(115200); + while (!Serial) + delay(10); // will pause Zero, Leonardo, etc until serial console opens + + Serial.println("Adafruit ISM330DHCX test!"); + + // if (!ism330dhcx.begin_I2C()) { + // if (!ism330dhcx.begin_SPI(LSM_CS)) { + if (!ism330dhcx.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) { + Serial.println("Failed to find ISM330DHCX chip1"); + while (1) { + delay(10); + } + } + + Serial.println("ISM330DHCX Found!"); + + // ism330dhcx.setAccelRange(LSM6DS_ACCEL_RANGE_2_G); + Serial.print("Accelerometer range set to: "); + switch (ism330dhcx.getAccelRange()) { + case LSM6DS_ACCEL_RANGE_2_G: + Serial.println("+-2G"); + break; + case LSM6DS_ACCEL_RANGE_4_G: + Serial.println("+-4G"); + break; + case LSM6DS_ACCEL_RANGE_8_G: + Serial.println("+-8G"); + break; + case LSM6DS_ACCEL_RANGE_16_G: + Serial.println("+-16G"); + break; + } + + // ism330dhcx.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS); + Serial.print("Gyro range set to: "); + switch (ism330dhcx.getGyroRange()) { + case LSM6DS_GYRO_RANGE_125_DPS: + Serial.println("125 degrees/s"); + break; + case LSM6DS_GYRO_RANGE_250_DPS: + Serial.println("250 degrees/s"); + break; + case LSM6DS_GYRO_RANGE_500_DPS: + Serial.println("500 degrees/s"); + break; + case LSM6DS_GYRO_RANGE_1000_DPS: + Serial.println("1000 degrees/s"); + break; + case LSM6DS_GYRO_RANGE_2000_DPS: + Serial.println("2000 degrees/s"); + break; + case ISM330DHCX_GYRO_RANGE_4000_DPS: + Serial.println("4000 degrees/s"); + break; + } + + // ism330dhcx.setAccelDataRate(LSM6DS_RATE_12_5_HZ); + Serial.print("Accelerometer data rate set to: "); + switch (ism330dhcx.getAccelDataRate()) { + case LSM6DS_RATE_SHUTDOWN: + Serial.println("0 Hz"); + break; + case LSM6DS_RATE_12_5_HZ: + Serial.println("12.5 Hz"); + break; + case LSM6DS_RATE_26_HZ: + Serial.println("26 Hz"); + break; + case LSM6DS_RATE_52_HZ: + Serial.println("52 Hz"); + break; + case LSM6DS_RATE_104_HZ: + Serial.println("104 Hz"); + break; + case LSM6DS_RATE_208_HZ: + Serial.println("208 Hz"); + break; + case LSM6DS_RATE_416_HZ: + Serial.println("416 Hz"); + break; + case LSM6DS_RATE_833_HZ: + Serial.println("833 Hz"); + break; + case LSM6DS_RATE_1_66K_HZ: + Serial.println("1.66 KHz"); + break; + case LSM6DS_RATE_3_33K_HZ: + Serial.println("3.33 KHz"); + break; + case LSM6DS_RATE_6_66K_HZ: + Serial.println("6.66 KHz"); + break; + } + + // ism330dhcx.setGyroDataRate(LSM6DS_RATE_12_5_HZ); + Serial.print("Gyro data rate set to: "); + switch (ism330dhcx.getGyroDataRate()) { + case LSM6DS_RATE_SHUTDOWN: + Serial.println("0 Hz"); + break; + case LSM6DS_RATE_12_5_HZ: + Serial.println("12.5 Hz"); + break; + case LSM6DS_RATE_26_HZ: + Serial.println("26 Hz"); + break; + case LSM6DS_RATE_52_HZ: + Serial.println("52 Hz"); + break; + case LSM6DS_RATE_104_HZ: + Serial.println("104 Hz"); + break; + case LSM6DS_RATE_208_HZ: + Serial.println("208 Hz"); + break; + case LSM6DS_RATE_416_HZ: + Serial.println("416 Hz"); + break; + case LSM6DS_RATE_833_HZ: + Serial.println("833 Hz"); + break; + case LSM6DS_RATE_1_66K_HZ: + Serial.println("1.66 KHz"); + break; + case LSM6DS_RATE_3_33K_HZ: + Serial.println("3.33 KHz"); + break; + case LSM6DS_RATE_6_66K_HZ: + Serial.println("6.66 KHz"); + break; + } + + ism330dhcx.configInt1(false, false, true); // accelerometer DRDY on INT1 + ism330dhcx.configInt2(false, true, false); // gyro DRDY on INT2 } void loop() { - // put your main code here, to run repeatedly: -} + // /* Get a new normalized sensor event */ + sensors_event_t accel; + sensors_event_t gyro; + sensors_event_t temp; + ism330dhcx.getEvent(&accel, &gyro, &temp); + + Serial.print("\t\tTemperature "); + Serial.print(temp.temperature); + Serial.println(" deg C"); + + /* Display the results (acceleration is measured in m/s^2) */ + Serial.print("\t\tAccel X: "); + Serial.print(accel.acceleration.x); + Serial.print(" \tY: "); + Serial.print(accel.acceleration.y); + Serial.print(" \tZ: "); + Serial.print(accel.acceleration.z); + Serial.println(" m/s^2 "); + + /* Display the results (rotation is measured in rad/s) */ + Serial.print("\t\tGyro X: "); + Serial.print(gyro.gyro.x); + Serial.print(" \tY: "); + Serial.print(gyro.gyro.y); + Serial.print(" \tZ: "); + Serial.print(gyro.gyro.z); + Serial.println(" radians/s "); + Serial.println(); + + delay(100); + + // // serial plotter friendly format + + // Serial.print(temp.temperature); + // Serial.print(","); + + // Serial.print(accel.acceleration.x); + // Serial.print(","); Serial.print(accel.acceleration.y); + // Serial.print(","); Serial.print(accel.acceleration.z); + // Serial.print(","); -// put function definitions here: -int myFunction(int x, int y) { - return x + y; + // Serial.print(gyro.gyro.x); + // Serial.print(","); Serial.print(gyro.gyro.y); + // Serial.print(","); Serial.print(gyro.gyro.z); + // Serial.println(); + // delayMicroseconds(10000); } \ No newline at end of file