-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathswarm_plugin.cpp
177 lines (129 loc) · 4.44 KB
/
swarm_plugin.cpp
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
/*
Gazebo plugin loaded into an empty world to make it easy add in many distinguishable drone models
The mavlink interface in sitl_gazebo listens for messages on a random port
and sends messages to the given config port (default 14560). Likewise the PX4
simulator module be default listens for messages on 14560
*/
#include <sdf/sdf.hh>
#include "gazebo/gazebo.hh"
#include "gazebo/common/Plugin.hh"
#include "gazebo/msgs/msgs.hh"
#include "gazebo/physics/physics.hh"
#include "gazebo/transport/transport.hh"
#include "spawn.pb.h"
#include <signal.h>
#include <unistd.h>
#include <string>
#include <fstream>
#include <streambuf>
#include <sys/wait.h>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <string>
typedef const boost::shared_ptr<const tansa::msgs::SpawnRequest> SpawnRequestPtr;
typedef const boost::shared_ptr<const gazebo::msgs::Pose> PosePtr;
namespace gazebo {
class SwarmPlugin : public WorldPlugin {
public:
void Load(physics::WorldPtr _parent, sdf::ElementPtr _sdf) {
node = transport::NodePtr(new transport::Node());
world = _parent;
node->Init(world->GetName());
spawnSub = node->Subscribe("~/spawn", &SwarmPlugin::spawn_callback, this);
requestPub = node->Advertise<gazebo::msgs::Request>("~/request");
}
void spawn_callback(SpawnRequestPtr &msg) {
world->SetPaused(true);
stop_sitl();
// Figure out what to do with existing models in the world
int nexist = 0;
for(physics::ModelPtr m : world->GetModels()) {
std::string name = m->GetName();
if(strncmp(name.c_str(), "vehicle_", 8) == 0) {
// TODO: This doesn't work
// See issue: https://bitbucket.org/osrf/gazebo/issues/1629/removing-model-from-plugin-crashes-with
//world->RemoveModel(models[i]);
int num = atoi(name.c_str() + 8);
if(num < msg->vehicles_size()) { // Reuse it
const tansa::msgs::SpawnRequest_Vehicle &v = msg->vehicles(num);
m->SetRelativePose(math::Pose(
v.pos().x(),
v.pos().y(),
v.pos().z(),
v.orient().x(),
v.orient().y(),
v.orient().z()
));
nexist++;
}
else { // Delete it
msgs::Request *msg = gazebo::msgs::CreateRequest("entity_delete", name);
requestPub->Publish(*msg, true);
}
}
}
std::ifstream t(msg->sdf_file());
std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
sdf::SDF file;
file.SetFromString(str);
sdf::ElementPtr root = file.Root();
sdf::ElementPtr model = root->GetFirstElement();
sdf::ElementPtr pose = model->AddElement("pose");
// Finding a reference to the plugin which handles mavlink
sdf::ElementPtr plugin = model->GetElement("plugin");
while(plugin->GetAttribute("name")->GetAsString() != "mavlink_interface") {
plugin = plugin->GetNextElement("plugin");
}
if(!plugin) {
// Couldn't find it
}
sdf::ElementPtr port = plugin->GetElement("mavlink_udp_port");
for(int i = nexist; i < msg->vehicles_size(); i++) {
const tansa::msgs::SpawnRequest_Vehicle &v = msg->vehicles(i);
model->GetAttribute("name")->Set("vehicle_" + std::to_string(i));
std::string p = std::to_string(v.pos().x()) + " " +
std::to_string(v.pos().y()) + " " +
std::to_string(v.pos().z()) + " " +
std::to_string(v.orient().x()) + " " +
std::to_string(v.orient().y()) + " " +
std::to_string(v.orient().z());
pose->Set<std::string>(p);
port->Set<int>(14561 + 10*i);
world->InsertModelSDF(file);
}
world->SetPaused(false);
start_sitl(msg->vehicles_size(), msg->rcs_file().c_str());
}
void stop_sitl() {
if(sitl_process == 0)
return;
kill(sitl_process, SIGINT);
waitpid(sitl_process, NULL, 0);
}
void start_sitl(int n, const char *rcs_file) {
int p = fork();
if(p == 0) { // Child
char *const bash = (char *const) "/bin/bash";
char *const script = (char *const) "scripts/start_many_instances.sh";
char num[16];
strcpy(num, std::to_string(n).c_str());
char file[1024];
strcpy(file, rcs_file);
char *const argv[] = { bash, script, num, file, NULL};
execv(bash, argv);
exit(0);
return;
}
sitl_process = p;
}
private:
transport::NodePtr node;
transport::SubscriberPtr spawnSub;
transport::PublisherPtr requestPub;
physics::WorldPtr world;
transport::SubscriberPtr world_sub;
int sitl_process = 0;
};
// Register this plugin with the simulator
GZ_REGISTER_WORLD_PLUGIN(SwarmPlugin)
}