Skip to content

Commit b483e7e

Browse files
Javier JM. MorenoBorja Outerelo
authored andcommitted
Feature/u ros agent node
1 parent d66fa96 commit b483e7e

3 files changed

Lines changed: 324 additions & 2 deletions

File tree

‎uROS_Agent/CMakeLists.txt‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ cmake_minimum_required(VERSION 3.5)
2020
project(uros_agent)
2121

2222

23+
set(CMAKE_C_CLANG_TIDY clang-tidy -checks=*)
24+
25+
# Default to C++14
26+
if(NOT CMAKE_CXX_STANDARD)
27+
set(CMAKE_CXX_STANDARD 14)
28+
endif()
29+
30+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
31+
add_compile_options(-Wall -Wextra -Wpedantic)
32+
endif()
33+
34+
2335
# Find packages depencences
2436
find_package(rosidl_cmake REQUIRED)
2537
find_package(ament_cmake REQUIRED)
@@ -193,11 +205,45 @@ set (_XmlDoc "<profiles>\n${_XmlDoc}</profiles>\n")
193205
file(WRITE "${_DEFAULT_FASTRTPS_PROFILES_PATH}" "${_XmlDoc}")
194206

195207

208+
# Only compile uROS agent if rclcpp is found
209+
find_package(rclcpp QUIET)
210+
if (rclcpp_FOUND)
211+
find_package(microxrcedds_agent_cmake_module REQUIRED)
212+
find_package(MicroXRCEDDS_Agent REQUIRED MODULE)
213+
214+
add_executable(${PROJECT_NAME} src/main.cpp)
215+
ament_target_dependencies(
216+
${PROJECT_NAME}
217+
rclcpp
218+
microxrcedds_agent
219+
)
220+
221+
# TODO(Javier) Temporal until thread error is solver
222+
target_link_libraries(
223+
${PROJECT_NAME}
224+
microxrcedds_agent
225+
)
226+
227+
else()
228+
message("uROS agent node will be not build")
229+
endif()
230+
231+
232+
196233

197234
# Install the package.xml file, and generate code for ``find_package`` so that other packages can get information about this package.
198235
ament_package()
199236

200237

