GazeboObjectInfo.cpp 10 KB
Newer Older
Jennifer Buehler's avatar
Jennifer Buehler committed
1
#include <gazebo_state_plugins/GazeboObjectInfo.h>
Jennifer Buehler's avatar
Jennifer Buehler committed
2
#include <gazebo_version_helpers/GazeboVersionHelpers.h>
3
#include <object_msgs_tools/ObjectFunctions.h>
Jennifer Buehler's avatar
Jennifer Buehler committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <gazebo/physics/Link.hh>
#include <gazebo/physics/BoxShape.hh>
#include <gazebo/physics/SphereShape.hh>
#include <gazebo/physics/CylinderShape.hh>
#include <gazebo/physics/Collision.hh>
#include <gazebo/physics/World.hh>

#define DEFAULT_PUBLISH_OBJECTS false
#define DEFAULT_WORLD_OBJECTS_TOPIC "world/objects"
#define DEFAULT_REQUEST_OBJECTS_TOPIC "world/request_object"
#define DEFAULT_ROOT_FRAME_ID "world"

//publishing rate
#define UPDATE_RATE 5

#define OBJECT_QUEUE_SIZE 100

using gazebo::GazeboObjectInfo;
Jennifer Buehler's avatar
Jennifer Buehler committed
22
using gazebo::GzVector3;
Jennifer Buehler's avatar
Jennifer Buehler committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

GZ_REGISTER_WORLD_PLUGIN(GazeboObjectInfo)

GazeboObjectInfo::GazeboObjectInfo() : 
    WorldPlugin(){
    reGenerateObjects=true;
}

void GazeboObjectInfo::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf){
    ros::NodeHandle node("/gazebo_state_plugins");

    node.param<bool>("publish_world_objects", PUBLISH_OBJECTS, DEFAULT_PUBLISH_OBJECTS);
    ROS_INFO("GazeboObjInfo: Got objects publish flag: <%i>", PUBLISH_OBJECTS);

    node.param<std::string>("world_objects_topic", WORLD_OBJECTS_TOPIC, DEFAULT_WORLD_OBJECTS_TOPIC);
    ROS_INFO("GazeboObjInfo: Got objects topic name: <%s>", WORLD_OBJECTS_TOPIC.c_str());
    
    node.param<std::string>("request_object_service", REQUEST_OBJECTS_TOPIC, DEFAULT_REQUEST_OBJECTS_TOPIC);
    ROS_INFO("GazeboObjInfo: Got objects topic name: <%s>", REQUEST_OBJECTS_TOPIC.c_str());
    
    node.param<std::string>("objects_frame_id", ROOT_FRAME_ID, DEFAULT_ROOT_FRAME_ID);
    ROS_INFO("GazeboObjInfo: Got objects frame id: <%s>", ROOT_FRAME_ID.c_str());
    
    world=_world;    

    if (PUBLISH_OBJECTS){
        update_connection = event::Events::ConnectWorldUpdateBegin(boost::bind(&GazeboObjectInfo::onWorldUpdate, this));
        object_pub = node.advertise<GazeboObjectInfo::ObjectMsg>(WORLD_OBJECTS_TOPIC, OBJECT_QUEUE_SIZE);
    }

    ros::Rate rate(UPDATE_RATE);
    publishTimer=node.createTimer(rate,&GazeboObjectInfo::advertEvent, this);

    request_object_srv = node.advertiseService(REQUEST_OBJECTS_TOPIC, &GazeboObjectInfo::requestObject,this);
}

