APS de Sistemas Operacionais / Controle 2 FINAL
Dependencies: EthernetInterface HCSR04 PID Servo mbed-rtos mbed
Fork of aps_so_c2_old by
Diff: main.cpp
- Revision:
- 4:40990500a7cc
- Parent:
- 3:3d094a31a283
- Child:
- 5:afe2339723f6
--- a/main.cpp Sat Nov 18 18:27:27 2017 +0000 +++ b/main.cpp Sat Nov 18 20:03:58 2017 +0000 @@ -4,6 +4,9 @@ #include "HCSR04.h" #include "Servo.h" #include "PID.h" +#include <string> +#include <iostream> +#include <sstream> #define SERIAL #define ETHERNET @@ -29,26 +32,113 @@ void sw2Callback() { if(motorPos<1) - motorPos+=0.1; + motorPos+=(float)0.1; } InterruptIn sw3(SW3); void sw3Callback() { if(motorPos>0) - motorPos-=0.1; + motorPos-=(float)0.1; } Thread ledSwitchThread; Thread serialOutThread; Thread controlSystemThread; -float distance = 0.0; +#ifdef ETHERNET +Thread ethernetSendThread; +Thread ethernetReceiveThread; +#endif + +float ballDistance = 0.0; float setpoint = 15.0; +#ifdef ETHERNET + +EthernetInterface eth; +TCPSocketConnection sock; + +void ethernetSend() +{ +#ifdef SERIAL + printf("ethernetThread started\n"); +#endif + std::stringstream ss; + + eth.init("192.168.1.2","255.255.255.0","192.168.1.1"); + eth.connect(); +#ifdef SERIAL + printf("IP Address is %s\n", eth.getIPAddress()); +#endif + + sock.connect("192.168.1.1", 12345); + + while(true) { + if(sock.is_connected()) { + ss.flush(); + ss << "Ball distance: " << ballDistance << "cm\n"; + ss << "Setpoint: " << setpoint << "cm\n"; + switch(statusFlag) { + case IDLE: + ss << "System is idle\n"; + break; + case ADJUSTING: + ss << "System is adjusting\n"; + break; + case STABLE: + ss << "System is stable\n"; + break; + default: + break; + } + sock.send_all((char*)ss.str().data(),ss.str().size()); + } else { + sock.connect("192.168.1.1", 12345); + + + } + Thread::wait(1000); + } +} + +void ethernetReceive() +{ + char buffer[10]; + int ret; + while(true) { + if(sock.is_connected()) { + + + ret = sock.receive(buffer, sizeof(buffer)-1); +#ifdef SERIAL + buffer[ret] = '\0'; + printf("Received %d chars from server:\n%s\n", ret, buffer); +#endif + + switch(ret) { + default: + break; + case 1: + setpoint = (buffer[0]); + break; + case 2: + setpoint = (buffer[0]-'0')*10 + buffer[1]-'0'; + break; + } + + + } else { + sock.connect("192.168.1.1", 12345); + } + + } +} +#endif + void ledSwitch() { #ifdef SERIAL - printf("ledSwitch thread started"); + printf("ledSwitch thread started\n"); #endif while (true) { switch(statusFlag) { @@ -80,9 +170,9 @@ void serialOut() { #ifdef SERIAL - printf("SerialOut thread started"); + printf("SerialOut thread started\n"); while(true) { - printf("Ball distance: %fcm\n",distance); + printf("Ball distance: %fcm\n",ballDistance); printf("Setpoint: %fcm\n",setpoint); switch(statusFlag) { case IDLE: @@ -105,12 +195,12 @@ void controlSystem() { #ifdef SERIAL - printf("controlSystem thread started"); + printf("controlSystem thread started\n"); #endif while(true) { - distance = ultrassonicSensor.distance(CM); + ballDistance = ultrassonicSensor.distance(CM); - if (distance != setpoint) { + if (ballDistance != setpoint) { statusFlag = ADJUSTING; } else { statusFlag = STABLE; @@ -118,9 +208,9 @@ //PID CONTROLLER //motor.write(motorPos); - controller.setProcessValue(distance); + controller.setProcessValue(ballDistance); motor.write(controller.compute()); - + Thread::wait(SAMPLE_RATE); } @@ -152,6 +242,11 @@ serialOutThread.start(serialOut); controlSystemThread.start(controlSystem); +#ifdef ETHERNET + ethernetSendThread.start(ethernetSend); + ethernetReceiveThread.start(ethernetReceive); +#endif + while(true) { //nothing }