18 #include "hrpsys/util/GLbody.h" 19 #include "hrpsys/util/GLutil.h" 20 #endif // USE_HRPSYSUTIL 21 #include "hrpsys/util/BVutil.h" 22 #include "hrpsys/idl/RobotHardwareService.hh" 26 #define deg2rad(x) ((x)*M_PI/180) 27 #define rad2deg(x) ((x)*180/M_PI) 33 "implementation_id",
"CollisionDetector",
34 "type_name",
"CollisionDetector",
35 "description",
"collisoin detector component",
36 "version", HRPSYS_PACKAGE_VERSION,
38 "category",
"example",
39 "activity_type",
"DataFlowComponent",
42 "lang_type",
"compile",
44 "conf.default.debugLevel",
"0",
49 static std::ostream&
operator<<(std::ostream& os,
const struct RTC::Time &tm)
51 int pre = os.precision();
52 os.setf(std::ios::fixed);
53 os << std::setprecision(6)
54 << (tm.sec + tm.nsec/1e9)
55 << std::setprecision(pre);
56 os.unsetf(std::ios::fixed);
63 m_qRefIn(
"qRef", m_qRef),
64 m_qCurrentIn(
"qCurrent", m_qCurrent),
65 m_servoStateIn(
"servoStateIn", m_servoState),
67 m_beepCommandOut(
"beepCommand", m_beepCommand),
68 m_CollisionDetectorServicePort(
"CollisionDetectorService"),
72 m_window(&m_scene, &m_log),
75 m_use_limb_collision(false),
82 collision_beep_count(0),
83 is_beep_port_connected(false),
88 m_log.enableRingBuffer(1);
89 #endif // USE_HRPSYSUTIL 103 std::cerr <<
"[" <<
m_profile.instance_name <<
"] onInitialize()" << std::endl;
134 std::string nameServer = rtcManager.
getConfig()[
"corba.nameservers"];
135 int comPos = nameServer.find(
",");
137 comPos = nameServer.length();
139 nameServer = nameServer.substr(0, comPos);
146 if ( prop[
"collision_viewer"] ==
"true" ) {
149 #ifdef USE_HRPSYSUTIL 154 #endif // USE_HRPSYSUTIL 156 OpenHRP::BodyInfo_var binfo;
158 CosNaming::NamingContext::_duplicate(
naming.getRootContext()));
159 if (CORBA::is_nil(binfo)){
160 std::cerr <<
"[" <<
m_profile.instance_name <<
"] failed to load model[" << prop[
"model"] <<
"]" 162 return RTC::RTC_ERROR;
164 #ifdef USE_HRPSYSUTIL 168 #endif // USE_HRPSYSUTIL 169 std::cerr <<
"[" <<
m_profile.instance_name <<
"] failed to load model[" << prop[
"model"] <<
"] in " 171 return RTC::RTC_ERROR;
173 #ifdef USE_HRPSYSUTIL 175 #endif // USE_HRPSYSUTIL 176 if ( prop[
"collision_model"] ==
"AABB" ) {
178 }
else if ( prop[
"collision_model"] ==
"convex hull" ||
179 prop[
"collision_model"] ==
"" ) {
184 if ( prop[
"collision_pair"] !=
"" ) {
185 std::cerr <<
"[" <<
m_profile.instance_name <<
"] prop[collision_pair] ->" << prop[
"collision_pair"] << std::endl;
186 std::istringstream iss(prop[
"collision_pair"]);
188 while (getline(iss, tmp,
' ')) {
189 size_t pos = tmp.find_first_of(
':');
190 std::string name1 = tmp.substr(0, pos), name2 = tmp.substr(pos+1);
191 if (
m_robot->link(name1)==NULL ) {
192 std::cerr <<
"[" <<
m_profile.instance_name <<
"] Could not find robot link " << name1 << std::endl;
193 std::cerr <<
"[" <<
m_profile.instance_name <<
"] please choose one of following :";
194 for (
unsigned int i=0;
i <
m_robot->numLinks();
i++) {
195 std::cerr <<
" " <<
m_robot->link(
i)->name;
197 std::cerr << std::endl;
200 if (
m_robot->link(name2)==NULL ) {
201 std::cerr <<
"[" <<
m_profile.instance_name <<
"] Could not find robot link " << name2 << std::endl;
202 std::cerr <<
"[" <<
m_profile.instance_name <<
"] please choose one of following :";
203 for (
unsigned int i=0;
i <
m_robot->numLinks();
i++) {
204 std::cerr <<
" " <<
m_robot->link(
i)->name;
206 std::cerr << std::endl;
209 std::cerr <<
"[" <<
m_profile.instance_name <<
"] check collisions between " <<
m_robot->link(name1)->name <<
" and " <<
m_robot->link(name2)->name << std::endl;
215 if ( prop[
"collision_loop"] !=
"" ) {
219 #ifdef USE_HRPSYSUTIL 224 #endif // USE_HRPSYSUTIL 232 if ( prop[
"collision_mask"] !=
"" ) {
233 std::cerr <<
"[" <<
m_profile.instance_name <<
"] prop[collision_mask] ->" << prop[
"collision_mask"] << std::endl;
235 if (mask_str.size() ==
m_robot->numJoints()) {
237 for (
size_t i = 0;
i <
m_robot->numJoints();
i++) {
239 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector will not control " <<
m_robot->joint(
i)->name << std::endl;
243 std::cerr <<
"[" <<
m_profile.instance_name <<
"] ERROR size of collision_mask is differ from robot joint number .. " << mask_str.size() <<
", " <<
m_robot->numJoints() << std::endl;
247 if ( prop[
"use_limb_collision"] !=
"" ) {
248 std::cerr <<
"[" <<
m_profile.instance_name <<
"] prop[use_limb_collision] -> " << prop[
"use_limb_collision"] << std::endl;
249 if ( prop[
"use_limb_collision"] ==
"true" ) {
251 std::cerr <<
"[" <<
m_profile.instance_name <<
"] Enable use_limb_collision" << std::endl;
272 for(
unsigned int i=0;
i<
m_robot->numJoints();
i++){
277 for(
unsigned int i = 0;
i <
m_robot->numJoints();
i++) {
280 status |= 1<< OpenHRP::RobotHardwareService::CALIB_STATE_SHIFT;
281 status |= 1<< OpenHRP::RobotHardwareService::POWER_STATE_SHIFT;
282 status |= 1<< OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
283 status |= 0<< OpenHRP::RobotHardwareService::SERVO_ALARM_SHIFT;
284 status |= 0<< OpenHRP::RobotHardwareService::DRIVER_TEMP_SHIFT;
320 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onActivated(" << ec_id <<
")" << std::endl;
327 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onDeactivated(" << ec_id <<
")" << std::endl;
331 #define DEBUGP ((m_debugLevel==1 && loop%200==0) || m_debugLevel > 1 ) 344 std::cerr <<
"[" <<
m_profile.instance_name<<
"] beepCommand data port connection found! Use BeeperRTC." << std::endl;
352 if (
DEBUGP || loop % 100 == 1) {
353 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CAUTION!! The robot is moving without checking self collision detection!!! please send enableCollisionDetection to CollisoinDetection RTC" << std::endl;
357 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
368 bool has_servoOn =
false;
369 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ){
370 int servo_state = (
m_servoState.data[
i][0] & OpenHRP::RobotHardwareService::SERVO_STATE_MASK) >> OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
371 has_servoOn = has_servoOn || (servo_state == 1);
377 #ifdef USE_HRPSYSUTIL 379 for (
unsigned int i=0;
i<m_glbody->numLinks();
i++){
380 ((
GLlink *)m_glbody->link(
i))->highlight(
false);
383 for (
unsigned int i=0;
i<m_glbody->numLinks();
i++){
387 for (
unsigned int i=0;
i<
m_robot->numLinks();
i++){
390 #endif // USE_HRPSYSUTIL 397 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ){
402 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ){
414 m_robot->calcForwardKinematics();
416 std::map<std::string, CollisionLinkPair *>::iterator it =
m_pair.begin();
417 for (
int i = 0; it !=
m_pair.end(); it++,
i++){
433 for (
unsigned int i = 0; it !=
m_pair.end();
i++, it++){
439 if ( loop%200==0 || last_safe_posture ) {
441 std::cerr <<
"[" <<
m_profile.instance_name <<
"] " <<
i <<
"/" <<
m_pair.size() <<
" pair: " << p->link(0)->name <<
"/" << p->link(1)->name <<
"(" << jointPath->numJoints() <<
"), distance = " << c->
distance << std::endl;
447 bool stop_all =
true;
450 for (
unsigned int i = 0;
i < jointPath->numJoints();
i++ ){
if (
m_init_collision_mask[jointPath->joint(
i)->jointId] == 1) stop_all =
false; }
451 for (
unsigned int i = 0;
i < jointPath->numJoints();
i++ ){
452 int id = jointPath->joint(
i)->jointId;
463 #ifdef USE_HRPSYSUTIL 465 ((
GLlink *)p->link(0))->highlight(
true);
466 ((
GLlink *)p->link(1))->highlight(
true);
468 #endif // USE_HRPSYSUTIL 476 <<
"] set safe posture" << std::endl;
477 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
487 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
492 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
500 std::cerr <<
"[" <<
m_profile.instance_name <<
"] collision_mask : ";
501 for (
size_t i = 0;
i <
m_robot->numJoints();
i++) {
504 std::cerr << std::endl;
516 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
522 static int collision_loop_recover = 0;
525 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
540 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
548 for (
int i = 0;
i <
m_q.data.length();
i++ ) {
552 if (collision_loop_recover != 0) {
553 collision_loop_recover--;
554 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
559 collision_loop_recover = 0;
567 std::cerr <<
"[" <<
m_profile.instance_name <<
"] check collisions for " <<
m_pair.size() <<
" pairs in " << (tm2.
sec()-tm1.
sec())*1000+(tm2.
usec()-tm1.
usec())/1000.0
571 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CAUTION!! The robot is moving without checking self collision detection!!! please define collision_pair in configuration file" << std::endl;
574 if (
DEBUGP || (loop % ((
int)(5/
m_dt))) == 1) {
575 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CAUTION!! The robot is moving while collision detection!!!, since we do not get safe_posture yet" << std::endl;
577 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
586 if (! has_servoOn ) {
607 #ifdef USE_HRPSYSUTIL
609 #endif // USE_HRPSYSUTIL 613 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ){
618 for (
unsigned int i = 0;
i <
m_robot->numLinks();
i++ ){
623 for(
unsigned int i = 0;
i < tp.
lines.size();
i++ ){
624 const std::pair<hrp::Vector3, hrp::Vector3>& line = tp.
lines[
i];
627 m_state.lines[
i].get_buffer()[0].length(3);
628 v =
m_state.lines[
i].get_buffer()[0].get_buffer();
629 v[0] = line.first.data()[0];
630 v[1] = line.first.data()[1];
631 v[2] = line.first.data()[2];
632 m_state.lines[
i].get_buffer()[1].length(3);
633 v =
m_state.lines[
i].get_buffer()[1].get_buffer();
634 v[0] = line.second.data()[0];
635 v[1] = line.second.data()[1];
636 v[2] = line.second.data()[2];
639 m_state.computation_time = (tm2-tm1)*1000.0;
648 #ifdef USE_HRPSYSUTIL 650 #endif // USE_HRPSYSUTIL 690 if (strcmp(i_link_pair_name,
"all") == 0 || strcmp(i_link_pair_name,
"ALL") == 0){
691 for ( std::map<std::string, CollisionLinkPair *>::iterator it =
m_pair.begin(); it !=
m_pair.end(); it++){
692 it->second->pair->setTolerance(i_tolerance);
694 }
else if (
m_pair.find(std::string(i_link_pair_name)) !=
m_pair.end() ) {
695 m_pair[std::string(i_link_pair_name)]->pair->setTolerance(i_tolerance);
703 if (input_loop > 0) {
720 for (
unsigned int i=0;
i<i_body->numLinks();
i++) {
721 assert(i_body->link(
i)->index ==
i);
728 for (
unsigned int i = 0;
i <
m_q.data.length();
i++ ) {
730 int servo_state = (
m_servoState.data[
i][0] & OpenHRP::RobotHardwareService::SERVO_STATE_MASK) >> OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
731 if (servo_state == 1 && abs(
m_q.data[
i] -
m_qRef.data[
i]) > 0.017)
return false;
739 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector is already enabled." << std::endl;
744 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector cannot be enabled because of different reference joint angle" << std::endl;
749 for (
unsigned int i = 0;
i <
m_robot->numJoints();
i++ ){
752 m_robot->calcForwardKinematics();
753 std::map<std::string, CollisionLinkPair *>::iterator it =
m_pair.begin();
754 for (
unsigned int i = 0; it !=
m_pair.end(); it++,
i++){
760 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector cannot be enabled because of collision" << std::endl;
761 std::cerr <<
"[" <<
m_profile.instance_name <<
"] " <<
i <<
"/" <<
m_pair.size() <<
" pair: " << p->link(0)->name <<
"/" << p->link(1)->name <<
"(" << jointPath->numJoints() <<
"), distance = " << c->
distance << std::endl;
765 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector is successfully enabled." << std::endl;
777 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector cannot be disabled because of different reference joint angle" << std::endl;
780 std::cerr <<
"[" <<
m_profile.instance_name <<
"] CollisionDetector is successfully disabled." << std::endl;
791 for (
int i = 0;
i < n;
i ++ ) {
797 i_vclip_model->
check();
798 fprintf(stderr,
"[Vclip] build finished, vcliip mesh of %s, %d -> %d\n",
799 i_link->
name.c_str(), n, (
int)(i_vclip_model->
verts().size()));
803 #ifndef USE_HRPSYSUTIL 808 #endif // USE_HRPSYSUTIL 817 RTC::Create<CollisionDetector>,
818 RTC::Delete<CollisionDetector>);
ComponentProfile m_profile
void convertToAABB(hrp::BodyPtr i_body)
png_infop png_charpp int png_charpp profile
RTC::CorbaPort m_CollisionDetectorServicePort
boost::intrusive_ptr< VclipLinkPair > VclipLinkPairPtr
bool m_use_limb_collision
InPort< TimedDoubleSeq > m_qCurrentIn
void collision(CollisionDetector *i_collision)
bool getCollisionStatus(OpenHRP::CollisionDetectorService::CollisionState &state)
HRPMODEL_API OpenHRP::BodyInfo_var loadBodyInfo(const char *url, int &argc, char *argv[])
bool stringTo(To &val, const char *str)
Vertex * addVertex(const char *name, const Vect3 &coords)
unsigned int m_debugLevel
ColdetModelPtr coldetModel
void setupVClipModel(hrp::BodyPtr i_body)
std::vector< int > m_init_collision_mask
bool setCollisionLoop(int input_loop)
bool setTolerance(const char *i_link_pair_name, double i_tolerance)
void setName(const std::string &_name)
InPort< TimedDoubleSeq > m_qRefIn
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
coil::Properties & getProperties()
OutPort< TimedLongSeq > m_beepCommandOut
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
static Manager & instance()
interpolator * m_interpolator
bool addOutPort(const char *name, OutPortBase &outport)
void setDataPort(RTC::TimedLongSeq &out_data)
int get_num_beep_info() const
boost::shared_ptr< Body > BodyPtr
bool is_beep_port_connected
std::vector< std::string > vstring
int gettimeofday(struct timeval *tv, struct timezone *tz)
coil::Properties & getConfig()
OutPort< TimedDoubleSeq > m_qOut
void startBeep(const int _freq, const int _length=50)
hrp::Link * GLlinkFactory()
ExecutionContextHandle_t UniqueId
std::map< std::string, CollisionLinkPair * > m_pair
std::vector< Vclip::Polyhedron * > m_VclipLinks
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
void convertToConvexHull(hrp::BodyPtr i_body)
void CollisionDetectorInit(RTC::Manager *manager)
const list< Vertex > & verts() const
CollisionDetector(RTC::Manager *manager)
Constructor.
char VertFaceName[VF_NAME_SZ]
OpenHRP::CollisionDetectorService::CollisionState m_state
virtual ~CollisionDetector()
Destructor.
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
static const char * component_spec[]
virtual RTC::ReturnCode_t onInitialize()
bool checkIsSafeTransition(void)
virtual RTC::ReturnCode_t onFinalize()
OpenHRP::TimedLongSeqSeq m_servoState
TimedLongSeq m_beepCommand
hrp::Link * hrplinkFactory()
std::string sprintf(char const *__restrict fmt,...)
void get(double *x_, bool popp=true)
bool addPort(PortBase &port)
virtual bool write(DataType &value)
std::vector< std::pair< hrp::Vector3, hrp::Vector3 > > lines
int loadBodyFromBodyInfo(::World *world, const char *_name, BodyInfo_ptr bodyInfo)
static std::ostream & operator<<(std::ostream &os, const struct RTC::Time &tm)
std::vector< int > m_curr_collision_mask
double * m_lastsafe_jointdata
const std::vector< OutPortConnector *> & connectors()
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
InPort< OpenHRP::TimedLongSeqSeq > m_servoStateIn
bool addInPort(const char *name, InPortBase &inport)
void setGoal(const double *gx, const double *gv, double time, bool online=true)
CollisionDetectorService_impl m_service0
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void start_beep(int freq, int length)
double * m_recover_jointdata
void set(const double *x, const double *v=NULL)
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
std::vector< double > posture