From ce5f00a8480034da141a42cc61788de67663252a Mon Sep 17 00:00:00 2001 From: hhau Date: Sat, 16 Dec 2023 00:16:09 +0100 Subject: [PATCH 1/5] chg: dev: specified interface for the imu --- src/motionDetection/MotionDetection.h | 96 ++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/src/motionDetection/MotionDetection.h b/src/motionDetection/MotionDetection.h index c01d0bb..d275d8e 100644 --- a/src/motionDetection/MotionDetection.h +++ b/src/motionDetection/MotionDetection.h @@ -1,6 +1,100 @@ +/** + * @file MotionDetection.h + * @author Hans Haupt + * @brief This component controls the IMU (Accelerometer & Gyroscope) ICM-42670-P + * @version 0.1 + * @date 2023-12-15 + * + * @copyright Copyright (c) 2023 + * + */ #ifndef MotionDetection_h #define MotionDetection_h -class MotionDetection{ +#include "driver/spi_master.h" +struct IMUResult{ + float x; + float y; + float z; +} + + +class MotionDetection{ +protected: + static const uint8_t MCU_HOST = SPI2_HOST; + static const uint8_t PIN_NUM_MISO = 37; + static const uint8_t PIN_NUM_MOSI = 35; + static const uint8_t PIN_NUM_CLK = 36; + static const uint8_t PIN_NUM_CS = 34; + + static const uint8_t PIN_INT0 = 28; + + + static const uint8_t CMD_READ = 0x80; + static const uint8_t CMD_WRITE = 0x00; + static const uint8_t ADDR_MASK = 0x7F; + + //Registers + static const uint8_t REG_TEMP_LOW = 0x0A; + static const uint8_t REG_TEMP_HIGH = 0X09; + + static const uint8_t ACCEL_DATA_X_HIGH = 0x0B; + static const uint8_t ACCEL_DATA_X_LOW = 0x0C; + static const uint8_t ACCEL_DATA_Y_HIGH = 0x0D; + static const uint8_t ACCEL_DATA_Y_LOW = 0x0E; + static const uint8_t ACCEL_DATA_Z_HIGH = 0x0F; + static const uint8_t ACCEL_DATA_Z_LOW = 0x10; + + static const uint8_t GYRO_DATA_X_HIGH = 0x11; + static const uint8_t GYRO_DATA_X_LOW = 0x12; + static const uint8_t GYRO_DATA_Y_HIGH = 0x13; + static const uint8_t GYRO_DATA_Y_LOW = 0x14; + static const uint8_t GYRO_DATA_Z_HIGH = 0x15; + static const uint8_t GYRO_DATA_Z_LOW = 0x16; + + uint16_t imu_read_double_register(uint8_t lowerByteAddress, spi_device_handle_t * spi); + esp_err_t spi_imu_read(spi_device_handle_t * ctx, uint8_t addr, uint8_t* out_data); + esp_err_t spi_imu_write(spi_device_handle_t* ctx, uint8_t addr, uint8_t data); + void cs_high(spi_transaction_t* t); + void cs_low(spi_transaction_t* t); + +public + MotionDetection(); + + /** + * @brief initialized the IMU Component. + * Wakes the IMU from Standby + * Set configuration + * + */ + void begin(void); + + /** + * @brief stops the component + * Sets the IMU to Low-Power-Mode + * + */ + void end(void); + + /** + * @brief Triggers a new Reading of the accelerationvalues and reads them from the IMU + * + * @return IMUResult that contains the new read values + */ + IMUResult getAcceleration(); + + /** + * @brief Triggers a new reading of the gyroscope and reads the values from the imu + * + * @return IMUResult + */ + IMUResult getRotation(); + + /** + * @brief Reads the current On Chip temperature of the IMU + * + * @return normalized temperature in degree Centigrade + */ + float getTemperature(); }; #endif //MotionDetection \ No newline at end of file From 21f8dc65f1af9809e680d5a5a63a78622ae8f600 Mon Sep 17 00:00:00 2001 From: hhau Date: Thu, 21 Dec 2023 09:35:32 +0100 Subject: [PATCH 2/5] implemented whoAmI for IMU --- example/example.ino | 29 ++++---- src/Dezibot.cpp | 2 +- src/motionDetection/MotionDetection.cpp | 92 +++++++++++++++++++++++++ src/motionDetection/MotionDetection.h | 43 ++++++------ 4 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 src/motionDetection/MotionDetection.cpp diff --git a/example/example.ino b/example/example.ino index f055251..8e2ea8b 100644 --- a/example/example.ino +++ b/example/example.ino @@ -1,18 +1,21 @@ -#include - -Dezibot dezibot = Dezibot(); -const uint8_t MYFOO = 10; +#include "Dezibot.h" +Dezibot dezibot; void setup() { - dezibot.begin(); - + + dezibot.begin(); + dezibot.motionDetection.begin(); + //dezibot.motionDetection.end(); + // put your setup code here, to run once: + Serial.begin(115200); + } void loop() { -dezibot.multiColorLight.setLed(TOP_LEFT,0x000000FF); -dezibot.multiColorLight.setLed(TOP_RIGHT,dezibot.multiColorLight.color(0,100,0)); -dezibot.multiColorLight.blink(10,0x00FF0000,BOTTOM,500); - -delay(1000); -dezibot.multiColorLight.turnOff(ALL); -delay(1000); + // put your main code here, to run repeatedly: + +//Serial.println(dezibot.motionDetection.getTemperature()); +//Serial.println(dezibot.motionDetection.getAcceleration().z); +//Serial.println(dezibot.motionDetection.getRotation().x); +Serial.println(dezibot.motionDetection.getWhoAmI()); +delay(5); } diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index dc1dcb7..cf2084e 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -12,7 +12,7 @@ #include "Dezibot.h" -Dezibot::Dezibot():multiColorLight(){ +Dezibot::Dezibot():multiColorLight(),motionDetection(){ }; void Dezibot::begin(void) { diff --git a/src/motionDetection/MotionDetection.cpp b/src/motionDetection/MotionDetection.cpp new file mode 100644 index 0000000..c0e2db1 --- /dev/null +++ b/src/motionDetection/MotionDetection.cpp @@ -0,0 +1,92 @@ +#include "MotionDetection.h" + +MotionDetection::MotionDetection(){//:handler(FSPI){ + handler = new SPIClass(FSPI); +}; + +void MotionDetection::begin(void){ + pinMode(34,OUTPUT); + digitalWrite(34,HIGH); + handler->begin(36,37,35,34); + + + //handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + + digitalWrite(34,LOW); + // set Accel and Gyroscop to Low Noise + //handler->transfer(0x1F); + //handler->transfer(0x0F); + //busy Wait for startup + //delayMicroseconds(200); + handler->transfer(cmdRead(WHO_AM_I)); + + digitalWrite(34,HIGH); + //handler->endTransaction();*/ +}; +void MotionDetection::end(void){ + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + digitalWrite(34,LOW); + handler->transfer(cmdWrite(PWR_MGMT0)); + //turn Accel and Gyroscope off + handler->transfer(0x00); + digitalWrite(34,HIGH); + handler->end(); +}; +IMUResult MotionDetection::getAcceleration(){ + IMUResult result; + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + digitalWrite(34,LOW); + result.x = handler->transfer16(cmdRead(ACCEL_DATA_X_HIGH,ACCEL_DATA_X_LOW)); + result.y = handler->transfer16(cmdRead(ACCEL_DATA_Y_HIGH,ACCEL_DATA_Y_LOW)); + result.z = handler->transfer16(cmdRead(ACCEL_DATA_Z_HIGH,ACCEL_DATA_Z_LOW)); + digitalWrite(34,HIGH); + handler->endTransaction(); + return result; +}; +IMUResult MotionDetection::getRotation(){ + IMUResult result; + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + digitalWrite(34,LOW); + result.x = handler->transfer16(cmdRead(GYRO_DATA_X_HIGH,GYRO_DATA_X_LOW)); + result.y = handler->transfer16(cmdRead(GYRO_DATA_Y_HIGH,GYRO_DATA_Y_LOW)); + result.z = handler->transfer16(cmdRead(GYRO_DATA_Z_HIGH,GYRO_DATA_Z_LOW)); + digitalWrite(34,HIGH); + handler->endTransaction(); + return result; +}; +float MotionDetection::getTemperature(){ + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + digitalWrite(34,LOW); + handler -> write(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); + uint16_t raw_temperatur = handler->transfer16(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); + + digitalWrite(34,HIGH); + handler->endTransaction(); + return raw_temperatur/128 +25; +}; + +uint8_t MotionDetection::getWhoAmI(){ + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + digitalWrite(34,LOW); + uint8_t result; + //handler->transferBytes(cmd,result,1); + result = handler->transfer(cmdRead(WHO_AM_I)); + result = handler->transfer(0x00); + digitalWrite(34,HIGH); + handler->endTransaction(); + return result; + +}; + +uint16_t MotionDetection::cmdRead(uint8_t regHigh,uint8_t regLow){ + return (CMD_READ | (regHigh & ADDR_MASK))<<8 & (CMD_READ | regLow & ADDR_MASK); +}; +uint16_t MotionDetection::cmdWrite(uint8_t regHigh,uint8_t regLow){ + return (CMD_WRITE | (regHigh & ADDR_MASK))<<8 & (CMD_WRITE | regLow & ADDR_MASK); +}; +uint8_t MotionDetection::cmdRead(uint8_t reg){ + return (CMD_READ | (reg & ADDR_MASK)); +}; +uint8_t MotionDetection::cmdWrite(uint8_t reg){ + return (CMD_WRITE | (reg & ADDR_MASK)); +}; \ No newline at end of file diff --git a/src/motionDetection/MotionDetection.h b/src/motionDetection/MotionDetection.h index d275d8e..e0c5a7d 100644 --- a/src/motionDetection/MotionDetection.h +++ b/src/motionDetection/MotionDetection.h @@ -10,26 +10,18 @@ */ #ifndef MotionDetection_h #define MotionDetection_h -#include "driver/spi_master.h" - +#include +#include struct IMUResult{ - float x; - float y; - float z; -} + int16_t x; + int16_t y; + int16_t z; +}; class MotionDetection{ protected: - static const uint8_t MCU_HOST = SPI2_HOST; - static const uint8_t PIN_NUM_MISO = 37; - static const uint8_t PIN_NUM_MOSI = 35; - static const uint8_t PIN_NUM_CLK = 36; - static const uint8_t PIN_NUM_CS = 34; - - static const uint8_t PIN_INT0 = 28; - - + static const uint8_t CMD_READ = 0x80; static const uint8_t CMD_WRITE = 0x00; static const uint8_t ADDR_MASK = 0x7F; @@ -52,13 +44,20 @@ protected: static const uint8_t GYRO_DATA_Z_HIGH = 0x15; static const uint8_t GYRO_DATA_Z_LOW = 0x16; - uint16_t imu_read_double_register(uint8_t lowerByteAddress, spi_device_handle_t * spi); - esp_err_t spi_imu_read(spi_device_handle_t * ctx, uint8_t addr, uint8_t* out_data); - esp_err_t spi_imu_write(spi_device_handle_t* ctx, uint8_t addr, uint8_t data); - void cs_high(spi_transaction_t* t); - void cs_low(spi_transaction_t* t); + static const uint8_t PWR_MGMT0 = 0x1F; + static const uint8_t WHO_AM_I = 0x75; + + static const uint frequency = 10000000; + + uint16_t cmdRead(uint8_t regHigh,uint8_t regLow); + uint16_t cmdWrite(uint8_t regHigh,uint8_t regLow); + uint8_t cmdRead(uint8_t reg); + uint8_t cmdWrite(uint8_t reg); + + SPIClass * handler = NULL; + -public +public: MotionDetection(); /** @@ -96,5 +95,7 @@ public * @return normalized temperature in degree Centigrade */ float getTemperature(); + + uint8_t getWhoAmI(); }; #endif //MotionDetection \ No newline at end of file From c948b5515bf7cc7f9e84000cd3675999a8d1210f Mon Sep 17 00:00:00 2001 From: hhaupt Date: Fri, 26 Apr 2024 10:11:47 +0200 Subject: [PATCH 3/5] added general readRegister Method for IMU --- example/example.ino | 15 +++++---- src/motionDetection/MotionDetection.cpp | 41 +++++++++++++++---------- src/motionDetection/MotionDetection.h | 11 ++++++- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/example/example.ino b/example/example.ino index 8e2ea8b..bf5898c 100644 --- a/example/example.ino +++ b/example/example.ino @@ -1,21 +1,20 @@ #include "Dezibot.h" Dezibot dezibot; void setup() { - + dezibot.begin(); dezibot.motionDetection.begin(); //dezibot.motionDetection.end(); // put your setup code here, to run once: Serial.begin(115200); - } void loop() { // put your main code here, to run repeatedly: - -//Serial.println(dezibot.motionDetection.getTemperature()); -//Serial.println(dezibot.motionDetection.getAcceleration().z); -//Serial.println(dezibot.motionDetection.getRotation().x); -Serial.println(dezibot.motionDetection.getWhoAmI()); -delay(5); + + //Serial.println(dezibot.motionDetection.getTemperature()); + Serial.println(dezibot.motionDetection.getAcceleration().z); + //Serial.println(dezibot.motionDetection.getRotation().x); + Serial.println(dezibot.motionDetection.getWhoAmI()); + delay(5000); } diff --git a/src/motionDetection/MotionDetection.cpp b/src/motionDetection/MotionDetection.cpp index c0e2db1..e2c2ca3 100644 --- a/src/motionDetection/MotionDetection.cpp +++ b/src/motionDetection/MotionDetection.cpp @@ -14,11 +14,10 @@ void MotionDetection::begin(void){ digitalWrite(34,LOW); // set Accel and Gyroscop to Low Noise - //handler->transfer(0x1F); - //handler->transfer(0x0F); + handler->transfer(0x1F); + handler->transfer(0x0F); //busy Wait for startup - //delayMicroseconds(200); - handler->transfer(cmdRead(WHO_AM_I)); + delayMicroseconds(200); digitalWrite(34,HIGH); //handler->endTransaction();*/ @@ -58,26 +57,19 @@ float MotionDetection::getTemperature(){ handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); digitalWrite(34,LOW); handler -> write(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); - uint16_t raw_temperatur = handler->transfer16(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); + int16_t raw_temperatur = handler->transfer16(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); digitalWrite(34,HIGH); handler->endTransaction(); return raw_temperatur/128 +25; }; -uint8_t MotionDetection::getWhoAmI(){ - handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); - digitalWrite(34,LOW); - uint8_t result; - //handler->transferBytes(cmd,result,1); - result = handler->transfer(cmdRead(WHO_AM_I)); - result = handler->transfer(0x00); - digitalWrite(34,HIGH); - handler->endTransaction(); - return result; - +int8_t MotionDetection::getWhoAmI(){ + return readRegister(WHO_AM_I); }; + + uint16_t MotionDetection::cmdRead(uint8_t regHigh,uint8_t regLow){ return (CMD_READ | (regHigh & ADDR_MASK))<<8 & (CMD_READ | regLow & ADDR_MASK); }; @@ -89,4 +81,19 @@ uint8_t MotionDetection::cmdRead(uint8_t reg){ }; uint8_t MotionDetection::cmdWrite(uint8_t reg){ return (CMD_WRITE | (reg & ADDR_MASK)); -}; \ No newline at end of file +}; + +int8_t MotionDetection::readRegister(uint8_t reg){ + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + digitalWrite(34,LOW); + uint8_t result; + result = handler->transfer(cmdRead(reg)); + result = handler->transfer(0x00); + digitalWrite(34,HIGH); + handler->endTransaction(); + return result; +}; + +int16_t MotionDetection::readDoubleRegister(uint8_t lowerReg){ + return 0; +} \ No newline at end of file diff --git a/src/motionDetection/MotionDetection.h b/src/motionDetection/MotionDetection.h index e0c5a7d..6d9bd5a 100644 --- a/src/motionDetection/MotionDetection.h +++ b/src/motionDetection/MotionDetection.h @@ -54,6 +54,9 @@ protected: uint8_t cmdRead(uint8_t reg); uint8_t cmdWrite(uint8_t reg); + int8_t readRegister(uint8_t reg); + int16_t readDoubleRegister(uint8_t lowerReg); + SPIClass * handler = NULL; @@ -96,6 +99,12 @@ public: */ float getTemperature(); - uint8_t getWhoAmI(); + /** + * @brief Returns the value of reading the whoAmI register + * When IMU working correctly, value should be 0x67 + * + * @return the value of the whoami register of the ICM-42670 + */ + int8_t getWhoAmI(); }; #endif //MotionDetection \ No newline at end of file From e42a471a33ca04f78cf5352b1a845323882ef9e6 Mon Sep 17 00:00:00 2001 From: Hans Haupt Date: Fri, 3 May 2024 09:29:53 +0200 Subject: [PATCH 4/5] wip:reading acceleration --- src/Dezibot.cpp | 1 + src/motionDetection/MotionDetection.cpp | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Dezibot.cpp b/src/Dezibot.cpp index cf2084e..273a5a2 100644 --- a/src/Dezibot.cpp +++ b/src/Dezibot.cpp @@ -17,4 +17,5 @@ Dezibot::Dezibot():multiColorLight(),motionDetection(){ }; void Dezibot::begin(void) { multiColorLight.begin(); + motionDetection.begin(); }; diff --git a/src/motionDetection/MotionDetection.cpp b/src/motionDetection/MotionDetection.cpp index e2c2ca3..5e648ec 100644 --- a/src/motionDetection/MotionDetection.cpp +++ b/src/motionDetection/MotionDetection.cpp @@ -8,9 +8,8 @@ void MotionDetection::begin(void){ pinMode(34,OUTPUT); digitalWrite(34,HIGH); handler->begin(36,37,35,34); - - //handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); digitalWrite(34,LOW); // set Accel and Gyroscop to Low Noise @@ -20,7 +19,7 @@ void MotionDetection::begin(void){ delayMicroseconds(200); digitalWrite(34,HIGH); - //handler->endTransaction();*/ + handler->endTransaction(); }; void MotionDetection::end(void){ handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); @@ -35,9 +34,12 @@ IMUResult MotionDetection::getAcceleration(){ IMUResult result; handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); digitalWrite(34,LOW); - result.x = handler->transfer16(cmdRead(ACCEL_DATA_X_HIGH,ACCEL_DATA_X_LOW)); - result.y = handler->transfer16(cmdRead(ACCEL_DATA_Y_HIGH,ACCEL_DATA_Y_LOW)); - result.z = handler->transfer16(cmdRead(ACCEL_DATA_Z_HIGH,ACCEL_DATA_Z_LOW)); + result.x = readRegister(ACCEL_DATA_X_HIGH)<<8; + result.x |= readRegister(ACCEL_DATA_X_LOW); + result.y = readRegister(ACCEL_DATA_Y_HIGH)<<8; + result.y |= readRegister(ACCEL_DATA_Y_LOW); + result.z = readRegister(ACCEL_DATA_Z_HIGH)<<8; + result.z |= readRegister(ACCEL_DATA_Z_LOW); digitalWrite(34,HIGH); handler->endTransaction(); return result; From 650250f9de53b8cd666ce50582fe2a641914d411 Mon Sep 17 00:00:00 2001 From: Hans Haupt Date: Tue, 7 May 2024 22:01:37 +0200 Subject: [PATCH 5/5] implemented read functions for Gyro / Accelerometer --- .../Upside_Downside_Detection.ino | 27 +++++++++ .../Led/ColorCycle/ColorCycle/ColorCycle.ino | 26 ++++++++ src/motionDetection/MotionDetection.cpp | 60 ++++++------------- 3 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 example/IMU/Upside_Downside_Detection/Upside_Downside_Detection.ino create mode 100644 example/Led/ColorCycle/ColorCycle/ColorCycle.ino diff --git a/example/IMU/Upside_Downside_Detection/Upside_Downside_Detection.ino b/example/IMU/Upside_Downside_Detection/Upside_Downside_Detection.ino new file mode 100644 index 0000000..e1aa2aa --- /dev/null +++ b/example/IMU/Upside_Downside_Detection/Upside_Downside_Detection.ino @@ -0,0 +1,27 @@ +#include "Dezibot.h" +Dezibot dezibot = Dezibot(); +void setup() { + // put your setup code here, to run once: + dezibot.begin(); + Serial.begin(115200); +} + +int indices = 0; +void loop() { + // put your main code here, to run repeatedly: + + + int zvalue = 0; + for(int i = 0; i<30;i++){ + zvalue += dezibot.motionDetection.getAcceleration().z; + } + zvalue = zvalue/30; + if(zvalue < -1700){ + dezibot.multiColorLight.setLed(ALL,0x00FF00); + } else if(zvalue > 1700){ + dezibot.multiColorLight.setLed(ALL,0xFF0000); + } else { + dezibot.multiColorLight.turnOffLed(); + } + } + diff --git a/example/Led/ColorCycle/ColorCycle/ColorCycle.ino b/example/Led/ColorCycle/ColorCycle/ColorCycle.ino new file mode 100644 index 0000000..28fe930 --- /dev/null +++ b/example/Led/ColorCycle/ColorCycle/ColorCycle.ino @@ -0,0 +1,26 @@ +#include "Dezibot.h" + + +Dezibot dezibot = Dezibot(); +void setup() { + // put your setup code here, to run once: +dezibot.begin(); +} + +void loop() { + // put your main code here, to run repeatedly: + for (int d = 0; d < 255; d++) { + dezibot.multiColorLight.setLed(ALL,dezibot.multiColorLight.color(d,0,255-d)); + delay(2); + } + + for (int d = 0; d < 255; d++) { + dezibot.multiColorLight.setLed(ALL, dezibot.multiColorLight.color(255-d, d, 0)); + delay(2); + } + + for (int d = 0; d < 255; d++) { + dezibot.multiColorLight.setLed(ALL, dezibot.multiColorLight.color(0, 255-d, d)); + delay(2); + } +} diff --git a/src/motionDetection/MotionDetection.cpp b/src/motionDetection/MotionDetection.cpp index 5e648ec..b675e28 100644 --- a/src/motionDetection/MotionDetection.cpp +++ b/src/motionDetection/MotionDetection.cpp @@ -5,19 +5,17 @@ MotionDetection::MotionDetection(){//:handler(FSPI){ }; void MotionDetection::begin(void){ - pinMode(34,OUTPUT); - digitalWrite(34,HIGH); - handler->begin(36,37,35,34); - - handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); + pinMode(34,OUTPUT); + digitalWrite(34,HIGH); + handler->begin(36,37,35,34); + handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); digitalWrite(34,LOW); - // set Accel and Gyroscop to Low Noise - handler->transfer(0x1F); - handler->transfer(0x0F); - //busy Wait for startup - delayMicroseconds(200); - + // set Accel and Gyroscop to Low Noise + handler->transfer(0x1F); + handler->transfer(0x0F); + //busy Wait for startup + delayMicroseconds(200); digitalWrite(34,HIGH); handler->endTransaction(); }; @@ -32,37 +30,27 @@ void MotionDetection::end(void){ }; IMUResult MotionDetection::getAcceleration(){ IMUResult result; - handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); - digitalWrite(34,LOW); result.x = readRegister(ACCEL_DATA_X_HIGH)<<8; result.x |= readRegister(ACCEL_DATA_X_LOW); result.y = readRegister(ACCEL_DATA_Y_HIGH)<<8; result.y |= readRegister(ACCEL_DATA_Y_LOW); result.z = readRegister(ACCEL_DATA_Z_HIGH)<<8; result.z |= readRegister(ACCEL_DATA_Z_LOW); - digitalWrite(34,HIGH); - handler->endTransaction(); return result; }; IMUResult MotionDetection::getRotation(){ IMUResult result; - handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); - digitalWrite(34,LOW); - result.x = handler->transfer16(cmdRead(GYRO_DATA_X_HIGH,GYRO_DATA_X_LOW)); - result.y = handler->transfer16(cmdRead(GYRO_DATA_Y_HIGH,GYRO_DATA_Y_LOW)); - result.z = handler->transfer16(cmdRead(GYRO_DATA_Z_HIGH,GYRO_DATA_Z_LOW)); - digitalWrite(34,HIGH); - handler->endTransaction(); + result.x = readRegister(GYRO_DATA_X_HIGH) <<8; + result.x |= readRegister(GYRO_DATA_X_LOW); + result.y = readRegister(GYRO_DATA_Y_HIGH)<<8; + result.y |= readRegister(GYRO_DATA_Y_LOW); + result.z = readRegister(GYRO_DATA_Z_HIGH)<<8; + result.z |= readRegister(GYRO_DATA_Z_LOW); return result; }; float MotionDetection::getTemperature(){ - handler->beginTransaction(SPISettings(frequency,SPI_MSBFIRST,SPI_MODE0)); - digitalWrite(34,LOW); - handler -> write(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); - int16_t raw_temperatur = handler->transfer16(cmdRead(REG_TEMP_HIGH,REG_TEMP_LOW)); - - digitalWrite(34,HIGH); - handler->endTransaction(); + int16_t raw_temperatur = readRegister(REG_TEMP_HIGH)<<8; + raw_temperatur |= readRegister(REG_TEMP_LOW); return raw_temperatur/128 +25; }; @@ -70,14 +58,6 @@ int8_t MotionDetection::getWhoAmI(){ return readRegister(WHO_AM_I); }; - - -uint16_t MotionDetection::cmdRead(uint8_t regHigh,uint8_t regLow){ - return (CMD_READ | (regHigh & ADDR_MASK))<<8 & (CMD_READ | regLow & ADDR_MASK); -}; -uint16_t MotionDetection::cmdWrite(uint8_t regHigh,uint8_t regLow){ - return (CMD_WRITE | (regHigh & ADDR_MASK))<<8 & (CMD_WRITE | regLow & ADDR_MASK); -}; uint8_t MotionDetection::cmdRead(uint8_t reg){ return (CMD_READ | (reg & ADDR_MASK)); }; @@ -94,8 +74,4 @@ int8_t MotionDetection::readRegister(uint8_t reg){ digitalWrite(34,HIGH); handler->endTransaction(); return result; -}; - -int16_t MotionDetection::readDoubleRegister(uint8_t lowerReg){ - return 0; -} \ No newline at end of file +}; \ No newline at end of file