Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <errno.h>
00039 #include <unistd.h>
00040 #include <string.h>
00041 #include <netinet/in.h>
00042 #include <arpa/inet.h>
00043 #include "ros/ros.h"
00044 #include <signal.h>
00045 #include <signal.h>
00046 #include <termios.h>
00047 #include "time.h"
00048 #include <sys/file.h>
00049 #include <pthread.h>
00050 #include <std_msgs/Bool.h>
00051 #include <termios.h>
00052 #include <ros/ros.h>
00053
00054 #define CLIENT_PORT 3333
00055 #define IP_ADDRESS "192.168.1.241"
00056 #define KEYCODE_R 0x43
00057 #define KEYCODE_L 0x44
00058 #define KEYCODE_U 0x41
00059 #define KEYCODE_D 0x42
00060 #define KEYCODE_Q 0x71
00061 #define KEYCODE_SPACE 0x20
00062 #define KEYCODE_0 0x30
00063 #define KEYCODE_ENTER 0x0A
00064
00065 int main(int argc, char** argv)
00066 {
00067
00068 char buff[5];
00069 char c;
00070 int sockfd;
00071 int g_kfd=0;
00072 struct termios g_cooked, g_raw;
00073 struct sockaddr_in server_sockaddr;
00074 sockfd=socket(AF_INET,SOCK_STREAM,0);
00075 if(sockfd<0)
00076 {
00077 ROS_INFO("socket create failed\n");
00078 return -1;
00079 }
00080 ROS_INFO("socket success!,sockfd = %d\n",sockfd);
00081
00082 memset(server_sockaddr.sin_zero, 0x00, 8);
00083 server_sockaddr.sin_family = AF_INET;
00084 server_sockaddr.sin_port = htons(CLIENT_PORT);
00085 server_sockaddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
00086
00087 ROS_INFO("connecting...\n");
00088
00089 if(connect(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))<0)
00090 {
00091 ROS_INFO("connect error");
00092 return -1;
00093 }
00094
00095 tcgetattr(g_kfd, &g_cooked);
00096 memcpy(&g_raw, &g_cooked, sizeof(struct termios));
00097 g_raw.c_lflag &= ~ (ICANON | ECHO);
00098
00099 g_raw.c_cc[VEOL] = 1;
00100 g_raw.c_cc[VEOF] = 2;
00101 tcsetattr(g_kfd, TCSANOW, &g_raw);
00102
00103 puts("Reading from keyboard");
00104 puts("---------------------------");
00105 puts("Use arrow keys to move the turtle.");
00106 for(;;)
00107 {
00108
00109 if(read(g_kfd, &c, 1) < 0)
00110 {
00111 perror("read():");
00112 exit(-1);
00113 }
00114
00115 switch(c)
00116 {
00117 case KEYCODE_L:
00118 ROS_DEBUG("LEFT");
00119 send(sockfd,"L",1,0);
00120 sleep(0.5);
00121 break;
00122 case KEYCODE_R:
00123 ROS_DEBUG("RIGHT");
00124 send(sockfd,"R",1,0);
00125 sleep(0.5);
00126 break;
00127 case KEYCODE_U:
00128 ROS_DEBUG("UP");
00129 send(sockfd,"F",1,0);
00130 sleep(0.5);
00131 break;
00132 case KEYCODE_D:
00133 ROS_DEBUG("DOWN");
00134 send(sockfd,"B",1,0);
00135 sleep(0.5);
00136 break;
00137 case KEYCODE_SPACE:
00138 ROS_DEBUG("STOP");
00139 send(sockfd,"S",1,0);
00140 break;
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 }
00153 }
00154 return(0);
00155 }
00156
00157
00158
00159
00160
00161
00162
00163