Gunar Kroeger / Mbed OS PID_floating_ball

Dependencies:   TextLCD

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Revision:
42:b69538bba4f9
Parent:
41:3bc2a3885b9d
Child:
43:5123f24e0b2c
--- a/main.cpp	Tue Jul 04 16:36:18 2017 +0000
+++ b/main.cpp	Wed Jul 05 21:05:56 2017 +0000
@@ -3,14 +3,18 @@
 #include "TextLCD.h"
 #include "pid.h"
 #include <Serial.h>
+#include "EthernetInterface.h"
 
 //definitions
 typedef struct {
     double input;
 } message_t;
 
+bool useUDP = false;
 const int heightResolution = 1000;
 const int BAUDRATE = 115200;
+const char* host_address = "10.1.1.101";
+const int host_port = 7;
 const int cameraFPS = 60;
 const int displayRefreshTime = 200;
 const double setPoint = 0.5;
@@ -36,9 +40,10 @@
 //Threads
 Thread lcdThread;
 Thread pidThread;
-Thread communicationThread;
+Thread serialThread;
 Thread hmiThread;
 Thread setPointThread;
+Thread udpThread;
 
 //Global variables
 TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7
@@ -52,7 +57,9 @@
 int simul = simulNONE;
 bool setPointSimul = false;
 
-void SerialCallback(int);
+
+// Network interface
+EthernetInterface net;
 
 //=============================Thread Methodes==================================
 void setPointMethode(void)
@@ -152,7 +159,7 @@
 }
 //------------------------------------------------------------------------------
 
-void CommunicationMethode(void)
+void SerialMethode(void)
 {
     Serial serial(USBTX, USBRX);
     serial.baud(BAUDRATE);
@@ -167,6 +174,31 @@
         messageQueue.put(message);
     }   
 }
+//------------------------------------------------------------------------------
+
+void UdpMethode(void)
+{
+    UDPSocket socket(&net);
+    socket.bind(host_port);
+    SocketAddress socketAddress;
+    socket.set_blocking(true);
+    uint16_t packet = 0;
+    double input = 0;
+    while(true)
+    {
+        //socket.sendto(host_address, host_port, (const void*)input, sizeof(input));
+        //input += 1;
+        socket.recvfrom(&socketAddress, &packet, sizeof(packet));
+        
+        input = double(packet) / heightResolution;
+        message_t *message = mpool.alloc();
+        message->input = input;
+        messageQueue.put(message);
+        Thread::wait(300);
+    }   
+}
+
+//------------------------------------------------------------------------------
 
 unsigned readButtons()
 {
@@ -313,12 +345,34 @@
     lcd.cls();
     lcd.printf("HELLO");
     
+    Thread::wait(2000);
+    if(readButtons() == btnDOWN)
+        useUDP = true;
+    
+    if(useUDP)
+    {
+        lcd.cls();
+        lcd.printf("Connecting...");
+        net.connect();
+
+        // Show the network address
+        const char *ip = net.get_ip_address();
+        lcd.cls();
+        lcd.printf("%s", ip ? ip : "No IP");
+        if(!ip)
+            while(1); //terminate program
+            
+        Thread::wait(5000);
+        udpThread.start(UdpMethode);
+    }
+    else
+        serialThread.start(SerialMethode);
+    
     pidController = new Pid(Kp, Ki, Kd);
     pidController->setSetPoint(setPoint);
     
     lcdThread.start(LcdMethode);
     pidThread.start(PidMethode);
-    communicationThread.start(CommunicationMethode);
     hmiThread.start(hmiMethode);
     setPointThread.start(setPointMethode);