Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BufferManager: add mutex, configuration from file, empty constructor workflow et al #65

Merged
merged 7 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ find_package(matioCpp REQUIRED)
find_package(Boost REQUIRED)
find_package(Threads REQUIRED)

option(YARP_TELEMETRY_USES_SYSTEM_nlohmann_json OFF)
# 3.9.2 is unreleased, this option is not working until that version is not released
if(YARP_TELEMETRY_USES_SYSTEM_nlohmann_json)
find_package(nlohmann_json 3.9.2 REQUIRED)
else()
include(FetchContent)

FetchContent_Declare(json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG develop) # awaiting 3.9.2 release for https://github.com/nlohmann/json/issues/2513

FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()


feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES)

Expand Down
20 changes: 15 additions & 5 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(MATIO_VECTOR_EXAMPLE_SRC matio_vector_example.cpp)
set(MATIO_MATRIX_EXAMPLE_SRC matio_matrix_example.cpp)
set(MATIO_TIMESERIES_EXAMPLE_SRC matio_timeseries_example.cpp)
set(TELEMETRY_BUFFER_EXAMPLE_SRC telemetry_buffer_example.cpp)
set(TELEMETRY_BUFFER_MANAGER_CONF_FILE telemetry_buffer_manager_conf_file.cpp)
set(TELEMETRY_BUFFER_MANAGER_EXAMPLE_SRC telemetry_buffer_manager_example.cpp)
set(TELEMETRY_BUFFER_PERIODIC_SAVE_SRC telemetry_buffer_periodic_save.cpp)
set(CB_TO_MATIO_EXAMPLE_SRC CB_to_matfile_example.cpp)
Expand All @@ -21,15 +22,17 @@ add_executable(matio_vector_example ${MATIO_VECTOR_EXAMPLE_SRC})
add_executable(matio_matrix_example ${MATIO_MATRIX_EXAMPLE_SRC})
add_executable(matio_timeseries_example ${MATIO_TIMESERIES_EXAMPLE_SRC})
add_executable(telemetry_buffer_example ${TELEMETRY_BUFFER_EXAMPLE_SRC})
add_executable(telemetry_buffer_manager_conf_file_example ${TELEMETRY_BUFFER_MANAGER_CONF_FILE})
add_executable(telemetry_buffer_manager_example ${TELEMETRY_BUFFER_MANAGER_EXAMPLE_SRC})
add_executable(telemetry_buffer_periodic_save ${TELEMETRY_BUFFER_PERIODIC_SAVE_SRC})
add_executable(CB_to_matfile_example ${CB_TO_MATIO_EXAMPLE_SRC})

target_compile_features(circular_buffer_example PUBLIC cxx_std_14)
target_compile_features(circular_buffer_record_example PUBLIC cxx_std_14)
target_compile_features(telemetry_buffer_example PUBLIC cxx_std_14)
target_compile_features(telemetry_buffer_manager_example PUBLIC cxx_std_14)
target_compile_features(telemetry_buffer_periodic_save PUBLIC cxx_std_14)
target_compile_features(circular_buffer_example PUBLIC cxx_std_17)
target_compile_features(circular_buffer_record_example PUBLIC cxx_std_17)
target_compile_features(telemetry_buffer_example PUBLIC cxx_std_17)
target_compile_features(telemetry_buffer_manager_conf_file_example PUBLIC cxx_std_17)
target_compile_features(telemetry_buffer_manager_example PUBLIC cxx_std_17)
target_compile_features(telemetry_buffer_periodic_save PUBLIC cxx_std_17)


target_link_libraries(circular_buffer_example Boost::boost)
Expand All @@ -42,6 +45,10 @@ target_link_libraries(telemetry_buffer_example YARP::YARP_conf
YARP::YARP_os
YARP::YARP_init
YARP::YARP_telemetry)
target_link_libraries(telemetry_buffer_manager_conf_file_example YARP::YARP_conf
YARP::YARP_os
YARP::YARP_init
YARP::YARP_telemetry)
target_link_libraries(telemetry_buffer_manager_example YARP::YARP_conf
YARP::YARP_os
YARP::YARP_init
Expand All @@ -59,3 +66,6 @@ target_link_libraries(CB_to_matfile_example PRIVATE matioCpp::matioCpp
YARP::YARP_conf
YARP::YARP_os
YARP::YARP_init ${CMAKE_THREAD_LIBS_INIT})

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/conf/test_json.json
DESTINATION ${CMAKE_BINARY_DIR}/bin)
12 changes: 12 additions & 0 deletions src/examples/conf/test_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"filename": "buffer_manager_test_conf_file",
"n_samples": 20,
"save_period": 1.0,
"data_threshold": 10,
"auto_save": true,
"save_periodically": true,
"channels": [
["one",[1,1]],
["two",[1,1]]
]
}
70 changes: 70 additions & 0 deletions src/examples/telemetry_buffer_manager_conf_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* 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 <yarp/os/Time.h>
#include <yarp/os/Network.h>
#include <yarp/telemetry/BufferManager.h>

