altb_pmic / Mbed 2 deprecated Grove-TF_Mini_LiDAR

Dependencies:   mbed

Revision:
3:57de536c4759
Parent:
2:41ff1808bdf2
Child:
4:d60840a04ddc
--- a/main.cpp	Wed Aug 14 15:41:47 2019 +0000
+++ b/main.cpp	Thu Aug 15 11:24:15 2019 +0000
@@ -1,141 +1,111 @@
+// FORMAT_CODE_START
+// FORMAT_CODE_START
+// FORMAT_CODE_START
 #include "mbed.h"
 
-/*  Notes pmic 13.08.2019:
+/*  Notes pmic 15.08.2019:
   -
 */
 
-// nt baudrate = 115200;
 Serial pc(SERIAL_TX, SERIAL_RX);  // serial connection via USB - programmer
 Timer twhile;                     // timer for time measurement (usage in while(1), without timer attached)
 
 Serial TFmini(PC_10, PC_11);  // TX, RX
+
+int16_t dist;            // actual distance measurements of LiDAR
+int16_t strength;        // signal strength of LiDAR
+uint16_t checksum;       // save check value
+
+volatile uint8_t uartData[9];  // buffer for seriel uart LiDar data
+volatile uint8_t cnt;          // dataCounter
+
+volatile bool  receiveComplete;
+const uint8_t  HEADER = 0x59;     //frame header of data package
+const uint16_t MAXDIST = 0xFFFF;  // max. value of distance
+
 void onCharReceived();
-int dist;               //actual distance measurements of LiDAR
-int strength;           //signal strength of LiDAR
-int check;              //save check value
-uint8_t uart[9];    //save data measured by LiDAR
-volatile bool was_readable;
-const uint8_t HEADER=0x59;  //frame header of data package
 
