Tetrapod Project
contact_sensor.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_sensor.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 // Constructor
31 {
32  this->InitGazebo();
33 
34  this->InitRos();
35  this->InitRosQueueThreads();
36 
37  this->footContacts[0] = 1;
38  this->footContacts[1] = 1;
39  this->footContacts[2] = 1;
40  this->footContacts[3] = 1;
41 }
42 
43 // Destructor
45 {
46  gazebo::client::shutdown();
47 
48  this->rosNode->shutdown();
49 
50  this->rosPublishQueue.clear();
51  this->rosPublishQueue.disable();
52  this->rosPublishQueueThread.join();
53 }
54 
55 // Front left contact sensor callback
56 void ContactSensor::OnFlContactCallback(const ConstContactsPtr &_msg)
57 {
58  if (_msg->contact_size() > 0)
59  {
60  this->footContacts[0] = 1;
61  }
62  else
63  {
64  this->footContacts[0] = 0;
65  }
66 }
67 
68 // Front right contact sensor callback
69 void ContactSensor::OnFrContactCallback(const ConstContactsPtr &_msg)
70 {
71  if (_msg->contact_size() > 0)
72  {
73  this->footContacts[1] = 1;
74  }
75  else
76  {
77  this->footContacts[1] = 0;
78  }
79 }
80 
81 // Rear left contact sensor callback
82 void ContactSensor::OnRlContactCallback(const ConstContactsPtr &_msg)
83 {
84  if (_msg->contact_size() > 0)
85  {
86  this->footContacts[2] = 1;
87  }
88  else
89  {
90  this->footContacts[2] = 0;
91  }
92 }
93 
94 // Rear rear contact sensor callback
95 void ContactSensor::OnRrContactCallback(const ConstContactsPtr &_msg)
96 {
97  if (_msg->contact_size() > 0)
98  {
99  this->footContacts[3] = 1;
100  }
101  else
102  {
103  this->footContacts[3] = 0;
104  }
105 }
106 
107 // Setup thread to publish messages
109 {
110  ros::Rate publish_rate(200);
111  while (this->rosNode->ok())
112  {
113  std_msgs::Int8MultiArray contacts_msg;
114 
115  contacts_msg.data.resize(4);
116 
117  for (size_t i = 0; i < 4; i++)
118  {
119  contacts_msg.data[i] = this->footContacts[i];
120  }
121 
122  this->contactStatePub.publish(contacts_msg);
123 
124  publish_rate.sleep();
125  }
126 }
127 
128 // Initialize ROS
130 {
131  gazebo::client::setup();
132 
133  this->gazeboNode.reset(new gazebo::transport::Node());
134 
135  this->gazeboNode->Init();
136 
137  this->flContactSensorSub = this->gazeboNode->Subscribe("~/my_robot/tetrapod/front_left_lower_leg/front_left_contact_sensor/contacts",
139  this);
140 
141  this->frContactSensorSub = this->gazeboNode->Subscribe("~/my_robot/tetrapod/front_right_lower_leg/front_right_contact_sensor/contacts",
143  this);
144 
145  this->rlContactSensorSub = this->gazeboNode->Subscribe("~/my_robot/tetrapod/rear_left_lower_leg/rear_left_contact_sensor/contacts",
147  this);
148 
149  this->rrContactSensorSub = this->gazeboNode->Subscribe("~/my_robot/tetrapod/rear_right_lower_leg/rear_right_contact_sensor/contacts",
151  this);
152 }
153 
154 // Initialize ROS
156 {
157  if (!ros::isInitialized())
158  {
159  int argc = 0;
160  char **argv = NULL;
161  ros::init(
162  argc,
163  argv,
164  "contact_sensor_node",
165  ros::init_options::NoSigintHandler
166  );
167  }
168 
169  this->rosNode.reset(new ros::NodeHandle("contact_sensor_node"));
170 
171  ros::AdvertiseOptions contact_state_ao =
172  ros::AdvertiseOptions::create<std_msgs::Int8MultiArray>(
173  "/my_robot/contact_state",
174  1,
175  ros::SubscriberStatusCallback(),
176  ros::SubscriberStatusCallback(),
177  ros::VoidPtr(),
178  &this->rosPublishQueue
179  );
180 
181  this->contactStatePub = this->rosNode->advertise(contact_state_ao);
182 }
183 
184 // Initialize ROS Publish and Process Queue Threads
186 {
187  this->rosPublishQueueThread = std::thread(
188  std::bind(&ContactSensor::PublishQueueThread, this)
189  );
190 }
191 
192 // Main
193 int main(int _argc, char **_argv)
194 {
195  ContactSensor cs;
196 
197  ros::spin();
198 
199  return 0;
200 }
A plugin for the contact sensors.
virtual ~ContactSensor()
Destructor.
std::unique_ptr< ros::NodeHandle > rosNode
Node used for ROS transport.
void OnRlContactCallback(const ConstContactsPtr &_msg)
The OnRlContactCallback function handles an incoming contact message from the rear left contact senso...
void InitRos()
The InitRos function is called to initialize ROS.
void InitRosQueueThreads()
The InitRosQueueThreads function is called to initialize the ROS Publish and Process Queue Threads.
void OnFrContactCallback(const ConstContactsPtr &_msg)
The OnFrContactCallback function handles an incoming contact message from the front right contact sen...
int footContacts[4]
Vector of foot contacts, zero indicates no contact and one indicates contact.
gazebo::transport::SubscriberPtr rrContactSensorSub
Gazebo rear right contact sensor Subscriber.
gazebo::transport::NodePtr gazeboNode
Node used for Gazebo transport.
void PublishQueueThread()
The PublishQueueThread function is a ROS helper function that publish state messages.
void OnRrContactCallback(const ConstContactsPtr &_msg)
The OnRrContactCallback function handles an incoming contact message from the rear rear contact senso...
std::thread rosPublishQueueThread
Thread running the rosPublishQueue.
void OnFlContactCallback(const ConstContactsPtr &_msg)
The OnFlContactCallback function handles an incoming contact message from the front left contact sens...
gazebo::transport::SubscriberPtr rlContactSensorSub
Gazebo rear left contact sensor Subscriber.
ros::Publisher contactStatePub
ROS Contact State Publisher.
ros::CallbackQueue rosPublishQueue
ROS callbackque that helps publish messages.
gazebo::transport::SubscriberPtr frContactSensorSub
Gazebo front right contact sensor Subscriber.
ContactSensor()
Constructor.
gazebo::transport::SubscriberPtr flContactSensorSub
Gazebo front left contact sensor Subscriber.
void InitGazebo()
The InitRos function is called to initialize Gazebo.
int main(int _argc, char **_argv)