API to interface sensor with host PC

Dependencies:   LidarSpi USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
TNU
Date:
Tue Apr 05 08:16:30 2016 +0000
Commit message:
Fixed tests.cpp;

Changed in this revision

LidarSpi.lib Show annotated file Show diff for this revision Revisions of this file
USBDevice.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
tests.cpp Show annotated file Show diff for this revision Revisions of this file
tests.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 10bd8964965a LidarSpi.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LidarSpi.lib	Tue Apr 05 08:16:30 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/TNU/code/LidarSpi/#d49102c10940
diff -r 000000000000 -r 10bd8964965a USBDevice.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Apr 05 08:16:30 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#d17693b10ae6
diff -r 000000000000 -r 10bd8964965a main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 05 08:16:30 2016 +0000
@@ -0,0 +1,152 @@
+#include "mbed.h"
+#include "LidarSpi.h"
+#include "LidarFunc.h"
+#include "USBSerial.h"
+#include "tests.h"
+//DigitalOut myled(LED1);
+
+/*Protocol:
+SEND: 
+    "CMD PAR1 PAR2\n"
+RETURN
+    status(S) nrOfResponseBytes(L) Response(R)
+    SLLLLR..R
+*/
+
+Serial pc(USBTX, USBRX);
+USBSerial serial;
+
+LocalFileSystem local("local"); 
+
+int main() {
+
+    LidarSpi lidar(p5, p6, p7, p8, p15, p21, p22, p23);
+    LidarFunc sens(&lidar);
+    
+    char cmd[10];
+    char msg[50];
+    uint32_t par1=0;
+    uint32_t par2=0;
+    int res;
+    int cnt=0;
+    int n;
+    uint32_t size;
+    uint32_t val;
+    uint8_t * pntrS;
+    uint8_t * pntrV;
+    pc.baud(115200);
+    while(1){
+      
+        pc.printf("Awaiting commands...\n\r");
+
+        do{
+            msg[cnt]=serial.getc();
+            cnt++;
+            msg[cnt]='\0';
+            //pc.printf("getc:  %s", msg);
+        } while (msg[cnt-1]!='\n' && cnt<50);
+        cnt=0;
+        pc.printf("getc:  %s\n\r", msg);
+        n = sscanf(msg,
+                           "%9[^' '] %x %x\n", 
+                           cmd,
+                           &par1,
+                           &par2
+                        );
+        
+    
+        
+        if((strcmp(cmd, "RREG")==0) && n==2){  //ReadRegister Address
+            pc.printf("ReadRegister, address: %x", par1);
+            
+            res=lidar.ReadReg(par1, &val);
+            val=(val>>16)&0xffff;
+            
+            
+            if(res<0){
+                size=0x0000;
+                pntrS=(uint8_t *)&size;
+                serial.putc(1);
+                for(int i=0;i<sizeof(size);i++) serial.putc(pntrS[i]);
+                pc.printf("Read Error\n\r");
+            }
+            else{
+                size=sizeof(val);
+                pntrS=(uint8_t *) &size;
+                pntrV=(uint8_t *) &val;
+                pc.printf("size: %d\n\r", *pntrS);
+                serial.putc(0);
+                for(int i=0;i<sizeof(size);i++) serial.putc(*(pntrS+i));
+                for(int i=0;i<sizeof(size);i++) pc.printf("putc Val: [%d] %d\n\r",i,pntrV[i]);
+                for(int i=0;i<sizeof(val);i++) serial.putc(*(pntrV+i));
+                pc.printf("Address Value: %x  ,   sizeof(val)= %d\n\r", *pntrV, sizeof(val));
+                
+            }
+            
+    
+        }
+        else if((strcmp(cmd, "WREG")==0) && n==3){  //WriteRegister Address Value
+            pc.printf("WriteRegister, address: %x, value:%x", par1, par2);
+            
+            res=lidar.WriteReg(par1, par2);
+            if(res<0){
+
+                size=0;
+                pntrS=(uint8_t *)&size;
+                serial.putc(1);
+                for(int i=0;i<sizeof(size);i++) serial.putc(pntrS[i]);
+            }
+            else{
+  
+                size=0;
+                pntrS=(uint8_t *)&size;
+                serial.putc(0);
+                for(int i=0;i<sizeof(size);i++) serial.putc(pntrS[i]);
+            }
+    
+        }
+        else if((strcmp(cmd, "PTCH")==0) && n==1){  //WriteRegister Address Value
+            pc.printf("LoadPatch\n\r");
+            
+            res=lidar.LoadPatch("/local/patch.hex", &pc);
+            if(res<0){
+
+                size=0;
+                pntrS=(uint8_t *)&size;
+                serial.putc(1);
+                for(int i=0;i<sizeof(size);i++) serial.putc(pntrS[i]);
+                pc.printf("Patch load failed\n\r");
+            }
+            else{
+  
+                size=0;
+                pntrS=(uint8_t *)&size;
+                serial.putc(0);
+                for(int i=0;i<sizeof(size);i++) serial.putc(pntrS[i]);
+                pc.printf("Patch load Success\n\r");
+            }
+    
+        }
+        else if((strcmp(cmd, "TRCE")==0) && n==2){  //WriteRegister Address Value
+            pc.printf("getTrace\n\r");
+            bool isEnabled;
+            if (par1>0) isEnabled=true;
+            else isEnabled=false;
+            res=sens.setLed(isEnabled);
+            res=lidar.setTrace();
+            res=traceTest(&lidar, &pc, &serial);
+        }
+        else
+        {
+            pc.printf("INVALID COMMAND\n\r");
+            size=0;
+            pntrS=(uint8_t *)&size;
+            serial.putc(1);
+            for(int i=0;i<sizeof(size);i++) serial.putc(pntrS[i]); 
+        }
+    }
+    
+    
+    
+    
+}
\ No newline at end of file
diff -r 000000000000 -r 10bd8964965a mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Apr 05 08:16:30 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c
\ No newline at end of file
diff -r 000000000000 -r 10bd8964965a tests.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests.cpp	Tue Apr 05 08:16:30 2016 +0000
@@ -0,0 +1,55 @@
+
+#include "tests.h"
+int traceTest(LidarSpi* lidar, Serial * pc, USBSerial * serial)
+{
+    int res=0;
+    uint16_t buf[9473]; 
+    uint32_t nBytes; 
+    res=lidar->setTrace();
+    pc->printf("Set trace result: %d\n\r");
+    memset(buf, 0, sizeof(buf[0])*9472);
+    //for(int i=0;i<64;i++) res=lidar.GetTraceOne( buf, 9472*2, 0, 0, i, &pc);
+    //res=lidar.GetTraceOne( buf, 9472*2, 0, 0, 0, &pc);
+    res=lidar->GetTrace( buf, 9472*2, 0, 0, pc);
+    pc->printf("============\n\r============\n\r==================\n\r");
+    pc->printf("Result: %d \tTrace values after Patch:\n\r", res);
+    
+    //"%d \n", ((buf[i] & 0x00ff) << 8) | ((buf[i] & 0xff00) >> 8)
+    //for (int i =0; i<(74*8*16);i++) pc->printf("%d ",((buf[i] & 0x00ff) << 8) | ((buf[i] & 0xff00) >> 8));
+    /*
+    for(int i =0;i<16;i++) serial->printf("CH%d;",i);
+    serial->printf("\n");
+    for (int i =0; i<(592);i++) {
+        for(int j=0;j<16;j++) serial->printf("%d;",((buf[i+(j*74*8)] & 0x00ff) << 8) | ((buf[i+(j*74*8)] & 0xff00) >> 8));
+        serial->printf("\n");
+    }
+    
+    serial->printf("\n");*/
+    
+    if(res<0) {
+        serial->putc(1);
+        for(int i=0;i<4;i++) serial->putc(0);
+        return 0;
+    }
+    else nBytes=res*2;
+
+    uint8_t * pntrS=(uint8_t *) &nBytes;
+    pc->printf("size: %d\n\r", *pntrS);
+    serial->putc(0);
+    for(int i=0;i<sizeof(nBytes);i++) serial->putc(*(pntrS+i));
+    uint8_t * pnt;
+    pnt=(uint8_t *)buf;
+    pc->printf("Shows some values of trace, nBytes: %d\n\r", nBytes*2);
+    
+    for(int i=0;i<(nBytes);i=i+100) pc->printf("Value= %d\n\r", pnt[i]);
+    pc->printf("Transmit trace\n\r");
+    for(int i=0;i<(nBytes);i++) {
+        serial->putc(pnt[i]);
+        if(i<50)pc->printf("Char [%d] -> %x \n\r", i, pnt[i]);
+        if(((i % 2)==0)&&i<50 )pc->printf("Transmit [%d] -> %x \n\r",i, ((pnt[i])<<8)|(pnt[i+1]));
+    }
+    pc->printf("Trace transmitted\n\r");
+    //delete buf;
+    wait_us(4);
+    return res;
+}
\ No newline at end of file
diff -r 000000000000 -r 10bd8964965a tests.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests.h	Tue Apr 05 08:16:30 2016 +0000
@@ -0,0 +1,11 @@
+#ifndef TESTS_H
+#define TESTS_H
+
+#include "mbed.h"
+#include "LidarSpi.h"
+#include "USBSerial.h"
+
+int traceTest(LidarSpi* lidar,Serial * pc, USBSerial * sensor);
+
+
+#endif
\ No newline at end of file