bool GazeboObjectInfo::requestObject(object_msgs::ObjectInfo::Request &req, object_msgs::ObjectInfo::Response &res) {

Jennifer Buehler's avatar
Jennifer Buehler committed
61
62
    std::string modelName = req.name;
    physics::ModelPtr model = gazebo::GetModelByName(world, modelName);
Jennifer Buehler's avatar
Jennifer Buehler committed
63
64

    if (!model.get()) {
65
        // ROS_ERROR("Model %s not found",modelName.c_str());
Jennifer Buehler's avatar
Jennifer Buehler committed
66
        res.success=false;
67
        res.error_code=object_msgs::ObjectInfo::Response::OBJECT_NOT_FOUND;
Jennifer Buehler's avatar
Jennifer Buehler committed
68
69
        return true;
    }
70
71
        
    res.error_code=object_msgs::ObjectInfo::Response::NO_ERROR;
Jennifer Buehler's avatar
Jennifer Buehler committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    res.success=true;
    res.object=createBoundingBoxObject(model,req.get_geometry);
    //ROS_INFO("Received service request for object info!");
    return true;
}

void GazeboObjectInfo::advertEvent(const ros::TimerEvent& e) {
    if (object_pub.getNumSubscribers()==0) return;
    std::vector<GazeboObjectInfo::ObjectMsg>::iterator it;
    for (it=lastGeneratedObjects.begin(); it!=lastGeneratedObjects.end(); ++it) {
        object_pub.publish(*it);
    }
    lastGeneratedObjects.clear();
    reGenerateObjects=true;
}


void GazeboObjectInfo::onWorldUpdate() {
    if (object_pub.getNumSubscribers()==0) return;

    if (!reGenerateObjects) return;

Jennifer Buehler's avatar
Jennifer Buehler committed
94
    physics::Model_V models = gazebo::GetModels(world);
Jennifer Buehler's avatar
Jennifer Buehler committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    physics::Model_V::iterator m_it;

    bool send_shape=false;
    for (m_it=models.begin(); m_it!=models.end(); ++m_it) {
        //ROS_INFO_STREAM("Have model "<<(*m_it)->GetName());
        GazeboObjectInfo::ObjectMsg obj=createBoundingBoxObject(*m_it,send_shape);
        lastGeneratedObjects.push_back(obj);
    }

    reGenerateObjects=false;
}


