Skip to content

Commit

Permalink
Merge pull request #62 from gsisinna/feat/encoders_struct_folders_1084
Browse files Browse the repository at this point in the history
OpticalEncodersDrift: Added features regarding folder architecture and date/time for saved files
  • Loading branch information
Nicogene authored Jun 13, 2022
2 parents 4ef3af9 + b83e99b commit 3104a0b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(iCub-Tests)

Expand Down
58 changes: 48 additions & 10 deletions src/opticalEncoders-drift/opticalEncodersDrift.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* iCub Robot Unit Tests (Robot Testing Framework)
*
* Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2015-2022 Istituto Italiano di Tecnologia (IIT)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -25,18 +25,23 @@
#include <yarp/os/Time.h>
#include <yarp/math/Math.h>
#include <yarp/os/Property.h>
#include <yarp/conf/environment.h>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include "opticalEncodersDrift.h"
#include <iostream>
#include <ctime>
#include <filesystem>

//example -v -t OpticalEncodersDrift.dll -p "--robot icub --part head --joints ""(0 1 2)"" --home ""(0 0 0)" --speed "(20 20 20)" --max "(10 10 10)" --min "(-10 -10 -10)" --cycles 100 --tolerance 1.0 "
//example2 -v -t OpticalEncodersDrift.dll -p "--robot icub --part head --joints ""(2)"" --home ""(0)"" --speed "(20 )" --max "(10 )" --min "(-10)" --cycles 100 --tolerance 1.0 "
using namespace robottestingframework;
using namespace yarp::os;
using namespace yarp::dev;
using namespace yarp::math;
using namespace std;
using namespace std::filesystem;

// prepare the plugin
ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(OpticalEncodersDrift)
Expand Down Expand Up @@ -76,6 +81,7 @@ bool OpticalEncodersDrift::setup(yarp::os::Property& property) {
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(property.check("tolerance"), "The max error tolerance must be given as the test parameter!");

robotName = property.find("robot").asString();

partName = property.find("part").asString();

Bottle* jointsBottle = property.find("joints").asList();
Expand Down Expand Up @@ -211,11 +217,17 @@ bool OpticalEncodersDrift::goHome()
return true;
}

void OpticalEncodersDrift::saveToFile(std::string filename, yarp::os::Bottle &b)
bool OpticalEncodersDrift::saveToFile(const std::string &filename, const yarp::os::Bottle &b)
{
std::fstream fs;
fs.open (filename.c_str(), std::fstream::out);

if ( (fs.rdstate() & std::ifstream::failbit ) != 0 )
{
std::cerr << "Error opening " << filename << "\n";
return false;
}

for (unsigned int i=0; i<b.size(); i++)
{
std::string s = b.get(i).toString();
Expand All @@ -225,6 +237,7 @@ void OpticalEncodersDrift::saveToFile(std::string filename, yarp::os::Bottle &b)
}

fs.close();
return true;
}

void OpticalEncodersDrift::run()
Expand Down Expand Up @@ -327,18 +340,42 @@ void OpticalEncodersDrift::run()
}
}

time_t now = time(0);
tm *ltm = localtime(&now);

char folder_time_buffer[80];
char file_time_buffer[80];

strftime(folder_time_buffer, sizeof(folder_time_buffer), "%d%m%Y", ltm);
strftime(file_time_buffer, sizeof(file_time_buffer), "%d%m%Y_%H%M", ltm);

std::string filename = "encDrift_plot_";
string folder_time_str(folder_time_buffer);
string file_time_str(file_time_buffer); //This string contain also minutes

// Create the filename with date and time
string filename = "encDrift_plot_";
filename += partName;
filename += "_";
filename += file_time_str;
filename += ".txt";

int num_j = jointsList.size();
saveToFile(filename,dataToPlot);

constexpr char default_robot_name[] = "RobotName";

string robot_str = yarp::conf::environment::get_string("YARP_ROBOT_NAME", default_robot_name);
string directory_tree = "results/" + robot_str + "/encoders-icub_" + folder_time_str + "/encDrift";
create_directories(directory_tree); // This function return false if there is an error or if the directories already exist

string filename_with_path = directory_tree + "/" + filename;
bool saved_files = saveToFile(filename_with_path,dataToPlot);

if(!saved_files)
{
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Error saving files to plot!");
}

char plotstring[1000];
//gnuplot -e "unset key; plot for [col=1:6] 'C:\software\icub-tests\build\plugins\Debug\plot.txt' using col with lines" -persist
sprintf (plotstring, "gnuplot -e \" unset key; plot for [col=1:%d] '%s' using col with lines \" -persist", num_j,filename.c_str());

sprintf (plotstring, "gnuplot -e \" unset key; plot for [col=1:%d] '%s' using col with lines \" -persist", (int)jointsList.size(),filename_with_path.c_str());

if(plot)
{
system (plotstring);
Expand All @@ -350,5 +387,6 @@ void OpticalEncodersDrift::run()
}

ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(isInHome, "This part is not in home. Suite test will be terminated!");
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Test is finished. Your files are saved in: \n" + filename_with_path);

}
}
2 changes: 1 addition & 1 deletion src/opticalEncoders-drift/opticalEncodersDrift.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OpticalEncodersDrift : public yarp::robottestingframework::TestCase {

bool goHome();
void setMode(int desired_mode);
void saveToFile(std::string filename, yarp::os::Bottle &b);
bool saveToFile(const std::string &filename, const yarp::os::Bottle &b);

private:
std::string robotName;
Expand Down

0 comments on commit 3104a0b

Please sign in to comment.