Skip to content

Commit

Permalink
Merge pull request #331 from mateusz-lichota/wait-for-rpc-connection
Browse files Browse the repository at this point in the history
Add waiting for RPC server to become available
  • Loading branch information
wouter-heerwegh authored Sep 15, 2022
2 parents c0b5580 + a72e77d commit 7f52b83
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 37 deletions.
6 changes: 5 additions & 1 deletion AirSim/AirLib/include/api/RpcLibClientBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RpcLibClientBase {
RpcLibClientBase(const string& ip_address = "localhost", uint16_t port = RpcLibPort, float timeout_sec = 60);
virtual ~RpcLibClientBase(); //required for pimpl

void confirmConnection();
void confirmConnection(double timeout);
void reset();

ConnectionState getConnectionState();
Expand Down Expand Up @@ -107,6 +107,10 @@ class RpcLibClientBase {
const void* getClient() const;

private:
std::string ip_address_;
uint16_t port_;
float timeout_sec_;

struct impl;
std::unique_ptr<impl> pimpl_;
};
Expand Down
18 changes: 14 additions & 4 deletions AirSim/AirLib/src/api/RpcLibClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ struct RpcLibClientBase::impl {

typedef msr::airlib_rpclib::RpcLibAdapatorsBase RpcLibAdapatorsBase;

RpcLibClientBase::RpcLibClientBase(const string& ip_address, uint16_t port, float timeout_sec)
RpcLibClientBase::RpcLibClientBase(const string& ip_address, uint16_t port, float timeout_sec) : ip_address_(ip_address), port_(port), timeout_sec_(timeout_sec)
{
pimpl_.reset(new impl(ip_address, port, timeout_sec));
pimpl_.reset(new impl(ip_address_, port_, timeout_sec_));
}

RpcLibClientBase::~RpcLibClientBase()
Expand Down Expand Up @@ -111,11 +111,21 @@ void RpcLibClientBase::reset()
pimpl_->client.call("reset");
}

void RpcLibClientBase::confirmConnection()
void RpcLibClientBase::confirmConnection(double timeout)
{
ClockBase* clock = ClockFactory::get();

clock->sleep_for(1);
double delay = 0.2;
for (double elapsed = 0.0; elapsed <= timeout; elapsed += delay) {
pimpl_.reset(new impl(ip_address_, port_, timeout_sec_));
if (getConnectionState() == RpcLibClientBase::ConnectionState::Connected)
{
break;
}
clock->sleep_for(delay);
}


if (getConnectionState() != RpcLibClientBase::ConnectionState::Connected)
{
throw std::runtime_error("Failed connecting to RPC server (airsim). Is the simulator running?");
Expand Down
6 changes: 3 additions & 3 deletions ros/src/fsds_ros_bridge/include/airsim_ros_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ struct SimpleMatrix
class AirsimROSWrapper
{
public:
AirsimROSWrapper(const ros::NodeHandle& nh, const ros::NodeHandle& nh_private, const std::string& host_ip);
AirsimROSWrapper(const ros::NodeHandle& nh, const ros::NodeHandle& nh_private, const std::string& host_ip, double timeout_sec);
~AirsimROSWrapper(){};

void initialize_airsim();
void initialize_airsim(double timeout);
void initialize_ros();
void publish_track();
void initialize_statistics();
Expand Down Expand Up @@ -223,4 +223,4 @@ class AirsimROSWrapper
/// ROS subscribers
ros::Subscriber control_cmd_sub;
ros::Subscriber finished_signal_sub_;
};
};
4 changes: 2 additions & 2 deletions ros/src/fsds_ros_bridge/scripts/cameralauncher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import roslaunch
from os.path import expanduser
import json
Expand Down Expand Up @@ -62,4 +62,4 @@ def signal_handler(sig, frame):
launch.spin()
finally:
# After Ctrl+C, stop all nodes from running
launch.stop()
launch.stop()
21 changes: 11 additions & 10 deletions ros/src/fsds_ros_bridge/src/airsim_ros_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
// PLUGINLIB_EXPORT_CLASS(AirsimROSWrapper, nodelet::Nodelet)
#include "common/AirSimSettings.hpp"

AirsimROSWrapper::AirsimROSWrapper(const ros::NodeHandle& nh, const ros::NodeHandle& nh_private, const std::string& host_ip) : nh_(nh),
AirsimROSWrapper::AirsimROSWrapper(const ros::NodeHandle& nh, const ros::NodeHandle& nh_private, const std::string& host_ip, double timeout_sec) : nh_(nh),
nh_private_(nh_private),
lidar_async_spinner_(1, &lidar_timer_cb_queue_), // same as above, but for lidar
airsim_client_(host_ip),
airsim_client_lidar_(host_ip)
airsim_client_(host_ip, RpcLibPort, timeout_sec),
airsim_client_lidar_(host_ip, RpcLibPort, timeout_sec)
{
initialize_airsim(timeout_sec);
try {
airsim_client_.confirmConnection();
ROS_INFO_STREAM("READING SETTINGS");
std::string settings_text = airsim_client_.getSettingsString();
msr::airlib::AirSimSettings::initializeSettings(settings_text);
ROS_INFO_STREAM("SETTINGS READ");

msr::airlib::AirSimSettings::initializeSettings(settings_text);
msr::airlib::AirSimSettings::singleton().load();
for (const auto &warning : msr::airlib::AirSimSettings::singleton().warning_messages)
{
Expand All @@ -35,14 +37,15 @@ AirsimROSWrapper::AirsimROSWrapper(const ros::NodeHandle& nh, const ros::NodeHan
ROS_INFO_STREAM("AirsimROSWrapper Initialized!");
}

void AirsimROSWrapper::initialize_airsim()
void AirsimROSWrapper::initialize_airsim(double timeout)
{
// todo do not reset if already in air?
try
{
double elapsed = 0.0;
ROS_INFO_STREAM("Waiting for connection - ");
airsim_client_.confirmConnection();
airsim_client_lidar_.confirmConnection();
airsim_client_.confirmConnection(timeout);
airsim_client_lidar_.confirmConnection(timeout);
ROS_INFO_STREAM("Connected to the simulator!");
}
catch (rpc::rpc_error& e)
Expand Down Expand Up @@ -257,8 +260,6 @@ void AirsimROSWrapper::create_ros_pubs_from_settings_json()
}

reset_srvr_ = nh_.advertiseService("reset", &AirsimROSWrapper::reset_srv_cb, this);

initialize_airsim();
}

ros::Time AirsimROSWrapper::make_ts(uint64_t unreal_ts)
Expand Down
5 changes: 4 additions & 1 deletion ros/src/fsds_ros_bridge/src/fsds_ros_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ int main(int argc, char ** argv)
ros::NodeHandle nh_private("~");

std::string host_ip = "localhost";
double timeout_sec = 10;
nh_private.getParam("host_ip", host_ip);
AirsimROSWrapper airsim_ros_wrapper(nh, nh_private, host_ip);
nh_private.getParam("timeout", timeout_sec);

AirsimROSWrapper airsim_ros_wrapper(nh, nh_private, host_ip, timeout_sec);

if (airsim_ros_wrapper.is_used_lidar_timer_cb_queue_)
{
Expand Down
5 changes: 4 additions & 1 deletion ros/src/fsds_ros_bridge/src/fsds_ros_bridge_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,12 @@ int main(int argc, char ** argv)
msr::airlib::CarRpcLibClient client(host_ip, RpcLibPort, 5);
airsim_api = &client;

double timeout_sec = 10.0;
nh.getParam("timeout", timeout_sec);

try {
std::cout << "Waiting for connection - " << std::endl;
airsim_api->confirmConnection();
airsim_api->confirmConnection(timeout_sec);
std::cout << "Connected to the simulator!" << std::endl;
} catch (const std::exception &e) {
std::string msg = e.what();
Expand Down
5 changes: 3 additions & 2 deletions ros2/src/fsds_ros2_bridge/include/airsim_ros_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ struct SimpleMatrix
class AirsimROSWrapper
{
public:
AirsimROSWrapper(const std::shared_ptr<rclcpp::Node>& nh, const std::string& host_ip);
AirsimROSWrapper(const std::shared_ptr<rclcpp::Node>& nh, const std::string& host_ip, double timeout_sec);
~AirsimROSWrapper(){};

void initialize_airsim();
void initialize_airsim(double timeout_sec);
void initialize_ros();
void publish_track();
void initialize_statistics();
Expand Down Expand Up @@ -182,6 +182,7 @@ class AirsimROSWrapper
bool competition_mode_;
rclcpp::Time go_timestamp_;

std::string host_ip_;
msr::airlib::CarRpcLibClient airsim_client_;
msr::airlib::CarRpcLibClient airsim_client_lidar_;

Expand Down
22 changes: 11 additions & 11 deletions ros2/src/fsds_ros2_bridge/src/airsim_ros_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

using dseconds = std::chrono::duration<double>;

AirsimROSWrapper::AirsimROSWrapper(const std::shared_ptr<rclcpp::Node>& nh, const std::string& host_ip)
AirsimROSWrapper::AirsimROSWrapper(const std::shared_ptr<rclcpp::Node>& nh, const std::string& host_ip, double timeout_sec)
: nh_(nh),
airsim_client_(host_ip),
airsim_client_lidar_(host_ip),
airsim_client_(host_ip, RpcLibPort, timeout_sec),
airsim_client_lidar_(host_ip, RpcLibPort, timeout_sec),
static_tf_pub_(this->nh_)
{
initialize_airsim(timeout_sec);
try {
airsim_client_.confirmConnection();
std::string settings_text = airsim_client_.getSettingsString();
msr::airlib::AirSimSettings::initializeSettings(settings_text);

Expand All @@ -37,14 +37,16 @@ AirsimROSWrapper::AirsimROSWrapper(const std::shared_ptr<rclcpp::Node>& nh, cons
RCLCPP_INFO(nh_->get_logger(), "AirsimROSWrapper Initialized!");
}

void AirsimROSWrapper::initialize_airsim()
void AirsimROSWrapper::initialize_airsim(double timeout)
{
// todo do not reset if already in air?
try
{
RCLCPP_INFO(nh_->get_logger(), "Waiting for connection");
airsim_client_.confirmConnection();
airsim_client_lidar_.confirmConnection();
{
double elapsed = 0.0;
RCLCPP_INFO_STREAM(nh_->get_logger(), "Waiting for connection");

airsim_client_.confirmConnection(timeout);
airsim_client_lidar_.confirmConnection(timeout);
RCLCPP_INFO(nh_->get_logger(), "Connected to the simulator!");
}
catch (rpc::rpc_error& e)
Expand Down Expand Up @@ -270,8 +272,6 @@ void AirsimROSWrapper::create_ros_pubs_from_settings_json()
}

reset_srvr_ = nh_->create_service<fs_msgs::srv::Reset>("reset", std::bind(&AirsimROSWrapper::reset_srv_cb, this, std::placeholders::_1, std::placeholders::_2));

initialize_airsim();
}

rclcpp::Time AirsimROSWrapper::make_ts(uint64_t unreal_ts) const
Expand Down
3 changes: 2 additions & 1 deletion ros2/src/fsds_ros2_bridge/src/fsds_ros2_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ int main(int argc, char ** argv)
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("fsds_ros2_bridge");

std::string host_ip = node->declare_parameter<std::string>("host_ip", "localhost");
double timeout_sec = node->declare_parameter<double>("timeout", 10.0);

AirsimROSWrapper airsim_ros_wrapper(node, host_ip);
AirsimROSWrapper airsim_ros_wrapper(node, host_ip, timeout_sec);

// if (airsim_ros_wrapper.is_used_lidar_timer_cb_queue_)
// {
Expand Down
4 changes: 3 additions & 1 deletion ros2/src/fsds_ros2_bridge/src/fsds_ros2_bridge_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ int main(int argc, char ** argv)
msr::airlib::CarRpcLibClient client(host_ip, RpcLibPort, 5);
airsim_api = &client;

double timeout_sec = nh->declare_parameter<double>("timeout", 10.0);

try {
std::cout << "Waiting for connection - " << std::endl;
airsim_api->confirmConnection();
airsim_api->confirmConnection(timeout_sec);
std::cout << "Connected to the simulator!" << std::endl;
} catch (const std::exception &e) {
std::string msg = e.what();
Expand Down

0 comments on commit 7f52b83

Please sign in to comment.