altb_pmic / Mbed 2 deprecated Grove-TF_Mini_LiDAR

Dependencies:   mbed

Revision:
4:d60840a04ddc
Parent:
3:57de536c4759
Child:
5:2f53a0220a37
Child:
6:4a3ab00525f4
--- a/main.cpp	Thu Aug 15 11:24:15 2019 +0000
+++ b/main.cpp	Thu Aug 15 12:54:11 2019 +0000
@@ -1,111 +1,87 @@
-// FORMAT_CODE_START
-// FORMAT_CODE_START
-// FORMAT_CODE_START
 #include "mbed.h"
+#include "TFmini.h"
 
 /*  Notes pmic 15.08.2019:
   -
 */
 
 Serial pc(SERIAL_TX, SERIAL_RX);  // serial connection via USB - programmer
-Timer twhile;                     // timer for time measurement (usage in while(1), without timer attached)
+InterruptIn Button(USER_BUTTON);  // User Button
+Ticker  LoopTimer;                // interrupt for control loop
+Timer t;                          // timer to analyse Button
 
-Serial TFmini(PC_10, PC_11);  // TX, RX
+RawSerial uart(PC_10, PC_11);
+TFmini tfmini(uart);
 
 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
+int16_t strength;
+int16_t distOld;         // actual distance measurements of LiDAR
+bool receiveComplete;
 
-void onCharReceived();
+int   k;
+bool  doRun;
+float Ts = 0.010f;       // sample time of main loop, 50 Hz
+float dt;
+Timer twhile;            // timer for time measurement (usage in while(1), without timer attached)
 
-/*
-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, 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
-*/
+// user defined functions
+void updateLoop(void);   // loop (via interrupt)
+void pressed(void);      // user Button pressed
+void released(void);     // user Button released
 
 // main program and control loop
 // -----------------------------------------------------------------------------
 int main()
 {
-    pc.baud(115200);                   // for serial communication to pc
-
-    TFmini.baud(115200);               // for serial uart communication from TFmini
+    pc.baud(115200);                   // for serial comm.
+    LoopTimer.attach(&updateLoop, Ts); // attach loop to timer interrupt
+    Button.fall(&pressed);             // attach key pressed function
+    Button.rise(&released);            // attach key pressed function
+    k = 0;
+    doRun = true;
+    // pt1.reset(0.0f);
     dist = 0;
     strength = 0;
-    checksum = 0;
-    cnt = 0;
-    receiveComplete = false;
-    TFmini.attach(&onCharReceived, Serial::RxIrq);
+    dt = 0.0f;
+    twhile.start();
+}
 
-    // twhile.start(); // timer for time measurement (usage in while(1), without timer attached)
-    while(1) {
-        if( receiveComplete ) {
-            pc.printf("%i %i \r\n", dist, strength);
-            // pc.printf("%10.6e %10i %10i \r\n", dt, dist, strength);
-            receiveComplete = false;
-        }
+// the updateLoop starts as soon as you pressed the blue botton
+void updateLoop(void)
+{
+    float dt = twhile.read();
+    twhile.reset();
+    
+    k++;
+    dist = tfmini();
+    strength = tfmini.readStrength();
+    if(doRun) {
+        pc.printf("%i %f %i %i \r\n", k, dt, dist, strength);
+        // pc.printf("%i %i %i \r\n", k, dist, strength);
+        receiveComplete = false;
     }
 }
 
-void onCharReceived()
+// buttonhandling
+// -----------------------------------------------------------------------------
+// start timer as soon as button is pressed
+void pressed()
 {
-    /*
-    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()) {
-        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++;
-        }
-    }
-    //*/
+    t.start();
 }
 
+// evaluating statemachine
+void released()
+{
+    // toggle state over boolean
+    if(doRun) {
+        k = 0;
+    }
+    doRun = !doRun;
+    t.stop();
+    t.reset();
+}
+
+
+
+