Skip to content

Commit

Permalink
Merge branch 'main' into fix/allow-param-defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolja Waschk committed Jan 17, 2025
2 parents ccb98a6 + 8a5777d commit ae2c63c
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/pf_driver/config/r2000_params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pf_r2000:
ros__parameters:
transport: "udp"
scanner_ip: "10.0.10.9"
# port: "3000" # optional
# port: 3000 # optional
device: "R2000"
start_angle: -1800000
max_num_points_scan: 0
Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/config/r2300_params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pf_r2300:
ros__parameters:
transport: "udp"
scanner_ip: "169.254.235.110"
# port: "3000" # optional
# port: 3000 # optional
start_angle: -450000
max_num_points_scan: 0
packet_type: "C1"
Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/config/r2300_single_layer_params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pf_r2300:
ros__parameters:
transport: "udp"
scanner_ip: "169.254.235.110"
# port: "3000" # optional
# port: 3000 # optional
start_angle: -450000
max_num_points_scan: 0
packet_type: "C1"
Expand Down
8 changes: 4 additions & 4 deletions src/pf_driver/include/pf_driver/communication/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class Transport
{
}

void set_port(std::string port)
void set_port(int port)
{
port_ = std::move(port);
port_ = port;
}

std::string get_port()
int get_port()
{
return port_;
}
Expand Down Expand Up @@ -65,7 +65,7 @@ class Transport
protected:
std::string address_;
std::string host_ip_;
std::string port_;
int port_;
bool is_connected_;
transport_type type_;
std::shared_ptr<boost::asio::io_service> io_service_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class UDPTransport : public Transport
{
public:
UDPTransport(std::string address, std::string port = "0");
UDPTransport(std::string address, int port = 0);

~UDPTransport();

Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/include/pf_driver/pf/handle_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct HandleInfo

int handle_type;
std::string hostname;
std::string port;
int actual_port;
std::string handle;
std::string endpoint;
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <curlpp/Options.hpp>
#include <json/json.h>

#include "pf_driver/pf/http_helpers/param_map_type.h"
#include "pf_driver/pf/http_helpers/param_vector_type.h"
#include "pf_driver/pf/http_helpers/param_type.h"

class CurlResource
Expand All @@ -32,7 +32,7 @@ class CurlResource

void append_query(const std::initializer_list<param_type>& list, bool do_encoding = false);

void append_query(const param_map_type& params, bool do_encoding = false);
void append_query(const param_vector_type& params, bool do_encoding = false);

void get(Json::Value& json_resp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <json/json.h>

#include "pf_driver/pf/http_helpers/param_type.h"
#include "pf_driver/pf/http_helpers/param_map_type.h"
#include "pf_driver/pf/http_helpers/param_vector_type.h"

class CurlResource;

Expand All @@ -29,7 +29,7 @@ class HTTPInterface
const Json::Value get(const std::string& command,
const std::initializer_list<param_type>& list = std::initializer_list<param_type>());

const Json::Value get(const std::string& command, const param_map_type& params = param_map_type());
const Json::Value get(const std::string& command, const param_vector_type& params = param_vector_type());

private:
const Json::Value get_(CurlResource& res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#pragma once

#include <map>
#include <string>
#include <vector>
#include "param_type.h"

using param_map_type = std::map<std::string, std::string>;
using param_vector_type = std::vector<param_type>;
6 changes: 3 additions & 3 deletions src/pf_driver/include/pf_driver/pf/pfsdp_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "pf_driver/pf/http_helpers/http_interface.h"
#include "pf_driver/pf/http_helpers/param_type.h"
#include "pf_driver/pf/http_helpers/param_map_type.h"
#include "pf_driver/pf/http_helpers/param_vector_type.h"
#include "pf_driver/pf/handle_info.h"
#include "pf_driver/pf/scan_config.h"
#include "pf_driver/pf/scan_parameters.h"
Expand All @@ -45,7 +45,7 @@ class PFSDPBase
const std::initializer_list<param_type>& query);
const std::map<std::string, std::string>
get_request(const std::string& command, const std::vector<std::string>& json_keys = std::vector<std::string>(),
const param_map_type& query = param_map_type());
const param_vector_type& query = param_vector_type());

bool get_request_bool(const std::string& command,
const std::vector<std::string>& json_keys = std::vector<std::string>(),
Expand Down Expand Up @@ -100,7 +100,7 @@ class PFSDPBase

std::string get_parameter_str(const std::string& param);

void request_handle_tcp();
void request_handle_tcp(int port = 0);

virtual void request_handle_udp();

Expand Down
1 change: 1 addition & 0 deletions src/pf_driver/include/pf_driver/pf/scan_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct ScanConfig
int start_angle = 0;
uint max_num_points_scan = 0;
uint skip_scans = 0;
uint port = 0;

// void print()
// {
Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/src/communication/tcp_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool TCPTransport::connect()
try
{
tcp::resolver resolver(*io_service_);
tcp::resolver::query query(address_, port_);
tcp::resolver::query query(address_, std::to_string(port_));
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

Expand Down
6 changes: 3 additions & 3 deletions src/pf_driver/src/communication/udp_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ using boost::asio::ip::udp;

auto udp_logger = rclcpp::get_logger("transport");

UDPTransport::UDPTransport(std::string address, std::string port) : Transport(address, transport_type::udp)
UDPTransport::UDPTransport(std::string address, int port) : Transport(address, transport_type::udp)
{
io_service_ = std::make_shared<boost::asio::io_service>();
udp::endpoint local_endpoint = udp::endpoint(boost::asio::ip::address_v4::from_string("0.0.0.0"), stoi(port));
udp::endpoint local_endpoint = udp::endpoint(boost::asio::ip::address_v4::from_string("0.0.0.0"), port);

socket_ = std::make_unique<udp::socket>(*io_service_, local_endpoint);
timer_ = std::make_shared<boost::asio::deadline_timer>(*io_service_.get());
Expand All @@ -27,7 +27,7 @@ bool UDPTransport::connect()
{
udp::endpoint udp_endpoint = udp::endpoint(boost::asio::ip::address::from_string(address_), 0);
socket_->connect(udp_endpoint);
port_ = std::to_string(socket_->local_endpoint().port());
port_ = socket_->local_endpoint().port();
host_ip_ = socket_->local_endpoint().address().to_string();

is_connected_ = true;
Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/src/pf/http_helpers/curl_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void CurlResource::append_query(const std::initializer_list<param_type>& list, b
url_.pop_back();
}

void CurlResource::append_query(const param_map_type& params, bool do_encoding)
void CurlResource::append_query(const param_vector_type& params, bool do_encoding)
{
url_ += "?";
for (const auto& p : params)
Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/src/pf/http_helpers/http_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Json::Value HTTPInterface::get(const std::string& command, const std::init
return get_(res);
}

const Json::Value HTTPInterface::get(const std::string& command, const param_map_type& params)
const Json::Value HTTPInterface::get(const std::string& command, const param_vector_type& params)
{
CurlResource res(host);
res.append_path(base_path);
Expand Down
8 changes: 4 additions & 4 deletions src/pf_driver/src/pf/pf_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,24 @@ bool PFInterface::init(std::shared_ptr<HandleInfo> info, std::shared_ptr<ScanCon

if (info->handle_type == HandleInfo::HANDLE_TYPE_UDP)
{
transport_ = std::make_unique<UDPTransport>(info->hostname, info->port);
transport_ = std::make_unique<UDPTransport>(info->hostname, config->port);
if (!transport_->connect())
{
RCLCPP_ERROR(node_->get_logger(), "Unable to establish UDP connection");
return false;
}

info->endpoint = transport_->get_host_ip();
info->port = transport_->get_port();
info->actual_port = transport_->get_port();
protocol_interface_->request_handle_udp();
}
else if (info->handle_type == HandleInfo::HANDLE_TYPE_TCP)
{
transport_ = std::make_unique<TCPTransport>(info->hostname);
protocol_interface_->request_handle_tcp();
protocol_interface_->request_handle_tcp(config->port);
// if initially port was not set, request_handle sets it
// set the updated port in transport
transport_->set_port(info_->port);
transport_->set_port(info->actual_port);
if (!transport_->connect())
{
RCLCPP_ERROR(node_->get_logger(), "Unable to establish TCP connection");
Expand Down
38 changes: 19 additions & 19 deletions src/pf_driver/src/pf/pfsdp_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ const std::map<std::string, std::string> PFSDPBase::get_request(const std::strin
const std::vector<std::string>& json_keys,
const std::initializer_list<param_type>& query)
{
return get_request(command, json_keys, param_map_type(query.begin(), query.end()));
return get_request(command, json_keys, param_vector_type(query.begin(), query.end()));
}

const std::map<std::string, std::string> PFSDPBase::get_request(const std::string& command,
const std::vector<std::string>& json_keys,
const param_map_type& query)
const param_vector_type& query)
{
Json::Value json_resp = http_interface->get(command, query);

Expand Down Expand Up @@ -254,29 +254,29 @@ std::string PFSDPBase::get_parameter_str(const std::string& param)
return resp[param];
}

void PFSDPBase::request_handle_tcp()
void PFSDPBase::request_handle_tcp(int port)
{
param_map_type query;
param_vector_type query;
if (!config_->packet_type.empty())
{
query["packet_type"] = config_->packet_type;
query.push_back(KV("packet_type", packet_type));
}
if (info_->port.compare("0") != 0)
if (port != 0)
{
query["port"] = info_->port;
query.push_back(KV("port", port));
}
auto resp = get_request("request_handle_tcp", { "handle", "port" }, query);

info_->handle = resp["handle"];
info_->port = resp["port"];
info_->actual_port = parser_utils::to_long(resp["port"]);
}

void PFSDPBase::request_handle_udp()
void PFSDPBase::request_handle_udp(const std::string& packet_type)
{
param_map_type query = { KV("address", info_->endpoint), KV("port", info_->port) };
param_vector_type query = { KV("address", info_->endpoint), KV("port", info_->actual_port) };
if (!config_->packet_type.empty())
{
query["packet_type"] = config_->packet_type;
query.push_back(KV("packet_type", config_->packet_type));
}
auto resp = get_request("request_handle_udp", { "handle", "port" }, query);
info_->handle = resp["handle"];
Expand All @@ -298,23 +298,23 @@ void PFSDPBase::get_scanoutput_config(const std::string& handle)

bool PFSDPBase::update_scanoutput_config()
{
param_map_type query = { KV("handle", info_->handle) };
param_vector_type query = { KV("handle", info_->handle) };

query.insert(KV("start_angle", config_->start_angle));
query.push_back(KV("start_angle", config_->start_angle));
if (!config_->packet_type.empty())
{
query.insert(KV("packet_type", config_->packet_type));
query.push_back(KV("packet_type", config_->packet_type));
}
query.insert(KV("max_num_points_scan", config_->max_num_points_scan));
query.insert(KV("skip_scans", config_->skip_scans));
query.push_back(KV("max_num_points_scan", config_->max_num_points_scan));
query.push_back(KV("skip_scans", config_->skip_scans));
if (config_->watchdogtimeout != 0)
{
query.insert(KV("watchdogtimeout", config_->watchdogtimeout));
query.push_back(KV("watchdogtimeout", config_->watchdogtimeout));
}
if (config_->watchdog == false)
{
/* Force watchdog off. Otherwise (if watchdog==true), use scanner default */
query.insert(KV("watchdog", "off"));
query.push_back(KV("watchdog", "off"));
}

auto resp = get_request("set_scanoutput_config", { "" }, query);
Expand Down Expand Up @@ -395,7 +395,7 @@ bool PFSDPBase::reconfig_callback_impl(const std::vector<rclcpp::Parameter>& par
}
else if (parameter.get_name() == "port")
{
info_->port = parameter.value_to_string();
config_->port = parameter.as_int();
}
else if (parameter.get_name() == "transport")
{
Expand Down
7 changes: 4 additions & 3 deletions src/pf_driver/src/ros/ros_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ int main(int argc, char* argv[])
std::string device;
std::string transport_str;
std::string scanner_ip;
std::string port("0"); /* 0 means: automatic */
std::string topic("/scan");
std::string frame_id("scanner_link");
std::string packet_type; /* empty means: use scanner default */
Expand Down Expand Up @@ -55,7 +54,7 @@ int main(int argc, char* argv[])
node->declare_parameter("port", port);
}
node->get_parameter("port", port);
RCLCPP_INFO(node->get_logger(), "port: %s", port.c_str());
RCLCPP_INFO(node->get_logger(), "port: %d", port);

if (!node->has_parameter("start_angle"))
{
Expand Down Expand Up @@ -143,9 +142,11 @@ int main(int argc, char* argv[])
info->handle_type = transport_str == "udp" ? HandleInfo::HANDLE_TYPE_UDP : HandleInfo::HANDLE_TYPE_TCP;

info->hostname = node->get_parameter("scanner_ip").get_parameter_value().get<std::string>();
info->port = node->get_parameter("port").get_parameter_value().get<std::string>();
info->actual_port = -1;

std::shared_ptr<ScanConfig> config = std::make_shared<ScanConfig>();

config->port = node->get_parameter("port").get_parameter_value().get<int>();
config->start_angle = node->get_parameter("start_angle").get_parameter_value().get<int>();
config->max_num_points_scan = node->get_parameter("max_num_points_scan").get_parameter_value().get<int>();
config->skip_scans = node->get_parameter("skip_scans").get_parameter_value().get<int>();
Expand Down
2 changes: 1 addition & 1 deletion src/pf_driver/tests/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST(PFPipeline_TestSuite, testPipelineReadWrite)
bool net_fail = false;

std::unique_ptr<Transport> transport = std::make_unique<TCPTransport>("127.0.0.1");
transport->set_port("1234");
transport->set_port(1234);

if (transport->connect())
{
Expand Down

0 comments on commit ae2c63c

Please sign in to comment.