#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
#include <vector>

using namespace std;
using namespace yarp::os;

constexpr size_t n_samples{20};
constexpr size_t threshold{10};
constexpr double check_period{1.0};



int main()
{
Network yarp;
yarp::telemetry::BufferConfig bufferConfig;

// we configure our API to use our periodic saving option
bufferConfig.n_samples = n_samples;
bufferConfig.save_period = check_period;
bufferConfig.data_threshold = threshold;
bufferConfig.save_periodically = true;
std::vector<yarp::telemetry::ChannelInfo> vars{ { "one",{2,3} },
{ "two",{3,2} } };
bufferConfig.channels = vars;

auto ok = bufferConfigToJson(bufferConfig, "test_json_write.json");

if (!ok) {
std::cout << "Problems saving configuration to json" << std::endl;
return 1;
}

yarp::telemetry::BufferManager<int32_t> bm;

ok = bufferConfigFromJson(bufferConfig,"test_json.json");

ok = ok && bm.configure(bufferConfig);

if (!ok) {
std::cout << "Problems configuring from file" << std::endl;
return 1;
}

std::cout << "Starting loop" << std::endl;
for (int i = 0; i < 40; i++) {
bm.push_back({ i }, "one");
yarp::os::Time::delay(0.01);
bm.push_back({ i + 1 }, "two");
}

yarp::os::Time::delay(3.0);
return 0;
}
12 changes: 7 additions & 5 deletions src/examples/telemetry_buffer_manager_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main()
std::cout << "Something went wrong..." << std::endl;

// now we test our API with the auto_save option enabled.
bufferConfig.m_auto_save = true;
bufferConfig.auto_save = true;

yarp::telemetry::BufferManager<int32_t> bm_m(bufferConfig);
bm_m.setFileName("buffer_manager_test_matrix");
Expand All @@ -72,7 +72,7 @@ int main()
return 1;
}
std::vector<yarp::telemetry::ChannelInfo> vars{ { "one",{2,3} },
{ "two",{3,2} } };
{ "two",{3,2} } };

ok = bm_m.addChannels(vars);
if (!ok) {
Expand All @@ -86,9 +86,11 @@ int main()
bm_m.push_back({ i * 1, i * 2, i * 3, i * 4, i * 5, i * 6 }, "two");
}

yarp::telemetry::BufferManager<double> bm_v("buffer_manager_test_vector",
{ {"one",{4,1}},
{"two",{4,1}} }, bufferConfig);

bufferConfig.channels = { {"one",{4,1}}, {"two",{4,1}} };
bufferConfig.filename = "buffer_manager_test_vector";

yarp::telemetry::BufferManager<double> bm_v(bufferConfig);
ok = bm_v.setNowFunction(now);
if (!ok) {
std::cout << "Problem setting the clock...."<<std::endl;
Expand Down
29 changes: 15 additions & 14 deletions src/examples/telemetry_buffer_periodic_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace yarp::os;

constexpr size_t n_samples{20};
constexpr size_t threshold{10};
constexpr double check_period{100.0};
constexpr double check_period{1.0};



Expand All @@ -34,8 +34,8 @@ int main()

// we configure our API to use our periodic saving option
bufferConfig.n_samples = n_samples;
bufferConfig.check_period = check_period;
bufferConfig.threshold = threshold;
bufferConfig.save_period = check_period;
bufferConfig.data_threshold = threshold;
bufferConfig.save_periodically = true;

yarp::telemetry::BufferManager<int32_t> bm(bufferConfig);
Expand All @@ -52,17 +52,17 @@ int main()
std::cout << "Problem adding variables...."<<std::endl;
return 1;
}

std::cout << "Starting loop" << std::endl;
for (int i = 0; i < 40; i++) {
bm.push_back({ i }, "one");
yarp::os::Time::delay(0.2);
yarp::os::Time::delay(0.01);
bm.push_back({ i + 1 }, "two");
}

std::cout << "Second example: " << std::endl;

// now we use also the auto_saving option
bufferConfig.m_auto_save = true;
bufferConfig.auto_save = true;

std::cout << "Second example: " << std::endl;

yarp::telemetry::BufferManager<int32_t> bm_m(bufferConfig);
bm_m.setFileName("buffer_manager_test_matrix");
Expand All @@ -77,19 +77,20 @@ int main()

for (int i = 0; i < 40; i++) {
bm_m.push_back({ i + 1, i + 2, i + 3, i + 4, i + 5, i + 6 }, "one");
yarp::os::Time::delay(0.2);
yarp::os::Time::delay(0.01);
bm_m.push_back({ i * 1, i * 2, i * 3, i * 4, i * 5, i * 6 }, "two");
}

std::cout << "Third example: " << std::endl;

