Go to the documentation of this file.00001
00002 #include <ros/ros.h>
00003
00004
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
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
00035
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
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
00062
00063
00064
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
00113
00114 setVelocity();
00115 setAcceleration();
00116
00117 std::vector<double> target_positions(2,0);
00118 movePos(target_positions);
00119 initialized_ = true;
00120
00121 return true;
00122 }
00123
00124
00125
00126
00127
00128
00129 bool MapDemonCtrlMaestro::runCalibration()
00130 {
00131 return true;
00132 }
00133
00134
00135
00136
00137
00138
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
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
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
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
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
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 positions_[i] = int2rad(buf[0] | buf[1]<<8);
00218 }
00219 is_moving_ = isMoving();
00220 return true;
00221 }
00222
00223 bool MapDemonCtrlMaestro::isMoving()
00224 {
00225
00226
00227
00228 writeCmd(IS_MOVING);
00229
00230
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
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
00249
00250 movePos(positions_);
00251
00252 return true;
00253 }
00257 bool MapDemonCtrlMaestro::close()
00258 {
00259
00260
00261
00262 return true;
00263 }
00264
00268 bool MapDemonCtrlMaestro::recover()
00269 {
00270
00271
00272
00273
00274
00275
00276
00277
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
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
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
00316
00317
00318
00319
00320 if(write(fd_, s.c_str(), s.size())!=s.size())
00321 ROS_WARN("could not send to serial");
00322 }