YARP
Yet Another Robot Platform
framerate/main.cpp

Measure the framerate of a data source. This example was originally designed for images but should work with anything.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <yarp/sig/Image.h>
#include <yarp/os/Time.h>
using namespace yarp::os;
using namespace yarp::sig;
static bool terminated=false;
static void handler (int) {
static int ct = 0;
ct++;
fprintf(stderr, "[try %d of %d] Asking to shut down smoothly\n",ct, 3);
terminated = true;
if (iPort!=0)
iPort->interrupt();
if (ct>2)
{
fprintf(stderr, "[try %d of %d] OK asking to abort...\n", ct, 3);
exit(-1);
}
}
int main(int argc, char *argv[]) {
signal(SIGINT, handler);
signal(SIGTERM, handler);
if (argc==1) {
printf("This program checks the framerate of an output port\n");
printf("Call as:\n");
printf(" framerate --remote /port_name --local /local_name --prot protocol\n");
printf("protocol can be for example tcp,udp,mcast\n");
exit(0);
}
iPort=&port; //give pointer to port to signal handler (used to interrupt port at shutdown
// get options
Property opt;
opt.fromCommand(argc,argv);
// name port
Value *val;
Value *prot;
std::string local = "/get_image";
if (opt.check("local",val)) {
local = val->asString().c_str();
}
port.open(local.c_str());
// connect port
if (opt.check("remote", val))
{
if (opt.check("prot", prot))
Network::connect(val->asString(), local.c_str(), prot->asString().c_str());
else
Network::connect(val->asString(), local.c_str());
}
// read
double first = Time::now();
double prev = 0;
int ct = 0;
bool spoke = false;
while (!terminated) {
Bottle *bot = port.read(true);
double now = Time::now()-first;
ct++;
if (now-prev>=2) {
double period = (now-prev)/ct;
printf("Period is %g ms per message, freq is %g (%d mgs in %g secs)\n",
period*1000, 1/period, ct, now-prev);
fflush(stdout);
ct = 0;
prev = now;
spoke = false;
}
if (bot!=NULL) {
if (!spoke) {
printf("Got something with %d top-level elements\n", bot->size());
fflush(stdout);
spoke = true;
}
}
}
iPort=0; //just in case
return 0;
}
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:72
yarp::manager::BrokerType::local
@ local
Network.h
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::os::BufferedPort::read
T * read(bool shouldWait=true) override
Read an available object from the port.
Definition: BufferedPort-inl.h:154
terminated
static bool terminated
Definition: Drivers.cpp:344
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:259
yarp::os::BufferedPort::interrupt
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: BufferedPort-inl.h:82
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:63
yarp::os::Property::fromCommand
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1057
Property.h
handler
static void handler(int sig)
Definition: RFModule.cpp:244
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::NetworkBase::connect
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:684
yarp::os::BufferedPort::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: BufferedPort-inl.h:41
yarp::os::Property::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
BufferedPort.h
Image.h
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::Network
Definition: Network.h:781
yarp
Definition: numeric.h:47
Time.h
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:46
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:34