Skip to content

Commit

Permalink
examples: add examples for configuration from and to json file
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicogene committed Feb 8, 2021
1 parent 70a34b9 commit eaa72fd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
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]]
]
}
66 changes: 66 additions & 0 deletions src/examples/telemetry_buffer_manager_conf_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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;

nlohmann::json j = bufferConfig;

std::cout << "Here is the resulting json file "<<std::endl;
std::cout << j << std::endl;

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

auto ok = bm.configureFromFile("test_json.json");

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;
}

0 comments on commit eaa72fd

Please sign in to comment.