YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SimulatedWorld_nwc_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
7
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12using namespace yarp::dev;
13using namespace yarp::os;
14using namespace yarp::sig;
15
16namespace {
17YARP_LOG_COMPONENT(SIMWORLDNWC, "yarp.device.SimulatedWorld_nwc_yarp")
18} // namespace
19
20
22{
23 if (!parseParams(config)) { return false; }
24
25 std::string local_rpc = m_local;
26 local_rpc += "/rpc:o";
27 std::string remote_rpc = m_remote;
28 remote_rpc += "/rpc:i";
29
31 {
32 yCError(SIMWORLDNWC, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
33 return false;
34 }
35
36 bool ok=Network::connect(local_rpc, remote_rpc);
37 if (!ok)
38 {
39 yCError(SIMWORLDNWC, "open() Could not connect %s -> %s", remote_rpc.c_str(), local_rpc.c_str());
40 return false;
41 }
43 {
44 yCError(SIMWORLDNWC, "Error! Cannot attach the rpcPort as a client");
45 return false;
46 }
47
48 return true;
49}
50
52{
54 return true;
55}
56
57ReturnValue SimulatedWorld_nwc_yarp::attach(std::string id, std::string link_name)
58{
59 std::lock_guard<std::mutex> lg(m_mutex);
60 auto ret = m_sim_RPC.attach(id, link_name);
61 if (!ret) {
62 yCError(SIMWORLDNWC, "Unable to attach");
63 return ret;
64 }
65 return ret;
66}
67
68ReturnValue SimulatedWorld_nwc_yarp::makeSphere (std::string id, double radius, Pose6D pose, ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable)
69{
70 std::lock_guard<std::mutex> lg(m_mutex);
71 auto ret = m_sim_RPC.makeSphere(id, radius, pose, color, frame_name, gravity_enable, collision_enable);
72 if (!ret) {
73 yCError(SIMWORLDNWC, "Unable to makeSphere");
74 return ret;
75 }
76 return ret;
77}
78
79ReturnValue SimulatedWorld_nwc_yarp::makeBox (std::string id, double width, double height, double thickness, Pose6D pose, ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable)
80{
81 std::lock_guard<std::mutex> lg(m_mutex);
82 auto ret = m_sim_RPC.makeBox(id, width, height, thickness, pose, color, frame_name, gravity_enable, collision_enable);
83 if (!ret) {
84 yCError(SIMWORLDNWC, "Unable to makeBox");
85 return ret;
86 }
87 return ret;
88}
89
90ReturnValue SimulatedWorld_nwc_yarp::makeCylinder (std::string id, double radius, double length, Pose6D pose, ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable)
91{
92 std::lock_guard<std::mutex> lg(m_mutex);
93 auto ret = m_sim_RPC.makeCylinder(id, radius, length, pose, color, frame_name, gravity_enable, collision_enable);
94 if (!ret) {
95 yCError(SIMWORLDNWC, "Unable to makeCylinder");
96 return ret;
97 }
98 return ret;
99}
100
101ReturnValue SimulatedWorld_nwc_yarp::makeFrame (std::string id, double size, Pose6D pose, ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable)
102{
103 std::lock_guard<std::mutex> lg(m_mutex);
104 auto ret = m_sim_RPC.makeFrame(id, size, pose, color, frame_name, gravity_enable, collision_enable);
105 if (!ret) {
106 yCError(SIMWORLDNWC, "Unable to makeFrame");
107 return ret;
108 }
109 return ret;
110}
111
113{
114 std::lock_guard<std::mutex> lg(m_mutex);
115 auto ret = m_sim_RPC.changeColor(id, color);
116 if (!ret) {
117 yCError(SIMWORLDNWC, "Unable to changeColor");
118 return ret;
119 }
120 return ret;
121}
122
123ReturnValue SimulatedWorld_nwc_yarp::setPose(std::string id, Pose6D pose, std::string frame_name)
124{
125 std::lock_guard<std::mutex> lg(m_mutex);
126 auto ret = m_sim_RPC.setPose(id, pose, frame_name);
127 if (!ret) {
128 yCError(SIMWORLDNWC, "Unable to setPose");
129 return ret;
130 }
131 return ret;
132}
133
135{
136 std::lock_guard<std::mutex> lg(m_mutex);
137 auto ret = m_sim_RPC.enableGravity(id, enable);
138 if (!ret) {
139 yCError(SIMWORLDNWC, "Unable to enableGravity");
140 return ret;
141 }
142 return ret;
143}
144
146{
147 std::lock_guard<std::mutex> lg(m_mutex);
148 auto ret = m_sim_RPC.enableCollision(id, enable);
149 if (!ret) {
150 yCError(SIMWORLDNWC, "Unable to enableCollision");
151 return ret;
152 }
153 return ret;
154}
155
156ReturnValue SimulatedWorld_nwc_yarp::getPose(std::string id, Pose6D& pose, std::string frame_name)
157{
158 std::lock_guard<std::mutex> lg(m_mutex);
159 auto ret = m_sim_RPC.getPose(id, frame_name);
160 if (!ret.retVal) {
161 yCError(SIMWORLDNWC, "Unable to getPose");
162 return ret.retVal;
163 }
164 pose = ret.pose;
165 return ret.retVal;
166}
167
168ReturnValue SimulatedWorld_nwc_yarp::makeModel(std::string id, std::string filename, Pose6D pose, std::string frame_name, bool gravity_enable, bool collision_enable)
169{
170 std::lock_guard<std::mutex> lg(m_mutex);
171 auto ret = m_sim_RPC.makeModel(id, filename, pose, frame_name, gravity_enable, collision_enable);
172 if (!ret) {
173 yCError(SIMWORLDNWC, "Unable to makeModel");
174 return ret;
175 }
176 return ret;
177}
178
180{
181 std::lock_guard<std::mutex> lg(m_mutex);
183 if (!ret) {
184 yCError(SIMWORLDNWC, "Unable to makeActor");
185 return ret;
186 }
187 return ret;
188}
189
191{
192 std::lock_guard<std::mutex> lg(m_mutex);
193 auto ret = m_sim_RPC.deleteObject(id);
194 if (!ret) {
195 yCError(SIMWORLDNWC, "Unable to deleteObject");
196 return ret;
197 }
198 return ret;
199}
200
202{
203 std::lock_guard<std::mutex> lg(m_mutex);
204 auto ret = m_sim_RPC.deleteAll();
205 if (!ret) {
206 yCError(SIMWORLDNWC, "Unable to deleteAll");
207 return ret;
208 }
209 return ret;
210}
211
212ReturnValue SimulatedWorld_nwc_yarp::getList(std::vector<std::string>& names)
213{
214 std::lock_guard<std::mutex> lg(m_mutex);
215 auto ret = m_sim_RPC.getList();
216 if (!ret.retVal) {
217 yCError(SIMWORLDNWC, "Unable to getList");
218 return ret.retVal;
219 }
220 names = ret.listnames;
221 return ret.retVal;
222
223}
224
226{
227 std::lock_guard<std::mutex> lg(m_mutex);
228 auto ret = m_sim_RPC.detach(id);
229 if (!ret) {
230 yCError(SIMWORLDNWC, "Unable to detach");
231 return ret;
232 }
233 return ret;
234}
235
236ReturnValue SimulatedWorld_nwc_yarp::rename(std::string old_name, std::string new_name)
237{
238 std::lock_guard<std::mutex> lg(m_mutex);
239 auto ret = m_sim_RPC.rename(old_name,new_name);
240 if (!ret) {
241 yCError(SIMWORLDNWC, "Unable to rename");
242 return ret;
243 }
244 return ret;
245}
virtual yarp::dev::ReturnValue rename(const std::string &old_name, const std::string &new_name)
virtual yarp::dev::ReturnValue makeModel(const std::string &id_name, const std::string &filename, const yarp::sig::Pose6D &pose, const std::string &frame_name, const bool gravity_enable, const bool collision_enable)
virtual yarp::dev::ReturnValue deleteAll()
virtual yarp::dev::ReturnValue deleteObject(const std::string &id)
virtual yarp::dev::ReturnValue detach(const std::string &id)
virtual yarp::dev::ReturnValue makeBox(const std::string &id_name, const double width, const double height, const double thickness, const yarp::sig::Pose6D &pose, const yarp::sig::ColorRGB &color, const std::string &frame_name, const bool gravity_enable, const bool collision_enable)
virtual yarp::dev::ReturnValue makeActor(const std::string &id_name, const std::string &skinfile, const std::string &animfile, const yarp::sig::Pose6D &pose)
virtual yarp::dev::ReturnValue enableGravity(const std::string &id, const bool enable)
virtual yarp::dev::ReturnValue changeColor(const std::string &id, const yarp::sig::ColorRGB &color)
virtual yarp::dev::ReturnValue enableCollision(const std::string &id, const bool enable)
virtual getListReturnValue getList()
virtual yarp::dev::ReturnValue makeCylinder(const std::string &id_name, const double radius, const double length, const yarp::sig::Pose6D &pose, const yarp::sig::ColorRGB &color, const std::string &frame_name, const bool gravity_enable, const bool collision_enable)
virtual yarp::dev::ReturnValue setPose(const std::string &id, const yarp::sig::Pose6D &pose, const std::string &frame_name)
virtual yarp::dev::ReturnValue makeFrame(const std::string &id_name, const double size, const yarp::sig::Pose6D &pose, const yarp::sig::ColorRGB &color, const std::string &frame_name, const bool gravity_enable, const bool collision_enable)
virtual getPoseReturnValue getPose(const std::string &id, const std::string &frame_name)
virtual yarp::dev::ReturnValue makeSphere(const std::string &id_name, const double radius, const yarp::sig::Pose6D &pose, const yarp::sig::ColorRGB &color, const std::string &frame_name, const bool gravity_enable, const bool collision_enable)
virtual yarp::dev::ReturnValue attach(const std::string &id, const std::string &link_name)
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue detach(std::string id) override
Detach a previously attached object.
yarp::dev::ReturnValue attach(std::string id, std::string link_name) override
Attach an object to a link of another object.
yarp::dev::ReturnValue makeSphere(std::string id, double radius, yarp::sig::Pose6D pose, yarp::sig::ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable) override
Make a sphere.
yarp::dev::ReturnValue makeModel(std::string id, std::string filename, yarp::sig::Pose6D pose, std::string frame_name, bool gravity_enable, bool collision_enable) override
Create a model from a file.
yarp::dev::ReturnValue deleteObject(std::string id) override
Delete an object.
yarp::dev::ReturnValue makeActor(std::string id, std::string skin_filename, std::string animation_filename, yarp::sig::Pose6D pose) override
Create an actor.
yarp::dev::ReturnValue enableCollision(std::string id, bool enable) override
Enable/disables collision detection for an object.
yarp::dev::ReturnValue enableGravity(std::string id, bool enable) override
Enable/disables gravity for an object.
yarp::dev::ReturnValue makeBox(std::string id, double width, double height, double thickness, yarp::sig::Pose6D pose, yarp::sig::ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable) override
Make a box.
yarp::dev::ReturnValue getList(std::vector< std::string > &names) override
List id of all objects that have been added to the world.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue makeFrame(std::string id, double size, yarp::sig::Pose6D pose, yarp::sig::ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable) override
Make a reference frame (a 3D model with 3 arrows representing the axes)
yarp::dev::ReturnValue rename(std::string old_name, std::string new_name) override
Change the names of an object.
yarp::dev::ReturnValue deleteAll() override
Delete all objects in the world.
yarp::dev::ReturnValue changeColor(std::string id, yarp::sig::ColorRGB color) override
Change the color of an object.
yarp::dev::ReturnValue makeCylinder(std::string id, double radius, double length, yarp::sig::Pose6D pose, yarp::sig::ColorRGB color, std::string frame_name, bool gravity_enable, bool collision_enable) override
Make a cylinder.
yarp::dev::ReturnValue setPose(std::string id, yarp::sig::Pose6D pose, std::string frame_name) override
Set new object pose.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getPose(std::string id, yarp::sig::Pose6D &pose, std::string frame_name) override
Get object pose.
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A base class for nested structures that can be searched.
Definition Searchable.h:31
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.