yarp::telemetry::BufferManager<double> bm_v("buffer_manager_test_vector",
{ {"one",{4,1}},
{"two",{4,1}} }, bufferConfig);
// bm_v.periodicSave();
bufferConfig.channels = { {"one",{4,1}}, {"two",{4,1}} };
bufferConfig.filename = "buffer_manager_test_vector";

yarp::telemetry::BufferManager<double> bm_v(bufferConfig);

for (int i = 0; i < 40; i++) {
bm_v.push_back({ i+1.0, i+2.0, i+3.0, i+4.0 }, "one");
yarp::os::Time::delay(0.2);
yarp::os::Time::delay(0.01);
bm_v.push_back({ (double)i, i*2.0, i*3.0, i*4.0 }, "two");
}

Expand Down
10 changes: 7 additions & 3 deletions src/libYARP_telemetry/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ set(YARP_telemetry_HDRS
yarp/telemetry/Test.h
yarp/telemetry/Record.h
yarp/telemetry/Buffer.h
yarp/telemetry/BufferConfig.h
yarp/telemetry/BufferManager.h
)
set(YARP_telemetry_SRCS
yarp/telemetry/Test.cpp
yarp/telemetry/BufferConfig.cpp
)
set(YARP_telemetry_IMPL_HDRS )
set(YARP_telemetry_IMPL_SRCS )
Expand Down Expand Up @@ -50,11 +52,13 @@ target_include_directories(YARP_telemetry
)
target_compile_features(YARP_telemetry PUBLIC cxx_std_17)

target_link_libraries(YARP_telemetry PUBLIC Boost::boost
matioCpp::matioCpp
${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(YARP_telemetry PUBLIC Boost::boost
matioCpp::matioCpp
${CMAKE_THREAD_LIBS_INIT}
PRIVATE nlohmann_json::nlohmann_json)
list(APPEND YARP_telemetry_PUBLIC_DEPS Boost
matioCpp)
list(APPEND YARP_telemetry_PRIVATE_DEPS nlohmann_json)

set_property(TARGET YARP_telemetry PROPERTY PUBLIC_HEADER ${YARP_telemetry_HDRS})
set_property(TARGET YARP_telemetry PROPERTY PRIVATE_HEADER ${YARP_telemetry_IMPL_HDRS})
Expand Down
4 changes: 4 additions & 0 deletions src/libYARP_telemetry/src/yarp/telemetry/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Buffer {
return m_buffer_ptr->empty();
}

void resize(size_t new_size) {
return m_buffer_ptr->resize(new_size);
}

bool full() const {
return m_buffer_ptr->full();
}
Expand Down
43 changes: 43 additions & 0 deletions src/libYARP_telemetry/src/yarp/telemetry/BufferConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
* 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 <nlohmann/json.hpp>
#include <yarp/telemetry/BufferConfig.h>
#include <fstream>
#include <iostream>

namespace yarp::telemetry {
// This expects that the name of the json keyword is the same of the relative variable
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(BufferConfig, filename, n_samples, save_period, data_threshold, auto_save, save_periodically, channels)
}
bool bufferConfigFromJson(yarp::telemetry::BufferConfig& bufferConfig, const std::string& config_filename) {
// read a JSON file
std::ifstream input_stream(config_filename);
if (!input_stream.is_open()) {
std::cout << "Failed to open " << config_filename << std::endl;
return false;
}
nlohmann::json jason_file;
input_stream >> jason_file;
bufferConfig = jason_file.get<yarp::telemetry::BufferConfig>();
input_stream.close();
return true;
}

bool bufferConfigToJson(const yarp::telemetry::BufferConfig& bufferConfig, const std::string& config_filename) {
// write to a JSON file
std::ofstream out_stream(config_filename);
if (!out_stream.is_open()) {
std::cout << "Failed to open " << config_filename << std::endl;
return false;
}
nlohmann::json j = bufferConfig;
out_stream << j;
out_stream.close();
return true;
}
37 changes: 37 additions & 0 deletions src/libYARP_telemetry/src/yarp/telemetry/BufferConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
* 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.
*/

#ifndef YARP_TELEMETRY_BUFFER_CONFIG_H
#define YARP_TELEMETRY_BUFFER_CONFIG_H

#include <yarp/telemetry/api.h>
#include <cstring>
#include <vector>

namespace yarp::telemetry {
using dimensions_t = std::vector<size_t>;

using ChannelInfo = std::pair< std::string, dimensions_t >;

struct YARP_telemetry_API BufferConfig {
std::string filename{ "" };
size_t n_samples{ 0 };
double save_period{ 0.010 };
size_t data_threshold{ 0 };
bool auto_save{ false };
bool save_periodically{ false };
std::vector<ChannelInfo> channels;
};

} // yarp::telemetry

bool YARP_telemetry_API bufferConfigFromJson(yarp::telemetry::BufferConfig& bufferConfig, const std::string& config_filename);

bool YARP_telemetry_API bufferConfigToJson(const yarp::telemetry::BufferConfig& bufferConfig, const std::string& config_filename);

#endif
Loading