shape_msgs::SolidPrimitive * GazeboObjectInfo::getSolidPrimitive(physics::CollisionPtr& c) {
    shape_msgs::SolidPrimitive solid;
    msgs::Geometry geom;
    physics::ShapePtr shape=c->GetShape();
    shape->FillMsg(geom);
    if (geom.has_box()) {
        //ROS_INFO("shape type %i of collision %s is a box! ", c->GetShapeType(), c->GetName().c_str());

        const gazebo::physics::BoxShape * box=dynamic_cast<const physics::BoxShape*>(shape.get());
        if (!box) {
            ROS_ERROR("Dynamic cast failed for box shape");
            return NULL;
        }
Jennifer Buehler's avatar
Jennifer Buehler committed
121
122
        GzVector3 bb = gazebo::GetSize3(*box);
        if ((GetX(bb) < 1e-05) || (GetY(bb) < 1e-05) || (GetZ(bb) < 1e-05)){
Jennifer Buehler's avatar
Jennifer Buehler committed
123
124
125
126
127
            ROS_WARN_ONCE("Skipping coll %s because its bounding box is flat",c->GetName().c_str());
            return NULL;
        }
        solid.type=shape_msgs::SolidPrimitive::BOX;
        solid.dimensions.resize(3);
Jennifer Buehler's avatar
Jennifer Buehler committed
128
129
130
        solid.dimensions[shape_msgs::SolidPrimitive::BOX_X]=GetX(bb);
        solid.dimensions[shape_msgs::SolidPrimitive::BOX_Y]=GetY(bb);
        solid.dimensions[shape_msgs::SolidPrimitive::BOX_Z]=GetZ(bb);
Jennifer Buehler's avatar
Jennifer Buehler committed
131
132
133
134
135
136
137
138
139
140
    }/*else if (geom.has_cylinder()) {
        ROS_INFO("shape type %i of collision %s is a cylinder! ", c->GetShapeType(), c->GetName().c_str());
        const gazebo::physics::CylinderShape * cyl=dynamic_cast<const physics::CylinderShape*>(shape.get());
        if (!cyl) {
            ROS_ERROR("Dynamic cast failed for cylinder shape");
            return NULL;
        }
        solid.type=shape_msgs::SolidPrimitive::CYLINDER;
        solid.dimensions[shape_msgs::SolidPrimitive::CYLINDER_HEIGHT]=cyl->GetLength();
        solid.dimensions[shape_msgs::SolidPrimitive::CYLINDER_RADIUS]=cyl->GetRadius();
Jennifer Buehler's avatar
Jennifer Buehler committed
141
142
143
    }*/
    else if (geom.has_sphere())
    {
Jennifer Buehler's avatar
Jennifer Buehler committed
144
145
        //ROS_INFO("shape type %i of collision %s is a sphere! ", c->GetShapeType(), c->GetName().c_str());

Jennifer Buehler's avatar
Jennifer Buehler committed
146
147
148
149
        const gazebo::physics::SphereShape * sp
          = dynamic_cast<const physics::SphereShape*>(shape.get());
        if (!sp)
        {
Jennifer Buehler's avatar
Jennifer Buehler committed
150
151
152
153
154
            ROS_ERROR("Dynamic cast failed for cylinder shape");
            return NULL;
        }

        solid.type=shape_msgs::SolidPrimitive::SPHERE;
Jennifer Buehler's avatar
Jennifer Buehler committed
155
156
157
158
        solid.dimensions[shape_msgs::SolidPrimitive::SPHERE_RADIUS] = sp->GetRadius();
    }
    else
    {
Jennifer Buehler's avatar
Jennifer Buehler committed
159
        ROS_WARN("shape type %i of collision %s not supported. Using bounding box instead. ", c->GetShapeType(),c->GetName().c_str());
Jennifer Buehler's avatar
Jennifer Buehler committed
160
161
162
        gz_math::Box box = GetBoundingBox(*c);
        GzVector3 bb = GetBoundingBoxDimensions(box);
        if ((GetX(bb) < 1e-05) || (GetY(bb) < 1e-05) || (GetZ(bb) < 1e-05)){
Jennifer Buehler's avatar
Jennifer Buehler committed
163
164
165
166
167
            ROS_WARN_ONCE("Skipping coll %s because its bounding box is flat",c->GetName().c_str());
            return NULL;
        }
        solid.type=shape_msgs::SolidPrimitive::BOX;
        solid.dimensions.resize(3);
Jennifer Buehler's avatar
Jennifer Buehler committed
168
169
170
        solid.dimensions[shape_msgs::SolidPrimitive::BOX_X]=GetX(bb);
        solid.dimensions[shape_msgs::SolidPrimitive::BOX_Y]=GetY(bb);
        solid.dimensions[shape_msgs::SolidPrimitive::BOX_Z]=GetZ(bb);
Jennifer Buehler's avatar
Jennifer Buehler committed
171
172
173
174
175
176
    }
    //ROS_INFO_STREAM("Solid computed."<<std::endl<<solid);
    return new shape_msgs::SolidPrimitive(solid);
}


