23 #include "SensorsDuplicateReadings.h"
24 #include <yarp/os/Time.h>
25 #include <yarp/os/Stamp.h>
26 #include <yarp/math/Math.h>
29 using namespace robottestingframework;
30 using namespace yarp::os;
31 using namespace yarp::math;
36 SensorsDuplicateReadings::SensorsDuplicateReadings() : yarp::robottestingframework::TestCase(
"SensorsDuplicateReadings") {
39 SensorsDuplicateReadings::~SensorsDuplicateReadings() { }
41 bool SensorsDuplicateReadings::setup(yarp::os::Property &property) {
44 if(property.check(
"name"))
45 setName(property.find(
"name").asString());
48 testTime = (
property.check(
"time")) ? property.find(
"time").asFloat64() : 2;
50 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(property.check(
"PORTS"),
51 "A list of the ports must be given");
53 yarp::os::Bottle portsSet =
property.findGroup(
"PORTS").tail();
54 for(
unsigned int i=0; i<portsSet.size(); i++) {
55 yarp::os::Bottle* btport = portsSet.get(i).asList();
56 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(btport && btport->size()>=3,
"The ports must be given as lists of <portname> <toleratedDuplicates>");
57 DuplicateReadingsPortInfo info;
58 info.name = btport->get(0).asString();
59 info.toleratedDuplicates = btport->get(1).asInt32();
60 ports.push_back(info);
64 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(port.open(
"..."),
65 "opening port, is YARP network available?");
69 void SensorsDuplicateReadings::tearDown() {
74 void SensorsDuplicateReadings::run() {
75 for(
unsigned int i=0; i<ports.size(); i++) {
77 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Checking port %s ...", ports[i].name.c_str()));
78 bool connected = Network::connect(ports[i].name.c_str(), port.getName());
79 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(connected,
80 Asserter::format(
"could not connect to remote port %s.", ports[i].name.c_str()));
83 Time::delay(testTime);
84 port.disableCallback();
86 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Computed a total of %lu duplicates out of %lu samples.",
87 port.getTotalNrOfDuplicates(),port.getCount()));
88 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Maximum number of consecutive duplicates: %lu Maximum jitter: %lf ",
89 port.getMaxNrOfDuplicates(), port.getMaxJitter()));
91 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(port.getTotalNrOfDuplicates() > ports[i].toleratedDuplicates,
92 Asserter::format(
"Number of duplicates (%lu) is higher than the tolerated (%d)",
93 port.getTotalNrOfDuplicates(),
94 ports[i].toleratedDuplicates));
96 Network::disconnect(ports[i].name.c_str(), port.getName());
101 void DuplicateDetector::onRead(yarp::sig::Vector& vec) {
102 double tcurrent = Time::now();
108 currentNrOfDuplicates = 0;
109 totalNrOfDuplicates = 0;
110 lastNewValueTime = tcurrent;
111 maxJitter = currentJitter;
112 maxNrOfDuplicates = currentNrOfDuplicates;
117 double diff = yarp::math::norm(vec-lastReading);
118 if( diff < this->tolerance )
121 currentNrOfDuplicates++;
122 currentJitter = tcurrent - lastNewValueTime;
124 totalNrOfDuplicates++;
126 maxJitter = std::max(currentJitter,maxJitter);
127 maxNrOfDuplicates = std::max(currentJitter,maxJitter);
134 currentNrOfDuplicates = 0;
136 lastNewValueTime = tcurrent;
Check if a yarp port is correctly publishing unique values at each update .