wlkeyctrl.cpp
Go to the documentation of this file.
00001 /*
00002 *  Software License Agreement (BSD License)
00003 *  Copyright (c) 2013, Intelligent Robotics Lab, DLUT.
00004 *  All rights reserved.
00005 *  Author:¡­¡­
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *   * Redistributions of source code must retain the above copyright
00011 *     notice, this list of conditions and the following disclaimer.
00012 *   * Redistributions in binary form must reproduce the above
00013 *     copyright notice, this list of conditions and the following
00014 *     disclaimer in the documentation and/or other materials provided
00015 *     with the distribution.
00016 *   * Neither the name of the Intelligent Robotics Lab nor the names of its
00017 *     contributors may be used to endorse or promote products derived
00018 *     from this software without specific prior written permission.
00019 *
00020 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 *  POSSIBILITY OF SUCH DAMAGE.
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   //ros::init(argc, argv, "teleop_key");
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   // get the console in g_raw mode                                                              
00095   tcgetattr(g_kfd, &g_cooked);
00096   memcpy(&g_raw, &g_cooked, sizeof(struct termios));
00097   g_raw.c_lflag &= ~ (ICANON | ECHO);
00098   // Setting a new line, then end of file                         
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     // get the next event from the keyboard  
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      /* case KEYCODE_0:
00142         ROS_DEBUG("BEGIN");
00143         send(sockfd,L,1,0);
00144         break;
00145       case KEYCODE_ENTER:
00146         ROS_DEBUG("END");
00147         send(sockfd,L,1,0);
00148         break;
00149       default :
00150         break;
00151     */  
00152     }
00153   }
00154   return(0);
00155 }
00156 
00157     
00158   
00159 
00160 
00161 
00162 
00163   


wlkeyctrl
Author(s): wnl
autogenerated on Thu Feb 27 2014 11:31:29