Skip to content

Commit

Permalink
Merge branch 'branch-source' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Sep 12, 2023
2 parents bc49543 + d20865a commit 7396eea
Show file tree
Hide file tree
Showing 91 changed files with 20,087 additions and 0 deletions.
29 changes: 29 additions & 0 deletions example/idl/rosPortable/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.16)

find_package(YARP COMPONENTS os idl_tools REQUIRED)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(ALLOW_IDL_GENERATION TRUE)
set(generated_libs_dir ${CMAKE_CURRENT_BINARY_DIR})
yarp_idl_to_dir(SharedData.msg ${generated_libs_dir})
include_directories(${generated_libs_dir}/include)

add_executable(sender)
target_sources(sender PRIVATE sender.cpp)
target_link_libraries(sender PRIVATE ${YARP_LIBRARIES})

add_executable(receiver)
target_sources(receiver PRIVATE receiver.cpp)
target_link_libraries(receiver PRIVATE ${YARP_LIBRARIES})

add_executable(sender2)
target_sources(sender2 PRIVATE sender2.cpp)
target_link_libraries(sender2 PRIVATE ${YARP_LIBRARIES})

add_executable(receiver2)
target_sources(receiver2 PRIVATE receiver2.cpp)
target_link_libraries(receiver2 PRIVATE ${YARP_LIBRARIES})
2 changes: 2 additions & 0 deletions example/idl/rosPortable/SharedData.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
string text
float64[] content
46 changes: 46 additions & 0 deletions example/idl/rosPortable/receiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/rosmsg/SharedData.h>
#include <iostream>
#include <yarp/os/Network.h>
#include <yarp/os/BufferedPort.h>

using namespace std;

int main()
{
yarp::os::Network network;

cout<<"Starting receiver\n";

yarp::os::Port port;
if (!port.open("/receiver"))
{
cerr<<"Error opening port, check your yarp network\n";
return -1;
}

network.connect("/sender", "/receiver");

int count=0;
while(true)
{
yarp::rosmsg::SharedData d;
port.read(d);

//access d
cout << count << " Received SharedData:\n";
cout << d.text << "\n";
for (int i=0; i<d.content.size(); i++)
{
cout<<d.content[i]<<" ";
}
cout<<"\n";
count++;
}

return 0;
}
48 changes: 48 additions & 0 deletions example/idl/rosPortable/receiver2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/rosmsg/SharedData.h>
#include <iostream>
#include <yarp/os/Network.h>
#include <yarp/os/Node.h>
#include <yarp/os/Subscriber.h>

using namespace std;

int main()
{
yarp::os::Network network;

cout<<"Starting receiver\n";

yarp::os::Node node("/receiver/node"); // added a Node
yarp::os::Subscriber<yarp::rosmsg::SharedData> port; // changed Port to Subscriber
if (!port.topic("/data")) // replaced open() with topic()
{
cerr<<"Error opening port, check your yarp network\n";
return -1;
}

// network.connect("/sender", "/receiver"); // don't need this anymore

int count=0;
while(true)
{
yarp::rosmsg::SharedData d;
port.read(d);

//access d
cout << count << " Received SharedData:\n";
cout << d.text << "\n";
for (int i=0; i<d.content.size(); i++)
{
cout<<d.content[i]<<" ";
}
cout<<"\n";
count++;
}

return 0;
}
45 changes: 45 additions & 0 deletions example/idl/rosPortable/sender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/rosmsg/SharedData.h>
#include <iostream>
#include <yarp/os/Network.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Time.h>

using namespace std;

int main()
{
yarp::os::Network network;

yarp::os::Port port;

if (!port.open("/sender"))
{
cerr<<"Error opening port, check your yarp network\n";
return -1;
}

cout<<"Starting sender\n";
double count=0.0;
while(true)
{
yarp::rosmsg::SharedData d;

// d.text is a string
d.text="Hello from sender";

//d.content is a vector, let's push some data
d.content.push_back(count++);
d.content.push_back(count++);

port.write(d);

yarp::os::Time::delay(0.1);
}

return 0;
}
47 changes: 47 additions & 0 deletions example/idl/rosPortable/sender2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/rosmsg/SharedData.h>
#include <iostream>
#include <yarp/os/Network.h>
#include <yarp/os/Node.h>
#include <yarp/os/Publisher.h>
#include <yarp/os/Time.h>

using namespace std;

