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-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include <yarp/os/Network.h>
7#include <yarp/os/RFModule.h>
8#include <yarp/os/Bottle.h>
11#include <yarp/os/Os.h>
12#include <yarp/os/Log.h>
13#include <yarp/os/LogStream.h>
14#include <yarp/os/Time.h>
15#include <yarp/sig/Vector.h>
16
17#include <QApplication>
18#include <QMainWindow>
19#include <iostream>
20#include <iomanip>
21#include <string>
22#include <cstdlib>
23#include <ctime>
24
25#include "display.h"
26#include "ui_display.h"
27
28using namespace yarp::os;
29using namespace yarp::dev;
30
31int main(int argc, char *argv[])
32{
33 // Guis shall run always on system clock. No need to have it in synch with network clock
35
36 if (!yarp.checkNetwork())
37 {
38 yError("Error initializing yarp network (is yarpserver running?)");
39 return 0;
40 }
41
43 rf.configure(argc,argv);
44
45 QApplication a(argc, argv);
46
47 if (rf.check("help"))
48 {
49 yInfo() << "Options:";
50 yInfo() << "--local <name> The name of the local port opened by this module. e.g. batteryMonitor";
51 yInfo() << "--remote <name> The name of the remote port to connect to, e.g. /mybattery.";
52 yInfo() << " The port name is completed by the client adding /data:o, /rpc:i";
53 yInfo() << "--refresh_period <seconds> Refresh period of the gui. Default value: 10s";
54 yInfo() << "--keep-above Keep window above the others";
55 return 0;
56 }
57
58 yarp::dev::IBattery* ibat = nullptr;
59 yarp::dev::PolyDriver* drv = nullptr;
60
61 std::string remotePort = "";
62 if (rf.check("remote"))
63 {
64 remotePort = rf.find("remote").asString();
65 }
66
67 double refresh_period = 10.0;
68 if (rf.check("refresh_period"))
69 {
70 refresh_period = rf.find("refresh_period").asFloat32();
71 if (refresh_period <= 0)
72 {
73 yError() << "refresh_period cannot be <=0. Using default value.";
74 refresh_period = 10;
75 }
76 yInfo() << "refresh_period set to:" << refresh_period << "s";
77 }
78 else
79 {
80 yInfo() << "refresh_period parameter missing. Using default value:" << refresh_period << "s";
81 }
82
83 char pname[500];
84 int trial = 0;
85 bool b;
86 do
87 {
88 sprintf(pname, "/batteryMonitor%d", trial);
90 trial++;
91 } while (b == true);
92 std::string localPort = pname;
93
94 if (rf.check("local"))
95 {
96 localPort = rf.find("local").asString();
97 }
98 yInfo() << "Using local port:" << localPort;
99
100 yarp::os::Property options;
101 options.put("device", "battery_nwc_yarp");
102 options.put("local", localPort);
103 options.put("remote", remotePort);
104
105 drv = new yarp::dev::PolyDriver(options);
106 if (!drv || !(drv->isValid()))
107 {
108 yError("Problems instantiating the device driver");
109 delete drv;
110 return 1;
111 }
112
113 drv->view(ibat);
114 if (ibat == nullptr)
115 {
116 yError("Problems viewing the battery interface");
117 drv->close();
118 delete drv;
119 return 2;
120 }
121
122 MainWindow w(rf, ibat, nullptr, refresh_period);
123
124 if (rf.check("keep-above"))
125 {
126 w.setWindowFlags(Qt::WindowStaysOnTopHint);
127 }
128
129 w.show();
130 int ret = a.exec();
131
132 if (drv)
133 {
134 yInfo("Closing Battery Interface");
135 drv->close();
136 delete drv;
137 }
138
139 return ret;
140
141}
bool ret
#define yInfo(...)
Definition Log.h:319
#define yError(...)
Definition Log.h:361
contains the definition of a Vector type
MainWindow class.
Definition display.h:22
bool view(T *&x)
Get an interface to the device driver.
A generic battery interface.
Definition IBattery.h:26
A container for a device driver.
Definition PolyDriver.h:23
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
A mini-server for performing network communication in the background.
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
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
Definition Value.cpp:216
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
int main(int argc, char *argv[])
Definition main.cpp:31
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