177
178
GazeboObjectInfo::ObjectMsg GazeboObjectInfo::createBoundingBoxObject(physics::ModelPtr& model, bool include_shape)
{
Jennifer Buehler's avatar
Jennifer Buehler committed
179
180
181
182
183
184
185
186
    GazeboObjectInfo::ObjectMsg obj;

    physics::Link_V links=model->GetLinks();
    physics::Link_V::iterator l_it;
        
    obj.name=model->GetName();
    obj.header.stamp=ros::Time::now();
    obj.header.frame_id=ROOT_FRAME_ID;
187
188
189
190
191
    // obj.type =  no object type given
    // the custum origin (Object::origin) is going to be set to the first link encountered
    bool origin_init = false;
    for (l_it=links.begin(); l_it!=links.end(); ++l_it)
    {
Jennifer Buehler's avatar
Jennifer Buehler committed
192
193
194
195
        physics::LinkPtr link=*l_it;
        
        std::string linkName=link->GetName();
        
Jennifer Buehler's avatar
Jennifer Buehler committed
196
        GzPose3 link_pose=GetWorldPose(link);
Jennifer Buehler's avatar
Jennifer Buehler committed
197
198
199
200
201
        //ROS_INFO("Link for model %s: %s, pos %f %f %f",model->GetName().c_str(),link->GetName().c_str(),link_pose.pos.x,link_pose.pos.y,link_pose.pos.z);
        //ROS_INFO("Link found for model %s: %s",model->GetName().c_str(),link->GetName().c_str());

        physics::Collision_V colls=link->GetCollisions();
        physics::Collision_V::iterator cit;
202
203
        for (cit=colls.begin(); cit!=colls.end(); ++cit)
        {
Jennifer Buehler's avatar
Jennifer Buehler committed
204
205
            physics::CollisionPtr c=*cit;
            
Jennifer Buehler's avatar
Jennifer Buehler committed
206
            GzPose3 rel_pose = GetRelativePose(*c);
Jennifer Buehler's avatar
Jennifer Buehler committed
207
208

            //ROS_INFO("Collision for model %s: %s, pos %f %f %f",model->GetName().c_str(),link->GetName().c_str(),rel_pose.pos.x,rel_pose.pos.y,rel_pose.pos.z);
Jennifer Buehler's avatar
Jennifer Buehler committed
209
            //GzPose3 w_pose=c->GetWorldPose();
Jennifer Buehler's avatar
Jennifer Buehler committed
210
211
            //ROS_INFO("World pos for model %s: %s, pos %f %f %f",model->GetName().c_str(),link->GetName().c_str(),w_pose.pos.x,w_pose.pos.y,w_pose.pos.z);

Jennifer Buehler's avatar
Jennifer Buehler committed
212
            GzPose3 coll_pose=rel_pose+link_pose; //XXX c->GetWorldPose() does not work for non-static objects, so it has to be relative pose and link world pose
Jennifer Buehler's avatar
Jennifer Buehler committed
213
            geometry_msgs::Pose pose;
Jennifer Buehler's avatar
Jennifer Buehler committed
214
215
216
217
218
219
220
            pose.position.x = GetX(GetPos(coll_pose));
            pose.position.y = GetY(GetPos(coll_pose));
            pose.position.z = GetZ(GetPos(coll_pose));
            pose.orientation.x = GetX(GetRot(coll_pose));
            pose.orientation.y = GetY(GetRot(coll_pose));
            pose.orientation.z = GetZ(GetRot(coll_pose));
            pose.orientation.w = GetW(GetRot(coll_pose));
Jennifer Buehler's avatar
Jennifer Buehler committed
221
222

            obj.primitive_poses.push_back(pose);
223
224
225
226
227
        
            if (!origin_init)
            {
                obj.origin = pose;
            }
Jennifer Buehler's avatar
Jennifer Buehler committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    
            if (include_shape) {

                shape_msgs::SolidPrimitive * solid=getSolidPrimitive(c);
                if (!solid) {
                    ROS_WARN("Skipping coll %s of link %s of model %s, could not get SolidPrimitive. ",c->GetName().c_str(),linkName.c_str(),obj.name.c_str());
                    continue;
                }

                obj.primitives.push_back(*solid);
                delete solid;
                obj.content=GazeboObjectInfo::ObjectMsg::SHAPE;
            }else {
                obj.content=GazeboObjectInfo::ObjectMsg::POSE;
                //ROS_INFO("Pub pose for %s (%s %s)",c->GetName().c_str(),link->GetName().c_str(),model->GetName().c_str());
            }
        }
    }
246
247
248
249

    obj.primitive_origin = GazeboObjectInfo::ObjectMsg::ORIGIN_AVERAGE;
    obj.mesh_origin = GazeboObjectInfo::ObjectMsg::ORIGIN_UNDEFINED;

Jennifer Buehler's avatar
Jennifer Buehler committed
250
251
252
253
    return obj;
}