int main()
{
yarp::os::Network network;

yarp::os::Node node("/sender/node"); // added a Node
yarp::os::Publisher<yarp::rosmsg::SharedData> port; // changed Port to Publisher

if (!port.topic("/data")) // replaced open() with topic()
{
cerr<<"Error opening port, check your yarp network\n";
return -1;
}

cout<<"Starting sender\n";
double count=0.0;
while(true)
{
yarp::rosmsg::SharedData d;

// d.text is a string
d.text="Hello from sender";

//d.content is a vector, let's push some data
d.content.push_back(count++);
d.content.push_back(count++);

port.write(d);

yarp::os::Time::delay(0.1);
}

return 0;
}
83 changes: 83 additions & 0 deletions example/ros/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.16)
project(yarp_ros_example)

find_package(YARP 3.3.103 COMPONENTS os dev math rosmsg idl_tools REQUIRED)

add_executable(add_int_client_v1)
target_sources(add_int_client_v1 PRIVATE add_int_client_v1.cpp)
target_link_libraries(add_int_client_v1 PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(add_int_client_v2)
target_sources(add_int_client_v2 PRIVATE add_int_client_v2.cpp)
target_link_libraries(add_int_client_v2 PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(add_int_server_v1)
target_sources(add_int_server_v1 PRIVATE add_int_server_v1.cpp)
target_link_libraries(add_int_server_v1 PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(listener_v1)
target_sources(listener_v1 PRIVATE listener_v1.cpp)
target_link_libraries(listener_v1 PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(listener_v2)
target_sources(listener_v2 PRIVATE listener_v2.cpp)
target_link_libraries(listener_v2 PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(listener)
target_sources(listener PRIVATE listener.cpp)
target_link_libraries(listener PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(talker)
target_sources(talker PRIVATE talker.cpp)
target_link_libraries(talker PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(rosmap_publisher)
target_sources(rosmap_publisher PRIVATE rosmap_publisher.cpp)
target_link_libraries(rosmap_publisher PRIVATE
YARP::YARP_os
YARP::YARP_dev
YARP::YARP_math
YARP::YARP_init
YARP::YARP_rosmsg)


yarp_idl_to_dir(INPUT_FILES package/src/yarp_test/srv/AddTwoInts.srv
OUTPUT_DIR ${CMAKE_BINARY_DIR}/msg
SOURCES_VAR AddTwoInts_SOURCES
HEADERS_VAR AddTwoInts_HEADERS
INCLUDE_DIRS_VAR AddTwoInts_INCLUDES)

add_executable(add_int_server_v1b)
target_sources(add_int_server_v1b PRIVATE add_int_server_v1b.cpp
${AddTwoInts_SOURCES}
${AddTwoInts_HEADERS})
target_include_directories(add_int_server_v1b PRIVATE ${AddTwoInts_INCLUDES})
target_link_libraries(add_int_server_v1b PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)

add_executable(add_int_client_v1b)
target_sources(add_int_client_v1b PRIVATE add_int_client_v1b.cpp
${AddTwoInts_SOURCES}
${AddTwoInts_HEADERS})
target_include_directories(add_int_client_v1b PRIVATE ${AddTwoInts_INCLUDES})
target_link_libraries(add_int_client_v1b PRIVATE YARP::YARP_os
YARP::YARP_init
YARP::YARP_rosmsg)
5 changes: 5 additions & 0 deletions example/ros/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Before trying these examples, make sure you read:

http://wiki.icub.org/wiki/YARP_ROS_Interoperation

There are prerequisites.
44 changes: 44 additions & 0 deletions example/ros/add_int_client_v1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/os/Bottle.h>
#include <yarp/os/LogComponent.h>
#include <yarp/os/Network.h>
#include <yarp/os/RpcClient.h>

using yarp::os::Bottle;
using yarp::os::Network;
using yarp::os::RpcClient;

namespace {
YARP_LOG_COMPONENT(CLIENT_V1, "yarp.example.ros.add_int_client_v1")
}

int main(int argc, char* argv[])
{
if (argc != 3) {
yCError(CLIENT_V1, "Call as %s X Y", argv[0]);
return 1;
}

Network yarp;
RpcClient client;
if (!client.open("/add_two_ints@/yarp_add_int_client")) {
yCError(CLIENT_V1, "Failed to open port");
return 1;
}

Bottle msg;
Bottle reply;
msg.addInt32(atoi(argv[1]));
msg.addInt32(atoi(argv[2]));
if (!client.write(msg, reply)) {
yCError(CLIENT_V1, "Failed to call service");
return 1;
}
yCInfo(CLIENT_V1, "Got %d", reply.get(0).asInt32());

return 0;
}
Loading

0 comments on commit 7396eea

Please sign in to comment.