First commit

Dependencies:   EthernetInterface FXAS21002 FXOS8700 NetworkAPI mbed-rtos mbed

Revision:
12:4257f5720975
Parent:
11:90554d22ade5
--- a/main.cpp	Sat Nov 15 21:46:59 2014 +0000
+++ b/main.cpp	Wed Sep 14 23:32:16 2016 +0000
@@ -1,5 +1,8 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+
+#include "FXAS21002.h"
+#include "FXOS8700.h"  
  
 #include "NetworkAPI/buffer.hpp"
 #include "NetworkAPI/select.hpp"
@@ -8,14 +11,100 @@
 using namespace network;
  
 #define MAX_CLIENTS 5
+#define TCP_PORT 55
+
+// Initialize RGB LEDs
+DigitalOut RGBLED1(LED1);
+DigitalOut RGBLED2(LED2);
+DigitalOut RGBLED3(LED3);
+
+int initPulses = 15;
+
+// Initialize Serial port
+Serial pc(USBTX, USBRX);
+
+// Initialize pins for I2C communication for sensors. Set jumpers J6,J7 in FRDM-STBC-AGM01 board accordingly.
+FXOS8700 accel(D14,D15);
+FXOS8700 mag(D14,D15);
+FXAS21002 gyro(D14,D15);
+
+float accel_data[3];
+float mag_data[3];
+float gyro_data[3];
  
