Go to the documentation of this file.00001 #include "ros/ros.h"
00002 #include "ssc32.hpp"
00003
00004 namespace Servo
00005 {
00006
00007 SSC32::SSC32(){
00008 nrOfPorts = 32;
00009 }
00010
00011
00012 SSC32::~SSC32() {}
00013
00014 void SSC32::initSerial()
00015 {
00016
00017 cfsetispeed(&port_settings, BAUDRATE);
00018 cfsetospeed(&port_settings, BAUDRATE);
00019
00020
00021 timeout.tv_sec = TIMEOUT_SEC;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 }
00043
00044 bool SSC32::startSerial(std::string COM_PORT_)
00045 {
00046 const char *COM_PORT;
00047 COM_PORT = COM_PORT_.c_str();
00048 int flags;
00049 printf("COM_PORT = %s \n",COM_PORT);
00050
00051 fd = open(COM_PORT, O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR);
00052
00053 if(fd == -1)
00054 {
00055 std::cout << "(SSC-32) Unable to open " << COM_PORT<< "\n" << std::endl;
00056 return false;
00057 }
00058
00059 if (tcflush(fd, TCIFLUSH) < 0)
00060 {
00061 close(fd);
00062 fd = -1;
00063 return false;
00064 }
00065 if (tcgetattr(fd, &port_settings) < 0)
00066 {
00067 close(fd);
00068 fd = -1;
00069 return false;
00070 }
00071
00072 cfmakeraw(&port_settings);
00073 cfsetispeed(&port_settings, BAUDRATE);
00074 cfsetospeed(&port_settings, BAUDRATE);
00075
00076 if (tcsetattr(fd, TCSAFLUSH, &port_settings) < 0)
00077 {
00078 perror("corobot_open():tcsetattr():");
00079 close(fd);
00080 fd = -1;
00081 return false;
00082 }
00083
00084
00085
00086 if ((flags = fcntl(fd, F_GETFL)) < 0)
00087 {
00088 perror("corobot_comm_open():fcntl():");
00089 close(fd);
00090 fd = -1;
00091 return false;
00092 }
00093 if (fcntl(fd, F_SETFL, flags ^ O_NONBLOCK) < 0)
00094 {
00095 perror("corobot_comm_open():fcntl()");
00096 close(fd);
00097 fd = -1;
00098 return false;
00099 }
00100 else
00101 {
00102 std::cout << "(SSC-32) Opened " << COM_PORT << "\n" << std::endl;
00103 return true;
00104 }
00105
00106 }
00107
00108 void SSC32::stopSerial()
00109 {
00110
00111 SendMessage(0,0,0);
00112 SendMessage(1,0,0);
00113
00114 close(fd);
00115 }
00116
00117 bool SSC32::SendMessage(int port, int position, int speed)
00118 {
00119
00120 if(port >= nrOfPorts || port < 0)
00121 {
00122 std::cout << "(SSC-32) Port number not valid: " << port << std::endl;
00123 return false;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 std::ostringstream temp, temp2, temp3;
00139 temp << port;
00140 Port = temp.str();
00141
00142 temp2 << position;
00143 Position = temp2.str();
00144
00145 temp3 << speed;
00146 Speed = temp3.str();
00147
00148
00149 if(speed == -1)
00150 command= "#" + Port + " P" + Position + " \r";
00151 else
00152 command= "#" + Port + " P" + Position + " T" + Speed + " \r";
00153 Output = command.c_str();
00154
00155
00156 int err = write(fd,Output,strlen(Output));
00157 std::cout << "(SSC32) " << command << "\n" << std::endl;
00158
00159 if(err == -1)
00160 ROS_ERROR("Error sending command to SSC32 controller");
00161 else if(err == 0)
00162 ROS_WARN("The command was not send to the SSC32 controller");
00163 else if(err != ((int) strlen(Output)))
00164 ROS_WARN("Only a part of the command was sent to the SSC32 controller, %d bits out of %d", err, (int) strlen(Output));
00165 else
00166 ROS_DEBUG("Command sent successfully to the SSC32 Controller");
00167 return true;
00168 }
00169
00170 bool SSC32::CheckIfFinished()
00171 {
00172 char someData;
00173 int err = write(fd,"Q \r",strlen("Q \r"));
00174 err = read(fd,&someData,1);
00175 if (char(someData) == '.')
00176 {
00177 std::cout << "(SSC32) Servos have finished rotating " << "\n" << std::endl;
00178 return true;
00179 }
00180 return false;
00181 }
00182 }