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 return 0;
91 }
92
93 if (finder.check("calib"))
94 {
95 LOG("Calibrate buttons on\n");
96 enable_calib_all = true;
97 }
98
99 if (finder.check("admin"))
100 {
101 LOG("Admin mode on.\n");
102 enable_calib_all = true;
103 }
104
105 if (finder.check("debug"))
106 {
107 LOG("Debug interface requested.\n");
108 }
109
110 if (finder.check("speed"))
111 {
112 LOG("Speed view requested.\n");
114 }
115
116 std::string descriprionServerRemotePort = "/yarpRobotInterface/devices/rpc";
117 if (finder.check("remoteRobotDescriptionPort"))
118 {
119 descriprionServerRemotePort = finder.find("remoteRobotDescriptionPort").asString();
120 }
121
122 if (finder.check("skip_description_server")==false) //option --skip_description_server is for debug only, users should not use it
123 {
124 //ask the robot part to the description server
125 int count = 0;
126 std::string descLocalName = "/yarpmotorgui" + std::to_string(count) + "/descriptionClient";
127 Contact adr = Network::queryName(descLocalName);
128 while (adr.isValid())
129 {
130 count++;
131 descLocalName = "/yarpmotorgui" + std::to_string(count) + "/descriptionClient";
132 adr = Network::queryName(descLocalName);
133 }
134
136 {
137 PolyDriver* desc_driver = nullptr;
139 std::vector<DeviceDescription> cbw2_list;
141 desc_driver_options.put("device", "robotDescription_nwc_yarp");
145 {
146 IRobotDescription* idesc = nullptr;
147 desc_driver->view(idesc);
148 if (idesc)
149 {
150 idesc->getAllDevicesByType("controlBoard_nws_yarp", cbw2_list);
151 std::vector<DeviceDescription> wrappers_list;
152 wrappers_list.reserve(cbw2_list.size());
153 wrappers_list.insert(wrappers_list.end(), cbw2_list.begin(), cbw2_list.end());
154 for (auto& i : wrappers_list)
155 {
157 yDebug() << i.device_name;
158 yDebug() << i.device_configuration;
159 yarp::os::Property prop(i.device_configuration.c_str());
160 std::string nws_port = prop.find("name").asString();
161 pParts.addString(nws_port);
162 }
163 }
164 else
165 {
166 yError() << "Unable to get IRobotDescription interface? This is a bug";
167 }
169 delete desc_driver;
170 }
171 else
172 {
173 yError() << "Unable to open robotDescription_nwc_yarp, robot parts will be set manually.";
174 }
175 }
176 else
177 {
178 yWarning() << "Attempt to connect to port:" << descriprionServerRemotePort << "failed";
179 yWarning() << "robotDescription_nws_yarp not found, robot parts will be set manually.";
180 }
181 }
182
183 std::string robotName = finder.find("robot").asString();
184 Bottle* b_part_skip = finder.find("skip_parts").asList();
185 Bottle* b_part = finder.find("parts").asList();
186 Bottle* b_name = finder.find("names").asList();
187 if (pParts.size() == 0)
188 {
189 if (robotName != "" && b_name != nullptr)
190 {
191 LOG_ERROR("You cannot use both --robot and --names options simultaneously\n");
192 QMessageBox::critical(nullptr, "Error", "You cannot use both --robot and --names options simultaneously");
193 return 1;
194 }
195
196 if (b_name != nullptr && b_part != nullptr)
197 {
198 LOG_ERROR("You cannot use both --parts and --names options simultaneously\n");
199 QMessageBox::critical(nullptr, "Error", "You cannot use both --parts and --names options simultaneously");
200 return 1;
201 }
202
203 if (b_name != nullptr && b_part == nullptr)
204 {
205 //check port names from config file
206 for (size_t i = 0; i < b_name->size(); i++)
207 {
208 pParts.addString(b_name->get(i).asString());
209 }
210 }
211 else if (robotName != "" && b_part != nullptr)
212 {
213 //check parts from config file
214 for (size_t i = 0; i < b_part->size(); i++)
215 {
216 std::string ss = b_part->get(i).asString();
217 if (ss.at(0) != '/')
218 {
219 ss.insert(0, "/" + robotName + "/");
220 }
221 else
222 {
223 LOG_ERROR("Option --parts should not contain /, please remove it\n");
224 QMessageBox::critical(nullptr, "Error", "Option --parts should not contain /, please remove it");
225 return 1;
226 }
227 pParts.addString(ss);
228 }
229 }
230 else if (robotName != "" && b_part == nullptr)
231 {
232 pParts.addString("/" + robotName + "/head");
233 pParts.addString("/" + robotName + "/torso");
234 pParts.addString("/" + robotName + "/left_arm");
235 pParts.addString("/" + robotName + "/right_arm");
236 pParts.addString("/" + robotName + "/left_leg");
237 pParts.addString("/" + robotName + "/right_leg");
238 }
239 else
240 {
241 //use default names
242 pParts = Bottle("/icub/head /icub/torso /icub/left_arm /icub/right_arm /icub/left_leg /icub/right_leg");
243 }
244 }
245
246 //Check 1 in the panel
247 for(size_t n = 0; n < pParts.size(); n++)
248 {
249 std::string part = pParts.get(n).asString();
250 //std::string part = QString("%1").arg(pParts.get(n).asString().c_str()).toStdString();
251 if (b_part_skip)
252 {
253 if (b_part_skip->check(part)) {
254 continue;
255 }
256 }
257 yDebug("Appending %s", part.c_str());
258 partsName.push_back(part);
259 }
260
261 if(!finder.check("skip"))
262 {
264 dlg.init(partsName);
265
266 if(dlg.exec() == QDialog::Accepted)
267 {
268 enabled = dlg.getEnabledParts();
269 }
270 else
271 {
272 yInfo("Cancel Button pressed. Closing..");
273 return 0;
274 }
275 }
276
277 for(int i = 0; i < partsName.size(); i++)
278 {
279 if(enabled.at(i))
280 {
281 std::string debug_s2 = partsName.at(i);
282 enabledParts.push_back(partsName.at(i));
283 }
284 }
285
286 std::signal(SIGINT, sighandler);
287 std::signal(SIGTERM, sighandler);
288
289 mainW = &w;
290 appRet = 0;
292
293 if(ret)
294 {
295 w.show();
296 appRet = a.exec();
297 }
298
299 return (appRet != 0 ? 1 : 0);
300}
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:64
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:234
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