Tetrapod Project
contact_plugin.cpp
Go to the documentation of this file.
1 /*******************************************************************/
2 /* AUTHOR: Paal Arthur S. Thorseth */
3 /* ORGN: Dept of Eng Cybernetics, NTNU Trondheim */
4 /* FILE: contact_plugin.cpp */
5 /* DATE: May 19, 2021 */
6 /* */
7 /* Copyright (C) 2021 Paal Arthur S. Thorseth, */
8 /* Adrian B. Ghansah */
9 /* */
10 /* This program is free software: you can redistribute it */
11 /* and/or modify it under the terms of the GNU General */
12 /* Public License as published by the Free Software Foundation, */
13 /* either version 3 of the License, or (at your option) any */
14 /* later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty */
18 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19 /* See the GNU General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU General Public */
22 /* License along with this program. If not, see */
23 /* <https://www.gnu.org/licenses/>. */
24 /* */
25 /*******************************************************************/
26 
28 
29 namespace gazebo
30 {
31 
32 // Tell Gazebo about this plugin, so that Gazebo can call Load on this plugin.
33 GZ_REGISTER_SENSOR_PLUGIN(ContactPlugin)
34 
35 // Constructor
36 ContactPlugin::ContactPlugin() : SensorPlugin()
37 {}
38 
39 // Destructor
41 {}
42 
43 // Load plugin
44 void ContactPlugin::Load(sensors::SensorPtr _sensor, sdf::ElementPtr /*_sdf*/)
45 {
46  // Get the parent sensor.
47  this->parentSensor = std::dynamic_pointer_cast<sensors::ContactSensor>(_sensor);
48 
49  // Validate parent sensor.
50  if (!this->parentSensor)
51  {
52  gzerr << "ContactPlugin requires a ContactSensor! \n";
53  return;
54  }
55 
56  // Connect to the sensor update event.
57  this->updateConnection = this->parentSensor->ConnectUpdated(std::bind(&ContactPlugin::OnUpdate,
58  this)
59  );
60 
61  // Ensure parent sensor is active.
62  this->parentSensor->SetActive(true);
63 }
64 
65 // Callback for sensor updates
67 {
68  msgs::Contacts contacts;
69 
70  contacts = this->parentSensor->Contacts();
71 
72  for(unsigned int i = 0; i < contacts.contact_size(); ++i)
73  {
74  ROS_INFO_STREAM("Collision between [" << contacts.contact(i).collision1()
75  << "] and [" << contacts.contact(i).collision2() << "] \n");
76 
77  for (unsigned int j = 0; j < contacts.contact(i).position_size(); ++j)
78  {
79  ROS_INFO_STREAM(j << " Position:"
80  << contacts.contact(i).position(j).x() << " "
81  << contacts.contact(i).position(j).y() << " "
82  << contacts.contact(i).position(j).z() << "\n");
83  ROS_INFO_STREAM(" Normal:"
84  << contacts.contact(i).normal(j).x() << " "
85  << contacts.contact(i).normal(j).y() << " "
86  << contacts.contact(i).normal(j).z() << "\n");
87  ROS_INFO_STREAM(" Depth:"
88  << contacts.contact(i).depth(j) << "\n");
89  }
90  }
91 }
92 
93 } // namespace gazebo
A plugin for the contact sensors.
virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf)
Load the sensor plugin.
sensors::ContactSensorPtr parentSensor
Pointer to the contact sensor.
event::ConnectionPtr updateConnection
Connection that maintains a link between the contact sensor's updated signal and the OnUpdate callbac...
virtual ~ContactPlugin()
Destructor.
virtual void OnUpdate()
Callback that receives the contact sensor's update signal.