Browse Source

Simulator: Added remote host option (#15443)

* Added an option to the Simulator module to connect to remote Gazebo servers.

This is usefull when the Gazebo simulation is running on a different host than the PX4 instance.
For example, we are running instances of PX4 with a companion application in multiple Dockers, for swarming simulations, which then connect to a remote Gazebo server.

A "-t" input argument has been added and can be called from the rcS startup script as: simulator start -t "192.168.178.122" $simulator_tcp_port

* _tcp_remote_ipaddr defaults to nullptr. This way testing against nullptr it can be determined if localhost or remote host is desired by the user.
* Documented the option in usage().

Signed-off-by: Peter Blom <peterblom.mail@gmail.com>
release/1.12
Muesli_Reep 4 years ago committed by GitHub
parent
commit
fe7908feb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      src/modules/simulator/simulator.cpp
  2. 3
      src/modules/simulator/simulator.h
  3. 5
      src/modules/simulator/simulator_mavlink.cpp

7
src/modules/simulator/simulator.cpp

@ -79,6 +79,12 @@ int Simulator::start(int argc, char *argv[]) @@ -79,6 +79,12 @@ int Simulator::start(int argc, char *argv[])
_instance->set_port(atoi(argv[4]));
}
if (argc == 5 && strcmp(argv[2], "-t") == 0) {
_instance->set_ip(InternetProtocol::TCP);
_instance->set_tcp_remote_ipaddr(argv[3]);
_instance->set_port(atoi(argv[4]));
}
_instance->run();
return 0;
@ -95,6 +101,7 @@ static void usage() @@ -95,6 +101,7 @@ static void usage()
PX4_INFO("Start simulator: simulator start");
PX4_INFO("Connect using UDP: simulator start -u udp_port");
PX4_INFO("Connect using TCP: simulator start -c tcp_port");
PX4_INFO("Connect to a remote server using TCP: simulator start -t ip_addr tcp_port");
}
__BEGIN_DECLS

3
src/modules/simulator/simulator.h

@ -123,6 +123,7 @@ public: @@ -123,6 +123,7 @@ public:
void set_ip(InternetProtocol ip) { _ip = ip; }
void set_port(unsigned port) { _port = port; }
void set_tcp_remote_ipaddr(char *tcp_remote_ipaddr) { _tcp_remote_ipaddr = tcp_remote_ipaddr; }
#if defined(ENABLE_LOCKSTEP_SCHEDULER)
bool has_initialized() { return _has_initialized.load(); }
@ -201,6 +202,8 @@ private: @@ -201,6 +202,8 @@ private:
InternetProtocol _ip{InternetProtocol::UDP};
char *_tcp_remote_ipaddr{nullptr};
double _realtime_factor{1.0}; ///< How fast the simulation runs in comparison to real system time
hrt_abstime _last_sim_timestamp{0};

5
src/modules/simulator/simulator_mavlink.cpp

@ -51,6 +51,7 @@ @@ -51,6 +51,7 @@
#include <pthread.h>
#include <sys/socket.h>
#include <termios.h>
#include <arpa/inet.h>
#include <limits>
@ -673,6 +674,10 @@ void Simulator::run() @@ -673,6 +674,10 @@ void Simulator::run()
_myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
_myaddr.sin_port = htons(_port);
if (_tcp_remote_ipaddr != nullptr) {
_myaddr.sin_addr.s_addr = inet_addr(_tcp_remote_ipaddr);
}
if (_ip == InternetProtocol::UDP) {
if ((_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {

Loading…
Cancel
Save