icub-test
CameraTest.cpp
1 /*
2  * iCub Robot Unit Tests (Robot Testing Framework)
3  *
4  * Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #include <iostream>
23 #include <stdlib.h> // for abs()
24 #include <robottestingframework/TestAssert.h>
25 #include <robottestingframework/dll/Plugin.h>
26 #include <yarp/os/Network.h>
27 #include <yarp/os/Time.h>
28 #include <yarp/os/Property.h>
29 
30 #include "CameraTest.h"
31 
32 using namespace robottestingframework;
33 using namespace yarp::os;
34 using namespace yarp::sig;
35 
36 #define TIMES 1
37 #define FREQUENCY 30
38 #define TOLERANCE 5
39 
40 
41 // prepare the plugin
42 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(CameraTest)
43 
44 CameraTest::CameraTest() : yarp::robottestingframework::TestCase("CameraTest") {
45 }
46 
47 CameraTest::~CameraTest() { }
48 
49 bool CameraTest::setup(yarp::os::Property& property) {
50 
51  if(property.check("name"))
52  setName(property.find("name").asString());
53 
54  // updating parameters
55  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check("portname"),
56  "The portname must be given as the test paramter!");
57  cameraPortName = property.find("portname").asString();
58  measure_time = property.check("measure_time") ? property.find("measure_time").asInt32() : TIMES;
59  expected_frequency = property.check("expected_frequency") ? property.find("expected_frequency").asInt32() : FREQUENCY;
60  tolerance = property.check("tolerance") ? property.find("tolerance").asInt32() : TOLERANCE;
61 
62  // opening port
63  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(port.open("/CameraTest/image:i"),
64  "opening port, is YARP network available?");
65 
66  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Listening to camera for %d seconds",
67  measure_time));
68 
69  // connecting
70  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("connecting from %s to %s",
71  port.getName().c_str(), cameraPortName.c_str()));
72  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(Network::connect(cameraPortName, port.getName()),
73  "could not connect to remote port, camera unavailable");
74  return true;
75 }
76 
77 void CameraTest::tearDown() {
78  Network::disconnect(cameraPortName, port.getName());
79  port.close();
80 }
81 
82 void CameraTest::run() {
83  ROBOTTESTINGFRAMEWORK_TEST_REPORT("Reading images...");
84  double timeStart=yarp::os::Time::now();
85  double timeNow=timeStart;
86 
87  int frames=0;
88  while(timeNow<timeStart+measure_time) {
89  Image *image=port.read(false);
90  if(image!=0)
91  frames++;
92  yarp::os::Time::delay(0.01);
93  timeNow=yarp::os::Time::now();
94  }
95 
96  int expectedFrames = measure_time*expected_frequency;
97  ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Received %d frames, expecting %d",
98  frames,
99  expectedFrames));
100  ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(abs(frames-expectedFrames)<tolerance,
101  "checking number of received frames");
102 }
Check if a camera is publishing images at desired framerate.
Definition: CameraTest.h:44