mirror of
https://gitlab.dit.htwk-leipzig.de/phillip.kuehne/dezibot.git
synced 2025-05-19 11:01:46 +02:00
implemented whoAmI for IMU
This commit is contained in:
parent
ce5f00a848
commit
21f8dc65f1
@ -1,18 +1,21 @@
|
||||
#include <Dezibot.h>
|
||||
|
||||
Dezibot dezibot = Dezibot();
|
||||
const uint8_t MYFOO = 10;
|
||||
#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() {
|
||||
dezibot.multiColorLight.setLed(TOP_LEFT,0x000000FF);
|
||||
dezibot.multiColorLight.setLed(TOP_RIGHT,dezibot.multiColorLight.color(0,100,0));
|
||||
dezibot.multiColorLight.blink(10,0x00FF0000,BOTTOM,500);
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
delay(1000);
|
||||
dezibot.multiColorLight.turnOff(ALL);
|
||||
delay(1000);
|
||||
//Serial.println(dezibot.motionDetection.getTemperature());
|
||||
//Serial.println(dezibot.motionDetection.getAcceleration().z);
|
||||
//Serial.println(dezibot.motionDetection.getRotation().x);
|
||||
Serial.println(dezibot.motionDetection.getWhoAmI());
|
||||
delay(5);
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
#include "Dezibot.h"
|
||||
|
||||
Dezibot::Dezibot():multiColorLight(){
|
||||
Dezibot::Dezibot():multiColorLight(),motionDetection(){
|
||||
|
||||
};
|
||||
void Dezibot::begin(void) {
|
||||
|
92
src/motionDetection/MotionDetection.cpp
Normal file
92
src/motionDetection/MotionDetection.cpp
Normal file
@ -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));
|
||||
};
|
@ -10,25 +10,17 @@
|
||||
*/
|
||||
#ifndef MotionDetection_h
|
||||
#define MotionDetection_h
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Arduino.h>
|
||||
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;
|
||||
@ -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;
|
||||
|
||||
public
|
||||
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:
|
||||
MotionDetection();
|
||||
|
||||
/**
|
||||
@ -96,5 +95,7 @@ public
|
||||
* @return normalized temperature in degree Centigrade
|
||||
*/
|
||||
float getTemperature();
|
||||
|
||||
uint8_t getWhoAmI();
|
||||
};
|
||||
#endif //MotionDetection
|
Loading…
x
Reference in New Issue
Block a user