Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface Servo mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "Servo.h" 00004 #include <string.h> 00005 00006 #define POS_CENTRO 1750 00007 #define POS_MAX (POS_CENTRO + 110) 00008 #define POS_MIN (POS_CENTRO - 110) 00009 #define DIST_MIN 10 00010 #define DIST_MAX 56 00011 #define RESOL 1 00012 #define PERIODO 10 00013 00014 const int ECHO_SERVER_PORT = 1234; 00015 const int BROADCAST_PORT = 58083; 00016 00017 bool conectado = false; 00018 00019 int setpoint; 00020 00021 Thread tcp_server; 00022 Thread broadcast; 00023 EthernetInterface eth; 00024 00025 Mutex m_dist; 00026 Mutex m_ref; 00027 00028 unsigned int dist; 00029 Servo Servo1(D6); 00030 00031 void tcp_server_thread (); 00032 void udp_broadcast (); 00033 00034 00035 int main (void) 00036 { 00037 //inicializa o módulo ethernet 00038 eth.init(); 00039 eth.connect(); 00040 00041 Servo1.Enable(POS_CENTRO,20000); 00042 // inicializa threads 00043 tcp_server.start(tcp_server_thread); 00044 broadcast.start(udp_broadcast); 00045 //controle.start(controle_thread); 00046 00047 broadcast.join(); 00048 printf("Todas as thread iniciadas\n"); 00049 00050 while (true) { 00051 00052 } 00053 } 00054 00055 void tcp_server_thread () 00056 { 00057 printf("\nServer IP Address is %s\n", eth.getIPAddress()); 00058 00059 TCPSocketServer server; 00060 server.bind(ECHO_SERVER_PORT); 00061 server.listen(); 00062 00063 printf("\nWait for new connection...\n"); 00064 TCPSocketConnection client; 00065 server.accept(client); 00066 client.set_blocking(false, 1500); // Timeout after (1.5)s 00067 00068 conectado = true; 00069 printf("\nConnection from: %s\n", client.get_address()); 00070 char buffer[256]; 00071 while (true) { 00072 int n = client.receive(buffer, sizeof(buffer)); 00073 00074 if (n <=1) { 00075 //printf("Waiting messenger..."); 00076 //break; 00077 } else { 00078 00079 // print received message to terminal 00080 buffer[n] = '\0'; 00081 printf("Received message from Client :'%s'\n",buffer); 00082 00083 int pos = buffer[8] - '0'; 00084 00085 if(pos == 1)setpoint = 20; 00086 else if(pos == 2)setpoint = 35; 00087 else if(pos == 3)setpoint = 50; 00088 printf("Setpoint: %d\n", setpoint); 00089 00090 m_ref.lock(); 00091 // ref = atof(buffer); 00092 sprintf (buffer, "%s", client.get_address()); 00093 m_ref.unlock(); 00094 00095 m_ref.lock(); 00096 sprintf (buffer, "%s", client.get_address()); 00097 m_ref.unlock(); 00098 00099 // print sending message to terminal 00100 printf("Sending message to Client: '%s'\n",buffer); 00101 00102 // Echo received message back to client 00103 //client.send_all(buffer, n); 00104 // if (n <= 0) break; 00105 memset(buffer,0,256); 00106 00107 if(buffer[0] == 'd') { 00108 for(int pos = 1000; pos < 2000; pos +=25) { 00109 Servo1.SetPosition(pos); 00110 } 00111 } else if(buffer[0] == 'e') { 00112 for(int pos = 2000; pos > 1000; pos -=25) { 00113 Servo1.SetPosition(pos); 00114 } 00115 } 00116 00117 } 00118 } 00119 00120 client.close(); 00121 } 00122 00123 void udp_broadcast () 00124 { 00125 UDPSocket sock; 00126 sock.init(); 00127 sock.set_broadcasting(); 00128 00129 Endpoint broadcast; 00130 broadcast.set_address("255.255.255.255", BROADCAST_PORT); 00131 00132 char out_buffer[] = "Coppeti"; 00133 00134 while (!conectado) { 00135 printf("Broadcasting...\n"); 00136 sock.sendTo(broadcast, out_buffer, sizeof(out_buffer)); 00137 Thread::wait(1000); 00138 } 00139 printf("Broadcast finalizado!\n"); 00140 }
Generated on Wed Jul 13 2022 02:57:29 by
1.7.2