A library to read from an Epson IMU

Dependents:   Test2

Files at this revision

API Documentation at this revision

Comitter:
GijsB
Date:
Fri Dec 13 10:58:02 2013 +0000
Parent:
0:b4479b51578c
Commit message:
rsse

Changed in this revision

IMU.cpp Show annotated file Show diff for this revision Revisions of this file
IMU.h Show annotated file Show diff for this revision Revisions of this file
USBIMU.cpp Show annotated file Show diff for this revision Revisions of this file
USBIMU.h Show annotated file Show diff for this revision Revisions of this file
diff -r b4479b51578c -r a2b5b17c949d IMU.cpp
--- a/IMU.cpp	Thu Nov 28 15:14:40 2013 +0000
+++ b/IMU.cpp	Fri Dec 13 10:58:02 2013 +0000
@@ -1,5 +1,6 @@
 #include "IMU.h"
 
+
 IMU::IMU(PinName tx, PinName rx){
     Serial *s = new Serial(tx, rx);
     init(s);
@@ -71,4 +72,4 @@
     return (LSByte & 0x60) != 0;
     
     
-}
\ No newline at end of file
+}
diff -r b4479b51578c -r a2b5b17c949d IMU.h
--- a/IMU.h	Thu Nov 28 15:14:40 2013 +0000
+++ b/IMU.h	Fri Dec 13 10:58:02 2013 +0000
@@ -3,7 +3,6 @@
 
 #include "mbed.h"
 
-
 #define TEMP_OUT 0x02
 #define XGYRO_OUT 0x04
 #define YGYRO_OUT 0x06
@@ -40,4 +39,5 @@
 
 };
 
+
 #endif
\ No newline at end of file
diff -r b4479b51578c -r a2b5b17c949d USBIMU.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBIMU.cpp	Fri Dec 13 10:58:02 2013 +0000
@@ -0,0 +1,89 @@
+#include "USBIMU.h"
+
+USBIMU::USBIMU(){
+    // TODO
+    USBHostSerial *s = new USBHostSerial();
+    init(s);
+}
+
+void USBIMU::init(USBHostSerial *s){
+    // TODO
+    
+    while (!s->connect()){
+        wait(50);
+    }
+    
+    faulty = false;
+    s_ = s;
+    wait(0.8);
+    while(!isReady()){
+        wait(0.1);
+    }
+    faulty = isFaulty();
+}
+
+USBIMU::~USBIMU(){
+    delete s_;
+}
+
+signed int USBIMU::getParam(unsigned char par){
+    // TODO
+    if(faulty){
+        return 0;
+    }
+    
+    goToStartOfSentence(par);
+    
+    unsigned char MSByte = s_->getc(); /*The most significant byte */
+    unsigned char LSByte = s_->getc(); /*The least significant byte */
+    s_->getc();                        /*The CR */
+    
+    signed int res = (MSByte<<8) & LSByte;
+    return res;
+}
+
+
+
+float USBIMU::getScaledParam(unsigned char par){
+    // TODO
+    float val =(float) getParam(par);
+    
+    if(val>=0){
+        return val/2147483647;
+    } else{
+        return val/2147483648;
+    }
+
+}
+
+void USBIMU::goToStartOfSentence(unsigned char par){
+    // TODO
+    s_->putc(par);
+    s_->putc(0x00);
+    s_->putc(CR);
+    
+    while(s_->getc()!=par);
+}
+
+bool USBIMU::isReady(){
+    // TODO
+    unsigned int param = getParam(0x3E);
+    return (param & 0x0400) == 0;
+}
+
+bool USBIMU::isFaulty(){
+    // TODO
+    goToStartOfSentence(0x3C);
+    
+    s_->getc();                         /*The most significant byte is nog interesting here*/
+    unsigned char LSByte = s_->getc(); /*The least significant byte */
+    s_->getc();                        /*The CR */
+    
+    return (LSByte & 0x60) != 0;
+    
+    
+}
+
+bool USBIMU::getFaulty(){
+    return faulty;
+}
\ No newline at end of file
diff -r b4479b51578c -r a2b5b17c949d USBIMU.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBIMU.h	Fri Dec 13 10:58:02 2013 +0000
@@ -0,0 +1,40 @@
+#ifndef USBIMU_H
+#define USBIMU_H
+
+#include "mbed.h"
+#include "USBHostSerial.h"
+
+#define TEMP_OUT 0x02
+#define XGYRO_OUT 0x04
+#define YGYRO_OUT 0x06
+#define ZGYRO_OUT 0x08
+#define XACCL_OUT 0x0A
+#define YACCL_OUT 0x0C
+#define ZACCL_OUT 0x0E
+
+#define CR 0x0D
+
+
+class USBIMU {
+
+    public:
+        USBIMU();
+        ~USBIMU();
+        signed int getParam(unsigned char par); 
+        float getScaledParam(unsigned char par);
+        bool getFaulty(); 
+        
+        
+    private:
+        USBHostSerial *s_;
+        bool faulty;
+        
+        void init(USBHostSerial *s);
+        bool isReady();
+        bool isFaulty();
+        void goToStartOfSentence(unsigned char par);
+        
+        
+};
+
+#endif
\ No newline at end of file