Go to the documentation of this file.00001 #include "segway_rmp200_driver.h"
00002 #include "mutexexceptions.h"
00003 #include <ros/console.h>
00004
00005 using namespace segway_rmp200_node;
00006
00007 SegwayRmp200Driver::SegwayRmp200Driver()
00008 {
00009 ftdi_server_ = CFTDIServer::instance();
00010 serial_number_ = "NULL";
00011 segway_ = new CSegwayRMP200();
00012 }
00013
00014 bool SegwayRmp200Driver::openDriver(void)
00015 {
00016 try
00017 {
00018 this->lock();
00019 if(this->serial_number_.compare("NULL") == 0)
00020 {
00021 ROS_DEBUG("No segway serial provided, attempting auto-connect");
00022 segway_->connect();
00023 serial_number_ = segway_->get_serial();
00024 }
00025 else
00026 {
00027 std::vector<int> ftdi_devs;
00028 std::string serial;
00029 unsigned int ii;
00030
00031 ftdi_devs = ftdi_server_->get_ids_by_description(CSegwayRMP200::description);
00032 for(ii=0; ii<ftdi_devs.size(); ii++)
00033 {
00034 serial = ftdi_server_->get_serial_number(ftdi_devs.at(ii));
00035 if(serial == serial_number_)
00036 {
00037 ROS_DEBUG("Segway serial provided matches FTDI serial list");
00038 segway_->connect(serial);
00039 break;
00040 }
00041 }
00042 if(ii == ftdi_devs.size())
00043 {
00044 serial_number_ = "NULL";
00045 this->unlock();
00046 return false;
00047 }
00048 }
00049
00050 setSegwayEvents();
00051
00052 this->unlock();
00053 }
00054 catch(CException &e)
00055 {
00056 this->unlock();
00057 ROS_FATAL("'%s'",e.what().c_str());
00058 return false;
00059 }
00060
00061 ROS_INFO("Segway %s successfully connected", serial_number_.c_str());
00062 return true;
00063 }
00064
00065 bool SegwayRmp200Driver::closeDriver(void)
00066 {
00067 try
00068 {
00069 this->lock();
00070 ROS_DEBUG("Stopping and closing platform");
00071 events_.clear();
00072 segway_->close();
00073
00074
00075 this->unlock();
00076 }
00077 catch (CException & e)
00078 {
00079 ROS_ERROR("'%s'", e.what().c_str());
00080 return false;
00081 }
00082
00083 return true;
00084 }
00085
00086 bool SegwayRmp200Driver::startDriver(void)
00087 {
00088 return true;
00089 }
00090
00091 bool SegwayRmp200Driver::stopDriver(void)
00092 {
00093 return true;
00094 }
00095
00096 void SegwayRmp200Driver::config_update(Config& new_cfg, uint32_t level)
00097 {
00098 this->lock();
00099
00100 try
00101 {
00102 if(this->getState() > SegwayRmp200Driver::CLOSED)
00103 {
00104 segway_->set_velocity_scale_factor(new_cfg.velocity_scale_factor);
00105 segway_->set_acceleration_scale_factor(new_cfg.acceleration_scale_factor);
00106 segway_->set_turnrate_scale_factor(new_cfg.turn_rate_scale_factor);
00107 segway_->set_currentlimit_scale_factor(new_cfg.current_limit_scale_factor);
00108 segway_->set_gain_schedule((gain)new_cfg.gain_schedule);
00109 segway_->set_operation_mode((op_mode)new_cfg.operation_mode);
00110 if(new_cfg.balance_lockout)
00111 segway_->lock_balance();
00112 else
00113 segway_->unlock_balance();
00114 }
00115 else
00116 {
00117
00118 serial_number_ = new_cfg.serial_number;
00119 }
00120
00121 if( segway_ != NULL)
00122 {
00123 new_cfg.gain_schedule = (int)segway_->get_gain_schedule();
00124 new_cfg.operation_mode = (int)segway_->get_operation_mode();
00125 new_cfg.serial_number = segway_->get_serial();
00126 }
00127 }
00128 catch(CException &e)
00129 {
00130 ROS_ERROR("'%s'", e.what().c_str());
00131 }
00132
00133
00134 config_ = new_cfg;
00135
00136 this->unlock();
00137 }
00138
00139 SegwayRmp200Driver::~SegwayRmp200Driver()
00140 {
00141 try
00142 {
00143 if(segway_ != NULL)
00144 delete segway_;
00145 segway_ = NULL;
00146 }
00147 catch(CMutexException &e)
00148 {
00149 ROS_ERROR("'%s'", e.what().c_str());
00150 }
00151 }
00152
00153 iri_segway_rmp_msgs::SegwayRMP200Status
00154 SegwayRmp200Driver::get_status(void)
00155 {
00156 iri_segway_rmp_msgs::SegwayRMP200Status status;
00157
00158 if (segway_ != NULL)
00159 status = build_ROS_status_msg(segway_->get_status());
00160
00161 return status;
00162 }
00163
00164 float SegwayRmp200Driver::get_pitch_angle(void)
00165 {
00166 if(segway_!=NULL)
00167 {
00168 return segway_->get_pitch_angle();
00169 }
00170 else
00171 return 0.0;
00172 }
00173
00174 float SegwayRmp200Driver::get_pitch_rate(void)
00175 {
00176 if(segway_!=NULL)
00177 {
00178 return segway_->get_pitch_rate();
00179 }
00180 else
00181 return 0.0;
00182 }
00183
00184 float SegwayRmp200Driver::get_roll_angle(void)
00185 {
00186 if(segway_!=NULL)
00187 {
00188 return segway_->get_roll_angle();
00189 }
00190 else
00191 return 0.0;
00192 }
00193
00194 float SegwayRmp200Driver::get_roll_rate(void)
00195 {
00196 if(segway_!=NULL)
00197 {
00198 return segway_->get_roll_rate();
00199 }
00200 else
00201 return 0.0;
00202 }
00203
00204 float SegwayRmp200Driver::get_left_wheel_velocity(void)
00205 {
00206 if(segway_!=NULL)
00207 {
00208 return segway_->get_left_wheel_velocity();
00209 }
00210 else
00211 return 0.0;
00212 }
00213
00214 float SegwayRmp200Driver::get_right_wheel_velocity(void)
00215 {
00216 if(segway_!=NULL)
00217 {
00218 return segway_->get_right_wheel_velocity();
00219 }
00220 else
00221 return 0.0;
00222 }
00223
00224 float SegwayRmp200Driver::get_yaw_rate(void)
00225 {
00226 if(segway_!=NULL)
00227 {
00228 return segway_->get_yaw_rate();
00229 }
00230 else
00231 return 0.0;
00232 }
00233
00234 float SegwayRmp200Driver::get_uptime(void)
00235 {
00236 if(segway_!=NULL)
00237 {
00238 return segway_->get_uptime();
00239 }
00240 else
00241 return 0.0;
00242 }
00243
00244 float SegwayRmp200Driver::get_left_wheel_displacement(void)
00245 {
00246 if(segway_!=NULL)
00247 {
00248 return segway_->get_left_wheel_displacement();
00249 }
00250 else
00251 return 0.0;
00252 }
00253
00254 float SegwayRmp200Driver::get_right_wheel_displacement(void)
00255 {
00256 if(segway_!=NULL)
00257 {
00258 return segway_->get_right_wheel_displacement();
00259 }
00260 else
00261 return 0.0;
00262 }
00263
00264 float SegwayRmp200Driver::get_forward_displacement(void)
00265 {
00266 if(segway_!=NULL)
00267 {
00268 return segway_->get_forward_displacement();
00269 }
00270 else
00271 return 0.0;
00272 }
00273
00274 float SegwayRmp200Driver::get_yaw_displacement(void)
00275 {
00276 if(segway_!=NULL)
00277 {
00278 return segway_->get_yaw_displacement();
00279 }
00280 else
00281 return 0.0;
00282 }
00283
00284 float SegwayRmp200Driver::get_left_motor_torque(void)
00285 {
00286 if(segway_!=NULL)
00287 {
00288 return segway_->get_left_motor_torque();
00289 }
00290 else
00291 return 0.0;
00292 }
00293
00294 float SegwayRmp200Driver::get_right_motor_torque(void)
00295 {
00296 if(segway_!=NULL)
00297 {
00298 return segway_->get_right_motor_torque();
00299 }
00300 else
00301 return 0.0;
00302 }
00303
00304 op_mode SegwayRmp200Driver::get_operation_mode(void)
00305 {
00306 if(segway_!=NULL)
00307 {
00308 return segway_->get_operation_mode();
00309 }
00310 else
00311 return (op_mode)-1;
00312 }
00313
00314 gain SegwayRmp200Driver::get_gain_schedule(void)
00315 {
00316 if(segway_!=NULL)
00317 {
00318 return segway_->get_gain_schedule();
00319 }
00320 else
00321 return (gain)-1;
00322 }
00323
00324 float SegwayRmp200Driver::get_ui_battery_voltage(void)
00325 {
00326 if(segway_!=NULL)
00327 {
00328 return segway_->get_ui_battery_voltage();
00329 }
00330 else
00331 return 0.0;
00332 }
00333
00334 float SegwayRmp200Driver::get_powerbase_battery_voltage(void)
00335 {
00336 if(segway_!=NULL)
00337 {
00338 return segway_->get_powerbase_battery_voltage();
00339 }
00340 else
00341 return 0.0;
00342 }
00343
00344 void SegwayRmp200Driver::reset(void)
00345 {
00346 if(segway_!=NULL)
00347 {
00348 segway_->reset();
00349 }
00350 }
00351
00352 void SegwayRmp200Driver::move_platform(float vT, float vR)
00353 {
00354 if(segway_!=NULL)
00355 {
00356 segway_->move(vT,vR);
00357 }
00358 }
00359
00360 void SegwayRmp200Driver::stop_platform(void)
00361 {
00362 if(segway_!=NULL)
00363 {
00364 segway_->stop();
00365 }
00366 }
00367
00368 const std::list<std::string> & SegwayRmp200Driver::getSegwayEvents() const
00369 {
00370 return events_;
00371 }
00372
00373 void SegwayRmp200Driver::setSegwayEvents(void)
00374 {
00375
00376
00377
00378 events_.push_back(segway_->get_cable_disconnected_event());
00379 events_.push_back(segway_->get_power_off_event());
00380 events_.push_back(segway_->get_no_heartbeat_event());
00381 events_.push_back(segway_->get_new_status_event());
00382 }
00383
00384 const std::string & SegwayRmp200Driver::getSegwayEventName(const unsigned int & ev)
00385 {
00386 static const std::string names[5] =
00387 {
00388 std::string("NO_USB"),
00389 std::string("POWER_OFF"),
00390 std::string("NO_HEART_BEAT"),
00391 std::string("NEW_STATUS"),
00392 std::string("UNKNOWN")
00393 };
00394
00395 if (ev <= 3)
00396 return names[ev];
00397 else
00398 return names[4];
00399 }
00400