demonstrator_control_maestro.cpp
Go to the documentation of this file.
00001 // ROS includes
00002 #include <ros/ros.h>
00003 
00004 // standard includes
00005 #include <boost/thread.hpp>
00006 #include <string>
00007 #include <cstring>
00008 #include <stdlib.h>
00009 #include <math.h>
00010 
00011 #include <termios.h>
00012 #include <fcntl.h>
00013 #include <unistd.h>
00014 
00015 // own includes
00016 #include <cob_3d_mapping_demonstrator/demonstrator_control_maestro.h>
00017 
00018 MapDemonCtrlMaestro::MapDemonCtrlMaestro(DemonstratorParams * params):
00019 MapDemonCtrl(params)
00020 {
00021 }
00022 
00024 MapDemonCtrlMaestro::~MapDemonCtrlMaestro()
00025 {
00026 }
00027 
00031 bool MapDemonCtrlMaestro::init(DemonstratorParams * params)
00032 {
00034   //std::string SerialDeviceName = m_params_->GetSerialDevice();
00035   //int SerialBaudrate = m_params_->GetBaudRate();
00036   std::ostringstream errorMsg;
00037 
00038   int DOF = params_->getDOF();
00039   positions_.resize(DOF, 0);
00040   old_positions_.resize(DOF, 0);
00041   velocities_.resize(DOF, 0);
00042 
00043 
00044   std::vector<std::string> joint_names = params_->getJointNames();
00045   //std::vector<double> MaxVel = params_->getMaxVel();
00046   std::vector<double> vels = params_->getVels();
00047   std::vector<double> offsets = params_->getOffsets();
00048   std::vector<double> lower_limits = params_->getLowerLimits();
00049   std::vector<double> upper_limits = params_->getUpperLimits();
00050 
00051 
00052   std::cout << "============================================================================== " << std::endl;
00053   std::cout << "Mapping Demonstrator Init: Trying to initialize with the following parameters: " << std::endl;
00054 
00055   std::cout << std::endl << "Joint Names:\t\t";
00056   for (int i = 0; i < DOF; i++)
00057   {
00058     std::cout << joint_names[i] << "\t";
00059   }
00060 
00061   /*std::cout << std::endl << "maxVel     :\t\t";
00062   for (int i = 0; i < DOF; i++)
00063   {
00064     std::cout << MaxVel[i] << "\t";
00065   }*/
00066 
00067   std::cout << std::endl << "velocities:\t\t";
00068   for (int i = 0; i < DOF; i++)
00069   {
00070     std::cout << vels[i] << "\t";
00071   }
00072 
00073   std::cout << std::endl << "upperLimits:\t\t";
00074   for (int i = 0; i < DOF; i++)
00075   {
00076     std::cout << upper_limits[i] << "\t";
00077   }
00078 
00079   std::cout << std::endl << "lowerLimits:\t\t";
00080   for (int i = 0; i < DOF; i++)
00081   {
00082     std::cout << lower_limits[i] << "\t";
00083   }
00084 
00085   std::cout << std::endl << "offsets    :\t\t";
00086   for (int i = 0; i < DOF; i++)
00087   {
00088     std::cout << offsets[i] << "\t";
00089   }
00090 
00091   std::cout << std::endl << "============================================================================== " << std::endl;
00092 
00094 
00095   fd_ = open(params_->getSerialDevice().c_str(), O_RDWR | O_NOCTTY);
00096 
00098   if (fd_ == -1 ) {
00099     errorMsg << "Could not open device " << params_->getSerialDevice();
00100     error_message_ = errorMsg.str();
00101     return false;
00102   }
00103 
00104   struct termios options;
00105   tcgetattr(fd_, &options);
00106   options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
00107   options.c_oflag &= ~(ONLCR | OCRNL);
00108   tcsetattr(fd_, TCSANOW, &options);
00109 
00110   usleep(500000);
00111 
00112   //if(!updatePositions()) return false;
00113 
00114   setVelocity();
00115   setAcceleration();
00116   //writeCmd(GO_HOME);
00117   std::vector<double> target_positions(2,0);
00118   movePos(target_positions);
00119   initialized_ = true;
00120 
00121   return true;
00122 }
00123 
00124 /*
00125  * \brief Run Calibration
00126  *
00127  *
00128  */
00129 bool MapDemonCtrlMaestro::runCalibration()
00130 {
00131   return true;
00132 }
00133 
00134 /*
00135  * \brief Move both joints to designed position, constant velocity
00136  *
00137  * This will use the velocity defined in parameter server as Velocity
00138  * \param JointNames Vector target_positions Vector
00139  */
00140 bool MapDemonCtrlMaestro::movePos( const std::vector<double>& target_positions )
00141 {
00142   int DOF = params_->getDOF();
00143 
00144   for(int i=0; i<DOF; i++) {
00145     if(target_positions[i] > params_->getUpperLimits()[i] || target_positions[i] < params_->getLowerLimits()[i])
00146     {
00147       std::stringstream error_msg;
00148       error_msg << "Position " << target_positions[i] << " exceeds limit for axis " << i;
00149       error_message_ = error_msg.str();
00150       return false;
00151     }
00152     int pos = rad2int(target_positions[i], i);
00153     //ROS_INFO("move pos %f", target_positions[i]);
00154     unsigned char list[2] = {(unsigned short)pos & 0x7F, (unsigned short)(pos >> 7) & 0x7F};
00155     writeCmd(SET_TARGET, i, list, 2);
00156   }
00157 
00158   return true;
00159 }
00160 
00161 /*
00162  * \brief Move joints with calculated velocities
00163  *
00164  * Calculating positions and times by desired value of the cob_trajectory_controller
00165  * \param velocities Vector
00166  */
00167 /*bool MapDemonCtrlMaestro::MoveVel(const std::vector<double>& target_velocities)
00168 {
00169   ROS_ERROR("not supported yet");
00170   return false;
00171 }*/
00172 
00173 /*
00174  * \brief Queries the robot its real position
00175  *
00176  * The robot answers p, e, t
00177  * \param
00178  */
00179 bool MapDemonCtrlMaestro::updatePositions()
00180 {
00181   int DOF = params_->getDOF();
00182 
00183   for(int i=0; i<DOF; i++) {
00184     writeCmd(GET_POSITION, i);
00185 
00186     //std::string str("");
00187 
00188     unsigned char buf[2];
00189     while(read(fd_,buf,sizeof(buf)) != sizeof(buf))
00190     {
00191       usleep(1000);
00192       ROS_ERROR("error reading");
00193       //return false;
00194     }
00195     /*unsigned int ctr=0;
00196     while(str.size()<2)
00197     {
00198       char buf[255];
00199       size_t nbytes;
00200 
00201       nbytes = read(fd_, buf, 255);
00202       str += std::string(buf, nbytes);
00203 
00204 //      std::cout<<"recv: ";
00205 //      for(int i=0; i<str.size(); i++)
00206 //        printf("%x ",(int)(unsigned char)str[i]);
00207 //      std::cout<<"\n";
00208 
00209       ++ctr;
00210       usleep(10000);
00211 
00212       if(ctr>100) {
00213         ROS_ERROR("failed to connect to device");
00214         return false;
00215       }
00216     }*/
00217     positions_[i] = int2rad(buf[0] | buf[1]<<8/*(unsigned char)str[0] + ((unsigned char)str[1])*256*/);
00218   }
00219   is_moving_ = isMoving();
00220   return true;
00221 }
00222 
00223 bool MapDemonCtrlMaestro::isMoving()
00224 {
00225   //int DOF = params_->getDOF();
00226 
00227   //for(int i=0; i<DOF; i++) {
00228     writeCmd(IS_MOVING);
00229 
00230     //std::string str("");
00231 
00232     unsigned char buf[1];
00233     while(read(fd_,buf,sizeof(buf)) != sizeof(buf))
00234     {
00235       usleep(1000);
00236       ROS_ERROR("error reading");
00237     }
00238     //std::cout << "isM: " << (unsigned int)buf[0] << std::endl;
00239     if((unsigned int)buf[0] == 1) return true;
00240     else return false;
00241   //}
00242 
00243   return true;
00244 }
00245 
00246 bool MapDemonCtrlMaestro::stop()
00247 {
00248   //updatePositions();
00249   //ROS_INFO("pos: %f, %f", positions_[0], positions_[1]);
00250   movePos(positions_);
00251   //ROS_ERROR("not implemented");
00252   return true;
00253 }
00257 bool MapDemonCtrlMaestro::close()
00258 {
00259   /*initialized_ = false;
00260   sd_->closePort();
00261   serial_device_opened_ = false;*/
00262   return true;
00263 }
00264 
00268 bool MapDemonCtrlMaestro::recover()
00269 {
00270   /*std::ostringstream errorMsg;
00271 
00272   if( !sd_->checkIfStillThere() )
00273   {
00274     errorMsg << "COB3DMD not detected anymore.";
00275     sd_->closePort();
00276     error_message_ = errorMsg.str();
00277     return false;
00278   }*/
00279   return true;
00280 }
00281 
00282 void MapDemonCtrlMaestro::setVelocity()
00283 {
00284   int DOF = params_->getDOF();
00285   for(int i=0; i<DOF; i++) {
00286     unsigned int v = params_->getVels()[i]*72;
00287     //ROS_INFO("Setting vel to %d.", v);
00288     unsigned char vel[2] = {(unsigned short)v & 0x7F, (unsigned short)(v >> 7) & 0x7F};
00289     writeCmd(SET_VEL, i, vel, 2);
00290   }
00291 }
00292 
00293 void MapDemonCtrlMaestro::setAcceleration()
00294 {
00295   int DOF = params_->getDOF();
00296   for(int i=0; i<DOF; i++) {
00297     unsigned int v = params_->getAccels()[i]*228.6;
00298     ROS_INFO("Setting accel to %d.", v);
00299     unsigned char accel[2] = {(unsigned short)v & 0x7F, (unsigned short)(v >> 7) & 0x7F};
00300     writeCmd(SET_ACCEL, i, accel, 2);
00301   }
00302 }
00303 
00304 void MapDemonCtrlMaestro::writeCmd(const unsigned char cmd, const unsigned char channel, const unsigned char *data, const int size)
00305 {
00306   //ROS_ASSERT(sd_);
00307   std::string s;
00308   s.push_back(cmd);
00309   if( channel != 255)
00310     s.push_back(channel);
00311   for(int i=0; i<size; i++)
00312     s.push_back(data[i]);
00313 
00314 
00315   /*std::cout<<"writing: ";
00316   for(unsigned int i=0; i<s.size(); i++)
00317     printf("%x ",(int)(unsigned char)s[i]);
00318   std::cout<<"\n";*/
00319 
00320   if(write(fd_, s.c_str(), s.size())!=s.size())
00321     ROS_WARN("could not send to serial");
00322 }


cob_3d_mapping_demonstrator
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:46