Receives a measured height of a ping-pong ball from a PC, and uses it to control the PWM of a fan to keep the height as set with the keypad. Information is shown on the LCD
Fork of mbed-os-example-mbed5-blinky by
Revision 42:b69538bba4f9, committed 2017-07-05
- Comitter:
- gunarthon
- Date:
- Wed Jul 05 21:05:56 2017 +0000
- Parent:
- 41:3bc2a3885b9d
- Child:
- 43:5123f24e0b2c
- Commit message:
- added UDP option (untested)
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| pid.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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);
--- a/pid.h Tue Jul 04 16:36:18 2017 +0000
+++ b/pid.h Wed Jul 05 21:05:56 2017 +0000
@@ -68,11 +68,11 @@
//I term
integrator += errorValue;
- double I = (Ki/60.0) * integrator;
+ double I = (Ki/6000.0) * integrator;
if(I > maxInteg || I < minInteg)
- led2 = 1;
- else led2 = 0;
+ led2.write(1);
+ else led2.write(0);
I = I>maxInteg? maxInteg : I;
I = I<minInteg? minInteg : I;
