Daiki Morita / Mbed OS UDP_ROS
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "EthernetInterface.h"
00002 #include "mbed.h"
00003 
00004 //イーサネット関連
00005 const char *myIP = "192.168.0.13";
00006 const char *mask = "255.255.255.0";
00007 const char *pcIP = "192.168.0.10";
00008 const int myPORT = 8080, pcPORT = 8080;
00009 EthernetInterface eth;
00010 UDPSocket socket;
00011 SocketAddress myAddr(myIP, myPORT);
00012 SocketAddress pcAddr(pcIP, pcPORT);
00013 #define MAX_LEN 1208
00014 char txdata[MAX_LEN];
00015 char rxdata[MAX_LEN];
00016 long counter = 0;
00017 
00018 void initialize_txdata() { // 0123...'A''B''C'...
00019   for (int i = 0; i < MAX_LEN; i++)
00020     txdata[i] = (i + counter) % 0x100;
00021   counter++;
00022 }
00023 
00024 int network_init() {
00025   initialize_txdata();
00026   // network ip set
00027   eth.set_network(myIP, mask, "");
00028   // connection test (return 0 @status is OK)
00029   if (eth.connect() != 0) { // occor error
00030     printf("eth connetction error\n");
00031     return -1;
00032   }
00033   socket.open(&eth);
00034   return true;
00035 }
00036 
00037 //スレッド管理
00038 Thread control_thread(osPriorityRealtime);
00039 #define CONTROL_THREAD_dt 1ms
00040 Thread communication_thread(osPriorityHigh);
00041 #define COMMUNICATION_THREAD_dt 1ms
00042 // Thread main_thread(osPriorityNormal);
00043 #define NORMAL_THREAD_dt 2000ms
00044 
00045 //表示LED
00046 DigitalOut led1(LED1);
00047 DigitalOut led2(LED2);
00048 DigitalOut led3(LED3);
00049 
00050 //制御ループ
00051 void control() {
00052   while (1) {
00053     led2 = !led2;
00054     ThisThread::sleep_for(CONTROL_THREAD_dt);
00055   }
00056 }
00057 
00058 //通信ループ
00059 void communication() {
00060   while (1) {
00061     led3 = !led3;
00062     initialize_txdata();
00063     if (0 > socket.sendto(pcAddr, txdata, sizeof(txdata))) {
00064       printf("eth sending error\n");
00065     }
00066     // printf("sended\n");
00067     ThisThread::sleep_for(COMMUNICATION_THREAD_dt);
00068   }
00069 }
00070 
00071 //その他ループ
00072 int main() {
00073   network_init();
00074   control_thread.start(control);
00075   communication_thread.start(communication);
00076   while (1) { // normal loop
00077     led1 = !led1;
00078     ThisThread::sleep_for(NORMAL_THREAD_dt);
00079   }
00080 }