From 650250f9de53b8cd666ce50582fe2a641914d411 Mon Sep 17 00:00:00 2001 From: Hans Haupt Date: Tue, 7 May 2024 22:01:37 +0200 Subject: [PATCH] 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