Dezibot 4
MotionDetection.h
Go to the documentation of this file.
1 
11 #ifndef MotionDetection_h
12 #define MotionDetection_h
13 #include <SPI.h>
14 #include <Arduino.h>
15 #include "IMU_CMDs.h"
16 struct IMUResult{
17  int16_t x;
18  int16_t y;
19  int16_t z;
20 };
21 enum Axis{
22  xAxis = 0x01,
23  yAxis = 0x02,
24  zAxis = 0x04
25 };
26 
27 struct Orientation{
28  int xRotation;
29  int yRotation;
30 };
31 
32 enum Direction{
40 };
41 
42 struct FIFO_Package{
43  int8_t header;
46  int16_t temperature;
47  int16_t timestamp;
48 };
49 
50 
52 protected:
54  static const uint frequency = 24000000;
55  static const uint16_t defaultShakeThreshold = 500;
56  const uint bufferLength = 64*16;
57  int8_t* buf = new int8_t[bufferLength];
58  uint8_t readFromRegisterBank(registerBank bank,uint8_t reg);
59  void writeToRegisterBank(registerBank bank, uint8_t reg, uint8_t value);
61 
62  uint16_t cmdRead(uint8_t regHigh,uint8_t regLow);
63  uint16_t cmdWrite(uint8_t regHigh,uint8_t regLow);
64  uint8_t cmdRead(uint8_t reg);
65  uint8_t cmdWrite(uint8_t reg);
66 
67  uint8_t readRegister(uint8_t reg);
68  int16_t readDoubleRegister(uint8_t lowerReg);
69  void writeRegister(uint8_t reg, uint8_t value);
70  void initFIFO();
71 
72  SPIClass * handler = NULL;
73 
74  uint gForceCalib = 4050;
75 
76 
77 public:
79 
86  void begin(void);
87 
93  void end(void);
94 
101 
107  IMUResult getRotation(void);
108 
114  float getTemperature(void);
115 
122  int8_t getWhoAmI(void);
123 
135  bool isShaken(uint32_t threshold = defaultShakeThreshold,uint8_t axis = xAxis|yAxis|zAxis);
136 
151 
152 
166  Direction getTiltDirection(uint tolerance = 10);
167 
176  void calibrateZAxis(uint gforceValue);
177 
185  uint getDataFromFIFO(FIFO_Package* buffer);
186 };
187 #endif //MotionDetection
MotionDetection::readDoubleRegister
int16_t readDoubleRegister(uint8_t lowerReg)
Right
@ Right
Definition: MotionDetection.h:35
Error
@ Error
Definition: MotionDetection.h:39
IMUResult::y
int16_t y
Definition: MotionDetection.h:18
FIFO_Package
Definition: MotionDetection.h:42
Left
@ Left
Definition: MotionDetection.h:34
MotionDetection::calibrateZAxis
void calibrateZAxis(uint gforceValue)
Definition: MotionDetection.cpp:74
MotionDetection::cmdRead
uint16_t cmdRead(uint8_t regHigh, uint8_t regLow)
Axis
Axis
Definition: MotionDetection.h:21
IMUResult
Definition: MotionDetection.h:16
Back
@ Back
Definition: MotionDetection.h:36
MotionDetection::frequency
static const uint frequency
Definition: MotionDetection.h:54
MotionDetection::getAcceleration
IMUResult getAcceleration(void)
Triggers a new Reading of the accelerationvalues and reads them from the IMU.
Definition: MotionDetection.cpp:28
MotionDetection::getRotation
IMUResult getRotation(void)
Triggers a new reading of the gyroscope and reads the values from the imu.
Definition: MotionDetection.cpp:35
MotionDetection::readFromRegisterBank
uint8_t readFromRegisterBank(registerBank bank, uint8_t reg)
Definition: MotionDetection.cpp:173
MotionDetection::end
void end(void)
stops the component Sets the IMU to Low-Power-Mode
Definition: MotionDetection.cpp:25
IMUResult::z
int16_t z
Definition: MotionDetection.h:19
yAxis
@ yAxis
Definition: MotionDetection.h:23
Orientation::yRotation
int yRotation
Definition: MotionDetection.h:29
zAxis
@ zAxis
Definition: MotionDetection.h:24
MotionDetection::getDataFromFIFO
uint getDataFromFIFO(FIFO_Package *buffer)
will read all availible packages from fifo, after 40ms Fifo is full
Definition: MotionDetection.cpp:245
MotionDetection::MotionDetection
MotionDetection()
Definition: MotionDetection.cpp:4
MotionDetection::getTilt
Orientation getTilt()
calculates how the robot is tilted. It is set, that when the robot is placed normally on a flat table...
Definition: MotionDetection.cpp:78
MotionDetection::MREG3
@ MREG3
Definition: MotionDetection.h:53
FIFO_Package::header
int8_t header
Definition: MotionDetection.h:43
MotionDetection::initFIFO
void initFIFO()
Definition: MotionDetection.cpp:231
MotionDetection::defaultShakeThreshold
static const uint16_t defaultShakeThreshold
Definition: MotionDetection.h:55
MotionDetection::writeToRegisterBank
void writeToRegisterBank(registerBank bank, uint8_t reg, uint8_t value)
Definition: MotionDetection.cpp:194
MotionDetection::MREG2
@ MREG2
Definition: MotionDetection.h:53
MotionDetection::getTemperature
float getTemperature(void)
Reads the current On Chip temperature of the IMU.
Definition: MotionDetection.cpp:45
MotionDetection::begin
void begin(void)
initialized the IMU Component. Wakes the IMU from Standby Set configuration
Definition: MotionDetection.cpp:8
FIFO_Package::accel
IMUResult accel
Definition: MotionDetection.h:45
Front
@ Front
Definition: MotionDetection.h:33
MotionDetection::isShaken
bool isShaken(uint32_t threshold=defaultShakeThreshold, uint8_t axis=xAxis|yAxis|zAxis)
Detects if at the time of calling is shaken. Therefore the sum over all accelerationvalues is calcula...
Definition: MotionDetection.cpp:55
FIFO_Package::temperature
int16_t temperature
Definition: MotionDetection.h:46
MotionDetection::getTiltDirection
Direction getTiltDirection(uint tolerance=10)
Checks in which direction (Front, Left, Right, Back) the robot is tilted.
Definition: MotionDetection.cpp:122
MotionDetection::MREG1
@ MREG1
Definition: MotionDetection.h:53
IMU_CMDs.h
Orientation::xRotation
int xRotation
Definition: MotionDetection.h:28
xAxis
@ xAxis
Definition: MotionDetection.h:22
FIFO_Package::gyro
IMUResult gyro
Definition: MotionDetection.h:44
MotionDetection::writeRegister
void writeRegister(uint8_t reg, uint8_t value)
Definition: MotionDetection.cpp:279
Orientation
Definition: MotionDetection.h:27
FIFO_Package::timestamp
int16_t timestamp
Definition: MotionDetection.h:47
MotionDetection::gForceCalib
uint gForceCalib
Definition: MotionDetection.h:74
Direction
Direction
Definition: MotionDetection.h:32
Neutral
@ Neutral
Definition: MotionDetection.h:37
MotionDetection::buf
int8_t * buf
Definition: MotionDetection.h:57
MotionDetection::cmdWrite
uint16_t cmdWrite(uint8_t regHigh, uint8_t regLow)
Flipped
@ Flipped
Definition: MotionDetection.h:38
IMUResult::x
int16_t x
Definition: MotionDetection.h:17
MotionDetection
Definition: MotionDetection.h:51
MotionDetection::readRegister
uint8_t readRegister(uint8_t reg)
Definition: MotionDetection.cpp:162
MotionDetection::handler
SPIClass * handler
Definition: MotionDetection.h:72
MotionDetection::getWhoAmI
int8_t getWhoAmI(void)
Returns the value of reading the whoAmI register When IMU working correctly, value should be 0x67.
Definition: MotionDetection.cpp:51
MotionDetection::registerBank
registerBank
Definition: MotionDetection.h:53
MotionDetection::bufferLength
const uint bufferLength
Definition: MotionDetection.h:56
MotionDetection::resetRegisterBankAccess
void resetRegisterBankAccess()
Definition: MotionDetection.cpp:224