-
Notifications
You must be signed in to change notification settings - Fork 9
/
auto_elev_door_plugin.cc
298 lines (242 loc) · 9.55 KB
/
auto_elev_door_plugin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2014 Mohit Shridhar, David Lee
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
#include <ros/ros.h>
#include <std_msgs/UInt32MultiArray.h>
#include <std_msgs/Float32MultiArray.h>
#include <std_msgs/UInt32.h>
#include <std_msgs/UInt8.h>
#include <std_msgs/Int32.h>
#define DEFAULT_SLIDE_DISTANCE 0.711305
#define DEFAULT_SLIDE_SPEED 1 // in m/s
#define HEIGHT_LEVEL_TOLERANCE 1.5
#define ELEV_DOOR_STATE_OPEN 1
#define ELEV_DOOR_STATE_CLOSE 0
#define ELEV_DOOR_STATE_FREE 2
/*
Limitations:
The door must be facing either the x axis or the y axis; not skewed in any sense
*/
enum DoorDirection {LEFT, RIGHT};
namespace gazebo
{
class AutoElevDoorPlugin : public ModelPlugin
{
private:
ros::NodeHandle *rosNode;
event::ConnectionPtr updateConnection;
ros::Subscriber target_floor_sub, est_floor_sub, open_close_sub, active_elevs_sub;
physics::ModelPtr model, elevatorModel;
physics::LinkPtr doorLink;
std::string model_domain_space, elevator_ref_name, elevator_domain_space;
int elevator_ref_num, targetFloor, estCurrFloor;
DoorDirection direction;
uint doorState;
float openVel, closeVel, slide_speed;
float max_trans_dist, maxPosX, maxPosY, minPosX, minPosY;
bool isActive;
public:
AutoElevDoorPlugin()
{
std::string name = "auto_elevator_door_plugin";
int argc = 0;
ros::init(argc, NULL, name);
}
~AutoElevDoorPlugin()
{
delete rosNode;
}
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
{
determineDomainSpace(_sdf);
determineCorresElev(_sdf);
determineDoorDirection(_sdf);
determineConstraints(_sdf);
establishLinks(_parent);
initVars();
}
private:
void OnUpdate()
{
ros::spinOnce();
activateDoors();
checkSlideConstraints();
}
void determineDomainSpace(sdf::ElementPtr _sdf)
{
if (!_sdf->HasElement("model_domain_space")) {
ROS_WARN("Model Domain Space not specified in the plugin reference. Defaulting to 'auto_door_'");
model_domain_space = "auto_door_";
} else {
model_domain_space = _sdf->GetElement("model_domain_space")->Get<std::string>();
}
rosNode = new ros::NodeHandle("");
if (!rosNode->hasParam("/model_dynamics_manager/elevator_domain_space")) {
ROS_ERROR("The parameter 'elevator_domain_space' does not exist. Check that the elevator plugin sets this param");
std::exit(EXIT_FAILURE);
} else {
rosNode->getParam("/model_dynamics_manager/elevator_domain_space", elevator_domain_space);
}
}
void determineCorresElev(sdf::ElementPtr _sdf)
{
if (!_sdf->HasElement("elevator_name")) {
ROS_ERROR("Elevator name not specified in the plugin reference. An auto door can exist only if there is a corresponding elevator.");
std::exit(EXIT_FAILURE);
} else {
elevator_ref_name = _sdf->GetElement("elevator_name")->Get<std::string>();
}
}
void determineDoorDirection(sdf::ElementPtr _sdf)
{
if (!_sdf->HasElement("door_direction")) {
ROS_WARN("Door direction not specified in the plugin reference. Defaulting to 'left'");
direction = LEFT;
} else {
std::string direction_str = _sdf->GetElement("door_direction")->Get<std::string>();
direction = direction_str.compare("right") == 0 ? RIGHT : LEFT;
}
}
void determineConstraints(sdf::ElementPtr _sdf)
{
if (!_sdf->HasElement("max_trans_dist")) {
ROS_WARN("Maximum translation distance not specified in the plugin reference. Defaulting to '0.711305'");
max_trans_dist = DEFAULT_SLIDE_DISTANCE;
} else {
max_trans_dist = _sdf->GetElement("max_trans_dist")->Get<float>();
}
if (!_sdf->HasElement("speed")) {
ROS_WARN("Sliding speed not specified in the plugin reference. Defaulting to '1.0 m/s'");
slide_speed = DEFAULT_SLIDE_SPEED;
} else {
slide_speed = _sdf->GetElement("speed")->Get<float>();
}
}
void establishLinks(physics::ModelPtr _parent)
{
model = _parent;
doorLink = model->GetLink("door");
target_floor_sub = rosNode->subscribe<std_msgs::Int32>("/elevator_controller/target_floor", 50, &AutoElevDoorPlugin::target_floor_cb, this);
est_floor_sub = rosNode->subscribe<std_msgs::Int32>("/elevator_controller/" + elevator_ref_name + "/estimated_current_floor", 50, &AutoElevDoorPlugin::est_floor_cb, this);
open_close_sub = rosNode->subscribe<std_msgs::UInt8>("/elevator_controller/door", 50, &AutoElevDoorPlugin::open_close_cb, this);
active_elevs_sub = rosNode->subscribe<std_msgs::UInt32MultiArray>("/elevator_controller/active", 50, &AutoElevDoorPlugin::active_elevs_cb, this);
updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&AutoElevDoorPlugin::OnUpdate, this));
}
void initVars()
{
// parse elevator reference number:
std::string elevator_ref_num_str = elevator_ref_name;
replaceSubstring(elevator_ref_num_str, elevator_domain_space, "");
elevator_ref_num = atoi(elevator_ref_num_str.c_str());
ROS_ASSERT(direction == LEFT || direction == RIGHT);
// compute open-close velocities
openVel = direction == RIGHT ? -slide_speed : slide_speed;
closeVel = direction == RIGHT ? slide_speed : -slide_speed;
// compute slide constraints
float spawnPosX = model->GetWorldPose().pos.x;
minPosX = direction == RIGHT ? spawnPosX - max_trans_dist : spawnPosX;
maxPosX = direction == RIGHT ? spawnPosX : spawnPosX + max_trans_dist;
float spawnPosY = model->GetWorldPose().pos.y;
minPosY = direction == RIGHT ? spawnPosY - max_trans_dist : spawnPosY;
maxPosY = direction == RIGHT ? spawnPosY : spawnPosY + max_trans_dist;
elevatorModel = model->GetWorld()->GetModel(elevator_ref_name);
}
void activateDoors()
{
if (!isActive) {
return;
}
float currElevHeight = elevatorModel->GetWorldPose().pos.z;
float currDoorHeight = model->GetWorldPose().pos.z;
float doorElevHeightDiff = fabs(currElevHeight - currDoorHeight);
// Primary condition: the elevator is behind the doors
if (doorElevHeightDiff > HEIGHT_LEVEL_TOLERANCE || estCurrFloor != targetFloor) {
setDoorSlideVel(closeVel);
return;
}
// Secondary condition: check if the door has to be forced open/closed [OVERIDE auto open-close]
if (doorState == ELEV_DOOR_STATE_OPEN) {
setDoorSlideVel(openVel);
return;
} else if (doorState == ELEV_DOOR_STATE_CLOSE) {
setDoorSlideVel(closeVel);
return;
}
// Else: open/close doors based on target floor reference
setDoorSlideVel(openVel);
}
void setDoorSlideVel(float vel)
{
doorLink->SetLinearVel(math::Vector3(vel, vel, 0)); // we set the vel for both x & y directions since we don't know which direction the door is facing
}
void checkSlideConstraints()
{
float currDoorPosX = model->GetWorldPose().pos.x;
float currDoorPosY = model->GetWorldPose().pos.y;
math::Pose constrainedPose;
if (currDoorPosX > maxPosX) {
constrainedPose.pos.x = maxPosX;
} else if (currDoorPosX < minPosX) {
constrainedPose.pos.x = minPosX;
} else {
constrainedPose.pos.x = currDoorPosX;
}
if (currDoorPosY > maxPosY) {
constrainedPose.pos.y = maxPosY;
} else if (currDoorPosY < minPosY) {
constrainedPose.pos.y = minPosY;
} else {
constrainedPose.pos.y = currDoorPosY;
}
constrainedPose.pos.z = model->GetWorldPose().pos.z;
constrainedPose.rot.x = model->GetWorldPose().rot.x;
constrainedPose.rot.y = model->GetWorldPose().rot.y;
constrainedPose.rot.z = model->GetWorldPose().rot.z;
model->SetWorldPose(constrainedPose);
}
void target_floor_cb(const std_msgs::Int32::ConstPtr& msg)
{
targetFloor = msg->data;
}
void est_floor_cb(const std_msgs::Int32::ConstPtr& msg)
{
estCurrFloor = msg->data;
}
void open_close_cb(const std_msgs::UInt8::ConstPtr& msg)
{
doorState = msg->data;
}
void active_elevs_cb(const std_msgs::UInt32MultiArray::ConstPtr& array)
{
isActive = false;
for (std::vector<uint32_t>::const_iterator it = array->data.begin(); it != array->data.end(); ++it) {
if (*it == elevator_ref_num) {
isActive = true;
}
}
}
std::string replaceSubstring(std::string &s, std::string toReplace, std::string replaceWith)
{
return(s.replace(s.find(toReplace), toReplace.length(), replaceWith));
}
};
GZ_REGISTER_MODEL_PLUGIN(AutoElevDoorPlugin);
}