Sistemas Operacionais - UTFPR

Dependencies:   EthernetInterface Servo mbed-rtos mbed

Committer:
marlonprudente
Date:
Thu Dec 14 12:59:02 2017 +0000
Revision:
5:c660c077ffc6
Parent:
3:269f1844a464
Sistemas Operacionais - UTFPR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marlonprudente 0:3f38d4693786 1 #include "mbed.h"
marlonprudente 0:3f38d4693786 2 #include "EthernetInterface.h"
marlonprudente 2:f3e4edf4d638 3 #include "Servo.h"
marlonprudente 1:7b6dd8cc286d 4 #include <string.h>
marlonprudente 0:3f38d4693786 5
marlonprudente 1:7b6dd8cc286d 6 #define POS_CENTRO 1750
marlonprudente 1:7b6dd8cc286d 7 #define POS_MAX (POS_CENTRO + 110)
marlonprudente 1:7b6dd8cc286d 8 #define POS_MIN (POS_CENTRO - 110)
marlonprudente 1:7b6dd8cc286d 9 #define DIST_MIN 10
marlonprudente 1:7b6dd8cc286d 10 #define DIST_MAX 56
marlonprudente 1:7b6dd8cc286d 11 #define RESOL 1
marlonprudente 1:7b6dd8cc286d 12 #define PERIODO 10
marlonprudente 0:3f38d4693786 13
marlonprudente 1:7b6dd8cc286d 14 const int ECHO_SERVER_PORT = 1234;
marlonprudente 0:3f38d4693786 15 const int BROADCAST_PORT = 58083;
marlonprudente 0:3f38d4693786 16
marlonprudente 0:3f38d4693786 17 bool conectado = false;
marlonprudente 0:3f38d4693786 18
marlonprudente 1:7b6dd8cc286d 19 int setpoint;
marlonprudente 1:7b6dd8cc286d 20
marlonprudente 1:7b6dd8cc286d 21 Thread tcp_server;
marlonprudente 1:7b6dd8cc286d 22 Thread broadcast;
marlonprudente 1:7b6dd8cc286d 23 EthernetInterface eth;
marlonprudente 1:7b6dd8cc286d 24
marlonprudente 1:7b6dd8cc286d 25 Mutex m_dist;
marlonprudente 1:7b6dd8cc286d 26 Mutex m_ref;
marlonprudente 1:7b6dd8cc286d 27
marlonprudente 1:7b6dd8cc286d 28 unsigned int dist;
marlonprudente 2:f3e4edf4d638 29 Servo Servo1(D6);
marlonprudente 1:7b6dd8cc286d 30
marlonprudente 1:7b6dd8cc286d 31 void tcp_server_thread ();
marlonprudente 1:7b6dd8cc286d 32 void udp_broadcast ();
marlonprudente 1:7b6dd8cc286d 33
marlonprudente 1:7b6dd8cc286d 34
marlonprudente 1:7b6dd8cc286d 35 int main (void)
marlonprudente 1:7b6dd8cc286d 36 {
marlonprudente 1:7b6dd8cc286d 37 //inicializa o módulo ethernet
marlonprudente 1:7b6dd8cc286d 38 eth.init();
marlonprudente 1:7b6dd8cc286d 39 eth.connect();
marlonprudente 3:269f1844a464 40
marlonprudente 2:f3e4edf4d638 41 Servo1.Enable(POS_CENTRO,20000);
marlonprudente 1:7b6dd8cc286d 42 // inicializa threads
marlonprudente 1:7b6dd8cc286d 43 tcp_server.start(tcp_server_thread);
marlonprudente 1:7b6dd8cc286d 44 broadcast.start(udp_broadcast);
marlonprudente 1:7b6dd8cc286d 45 //controle.start(controle_thread);
marlonprudente 3:269f1844a464 46
marlonprudente 1:7b6dd8cc286d 47 broadcast.join();
marlonprudente 1:7b6dd8cc286d 48 printf("Todas as thread iniciadas\n");
marlonprudente 1:7b6dd8cc286d 49
marlonprudente 0:3f38d4693786 50 while (true) {
marlonprudente 1:7b6dd8cc286d 51
marlonprudente 0:3f38d4693786 52 }
marlonprudente 0:3f38d4693786 53 }
marlonprudente 0:3f38d4693786 54
marlonprudente 1:7b6dd8cc286d 55 void tcp_server_thread ()
marlonprudente 1:7b6dd8cc286d 56 {
marlonprudente 1:7b6dd8cc286d 57 printf("\nServer IP Address is %s\n", eth.getIPAddress());
marlonprudente 0:3f38d4693786 58
marlonprudente 0:3f38d4693786 59 TCPSocketServer server;
marlonprudente 0:3f38d4693786 60 server.bind(ECHO_SERVER_PORT);
marlonprudente 0:3f38d4693786 61 server.listen();
marlonprudente 3:269f1844a464 62
marlonprudente 3:269f1844a464 63 printf("\nWait for new connection...\n");
marlonprudente 3:269f1844a464 64 TCPSocketConnection client;
marlonprudente 3:269f1844a464 65 server.accept(client);
marlonprudente 3:269f1844a464 66 client.set_blocking(false, 1500); // Timeout after (1.5)s
marlonprudente 0:3f38d4693786 67
marlonprudente 3:269f1844a464 68 conectado = true;
marlonprudente 3:269f1844a464 69 printf("\nConnection from: %s\n", client.get_address());
marlonprudente 3:269f1844a464 70 char buffer[256];
marlonprudente 3:269f1844a464 71 while (true) {
marlonprudente 3:269f1844a464 72 int n = client.receive(buffer, sizeof(buffer));
marlonprudente 3:269f1844a464 73
marlonprudente 3:269f1844a464 74 if (n <=1) {
marlonprudente 3:269f1844a464 75 //printf("Waiting messenger...");
marlonprudente 3:269f1844a464 76 //break;
marlonprudente 3:269f1844a464 77 } else {
marlonprudente 0:3f38d4693786 78
marlonprudente 0:3f38d4693786 79 // print received message to terminal
marlonprudente 0:3f38d4693786 80 buffer[n] = '\0';
marlonprudente 0:3f38d4693786 81 printf("Received message from Client :'%s'\n",buffer);
marlonprudente 3:269f1844a464 82
marlonprudente 0:3f38d4693786 83 int pos = buffer[8] - '0';
marlonprudente 3:269f1844a464 84
marlonprudente 0:3f38d4693786 85 if(pos == 1)setpoint = 20;
marlonprudente 0:3f38d4693786 86 else if(pos == 2)setpoint = 35;
marlonprudente 0:3f38d4693786 87 else if(pos == 3)setpoint = 50;
marlonprudente 0:3f38d4693786 88 printf("Setpoint: %d\n", setpoint);
marlonprudente 3:269f1844a464 89
marlonprudente 0:3f38d4693786 90 m_ref.lock();
marlonprudente 0:3f38d4693786 91 // ref = atof(buffer);
marlonprudente 0:3f38d4693786 92 sprintf (buffer, "%s", client.get_address());
marlonprudente 0:3f38d4693786 93 m_ref.unlock();
marlonprudente 3:269f1844a464 94
marlonprudente 0:3f38d4693786 95 m_ref.lock();
marlonprudente 0:3f38d4693786 96 sprintf (buffer, "%s", client.get_address());
marlonprudente 0:3f38d4693786 97 m_ref.unlock();
marlonprudente 3:269f1844a464 98
marlonprudente 0:3f38d4693786 99 // print sending message to terminal
marlonprudente 0:3f38d4693786 100 printf("Sending message to Client: '%s'\n",buffer);
marlonprudente 3:269f1844a464 101
marlonprudente 0:3f38d4693786 102 // Echo received message back to client
marlonprudente 0:3f38d4693786 103 //client.send_all(buffer, n);
marlonprudente 3:269f1844a464 104 // if (n <= 0) break;
marlonprudente 3:269f1844a464 105 memset(buffer,0,256);
marlonprudente 3:269f1844a464 106
marlonprudente 3:269f1844a464 107 if(buffer[0] == 'd') {
marlonprudente 3:269f1844a464 108 for(int pos = 1000; pos < 2000; pos +=25) {
marlonprudente 3:269f1844a464 109 Servo1.SetPosition(pos);
marlonprudente 3:269f1844a464 110 }
marlonprudente 3:269f1844a464 111 } else if(buffer[0] == 'e') {
marlonprudente 3:269f1844a464 112 for(int pos = 2000; pos > 1000; pos -=25) {
marlonprudente 3:269f1844a464 113 Servo1.SetPosition(pos);
marlonprudente 3:269f1844a464 114 }
marlonprudente 1:7b6dd8cc286d 115 }
marlonprudente 0:3f38d4693786 116
marlonprudente 3:269f1844a464 117 }
marlonprudente 3:269f1844a464 118 }
marlonprudente 3:269f1844a464 119
marlonprudente 3:269f1844a464 120 client.close();
marlonprudente 0:3f38d4693786 121 }
marlonprudente 0:3f38d4693786 122
marlonprudente 1:7b6dd8cc286d 123 void udp_broadcast ()
marlonprudente 1:7b6dd8cc286d 124 {
marlonprudente 0:3f38d4693786 125 UDPSocket sock;
marlonprudente 0:3f38d4693786 126 sock.init();
marlonprudente 0:3f38d4693786 127 sock.set_broadcasting();
marlonprudente 0:3f38d4693786 128
marlonprudente 0:3f38d4693786 129 Endpoint broadcast;
marlonprudente 0:3f38d4693786 130 broadcast.set_address("255.255.255.255", BROADCAST_PORT);
marlonprudente 0:3f38d4693786 131
marlonprudente 1:7b6dd8cc286d 132 char out_buffer[] = "Coppeti";
marlonprudente 0:3f38d4693786 133
marlonprudente 0:3f38d4693786 134 while (!conectado) {
marlonprudente 0:3f38d4693786 135 printf("Broadcasting...\n");
marlonprudente 0:3f38d4693786 136 sock.sendTo(broadcast, out_buffer, sizeof(out_buffer));
marlonprudente 0:3f38d4693786 137 Thread::wait(1000);
marlonprudente 0:3f38d4693786 138 }
marlonprudente 0:3f38d4693786 139 printf("Broadcast finalizado!\n");
marlonprudente 0:3f38d4693786 140 }