Aditya Mehrotra / Mbed OS Sensor_Training_BoardV2
Revision:
4:ab834eebd496
Parent:
3:4dcadafb89a2
Child:
5:e4e96d1b446f
diff -r 4dcadafb89a2 -r ab834eebd496 main.cpp
--- a/main.cpp	Tue Nov 16 21:21:10 2021 +0000
+++ b/main.cpp	Wed Nov 17 15:13:38 2021 +0000
@@ -13,12 +13,18 @@
 /*---------------------- Initial setup ----------------------*/
 // Serial port
 Serial       pc(USBTX, USBRX);
-// Ticker for logging at a fixed frequency
-Ticker datalog;
-bool tickerActivated = false;
-void log_data(){
-    tickerActivated = true;
+// Ticker for logging from LTC at a fixed frequency
+Ticker LTC_log;
+// Ticker for logging from force sensor, sending ethernet message at a fixed frequency
+Ticker eth_send;
+bool LTC_ticker_activated = false;
+bool eth_ticker_activated = false;
+void log_LTC_data(){
+    LTC_ticker_activated = true;
 } 
+void send_eth_data(){
+    eth_ticker_activated = true;
+}
 
 /*---------------------- Ethernet setup ----------------------*/
 const int SERVER_PORT = 2;
@@ -62,6 +68,8 @@
 uint8_t rx_buff[BUFF_SIZE]; //each is an 8-bit word 
 ltc_spi adc_data;
 int16_t ati_data[6];
+float ati_filt[6];
+float FILT_COEF = 0.05f;
 
 // calibration matrix for ATI sensor
 float Fxc[6] = {-0.1971322934773, -0.04349257334311, 2.298051028435, -80.35044049387, 1.362983909976, 78.23673392118};
@@ -128,12 +136,20 @@
     ati_data[5] = adc_data.channel[5].cnv_upper<<8 | adc_data.channel[5].cnv_lower;
 }
 
+void filter_LTC_data(){
+    // filter the incoming LTC data
+    for(int i=0; i<6; i++){
+        ati_filt[i] = FILT_COEF*(float)ati_data[i] + (1.0-FILT_COEF)*ati_filt[i];
+    }
+}
+
 // convert integer readings to voltages to f/t values
 void convert_LTC_data(){
     // dummy buffer to store converted ADC vals
     float buff[6];
     for(int i=0; i<6; i++){
-        buff[i] = CONV_FACTOR*((float)ati_data[i]-bias[i]); // bias[] is in same units as msg[]
+        //buff[i] = CONV_FACTOR*((float)ati_data[i]-bias[i]); // bias[] is in same units as msg[] // for non-filtered LTC data
+        buff[i] = CONV_FACTOR*(ati_filt[i]-bias[i]); // for filtered data
         ft_data[i] = 0; // also zero out ft_data here
     }
     // convert each f/t value separately
@@ -387,22 +403,31 @@
     }
     pc.printf("ATI sensor is calibrated with biases of: %.2f, %.2f, %.2f, %.2f, %.2f, %.2f \n\r", bias[0],bias[1],bias[2],bias[3],bias[4],bias[5]);
       
-    // Attach sampling interrupt
+    // Attach sampling and sending interrupts
 //    datalog.attach_us(&log_data,50000); // 1000us = 1ms (10000 = 10 ms = 100 Hz)
-    datalog.attach(&log_data,0.1); // 10Hz
+    LTC_log.attach_us(&log_LTC_data,5000); // 200Hz
+    eth_send.attach(&send_eth_data,0.1); // 10Hz
+    
     
     while (true) {
         
-        if(tickerActivated == true) { 
-
-            tickerActivated = false;
+        if(LTC_ticker_activated == true) {
+            // Clear flag
+            LTC_ticker_activated = false;
+            // Sample from LTC chip 
+            // TODO: sample LTC chip at a much higher frequency (10kHz?)
+            read_LTC_data();
+            filter_LTC_data();
+        }   
+        
+        if(eth_ticker_activated == true) { 
+            // Clear flag
+            eth_ticker_activated = false;
             
-            // Sample from LTC chip 
-            read_LTC_data();
             convert_LTC_data(); // TODO: remove this eventually, since we'll convert the data after sending over ethernet
             // Print received data
             //pc.printf("%6d,%6d,%6d,%6d,%6d,%6d\n\r", ati_data[0],ati_data[1],ati_data[2],ati_data[3],ati_data[4],ati_data[5]);
-            // Print converted data
+            // Print converted LTC data
             pc.printf("%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,  ", ft_data[0],ft_data[1],ft_data[2],ft_data[3],ft_data[4],ft_data[5]);
             
             // Sample from force sensor