Qian Yuyang / Mbed 2 deprecated Balance_Car_QianYuyang

Dependencies:   mbed QEI-1 nRF24L01P xiugai

Files at this revision

API Documentation at this revision

Comitter:
zhangyx
Date:
Fri Dec 13 01:32:26 2019 +0000
Parent:
13:99e774135a00
Child:
15:934289377f7a
Commit message:
add JY901 lib

Changed in this revision

JY901.cpp Show annotated file Show diff for this revision Revisions of this file
JY901.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JY901.cpp	Fri Dec 13 01:32:26 2019 +0000
@@ -0,0 +1,93 @@
+
+#include "JY901.h"
+void JY901::receiveData()
+{
+    char ch;
+    while(mod.readable()){
+        ch = mod.getc();
+        parseInput(&ch, 1);
+    }
+}
+void JY901::parseCmpt(int token, unsigned char* payloadBuf, int payloadLen)
+{
+    switch(token){
+        case 0x51:
+            for (int i = 0; i < 3; ++i)
+            {
+                acc[i] = payloadBuf[i*2]|((int)payloadBuf[i*2+1]<<8);
+                acc[i] = acc[i] * 16 * 9.8 / 32768;
+            }
+            // lcd.cls();
+            // lcd.printf("Ax=%d\n",int(data[0]*100));
+            //pc.printf("Ax=%.2f\tAy=%.2f\tAz=%.2f\r\n", data[0], data[1], data[2]);
+            break;
+        case 0x52:
+            for (int i = 0; i < 3; ++i)
+            {
+                gyo[i] = payloadBuf[i*2]|((int)payloadBuf[i*2+1]<<8);
+                gyo[i] = gyo[i] * 2000 / 32768;
+            }
+            //pc.printf("Wx=%.2f\tWy=%.2f\tWz=%.2f\r\n", data[0], data[1], data[2]);
+            break;
+        case 0x53:
+            for (int i = 0; i < 3; ++i)
+            {
+                att[i] = payloadBuf[i*2]|((int)payloadBuf[i*2+1]<<8);
+                att[i] = att[i] * 180 / 32768;
+            }
+            // pc.printf("Roll=%.2f\tPitch=%.2f\tYaw=%.2f\r\n", data[0], data[1], data[2]);
+            break;
+        case 0x54:
+            for (int i = 0; i < 3; ++i)
+            {
+                mag[i] = payloadBuf[i*2]|((int)payloadBuf[i*2+1]<<8);
+            }
+            //pc.printf("Hx=%.2f\tHy=%.2f\tHz=%.2f\r\n", data[0], data[1], data[2]);
+            break;
+    }
+}
+void JY901::parseInput(const char* data, int len)
+{
+    for (int i = 0; i < len; ++i)
+    {
+        unsigned char ch = data[i], sum;
+        switch(state){
+            case 0:
+                if(ch == 0x55)
+                    state = 1;
+                break;
+            case 1:
+                token = ch;
+                if(0x51 <= token && token <= 0x54){
+                    payloadLen = 8;
+                    recvLen = 0;
+                    state = 2;
+                }else{
+                    // pc.printf("%s %x\r\n", "unknown token", token);
+                    state = 0;
+                }
+                break;
+            case 2:
+                payloadBuf[recvLen++] = ch;
+                if(recvLen == payloadLen){
+                    state = 3;
+                }
+                break;
+            case 3:
+                sum = 0x55;
+                sum += token;
+                for (int i = 0; i < payloadLen; ++i)
+                {
+                    sum += payloadBuf[i];
+                }
+                if(sum != ch){
+                    // pc.printf("wrong checksum\r\n");
+                }else{
+                    parseCmpt(token, payloadBuf, payloadLen);
+                    //myled = !myled;
+                }
+                state = 0;
+                break;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JY901.h	Fri Dec 13 01:32:26 2019 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+#include "BufferedSerial.h"
+class JY901
+{
+    int state, token, payloadLen, recvLen;
+    unsigned char payloadBuf[16];
+protected:
+    BufferedSerial mod;
+    float acc[3], gyo[3], mag[3], att[3];
+    void parseCmpt(int token, unsigned char* payloadBuf, int payloadLen);
+    void parseInput(const char* data, int len);
+public:
+    JY901(PinName TX, PinName RX) : mod(TX, RX, 32){}
+    ~JY901() {}
+    void    baud (int baudrate) {mod.baud(baudrate);}
+    void receiveData();
+    void getAcc(float &x, float &y, float &z){
+        x = acc[0];
+        y = acc[1];
+        z = acc[2];
+    }
+    void getGyo(float &x, float &y, float &z){
+        x = gyo[0];
+        y = gyo[1];
+        z = gyo[2];
+    }
+    void getMag(float &x, float &y, float &z){
+        x = mag[0];
+        y = mag[1];
+        z = mag[2];
+    }
+    void getAttitude(float &roll, float &pitch, float &yaw){
+        roll = att[0];
+        pitch = att[1];
+        yaw = att[2];
+    }
+};
\ No newline at end of file
--- a/main.cpp	Mon Dec 02 06:53:14 2019 +0000
+++ b/main.cpp	Fri Dec 13 01:32:26 2019 +0000
@@ -4,23 +4,18 @@
 #include <string>
 typedef bool boolean;
 typedef std::string String;
-#include "eeprom.h"
-#include "ir.h"
+#include "sensors.h"
+#include "JY901.h"
 
-EEPROM eeprom(PB_7,PB_6,0,EEPROM::T24C02);
-Serial Serial_2(PA_2,PA_3);
+char buf[10] ;
+int len;
+int val;
+
+JY901 jy901_Serial_1(PB_6,PA_10);
 
 int main() {
 
 
-Serial_2.baud(9600);
-
-IR_Init(PB_10,PB_11,PA_5,eeprom);
-
-Serial_2.printf("learning\n");
-IR_StartLearning(1);
-Serial_2.printf("learned\n");
-IR_Emit(1);
-Serial_2.printf("sent\n");
+jy901_Serial_1.baud(9600);
 
 }
\ No newline at end of file