gpr

Dependencies:   MbedJSONValue QEI

Files at this revision

API Documentation at this revision

Comitter:
castrena23
Date:
Sun Nov 06 09:56:10 2022 +0000
Parent:
12:19b1755d34e9
Commit message:
gpr

Changed in this revision

QEI.lib 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/QEI.lib	Sun Nov 06 09:56:10 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/aberk/code/QEI/#5c2ad81551aa
--- a/main.cpp	Thu Sep 01 13:39:23 2022 +0000
+++ b/main.cpp	Sun Nov 06 09:56:10 2022 +0000
@@ -1,23 +1,30 @@
 #include "mbed.h"
 #include "stdio.h"
 #include "MbedJSONValue.h"
+#include "QEI.h"
 #include "string.h"
 #include "stdint.h"
 #include <cstdlib>
 #define NUM_FIELDS  3 
-#define MAX_BUFFER 150
+#define MAX_BUFFER 500
+
+
 
 Thread thread;
+//Ticker timer;
 
 Serial pc(USBTX,USBRX);
 AnalogOut DataDAC(p18);
 Serial gps(p13, p14);
+QEI wheel (p29, p30, NC, 250, QEI::X4_ENCODING);
+
 
 char eDataBuffer[500];
 int i = 0;
 int h_time,m_time,s_time;
 char testStringBuffer[MAX_BUFFER]={0};
 bool stringComplete = false;
+double DAC_Value;
 int len = 0;
 //number of comma seperated values in the data...TODO does this remain constant?
 float latitude, longitude, timefix, speed, altitude;
@@ -26,7 +33,10 @@
 void parseGPS(char *cmd, int n);
 void parseTime (float timeval);
 void baca_gps();
- 
+void input_DAC();
+
+
+
 
 void clearBuffer(char* buffer , int len){
     for (int i =0; i<len ; i++){
@@ -41,21 +51,35 @@
     testStringBuffer[len] = inChar;    
     len++;       
     if (inChar == '\n') {
-                pc.printf("%s",testStringBuffer);
+             //   pc.printf("%s",testStringBuffer);
                 stringComplete = true;
                 len = 0;
      }
+    }
 }
-}
+
 void Init()
 {
      gps.baud(9600);
      pc.baud(115200);
      pc.attach(&onSerialInput);
-     baca_gps();
+     gps.attach(&baca_gps);
      pc.printf("Init OK\n");
 }
 
+float baca_enc() 
+{ 
+    float lp;
+    int pul =wheel.getPulses();
+    lp = (pul / 4*250)*0.04 ;
+    return lp;
+}
+
+void reset_enc() 
+{ 
+    wheel.reset();
+}
+
 void parseTime (float timeval)
 {
     //format utc time to beijing time,add 8 time zone
@@ -89,7 +113,7 @@
      else if(strncmp(cmd,"$GPGSA", 6) == 0) 
     {
         sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
-       // pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
+        //pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
     }
     
     // Geographic position, Latitude and Longitude
@@ -134,7 +158,7 @@
     else if(strncmp(cmd,"$GNGLL", 6) == 0) 
     {
         sscanf(cmd, "$GNGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
-       //pc.printf("GNGLL Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
+        //pc.printf("GNGLL Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
     }
     
     else
@@ -142,6 +166,9 @@
    //     pc.printf("Unknown message type\n");
     }
 }
+/*void input_DAC(){
+    DataDAC = DAC_Value;
+}*/
 
 char e;
 void baca_gps() 
@@ -170,54 +197,83 @@
          } 
     
 }
-
-//int main(int argc, char* argv[])
-#define TOTAL_DATA 100
+#define TOTAL_DATA 20
 bool threadFlag = false;
 MbedJSONValue threadDAC;
 void DAC_Thread(){
-    int periode;
-    double DAC_Value;
-    int counterDAC = 0;
-    while (true) {
+    int periode;    
+    int counterDAC = 0;    
+    while (true) {         
         if (threadFlag){
+           
             periode = threadDAC["periode"].get<int>();
-            DAC_Value= threadDAC["value"][counterDAC].get<double>();
-            DataDAC = DAC_Value;
-            pc.printf("DAC: %f \n",DAC_Value);
-            wait(periode/TOTAL_DATA);
-            counterDAC++;
-            if(counterDAC>=TOTAL_DATA){
-                counterDAC = 0;    
+            float m = periode/20;
+                                           
+                DAC_Value = threadDAC["value"][counterDAC].get<double>();
+                
+                //for (int k=0;k<20;k++){ 
+                DataDAC = DAC_Value;              
+                wait_us(m*100);
+                
+                //}
+                
+                counterDAC++;   
+                if(counterDAC>=TOTAL_DATA){
+                   counterDAC = 0;   
+                 //continue;
+                } 
+                
             }
-        }
-        else {
+            
+            
+       
+        else {            
             DataDAC = 0;
-            counterDAC = 0;    
+            counterDAC = 0;        
+                  
         }
         
     }
 }
+
 int main()
 {
     //float e[5]={0};
     Init();  
     MbedJSONValue json;
     MbedJSONValue jsonGPS;
+    MbedJSONValue jsonENCO;
     thread.start(DAC_Thread);
     std::string buffer;
     
     while(1){
-       
        if (stringComplete){
             parse(json,testStringBuffer);
             if (json.hasMember("type")){
-                if (json["type"].get<std::string>() == "DAC"){
+                if(json["type"].get<std::string>() == "STOP"){
+                    
+                    threadFlag = false; 
+                    DataDAC = 0;
+                    wait(1);
+                    NVIC_SystemReset();
+                    
+                      
+                }
+                else if (json["type"].get<std::string>() == "DAC"){
+                    
                     threadDAC = json;   
-                    threadFlag = true;         
+                    threadFlag = true;   
+                    
                 }
-                else if(json["type"].get<std::string>() == "STOP"){
-                    threadFlag = false; 
+                else if(json["type"].get<std::string>() == "ENCO"){
+                    jsonENCO["type"] = "ENCO";
+                    jsonENCO["Pos"] = baca_enc();
+                    buffer = jsonENCO.serialize();
+                    pc.printf("%s\n",buffer.c_str());
+                    //wait(0.5);
+                }
+                else if(json["type"].get<std::string>() == "ENCO_RESET"){
+                    reset_enc();
                 }
                 else if(json["type"].get<std::string>() == "GPS"){              
                     jsonGPS["type"] = "GPS";
@@ -227,7 +283,7 @@
                     jsonGPS["ew"]  = ew;
                     buffer = jsonGPS.serialize();
                     pc.printf("%s\n",buffer.c_str());
-                    //pc.printf("Latitude: %f %c, Longitude: %f %c \n", latitude, ns, longitude, ew);
+                   
                     
                 }
                
@@ -245,8 +301,6 @@
 
             
         }
-        //wait(1);
-        //return 0;
-    
+        
     }
 }
\ No newline at end of file