Revision 0:f37d59175b91, committed 2012-07-09
- Comitter:
- asy_paris90
- Date:
- Mon Jul 09 02:17:49 2012 +0000
- Commit message:
- Accelerometer7361
Changed in this revision
diff -r 000000000000 -r f37d59175b91 Accelerometer.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Accelerometer.cpp Mon Jul 09 02:17:49 2012 +0000
@@ -0,0 +1,93 @@
+#include "Accelerometer.h"
+Accelerometer::Accelerometer(PinName xoutPin,PinName youtPin,PinName zoutPin,PinName sleepPin,PinName zeroGDetectPin,PinName gSelectPin):
+
+ xout(xoutPin),yout(youtPin),zout(zoutPin),zeroGDetect(zeroGDetectPin),sleep(sleepPin),gSelect(gSelectPin),zeroG(zeroGDetectPin)
+ {
+ sleep = 1; //normal mode
+ gSelect = 0; //1.5G mode
+ scale = 0.8;
+ }
+
+ float Accelerometer ::getAccel(){
+ float x = getAccelX();
+ float y = getAccelY();
+ float z = getAccelZ();
+ return sqrt (x*x + y*y + z*z);
+
+ }
+
+ float Accelerometer :: getAccelX(){
+ return ((xout*3.3)-1.65)/scale;
+ }
+
+ float Accelerometer :: getAccelY(){
+ return ((yout*3.3)-1.65)/scale;
+ }
+
+ float Accelerometer :: getAccelZ(){
+ return ((zout*3.3)-1.65)/scale;
+ }
+
+ float Accelerometer :: getTiltX() {
+
+ float x =getAccelX();
+ float y =getAccelY();
+ float z =getAccelZ();
+ float a =sqrt(x*x + y*y + z*z);
+ return asin (x/a);
+
+ }
+
+ float Accelerometer :: getTiltY(){
+
+ float x =getAccelX();
+ float y =getAccelY();
+ float z =getAccelZ();
+ float a =sqrt(x*x + y*y + z*z);
+ return asin (y/a);
+
+ }
+
+ float Accelerometer :: getTiltZ(){
+
+ float x =getAccelX();
+ float y =getAccelY();
+ float z =getAccelZ();
+ float a =sqrt(x*x + y*y + z*z);
+ return asin (z/a);
+
+ }
+
+ void Accelerometer :: setScale(Scale scale){
+ switch (scale){
+
+ case SCALE_1_5G:
+ this->scale =0.8;
+ gSelect = 0;
+ break;
+ case SCALE_6G:
+ this->scale = 0.206;
+ gSelect = 1;
+ break;
+
+ }
+
+ }
+
+
+ void Accelerometer ::setSleep(bool on){
+ sleep= !on;
+ }
+
+
+ bool Accelerometer :: detectedZeroG(){
+ return zeroGDetect;
+}
+
+ void Accelerometer ::setZeroGDetectListener(void(*func)(void)){
+ zeroG.rise(func);
+}
+
+ template<typename T> void Accelerometer ::setZeroGDetectListener(T*t,void (T::*func)(void)){
+ zeroG.rise(t,func);
+}
\ No newline at end of file
diff -r 000000000000 -r f37d59175b91 Accelerometer.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Accelerometer.h Mon Jul 09 02:17:49 2012 +0000
@@ -0,0 +1,34 @@
+#ifndef ACCELEROMETER_H
+#define ACCELEROMETER_H
+#include "mbed.h"
+
+class Accelerometer{
+public:
+ Accelerometer (PinName XoutPin, PinName youPin,PinName zoutPin,PinName sleepPin, PinName zeroGDetectPin,PinName gSelectPin);
+
+ enum Scale {SCALE_1_5G,SCALE_6G};
+ float getAccel();
+ float getAccelX();
+ float getAccelY();
+ float getAccelZ();
+ float getTiltX();
+ float getTiltY();
+ float getTiltZ();
+
+ void setScale (Scale scale);
+ void setSleep(bool on);
+ bool detectedZeroG();
+ void setZeroGDetectListener(void (*func)(void));
+
+ template<typename T> void setZeroGDetectListener(T* t,void (T::*func)(void));
+ private:
+
+ AnalogIn xout,yout,zout;
+ DigitalIn zeroGDetect;
+ DigitalOut sleep;
+ DigitalOut gSelect;
+ InterruptIn zeroG;
+ float scale;
+
+ };
+ #endif
\ No newline at end of file