-int
-main()
-{
-    EthernetInterface interface;
-    interface.init();
-    interface.connect();
-    printf("IP Address is %s\n\r", interface.getIPAddress());
+// Ethernet initialization and connection return values
+int retInit;
+int retConn;
+
+int main() {
+    // Start with the LEDs off
+    RGBLED1 = 1;
+    RGBLED2 = 1;
+    RGBLED3 = 1;
+
+    // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 & Gyroscope FXAS21002
+    accel.accel_config();
+    mag.mag_config();
+    gyro.gyro_config();
+
+    // Set baud rate
+    pc.baud(115200);
+    pc.printf("Attempting to connect to the network\r\n"); 
+   
+    // Intialize Ethernet connection
+    EthernetInterface eth;
+    
+    pc.printf("Initializing connection...");   
+    retInit = eth.init("10.12.1.55","255.255.0.0","10.12.0.1"); //Use a static IP address
+    
+    if (retInit == 0)
+    {
+        pc.printf("OK (IP = %s)\r\n", eth.getIPAddress());
+        
+        for (int a = 0; a < initPulses; a = a+1)
+        { 
+            RGBLED1 = 1;
+            RGBLED2 = 1;
+            wait(0.025);
+            RGBLED1 = 0;
+            RGBLED2 = 0;
+            wait(0.025);
+        }
+        
+        pc.printf("Connecting..."); 
+        retConn = eth.connect();
+
+        if (retConn == -1)
+        //if (retConn == 0)
+        {
+            pc.printf("OK\r\n");
+            pc.printf("Success! Device with IP Address %s is connected to network!\r\n", eth.getIPAddress());
+
+            RGBLED1 = 1;            
+
+            for (int a = 0; a < initPulses; a = a+1)
+            { 
+                RGBLED2 = 1;
+                wait(0.025);
+                RGBLED2 = 0;
+                wait(0.025);
+            }
+        }
+        else
+        {
+            pc.printf("FAILED\r\n");
+            pc.printf("Connection failure! Device not connected to network!\r\n", eth.getIPAddress());
+
+            RGBLED1 = 0;
+        }
+    }
+    else
+    {
+        pc.printf("FAILED\r\n");
+        pc.printf("Initialization failure! Device not connected to network!\r\n", eth.getIPAddress());
+        
+        RGBLED1 = 0;
+    }
      
     Select select;
     tcp::Socket server;
@@ -24,13 +113,17 @@
      
     int result = 0;
     int index = 0;
+
+    char imuValStr [1024];
+    char welcomeMsgStr [128] = "Connected to IMU";
      
-    network::Buffer buffer(256);
-    std::string message("Hello world!");
+    network::Buffer inbuffer(256);
+    network::Buffer outbuffer(256);
      
     // Configure the server socket (assume everty thing works)
     server.open();
-    server.bind(1234);
+    server.bind(TCP_PORT);
+    pc.printf("TCP Port: %d\r\n\n", TCP_PORT);
     server.listen(MAX_CLIENTS);
    
     // Add sockets to the select api
@@ -43,7 +136,7 @@
         // Wait for activity
         result = select.wait();
         if (result < -1) {
-            printf("Failed to select\n\r");
+            pc.printf("Failed to select\n\r");
             break;
         }
          
@@ -62,26 +155,26 @@
                  
                 // Maximum connections reached
                 if (index == MAX_CLIENTS) {
-                    printf("Maximum connections reached\n\r");
+                    pc.printf("Maximum connections reached\n\r");
                     continue;
                 }
              
                 // Accept the client
                 socket->accept(client[index]);
-                printf("Client connected %s:%d\n\r",
+                pc.printf("Client connected %s:%d\n\r",
                     client[index].getRemoteEndpoint().getAddress().toString().c_str(),
                     client[index].getRemoteEndpoint().getPort());
-                     
-                // Send a nice message to the client
-                client[index].write((void *)message.data(), message.size());
+                
+                // Send data to the client
+                client[index].write(welcomeMsgStr,strlen(welcomeMsgStr));
                 continue;
             }
              
             // It was not the server socket, so it must be a client talking to us.
-            switch (socket->read(buffer)) {
+            switch (socket->read(inbuffer)) {
                 case 0:
                     // Remote end disconnected
-                    printf("Client disconnected %s:%d\n\r",
+                    pc.printf("Client disconnected %s:%d\n\r",
                         socket->getRemoteEndpoint().getAddress().toString().c_str(),
                         socket->getRemoteEndpoint().getPort());
                      
@@ -90,16 +183,49 @@
                     break;
                  
                 case -1:
-                    printf("Error while reading data from socket\n\r");
+                    pc.printf("Error while reading data from socket\n\r");
                     socket->close();
                     break;
                  
                 default:
-                    printf("Message from %s:%d\n\r",
+                    pc.printf("Message from %s:%d\n\r",
                         socket->getRemoteEndpoint().getAddress().toString().c_str(),
                         socket->getRemoteEndpoint().getPort());
                          
-                    printf("%s\n\r", (char *)buffer.data());
+                    pc.printf("%s\n\r", (char *)inbuffer.data());
+
+                    // Get IMU data
+                    accel.acquire_accel_data_g(accel_data);     
+                    gyro.acquire_gyro_data_dps(gyro_data);     
+                    mag.acquire_mag_data_uT(mag_data);
+                    
+                    // Print IMU data to serial port
+                    pc.printf("Message to %s:%d",
+                        socket->getRemoteEndpoint().getAddress().toString().c_str(),
+                        socket->getRemoteEndpoint().getPort());
+                    pc.printf("ACC(g):%4.4f,%4.4f,%4.4f|GYRO(deg/s):%4.4f,%4.4f,%4.4f|MAG(uT):%4.4f,%4.4f,%4.4f\n",
+                        accel_data[0],
+                        accel_data[1],
+                        accel_data[2],
+                        gyro_data[0],
+                        gyro_data[1],
+                        gyro_data[2],
+                        mag_data[0],
+                        mag_data[1],
+                        mag_data[2]); 
+                    sprintf(imuValStr,"ACC(g):%4.4f,%4.4f,%4.4f|GYRO(deg/s):%4.4f,%4.4f,%4.4f|MAG(uT):%4.4f,%4.4f,%4.4f\n",
+                        accel_data[0],
+                        accel_data[1],
+                        accel_data[2],
+                        gyro_data[0],
+                        gyro_data[1],
+                        gyro_data[2],
+                        mag_data[0],
+                        mag_data[1],
+                        mag_data[2]);
+                    
+                    // Send data to the client
+                    client[index].write(imuValStr,strlen(imuValStr));
                     break;
             }
         }