238+
# Only install if uROS_Agent is comiled
239+
if (rclcpp_FOUND)
240+
install(TARGETS
241+
${PROJECT_NAME}
242+
DESTINATION lib/${PROJECT_NAME}
243+
)
244+
endif()
245+
246+
201247
#Install
202248
install(
203249
FILES "${_DEFAULT_FASTRTPS_PROFILES_PATH}"
@@ -209,4 +255,4 @@ install(
209255
#install(
210256
# DIRECTORY cmake resource
211257
# DESTINATION share/${PROJECT_NAME}
212-
#)
258+
#)

‎uROS_Agent/package.xml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
<depend>fastcdr</depend>
1616
<depend>fastrtps</depend>
17-
<depend>microxrcedds_agent</depend>
17+
<depend>microxrcedds_agent_cmake_module</depend>
1818
<depend>rosidl_cmake</depend>
19+
20+
<build_depend>rclcpp</build_depend>
21+
22+
<exec_depend>rclcpp</exec_depend>
1923

2024
<test_depend>ament_lint_auto</test_depend>
2125
<test_depend>ament_lint_common</test_depend>

‎uROS_Agent/src/main.cpp‎

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
2+
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#ifdef _WIN32
17+
#include <uxr/agent/transport/udp/UDPServerWindows.hpp>
18+
#include <uxr/agent/transport/tcp/TCPServerWindows.hpp>
19+
#else
20+
#include <uxr/agent/transport/serial/SerialServerLinux.hpp>
21+
#include <uxr/agent/transport/udp/UDPServerLinux.hpp>
22+
#include <uxr/agent/transport/tcp/TCPServerLinux.hpp>
23+
#include <termios.h>
24+
#include <fcntl.h>
25+
#endif //_WIN32
26+
#include <iostream>
27+
#include <string>
28+
#include <limits>
29+
#include "rclcpp/rclcpp.hpp"
30+
31+
void showHelp()
32+
{
33+
std::cout << "Usage: program <command>" << std::endl;
34+
std::cout << "List of commands:" << std::endl;
35+
std::cout << " serial <device_name>" << std::endl;
36+
std::cout << " pseudo-serial" << std::endl;
37+
std::cout << " udp <local_port> [<discovery_port>]" << std::endl;
38+
std::cout << " tcp <local_port> [<discovery_port>]" << std::endl;
39+
}
40+
41+
void initializationError()
42+
{
43+
std::cout << "Error: Invalid arguments." << std::endl;
44+
showHelp();
45+
std::exit(EXIT_FAILURE);
46+
}
47+
48+
uint16_t parsePort(const char* str_port)
49+
{
50+
uint16_t valid_port = 0;
51+
try
52+
{
53+
int port = std::stoi(str_port);
54+
if(port > (std::numeric_limits<uint16_t>::max)())
55+
{
56+
std::cout << "Error: port number '" << port << "out of range." << std::endl;
57+
initializationError();
58+
}
59+
valid_port = uint16_t(port);
60+
}
61+
catch (const std::invalid_argument& )
62+
{
63+
initializationError();
64+
}
65+
return valid_port;
66+
}
67+
68+
69+
class uros_agent : public rclcpp::Node
70+
{
71+
public:
72+
uros_agent() : Node("uROS_Agent")
73+
{
74+
}
75+
76+
~uros_agent()
77+
{
78+
}
79+
80+
private:
81+
82+
};
83+
84+
85+
int main(int argc, char** argv)
86+
{
87+
bool initialized = false;
88+
89+
#ifndef _WIN32
90+
if(argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))
91+
{
92+
showHelp();
93+
}
94+
else if(argc >= 3 && strcmp(argv[1], "udp") == 0)
95+
{
96+
std::cout << "UDP agent initialization... ";
97+
uint16_t port = parsePort(argv[2]);
98+
eprosima::uxr::UDPServer* udp_server = (argc == 4) //discovery port
99+
? new eprosima::uxr::UDPServer(port, parsePort(argv[3]))
100+
: new eprosima::uxr::UDPServer(port);
101+
if (udp_server->run())
102+
{
103+
initialized = true;
104+
}
105+
std::cout << ((!initialized) ? "ERROR" : "OK") << std::endl;
106+
}
107+
else if(argc >= 3 && strcmp(argv[1], "tcp") == 0)
108+
{
109+
std::cout << "TCP agent initialization... ";
110+
uint16_t port = parsePort(argv[2]);
111+
eprosima::uxr::TCPServer* tcp_server = (argc == 4) //discovery port
112+
? new eprosima::uxr::TCPServer(port, parsePort(argv[3]))
113+
: new eprosima::uxr::TCPServer(port);
114+
if (tcp_server->run())
115+
{
116+
initialized = true;
117+
}
118+
std::cout << ((!initialized) ? "ERROR" : "OK") << std::endl;
119+
}
120+
else if(argc == 3 && strcmp(argv[1], "serial") == 0)
121+
{
122+
std::cout << "Serial agent initialization... ";
123+
124+
/* Open serial device. */
125+
int fd = open(argv[2], O_RDWR | O_NOCTTY);
126+
if (0 < fd)
127+
{
128+
struct termios tty_config;
129+
memset(&tty_config, 0, sizeof(tty_config));
130+
if (0 == tcgetattr(fd, &tty_config))
131+
{
132+
/* Setting CONTROL OPTIONS. */
133+
tty_config.c_cflag |= CREAD; // Enable read.
134+
tty_config.c_cflag |= CLOCAL; // Set local mode.
135+
tty_config.c_cflag &= ~PARENB; // Disable parity.
136+
tty_config.c_cflag &= ~CSTOPB; // Set one stop bit.
137+
tty_config.c_cflag &= ~CSIZE; // Mask the character size bits.
138+
tty_config.c_cflag |= CS8; // Set 8 data bits.
139+
tty_config.c_cflag &= ~CRTSCTS; // Disable hardware flow control.
140+
141+
/* Setting LOCAL OPTIONS. */
142+
tty_config.c_lflag &= ~ICANON; // Set non-canonical input.
143+
tty_config.c_lflag &= ~ECHO; // Disable echoing of input characters.
144+
tty_config.c_lflag &= ~ECHOE; // Disable echoing the erase character.
145+
tty_config.c_lflag &= ~ISIG; // Disable SIGINTR, SIGSUSP, SIGDSUSP and SIGQUIT signals.
146+
147+
/* Setting INPUT OPTIONS. */
148+
tty_config.c_iflag &= ~IXON; // Disable output software flow control.
149+
tty_config.c_iflag &= ~IXOFF; // Disable input software flow control.
150+
tty_config.c_iflag &= ~INPCK; // Disable parity check.
151+
tty_config.c_iflag &= ~ISTRIP; // Disable strip parity bits.
152+
tty_config.c_iflag &= ~IGNBRK; // No ignore break condition.
153+
tty_config.c_iflag &= ~IGNCR; // No ignore carrier return.
154+
tty_config.c_iflag &= ~INLCR; // No map NL to CR.
155+
tty_config.c_iflag &= ~ICRNL; // No map CR to NL.
156+
157+
/* Setting OUTPUT OPTIONS. */
158+
tty_config.c_oflag &= ~OPOST; // Set raw output.
159+
160+
/* Setting OUTPUT CHARACTERS. */
161+
tty_config.c_cc[VMIN] = 10;
162+
tty_config.c_cc[VTIME] = 1;
163+
164+
/* Setting BAUD RATE. */
165+
cfsetispeed(&tty_config, B115200);
166+
cfsetospeed(&tty_config, B115200);
167+
168+
if (0 == tcsetattr(fd, TCSANOW, &tty_config))
169+
{
170+
eprosima::uxr::SerialServer* serial_server = new eprosima::uxr::SerialServer(fd, 0);
171+
if (serial_server->run())
172+
{
173+
initialized = true;
174+
}
175+
}
176+
}
177+
}
178+
179+
std::cout << ((!initialized) ? "ERROR" : "OK") << std::endl;
180+
}
181+
else if (argc == 2 && strcmp(argv[1], "pseudo-serial") == 0)
182+
{
183+
std::cout << "Pseudo-Serial initialization... ";
184+
185+
/* Open pseudo-terminal. */
186+
char* dev = NULL;
187+
int fd = posix_openpt(O_RDWR | O_NOCTTY);
188+
if (-1 != fd)
189+
{
190+
if (grantpt(fd) == 0 && unlockpt(fd) == 0 && (dev = ptsname(fd)))
191+
{
192+
struct termios attr;
193+
tcgetattr(fd, &attr);
194+
cfmakeraw(&attr);
195+
tcflush(fd, TCIOFLUSH);
196+
tcsetattr(fd, TCSANOW, &attr);
197+
}
198+
}
199+
200+
/* Launch server. */
201+
eprosima::uxr::SerialServer* serial_server = new eprosima::uxr::SerialServer(fd, 0x00);
202+
if (serial_server->run())
203+
{
204+
initialized = true;
205+
}
206+
207+
if (initialized)
208+
{
209+
std::cout << "OK" << std::endl;
210+
std::cout << "Device: " << dev << std::endl;
211+
}
212+
else
213+
{
214+
std::cout << "ERROR" << std::endl;
215+
}
216+
}
217+
else
218+
{
219+
initializationError();
220+
}
221+
#else
222+
(void) argc;
223+
(void) argv;
224+
225+
std::string server_type = "";
226+
std::cout << "Select server type [udp|tcp]: ";
227+
std::cin >> server_type;
228+
229+
if ("udp" == server_type)
230+
{
231+
uint16_t port = 0;
232+
std::cout << "Select port: ";
233+
std::cin >> port;
234+
235+
std::cout << "UDP agent initialization.... ";
236+
eprosima::uxr::UDPServer* udp_server = new eprosima::uxr::UDPServer(port);
237+
if (udp_server->run())
238+
{
239+
initialized = true;
240+
}
241+
std::cout << ((!initialized) ? "ERROR" : "OK") << std::endl;
242+
}
243+
else if ("tcp" == server_type)
244+
{
245+
uint16_t port = 0;
246+
std::cout << "Select port: ";
247+
std::cin >> port;
248+
249+
std::cout << "UDP agent initialization.... ";
250+
eprosima::uxr::TCPServer* tcp_server = new eprosima::uxr::TCPServer(port);
251+
if (tcp_server->run())
252+
{
253+
initialized = true;
254+
}
255+
std::cout << ((!initialized) ? "ERROR" : "OK") << std::endl;
256+
}
257+
else
258+
{
259+
std::cout << "Error server type" << std::endl;
260+
}
261+
262+
#endif
263+
264+
if(initialized)
265+
{
266+
rclcpp::init(argc, argv);
267+
rclcpp::spin(std::make_shared<uros_agent>());
268+
rclcpp::shutdown();
269+
}
270+
271+
return 0;
272+
}

0 commit comments

Comments
 (0)