APS de Sistemas Operacionais / Controle 2 FINAL

Dependencies:   EthernetInterface HCSR04 PID Servo mbed-rtos mbed

Fork of aps_so_c2_old by Felipe dos Santos Neves

Revision:
5:afe2339723f6
Parent:
4:40990500a7cc
Child:
6:ee9361616596
--- a/main.cpp	Sat Nov 18 20:03:58 2017 +0000
+++ b/main.cpp	Sat Nov 18 20:42:14 2017 +0000
@@ -8,7 +8,7 @@
 #include <iostream>
 #include <sstream>
 
-#define SERIAL
+//#define SERIAL
 #define ETHERNET
 
 #define SAMPLE_RATE 10 //sample rate in miliseconds
@@ -48,6 +48,7 @@
 #ifdef ETHERNET
 Thread ethernetSendThread;
 Thread ethernetReceiveThread;
+Thread ethernetKeepAliveThread;
 #endif
 
 float ballDistance = 0.0;
@@ -58,20 +59,29 @@
 EthernetInterface eth;
 TCPSocketConnection sock;
 
-void ethernetSend()
+void ethernetKeepAlive()
 {
 #ifdef SERIAL
-    printf("ethernetThread started\n");
+    printf("ethernetKeepAliveThread started\n");
 #endif
     std::stringstream ss;
 
-    eth.init("192.168.1.2","255.255.255.0","192.168.1.1");
-    eth.connect();
+    while(true) {
+        if(sock.is_connected()) {
+            sock.send_all(NULL,0);
+        } else {
+            sock.connect("192.168.1.1", 12345);
+        }
+        Thread::wait(100);
+    }
+}
+
+void ethernetSend()
+{
 #ifdef SERIAL
-    printf("IP Address is %s\n", eth.getIPAddress());
+    printf("ethernetSendThread started\n");
 #endif
-
-    sock.connect("192.168.1.1", 12345);
+    std::stringstream ss;
 
     while(true) {
         if(sock.is_connected()) {
@@ -103,6 +113,9 @@
 
 void ethernetReceive()
 {
+#ifdef SERIAL
+    printf("ethernetReceiveThread started\n");
+#endif
     char buffer[10];
     int ret;
     while(true) {
@@ -119,7 +132,7 @@
                 default:
                     break;
                 case 1:
-                    setpoint = (buffer[0]);
+                    setpoint = (buffer[0]-'0');
                     break;
                 case 2:
                     setpoint = (buffer[0]-'0')*10 + buffer[1]-'0';
@@ -130,6 +143,7 @@
         } else {
             sock.connect("192.168.1.1", 12345);
         }
+        //Thread::wait(100);
 
     }
 }
@@ -239,12 +253,23 @@
     sw2.rise(&sw2Callback);
     sw3.rise(&sw3Callback);
     ledSwitchThread.start(ledSwitch);
+    #ifdef SERIAL
     serialOutThread.start(serialOut);
+    #endif 
     controlSystemThread.start(controlSystem);
 
 #ifdef ETHERNET
+    eth.init("192.168.1.2","255.255.255.0","192.168.1.1");
+    eth.connect();
+    sock.connect("192.168.1.1", 12345);
+    sock.set_blocking(0);
+#ifdef SERIAL
+    printf("IP Address is %s\n", eth.getIPAddress());
+#endif
+    
     ethernetSendThread.start(ethernetSend);
     ethernetReceiveThread.start(ethernetReceive);
+    ethernetKeepAliveThread.start(ethernetKeepAlive);
 #endif
 
     while(true) {