12 #include <octomap/octomap.h> 18 inline bool isnan(
double x)
32 "implementation_id",
"OccupancyGridMap3D",
33 "type_name",
"OccupancyGridMap3D",
34 "description",
"null component",
35 "version", HRPSYS_PACKAGE_VERSION,
37 "category",
"example",
38 "activity_type",
"DataFlowComponent",
41 "lang_type",
"compile",
43 "conf.default.occupiedThd",
"0.5",
44 "conf.default.resolution",
"0.1",
45 "conf.default.initialMap",
"",
46 "conf.default.knownMap",
"",
47 "conf.default.debugLevel",
"0",
55 m_rangeIn(
"range", m_range),
56 m_cloudIn(
"cloud", m_cloud),
57 m_poseIn(
"pose", m_pose),
58 m_sensorPosIn(
"sensorPos", m_sensorPos),
59 m_updateIn(
"update", m_update),
60 m_updateOut(
"updateSignal", m_updateSignal),
61 m_OGMap3DServicePort(
"OGMap3DService"),
78 std::cout <<
m_profile.instance_name <<
": onInitialize()" << std::endl;
114 char *p = getcwd(cwd, PATH_MAX);
120 m_pose.data.position.x = 0;
121 m_pose.data.position.y = 0;
122 m_pose.data.position.z = 0;
123 m_pose.data.orientation.r = 0;
124 m_pose.data.orientation.p = 0;
125 m_pose.data.orientation.y = 0;
160 std::cout <<
m_profile.instance_name<<
": onActivated(" << ec_id <<
")" << std::endl;
177 std::cout <<
m_profile.instance_name <<
": initial tree depth = " <<
m_map->getTreeDepth() << std::endl;
178 std::cout <<
m_profile.instance_name <<
": initial tree size = " <<
m_map->size() << std::endl;
186 OcTreeNode *result =
m_map->search(p);
188 double prob = result->getOccupancy();
189 std::cout <<
m_profile.instance_name <<
": initial " << p <<
" " << prob << std::endl;
190 std::cout <<
m_profile.instance_name <<
": second tree depth = " <<
m_map->getTreeDepth() << std::endl;
191 std::cout <<
m_profile.instance_name <<
": second tree size = " <<
m_map->size() << std::endl;
200 std::cout <<
m_profile.instance_name<<
": onDeactivated(" << ec_id <<
")" << std::endl;
225 for (
unsigned int i=0;
i<
m_range.ranges.length();
i++){
229 cloud.push_back(point3d(-d*sin(th), 0, -d*cos(th)));
231 point3d sensor(0,0,0);
232 Pose3D &pose =
m_range.geometry.geometry.pose;
233 pose6d
frame(pose.position.x,
239 m_map->insertPointCloud(cloud, sensor, frame);
247 float *
ptr = (
float *)
m_cloud.data.get_buffer();
248 if (strcmp(
m_cloud.type,
"xyz")==0
249 || strcmp(
m_cloud.type,
"xyzrgb")==0){
251 for (
unsigned int i=0;
i<
m_cloud.data.length()/16;
i++, ptr+=4){
252 if (isnan(ptr[0]))
continue;
253 cloud.push_back(point3d(ptr[0],ptr[1],ptr[2]));
261 m_pose.data.orientation.r,
262 m_pose.data.orientation.p,
263 m_pose.data.orientation.y);
264 m_map->insertPointCloud(cloud, sensor, frame);
265 }
else if (strcmp(
m_cloud.type,
"xyzv")==0){
268 p[0] =
m_pose.data.position.x;
269 p[1] =
m_pose.data.position.y;
270 p[2] =
m_pose.data.position.z;
272 m_pose.data.orientation.p,
273 m_pose.data.orientation.y);
277 point3d pp1,pp2,pp3,pp4,pp5;
296 OcTreeNode *result1 =
m_map->search(pp1);
297 OcTreeKey result1_key;
298 m_map->genKey(pp1, result1_key);
299 OcTreeNode *result2 =
m_map->search(pp2);
300 OcTreeKey result2_key;
301 m_map->genKey(pp2, result2_key);
302 OcTreeNode *result3 =
m_map->search(pp3);
303 OcTreeKey result3_key;
304 m_map->genKey(pp3, result3_key);
305 OcTreeNode *result4 =
m_map->search(pp4);
306 OcTreeKey result4_key;
307 m_map->genKey(pp4, result4_key);
308 OcTreeNode *result5 =
m_map->search(pp5);
309 OcTreeKey result5_key;
310 m_map->genKey(pp5, result5_key);
311 double prob1,prob2,prob3,prob4,prob5;
313 prob1 = result1->getOccupancy();
314 std::cout <<
m_profile.instance_name <<
": " << pp1 <<
" original prob = " << prob1 << std::endl;
315 std::cout <<
m_profile.instance_name <<
": " << pp1 <<
" key = " << result1_key[0] <<
" " << result1_key[1] <<
" " << result1_key[2] << std::endl;
318 std::cout <<
m_profile.instance_name <<
": " << pp1 <<
" can not be searched." << std::endl;
320 prob2 = result2->getOccupancy();
321 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" original prob = " << prob2 << std::endl;
322 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" key = " << result2_key[0] <<
" " << result2_key[1] <<
" " << result2_key[2] << std::endl;
325 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" can not be searched." << std::endl;
327 prob3 = result3->getOccupancy();
328 std::cout <<
m_profile.instance_name <<
": " << pp3 <<
" original prob = " << prob3 << std::endl;
329 std::cout <<
m_profile.instance_name <<
": " << pp3 <<
" key = " << result3_key[0] <<
" " << result3_key[1] <<
" " << result3_key[2] << std::endl;
332 std::cout <<
m_profile.instance_name <<
": " << pp3 <<
" can not be searched." << std::endl;
334 prob4 = result4->getOccupancy();
335 std::cout <<
m_profile.instance_name <<
": " << pp4 <<
" original prob = " << prob4 << std::endl;
336 std::cout <<
m_profile.instance_name <<
": " << pp4 <<
" key = " << result4_key[0] <<
" " << result4_key[1] <<
" " << result4_key[2] << std::endl;
339 std::cout <<
m_profile.instance_name <<
": " << pp4 <<
" can not be searched." << std::endl;
341 prob5 = result5->getOccupancy();
342 std::cout <<
m_profile.instance_name <<
": " << pp5 <<
" original prob = " << prob5 << std::endl;
343 std::cout <<
m_profile.instance_name <<
": " << pp5 <<
" key = " << result5_key[0] <<
" " << result5_key[1] <<
" " << result5_key[2] << std::endl;
346 std::cout <<
m_profile.instance_name <<
": " << pp5 <<
" can not be searched." << std::endl;
349 OcTreeKey result_key;
353 for(
int i=0;
i<5;
i++){
354 pp.x() = -0.3 - 0.1*
i;
355 for(
int j=0; j<5; j++){
356 pp.y() = 2.8 + 0.1*j;
357 result =
m_map->search(pp);
358 m_map->genKey(pp, result_key);
360 prob = result->getOccupancy();
361 std::cout <<
m_profile.instance_name <<
": " << pp <<
" original prob = " << prob << std::endl;
362 std::cout <<
m_profile.instance_name <<
": " << pp <<
" key = " << result_key[0] <<
" " << result_key[1] <<
" " << result_key[2] << std::endl;
365 std::cout <<
m_profile.instance_name <<
": " << pp <<
" can not be searched." << std::endl;
369 for (
unsigned int i=0;
i<
m_cloud.data.length()/16;
i++, ptr+=4){
370 if (isnan(ptr[0]))
continue;
373 point3d pog(pworld[0],pworld[1],pworld[2]);
375 OcTreeNode *target_node;
376 OcTreeKey target_key;
377 if((pworld[0]>(pp1.x()-0.1)&&pworld[0]<pp1.x()&&pworld[1]>pp1.y()&&pworld[1]<(pp1.y()+0.1)&&pworld[2]>pp1.z()&&pworld[2]<(pp1.z()+0.1)) ||
378 (pworld[0]>(pp2.x()-0.1)&&pworld[0]<pp2.x()&&pworld[1]>pp2.y()&&pworld[1]<(pp2.y()+0.1)&&pworld[2]>pp2.z()&&pworld[2]<(pp2.z()+0.1)) ||
379 (pworld[0]>(pp3.x()-0.1)&&pworld[0]<pp3.x()&&pworld[1]>pp3.y()&&pworld[1]<(pp3.y()+0.1)&&pworld[2]>pp3.z()&&pworld[2]<(pp3.z()+0.1))){
380 target_node =
m_map->search(pog);
381 m_map->genKey(pog, target_key);
383 double beforeprob = target_node->getOccupancy();
384 std::cout <<
m_profile.instance_name <<
": " << pog <<
" before prob = " << beforeprob << std::endl;
385 std::cout <<
m_profile.instance_name <<
": " << pog <<
" key = " << target_key[0] <<
" " << target_key[1] <<
" " << target_key[2] << std::endl;
388 std::cout <<
m_profile.instance_name <<
": " << pog <<
" can not be searched." << std::endl;
392 OcTreeNode *updated_node =
m_map->updateNode(pog, ptr[3]>0.0?
true:
false,
false);
395 std::cout <<
m_profile.instance_name <<
": tree depth = " <<
m_map->getTreeDepth() << std::endl;
396 std::cout <<
m_profile.instance_name <<
": tree size = " <<
m_map->size() << std::endl;
398 if((pworld[0]>(pp1.x()-0.1)&&pworld[0]<pp1.x()&&pworld[1]>pp1.y()&&pworld[1]<(pp1.y()+0.1)&&pworld[2]>pp1.z()&&pworld[2]<(pp1.z()+0.1)) ||
399 (pworld[0]>(pp2.x()-0.1)&&pworld[0]<pp2.x()&&pworld[1]>pp2.y()&&pworld[1]<(pp2.y()+0.1)&&pworld[2]>pp2.z()&&pworld[2]<(pp2.z()+0.1)) ||
400 (pworld[0]>(pp3.x()-0.1)&&pworld[0]<pp3.x()&&pworld[1]>pp3.y()&&pworld[1]<(pp3.y()+0.1)&&pworld[2]>pp3.z()&&pworld[2]<(pp3.z()+0.1))){
401 target_node =
m_map->search(pog);
402 m_map->genKey(pog, target_key);
404 double afterprob = target_node->getOccupancy();
405 std::cout <<
m_profile.instance_name <<
": " << pog <<
" after prob = " << afterprob << std::endl;
408 double updatedprob = updated_node->getOccupancy();
409 std::cout <<
m_profile.instance_name <<
": " << pog <<
" updated prob = " << updatedprob << std::endl;
412 result2 =
m_map->search(pp2);
413 m_map->genKey(pp2, result2_key);
415 double newprob = result2->getOccupancy();
419 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" updated prob = " << newprob <<
" by " << pog <<
" prob = " << prob2 << std::endl;
420 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" key = " << result2_key[0] <<
" " << result2_key[1] <<
" " << result2_key[2] << std::endl;
421 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" hasChildren = " << result2->hasChildren() << std::endl;
426 std::cout <<
m_profile.instance_name <<
": " << pp2 <<
" can not be searched." << std::endl;
429 ptr[3]>0.0?ocnum++:emnum++;
431 if(
KDEBUG) std::cout <<
m_profile.instance_name <<
": " << ocnum <<
" " << emnum <<
" " << p << std::endl;
433 std::cout <<
"point type(" <<
m_cloud.type
434 <<
") is not supported" << std::endl;
435 return RTC::RTC_ERROR;
443 std::cout <<
"OccupancyGridMap3D::onExecute() : " 444 << dt.
sec()*1e3+dt.
usec()/1e3 <<
"[ms]" << std::endl;
490 OpenHRP::OGMap3D *
map =
new OpenHRP::OGMap3D;
492 map->resolution = size;
495 m_map->getMetricMin(min[0],min[1],min[2]);
497 m_map->getMetricMax(max[0],max[1],max[2]);
498 for (
int i=0;
i<3;
i++){
505 m_knownMap->getMetricMin(kmin[0],kmin[1],kmin[2]);
507 m_knownMap->getMetricMax(kmax[0],kmax[1],kmax[2]);
508 for (
int i=0;
i<3;
i++){
511 if (kmin[
i] < min[
i]) min[i] = kmin[i];
512 if (kmax[i] > max[i]) max[i] = kmax[i];
522 e[0] = region.pos.x + region.size.l;
523 e[1] = region.pos.y + region.size.w;
524 e[2] = region.pos.z + region.size.h;
527 for (
int i=0;
i<3;
i++){
528 if (e[
i] < min[
i] || s[
i] > max[
i]){
531 if (s[i] < min[i]) s[i] = min[i];
532 if (e[i] > max[i]) e[i] = max[i];
537 #ifdef USE_ONLY_GRIDS 538 map->pos.x = ((
int)(s[0]/size))*size;
539 map->pos.y = ((
int)(s[1]/size))*size;
540 map->pos.z = ((
int)(s[2]/size))*size;
547 std::cout <<
m_profile.instance_name <<
": pos = " << map->pos.x <<
" " << map->pos.y <<
" " << map->pos.z <<
" " << std::endl;
548 map->pos.x += size/2.0;
549 map->pos.y += size/2.0;
550 map->pos.z += size/2.0;
553 map->pos.x = ((
int)(s[0]/size)+0.5)*size;
554 map->pos.y = ((
int)(s[1]/size)+0.5)*size;
555 map->pos.z = ((
int)(s[2]/size)+0.5)*size;
562 map->cells.length(map->nx*map->ny*map->nz);
564 int no=0, ne=0, nu=0;
566 for (
int i=0;
i<map->nx;
i++){
567 p.x() = map->pos.x +
i*size;
568 for (
int j=0; j<map->ny; j++){
569 p.y() = map->pos.y + j*size;
570 for (
int k=0; k<map->nz; k++){
571 p.z() = map->pos.z + k*size;
572 OcTreeNode *result =
m_map->search(p);
574 if (result && !(result->hasChildren())){
575 double prob = result->getOccupancy();
577 map->cells[
rank] = prob*0xfe;
581 map->cells[
rank] = OpenHRP::gridEmpty;
586 map->cells[
rank] = OpenHRP::gridUnknown;
592 double prob = result->getOccupancy();
594 map->cells[
rank] = prob*0xfe;
603 std::cout <<
"occupied/empty/unknown = " << no <<
"/" << ne <<
"/" 610 std::cout <<
"OccupancyGridMap3D::getOGMap3D() : " 611 << dt.
sec()*1e3+dt.
usec()/1e3 <<
"[ms]" << std::endl;
620 m_map->writeBinary(filename);
637 RTC::Create<OccupancyGridMap3D>,
638 RTC::Delete<OccupancyGridMap3D>);
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
PointCloudTypes::PointCloud m_cloud
RTC::CorbaPort m_OGMap3DServicePort
void save(const char *filename)
coil::Guard< coil::Mutex > Guard
OccupancyGridMap3D(RTC::Manager *manager)
Constructor.
bool addOutPort(const char *name, OutPortBase &outport)
Matrix33 rotFromRpy(const Vector3 &rpy)
InPort< TimedLong > m_updateIn
int gettimeofday(struct timeval *tv, struct timezone *tz)
ExecutionContextHandle_t UniqueId
OpenHRP::OGMap3D * getOGMap3D(const OpenHRP::AABB ®ion)
InPort< RangeData > m_rangeIn
OutPort< TimedLong > m_updateOut
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
InPort< TimedPose3D > m_poseIn
octomap::OcTree * m_knownMap
void OccupancyGridMap3DInit(RTC::Manager *manager)
InPort< PointCloudTypes::PointCloud > m_cloudIn
static const char * occupancygridmap3d_spec[]
bool addPort(PortBase &port)
virtual bool write(DataType &value)
bool addInPort(const char *name, InPortBase &inport)
std::string m_knownMapPath
virtual RTC::ReturnCode_t onInitialize()
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
InPort< TimedPoint3D > m_sensorPosIn
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
OGMap3DService_impl m_service0
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
virtual ~OccupancyGridMap3D()
Destructor.