+/*
 const uint8_t enter_config_buf[8] = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02}; // 42 57 02 00 00 00 01 02
-const uint8_t config_buf[8]       = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x01, 0x06}; // 42 57 02 00 00 00 01 06 (Standard format, as show in in Table 6)
+// const uint8_t config_buf[8]       = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x01, 0x06}; // 42 57 02 00 00 00 01 06 (Standard format, as show in in Table 6)
 // const uint8_t config_buf[8]       = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x00, 0x1A}; // 42 57 02 00 00 00 00 1A (Output unit of distance data is mm)
+const uint8_t config_buf[8]       = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x01, 0x1A}; // 42 57 02 00 00 00 00 1A (Output unit of distance data is cm)
 // const uint8_t config_buf[8]       = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x04, 0x06}; // 42 57 02 00 00 00 04 06 (Pixhawk data format)
 const uint8_t exit_config_buf[8]  = {0x42, 0x57, 0x02, 0x00, 0x00, 0x00, 0x00, 0x02}; // 42 57 02 00 00 00 00 02
-
-// float dt = 0.0f;
-int cnt = 0;
+*/
 
 // main program and control loop
 // -----------------------------------------------------------------------------
 int main()
 {
-    pc.baud(115200);                   // for serial comm. to pc
-    TFmini.baud(115200);               // for serial comm. from TFmini
-    was_readable = false;
+    pc.baud(115200);                   // for serial communication to pc
+
+    TFmini.baud(115200);               // for serial uart communication from TFmini
+    dist = 0;
+    strength = 0;
+    checksum = 0;
+    cnt = 0;
+    receiveComplete = false;
     TFmini.attach(&onCharReceived, Serial::RxIrq);
-    
-    wait_ms(1000);
-    
-    ///*
-    for(int i = 0; i<8; i++) {
-        // pc.printf("%X ", enter_config_buf[i]);
-        while( !TFmini.writable() )
-        TFmini.putc(enter_config_buf[i]);
-        wait_ms(10);
-    }
-    // pc.printf("\r\n");
-    wait_ms(100);
-    for(int i = 0; i<8; i++) {
-        // pc.printf("%X ", config_buf[i]);
-        while( !TFmini.writable() )
-        TFmini.putc(config_buf[i]);
-        wait_ms(10);
-    }
-    // pc.printf("\r\n");
-    wait_ms(100);
-    for(int i = 0; i<8; i++) {
-        // pc.printf("%X ", exit_config_buf[i]);
-        while( !TFmini.writable() )
-        TFmini.putc(exit_config_buf[i]);
-        wait_ms(10);
-    }
-    // pc.printf("\r\n");
-    wait_ms(100);
-    //*/
-    
+
     // twhile.start(); // timer for time measurement (usage in while(1), without timer attached)
     while(1) {
-
-        /*
-        if (TFmini.readable()) {  //check if serial port has data input
-            was_readable = true;
-            if(TFmini.getc() == HEADER) {  //assess data package frame header 0x59
-                uart[0]=HEADER;
-                if (TFmini.getc() == HEADER) { //assess data package frame header 0x59
-                    uart[1] = HEADER;
-                    for (i = 2; i < 9; i++) { //save data in array
-                        uart[i] = TFmini.getc();
-                    }
-                    check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
-                    if (uart[8] == (check & 0xff)) { //verify the received data as per protocol
-                        dist = uart[2] + uart[3] * 256;     //calculate distance value
-                        strength = uart[4] + uart[5] * 256; //calculate signal strength value
-                    }
-                }
-            }
-        } else {
-            was_readable = false;
-        }
-        */
-
-        // distf = pt1((float)dist);
-        // k++;
-        // if(was_readable) pc.printf("%10i was readable\r\n", k);
-        // else pc.printf("%10i was not readable\r\n", k);
-        // if( TFmini.readable()) pc.printf("%10i %10i\r\n", k, TFmini.getc());
-        if( was_readable ) {
-            pc.printf("%i \r\n", uart[2] + uart[3] * 256);
+        if( receiveComplete ) {
+            pc.printf("%i %i \r\n", dist, strength);
             // pc.printf("%10.6e %10i %10i \r\n", dt, dist, strength);
-            was_readable = false;
+            receiveComplete = false;
         }
     }
 }
 
 void onCharReceived()
 {
+    /*
+    if(TFmini.readable()) {
+        uartData[cnt] = TFmini.getc();
+        if( uartData[0] != HEADER ) {
+            cnt = 0;
+        } else if( cnt == 1 && uartData[1] != HEADER ) {
+            cnt = 0;
+        } else if( cnt == 8 ) {
+            checksum = 0;
+            for(uint8_t j = 0; j < 8; j++) {
+                checksum += uartData[j];
+            }
+            if( uartData[8] == (checksum & 0xff) ) {
+                dist = uartData[2] + uartData[3] * 256;
+                strength = uartData[4] + uartData[5] * 256;
+                receiveComplete = true;
+            }
+            cnt = 0;
+        } else {
+            cnt++;
+        }
+    }
+    */
     ///*
-    if( TFmini.readable() ) {
-        uart[cnt++] = TFmini.getc();
-        
-        if(cnt == 9) {
-            was_readable = true;
+    if(TFmini.readable()) {
+        uartData[cnt] = TFmini.getc();
+        checksum += uartData[cnt];
+        if( uartData[0] != HEADER ) {
+            cnt = 0;
+            checksum = 0;
+        } else if( cnt == 1 && uartData[1] != HEADER ) {
             cnt = 0;
+            checksum = 0;
+        } else if( cnt == 8 ) {
+            checksum -= uartData[cnt];
+            if( uartData[8] == (checksum & 0xff) ) {
+                dist = uartData[2] + uartData[3] * 256;
+                strength = uartData[4] + uartData[5] * 256;
+                receiveComplete = true;
+            }
+            cnt = 0;
+            checksum = 0;
+        } else {
+            cnt++;
         }
-        // dt = twhile.read()*1000.0f;
-        // twhile.reset(); // 7.0312e-04
     }
     //*/
-    
-    /*
-    if (TFmini.readable()) {  //check if serial port has data input
-        if(TFmini.getc() == HEADER) {  //assess data package frame header 0x59
-            uart[0]=HEADER;
-            if (TFmini.getc() == HEADER) { //assess data package frame header 0x59
-                uart[1] = HEADER;
-                for (i = 2; i < 9; i++) { //save data in array
-                    uart[i] = TFmini.getc();
-                }
-                check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
-                if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
-                    dist = uart[2] + uart[3] * 256;     //calculate distance value
-                    strength = uart[4] + uart[5] * 256; //calculate signal strength value
-                }
-            }
-        }
-        was_readable = true;
-        dt = twhile.read()*1000.0f;
-        twhile.reset(); // 7.0312e-04
-    }
-    */
-}
\ No newline at end of file
+}
+