YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7#include "mainwindow.h"
8#include "log.h"
9#include "yarpmotorgui.h"
10#include "startdlg.h"
11#include "sequencewindow.h"
12
15#include <yarp/dev/Drivers.h>
16#include <yarp/os/LogStream.h>
17#include <yarp/os/Os.h>
18
19#include <QApplication>
20#include <QDebug>
21#include <QMessageBox>
22#include <QtGlobal>
23#include <yarp/os/Log.h>
24#include <yarp/os/LogStream.h>
25#include <csignal>
26
27using namespace yarp::dev;
28using namespace yarp::os;
29
30
31std::vector<std::string> partsName;
32MainWindow* mainW = nullptr;
33
36bool enable_calib_all = false;
37
38static void sighandler(int sig)
39{
40 Q_UNUSED(sig);
41 yDebug("\nCAUGHT Ctrl-c\n");// << "\nCAUGHT Ctrl-c" << endl;
42 if(mainW)
43 {
44 mainW->term();
45 }
46}
47
48int main(int argc, char *argv[])
49{
50#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
51 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
52#else
53 qputenv("QT_DEVICE_PIXEL_RATIO", QByteArray("auto"));
54#endif
55
57 if (!yarp.checkNetwork())
58 {
59 LOG_ERROR("Error initializing yarp network (is yarpserver running?)\n");
60 QMessageBox::critical(nullptr, "Error", "Error initializing yarp network (is yarpserver running?)");
61 return 1;
62 }
63
64 bool ret;
65 int appRet;
66 QApplication a(argc, argv);
68 //retrieve information for the list of parts
69 finder.setDefaultConfigFile("yarpmotorgui.ini");
70 finder.configure(argc, argv);
71
73 std::vector<std::string> enabledParts;
74 std::vector<bool> enabled;
75 MainWindow w;
76
78 qRegisterMetaType<SequenceItem>("SequenceItem");
79 qRegisterMetaType<QList<SequenceItem> >("QList<SequenceItem>");
80
81 if (finder.check("help"))
82 {
83 yInfo("yarpmotorgui options:");
84 yInfo("--robot <name>: name of the robot");
85 yInfo("--parts ""( <name1> <name2> )"": parts of the robot to add to the list. (e.g. left_arm)");
86 yInfo("--names ""( <name1> <name2> )"": full name of the ports of the robot to add to the list. (e.g. /icub/left_arm). This option is mutually exclusive with --robot --parts options");
87 yInfo("--skip_parts ""( <name1> <name2> )"": parts of the robot to skip.");
88 yInfo("--calib to enable calibration buttons (be careful!)");
89 yInfo("--remoteRobotDescriptionPort <portname>: port opened by the robotDescription_nws_yarp (yarprbotinterface). Default Value is /yarpRobotInterface/devices/rpc");
90 yInfo("--reduce_bandwidth: attempts to reduce network bandwidth usage by throttling streaming communication with the robot");
91 yInfo("--rcb_protocol <carrier>: the yarp network protocol to connect with the ports opened by the robot (e.g. tcp, udp). Portmonitors can be included too.");
92 return 0;
93 }
94
95 if (finder.check("calib"))
96 {
97 LOG("Calibrate buttons on\n");
98 enable_calib_all = true;
99 }
100
101 if (finder.check("admin"))
102 {
103 LOG("Admin mode on.\n");
104 enable_calib_all = true;
105 }
106
107 if (finder.check("debug"))
108 {
109 LOG("Debug interface requested.\n");
110 }
111
112 if (finder.check("speed"))
113 {
114 LOG("Speed view requested.\n");
116 }
117
118 //*remote control board portocol configuration*/
119 std::string rcb_protocol = "fast_tcp"; //fast_tcp is the default
120 if (finder.check("rcb_protocol")) {
121 rcb_protocol = finder.find("rcb_protocol").asString();
122 }
123 if (finder.check("reduce_bandwidth")) {
124 // The following line is used to keep the bandwidth consumption low,
125 // throttling down to 10Hz the data from controlBoard_nws to RemoteControlBoard (controlBoard_nwc)
126 rcb_protocol = "fast_tcp+send.portmonitor+type.dll+file.throttleDown+period_ms.100";
127 }
128
129 std::string descriptionServerRemotePort = "/yarpRobotInterface/devices/rpc";
130 if (finder.check("remoteRobotDescriptionPort"))
131 {
132 descriptionServerRemotePort = finder.find("remoteRobotDescriptionPort").asString();
133 }
134
135 if (finder.check("skip_description_server")==false) //option --skip_description_server is for debug only, users should not use it
136 {
137 //ask the robot part to the description server
138 int count = 0;
139 std::string descLocalName = "/yarpmotorgui" + std::to_string(count) + "/descriptionClient";
140 Contact adr = Network::queryName(descLocalName);
141 while (adr.isValid())
142 {
143 count++;
144 descLocalName = "/yarpmotorgui" + std::to_string(count) + "/descriptionClient";
145 adr = Network::queryName(descLocalName);
146 }
147
149 {
150 PolyDriver* desc_driver = nullptr;
152 std::vector<DeviceDescription> cbw2_list;
154 desc_driver_options.put("device", "robotDescription_nwc_yarp");
158 {
159 IRobotDescription* idesc = nullptr;
160 desc_driver->view(idesc);
161 if (idesc)
162 {
163 idesc->getAllDevicesByType("controlBoard_nws_yarp", cbw2_list);
164 std::vector<DeviceDescription> wrappers_list;
165 wrappers_list.reserve(cbw2_list.size());
166 wrappers_list.insert(wrappers_list.end(), cbw2_list.begin(), cbw2_list.end());
167 for (auto& i : wrappers_list)
168 {
170 yDebug() << i.device_name;
171 yDebug() << i.device_configuration;
172 yarp::os::Property prop(i.device_configuration.c_str());
173 std::string nws_port = prop.find("name").asString();
174 pParts.addString(nws_port);
175 }
176 }
177 else
178 {
179 yError() << "Unable to get IRobotDescription interface? This is a bug";
180 }
182 delete desc_driver;
183 }
184 else
185 {
186 yError() << "Unable to open robotDescription_nwc_yarp, robot parts will be set manually.";
187 }
188 }
189 else
190 {
191 yWarning() << "Attempt to connect to port:" << descriptionServerRemotePort << "failed";
192 yWarning() << "robotDescription_nws_yarp not found, robot parts will be set manually.";
193 }
194 }
195
196 std::string robotName = finder.find("robot").asString();
197 Bottle* b_part_skip = finder.find("skip_parts").asList();
198 Bottle* b_part = finder.find("parts").asList();
199 Bottle* b_name = finder.find("names").asList();
200 if (pParts.size() == 0)
201 {
202 if (robotName != "" && b_name != nullptr)
203 {
204 LOG_ERROR("You cannot use both --robot and --names options simultaneously\n");
205 QMessageBox::critical(nullptr, "Error", "You cannot use both --robot and --names options simultaneously");
206 return 1;
207 }
208
209 if (b_name != nullptr && b_part != nullptr)
210 {
211 LOG_ERROR("You cannot use both --parts and --names options simultaneously\n");
212 QMessageBox::critical(nullptr, "Error", "You cannot use both --parts and --names options simultaneously");
213 return 1;
214 }
215
216 if (b_name != nullptr && b_part == nullptr)
217 {
218 //check port names from config file
219 for (size_t i = 0; i < b_name->size(); i++)
220 {
221 pParts.addString(b_name->get(i).asString());
222 }
223 }
224 else if (robotName != "" && b_part != nullptr)
225 {
226 //check parts from config file
227 for (size_t i = 0; i < b_part->size(); i++)
228 {
229 std::string ss = b_part->get(i).asString();
230 if (ss.at(0) != '/')
231 {
232 ss.insert(0, "/" + robotName + "/");
233 }
234 else
235 {
236 LOG_ERROR("Option --parts should not contain /, please remove it\n");
237 QMessageBox::critical(nullptr, "Error", "Option --parts should not contain /, please remove it");
238 return 1;
239 }
240 pParts.addString(ss);
241 }
242 }
243 else if (robotName != "" && b_part == nullptr)
244 {
245 pParts.addString("/" + robotName + "/head");
246 pParts.addString("/" + robotName + "/torso");
247 pParts.addString("/" + robotName + "/left_arm");
248 pParts.addString("/" + robotName + "/right_arm");
249 pParts.addString("/" + robotName + "/left_leg");
250 pParts.addString("/" + robotName + "/right_leg");
251 }
252 else
253 {
254 //use default names
255 pParts = Bottle("/icub/head /icub/torso /icub/left_arm /icub/right_arm /icub/left_leg /icub/right_leg");
256 }
257 }
258
259 //Check 1 in the panel
260 for(size_t n = 0; n < pParts.size(); n++)
261 {
262 std::string part = pParts.get(n).asString();
263 //std::string part = QString("%1").arg(pParts.get(n).asString().c_str()).toStdString();
264 if (b_part_skip)
265 {
266 if (b_part_skip->check(part)) {
267 continue;
268 }
269 }
270 yDebug("Appending %s", part.c_str());
271 partsName.push_back(part);
272 }
273
274 if(!finder.check("skip"))
275 {
277 dlg.init(partsName);
278
279 if(dlg.exec() == QDialog::Accepted)
280 {
281 enabled = dlg.getEnabledParts();
282 }
283 else
284 {
285 yInfo("Cancel Button pressed. Closing..");
286 return 0;
287 }
288 }
289
290 for(int i = 0; i < partsName.size(); i++)
291 {
292 if(enabled.at(i))
293 {
294 std::string debug_s2 = partsName.at(i);
295 enabledParts.push_back(partsName.at(i));
296 }
297 }
298
299 std::signal(SIGINT, sighandler);
300 std::signal(SIGTERM, sighandler);
301
302 mainW = &w;
303 appRet = 0;
305
306 if(ret)
307 {
308 w.show();
309 appRet = a.exec();
310 }
311
312 return (appRet != 0 ? 1 : 0);
313}
define control board standard interfaces
bool ret
#define yInfo(...)
Definition Log.h:319
#define yError(...)
Definition Log.h:361
#define yDebug(...)
Definition Log.h:275
#define yWarning(...)
Definition Log.h:340
MainWindow class.
Definition display.h:22
void init(yarp::os::Property config)
Init the application with the current configuration.
This interface allows users to retrieve a list which contains the names and the types of the currentl...
A container for a device driver.
Definition PolyDriver.h:23
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
static bool exists(const std::string &port, bool quiet=true, bool checkVer=true)
Check for a port to be ready and responsive.
Definition Network.cpp:746
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Helper class for finding config files and other external resources.
static ResourceFinder & getResourceFinderSingleton()
Access a ResourceFinder singleton whose lifetime will match that of the YARP library.
virtual std::string asString() const
Get string value.
Definition Value.cpp:246
int main(int argc, char *argv[])
Definition main.cpp:48
bool speedview_param_enabled
Definition main.cpp:35
static void sighandler(int sig)
Definition main.cpp:38
std::vector< std::string > partsName
Definition main.cpp:31
bool debug_param_enabled
Definition main.cpp:34
MainWindow * mainW
Definition main.cpp:32
bool enable_calib_all
Definition main.cpp:36
#define LOG(...)
Definition log.h:13
#define LOG_ERROR(...)
Definition log.h:19
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16