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 #include "sick_laser/motor.h"
00034 #include <ros/ros.h>
00035
00036 void Com::comOpen ()
00037 {
00038 if (m_comport_ == 1)
00039 {
00040 m_fd_ = open ("/dev/ttyr00", O_RDWR | O_NOCTTY | O_NDELAY);
00041 if (-1 == m_fd_)
00042 {
00043 perror ("open port 1 FAIL!\n");
00044 }
00045 else
00046 {
00047 ROS_INFO ("open port 1 SUCCESSFUL");
00048 }
00049 }
00050 else if (m_comport_ == 2)
00051 {
00052 m_fd_ = open ("/dev/ttyr01", O_RDWR | O_NOCTTY | O_NDELAY);
00053 if (-1 == m_fd_)
00054 {
00055 perror ("open port 2 FAIL!\n");
00056 }
00057 else
00058 {
00059 ROS_INFO ("open port 2 SUCCESSFUL");
00060 }
00061 }
00062
00063
00064 if (fcntl (m_fd_, F_SETFL, 0) < 0)
00065 ROS_INFO ("set the port to Blocked FAIL!");
00066 else
00067 ROS_INFO ("set the port to Blocked");
00068 if (isatty (STDIN_FILENO) == 0)
00069 ROS_INFO ("It is NOT a terminal equipment");
00070 else
00071 ROS_INFO ("It is a tty equipment");
00072 }
00073
00074 void Com::comSet (int n_speed, int n_bits, char c_event, int n_stop)
00075 {
00076 struct termios newtio, oldtio;
00077 if (tcgetattr (m_fd_, &oldtio) != 0)
00078 {
00079 perror ("save the old parameters FAIL!\n");
00080 }
00081 bzero (&newtio, sizeof (newtio));
00082
00083 newtio.c_cflag |= CLOCAL | CREAD;
00084 newtio.c_cflag &= ~CSIZE;
00085
00086 switch (n_bits)
00087 {
00088 case 7:
00089 newtio.c_cflag |= CS7;
00090 break;
00091 case 8:
00092 newtio.c_cflag |= CS8;
00093 break;
00094 }
00095
00096 switch (c_event)
00097 {
00098 case 'O':
00099 newtio.c_cflag |= PARENB;
00100 newtio.c_cflag |= PARODD;
00101 newtio.c_iflag |= (INPCK | ISTRIP);
00102 break;
00103 case 'E':
00104 newtio.c_iflag |= (INPCK | ISTRIP);
00105 newtio.c_cflag |= PARENB;
00106 newtio.c_cflag &= ~PARODD;
00107 break;
00108 case 'N':
00109 newtio.c_cflag &= ~PARENB;
00110 break;
00111 }
00112
00113 switch (n_speed)
00114 {
00115 case 2400:
00116 cfsetispeed (&newtio, B2400);
00117 cfsetospeed (&newtio, B2400);
00118 break;
00119 case 4800:
00120 cfsetispeed (&newtio, B4800);
00121 cfsetospeed (&newtio, B4800);
00122 break;
00123 case 9600:
00124 cfsetispeed (&newtio, B9600);
00125 cfsetospeed (&newtio, B9600);
00126 break;
00127 case 19200:
00128 cfsetispeed (&newtio, B19200);
00129 cfsetospeed (&newtio, B19200);
00130 break;
00131 case 38400:
00132 cfsetispeed (&newtio, B38400);
00133 cfsetospeed (&newtio, B38400);
00134 break;
00135 case 57600:
00136 cfsetispeed (&newtio, B57600);
00137 cfsetospeed (&newtio, B57600);
00138 break;
00139 case 115200:
00140 cfsetispeed (&newtio, B115200);
00141 cfsetospeed (&newtio, B115200);
00142 break;
00143 default:
00144 cfsetispeed (&newtio, B9600);
00145 cfsetospeed (&newtio, B9600);
00146 break;
00147 }
00148
00149 if (n_stop == 1)
00150 newtio.c_cflag &= ~CSTOPB;
00151 else if (n_stop == 2)
00152 newtio.c_cflag |= CSTOPB;
00153
00154 newtio.c_cc[VTIME] = 0;
00155 newtio.c_cc[VMIN] = 0;
00156
00157 tcflush (m_fd_, TCIFLUSH);
00158
00159 if ((tcsetattr (m_fd_, TCSANOW, &newtio)) != 0)
00160 {
00161 perror ("activate the new set FAIL");
00162 }
00163 ROS_INFO ("set port SUCCESSFUL!");
00164 }
00165
00166 void Com::comWrite (char *buf)
00167 {
00168 int n_write;
00169 n_write = write (m_fd_, buf, strlen (buf));
00170 if (n_write < 0)
00171 {
00172 ROS_INFO ("write error");
00173 }
00174 }
00175
00176 void Com::comClose ()
00177 {
00178 close (m_fd_);
00179 }
00180
00181 int Motor::getPulseNum ()
00182 {
00183 return (m_degree_ * TRANS_RATIO / STEP_DIS / SUBDIVISION);
00184 }
00185
00186 double Motor::motorMove ()
00187 {
00188 comOpen ();
00189 comSet (19200, 8, 'N', 1);
00190 char buf[30];
00191 int n_pnum = 0;
00192 n_pnum = getPulseNum ();
00193 sprintf(buf, "M%c%06d%06d", m_direction_, m_frequence_, n_pnum);
00194 comWrite (buf);
00195 double time_motor_move = ros::Time::now ().toSec ();
00196
00197 ROS_INFO ("The command is %s", buf);
00198 comClose ();
00199
00200 return time_motor_move;
00201 }