motor.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:Zhao Cilang,Yan Fei,Zhuang Yan.
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 #include "sick_laser/motor.h"
00034 #include <ros/ros.h>
00035 
00036 void Com::comOpen ()
00037 {
00038   if (m_comport_ == 1)          //serial port 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)     //serial port 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   /*set the port's status */
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   /*set character size */
00083   newtio.c_cflag |= CLOCAL | CREAD;
00084   newtio.c_cflag &= ~CSIZE;
00085   /*set data bit */
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   /*set parity bit */
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   /*set baud rate */
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   /*set stop bit */
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   /*activate the new set */
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);    //set the port's parameters
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);    //get the motor control command with paraters that we set.
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 }


sick_laser
Author(s): Zhao Cilang,Yan Fei,Zhuang Yan/zhuang@dlut.edu.cn
autogenerated on Sun Jan 5 2014 11:05:03