Skip to content

Commit acdfc3d

Browse files
committed
Update examples for FW 1.1.x
1 parent 51d563f commit acdfc3d

14 files changed

+253
-201
lines changed

CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
3+
project(o3r_examples CXX)
4+
5+
#Global compiler flags
6+
set(CMAKE_BUILD_TYPE Release) # Release or Debug
7+
set(CMAKE_CXX_EXTENSIONS OFF) # OFF -> -std=c++14, ON -> -std=gnu++14
8+
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_CXX_STANDARD_REQUIRED true)
10+
11+
#find ifm3d on the system
12+
find_package(ifm3d 1.3.3 CONFIG
13+
REQUIRED COMPONENTS device framegrabber deserialize
14+
)
15+
16+
add_subdirectory(ODS/Cpp/Examples)

Dockerfile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM ghcr.io/ifm/ifm3d:v1.3.3-ubuntu-amd64
2+
3+
RUN sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y\
4+
build-essential \
5+
cmake \
6+
coreutils \
7+
git \
8+
jq
9+
10+
11+
# c++ examples
12+
WORKDIR /home/ifm
13+
# Need to install a more recent version of nlhomann json
14+
# than what is provided by apt, to use with the schema validator.
15+
RUN git clone --branch v3.11.2 https://github.com/nlohmann/json.git && \
16+
cd json && \
17+
mkdir build && \
18+
cd build && \
19+
cmake -DCMAKE_INSTALL_PREFIX=/usr -DJSON_BuildTests=OFF .. && \
20+
make && \
21+
sudo make install
22+
23+
RUN git clone https://github.com/pboettch/json-schema-validator.git && \
24+
cd json-schema-validator && \
25+
mkdir build && \
26+
cd build && \
27+
cmake -DCMAKE_INSTALL_PREFIX=/usr -DJSON_VALIDATOR_BUILD_TESTS=OFF -DJSON_VALIDATOR_BUILD_EXAMPLES=OFF .. && \
28+
make && \
29+
sudo make install
30+
31+
32+
COPY --chown=ifm . /home/ifm/examples
33+
WORKDIR /home/ifm/examples/build
34+
RUN cmake .. && cmake --build .
35+
36+
# python examples
37+
WORKDIR /home/ifm/examples
38+
RUN pip install -r requirements.txt

ODS/Cpp/Examples/CMakeLists.txt

+36-25
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,60 @@ set(CMAKE_CXX_STANDARD 17)
99
set(CMAKE_CXX_STANDARD_REQUIRED true)
1010

1111
#find ifm3d on the system
12-
find_package(ifm3d 1.2.6 CONFIG
13-
REQUIRED COMPONENTS device framegrabber deserialize
12+
find_package(ifm3d 1.3.3 CONFIG
13+
REQUIRED COMPONENTS device framegrabber deserialize
14+
)
15+
16+
# Copy the config files used by the executables
17+
add_custom_target(copyConfigs ALL
18+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Configs/ ${CMAKE_CURRENT_BINARY_DIR}/../Configs
19+
COMMENT "Copying the configs files to the build directory"
1420
)
1521

22+
add_executable(ods_config ods_config.cpp)
1623
#Optional json validator package:
1724
find_package(nlohmann_json_schema_validator)
18-
if (NlohmannJsonSchemaValidator_FOUND)
19-
set(ENV{NlohmannJsonSchemaValidator_FOUND} TRUE)
20-
else()
21-
set(ENV{NlohmannJsonSchemaValidator_FOUND} FALSE)
25+
message(Schema found: ${nlohmann_json_schema_validator_FOUND})
26+
27+
add_library(ods_config_lib ods_config.h)
28+
set_target_properties(ods_config_lib PROPERTIES LINKER_LANGUAGE CXX)
29+
if (nlohmann_json_schema_validator_FOUND)
30+
target_compile_definitions(ods_config_lib PUBLIC "USE_JSONSCHEMA")
31+
target_link_libraries(ods_config_lib
32+
nlohmann_json_schema_validator
33+
)
2234
endif()
23-
message("Variable is $ENV{NlohmannJsonSchemaValidator_FOUND}")
2435

25-
# Configuration example
26-
add_executable(ods_config ods_config.cpp)
36+
target_link_libraries(ods_config_lib
37+
ifm3d::device
38+
)
2739
target_link_libraries(ods_config
28-
nlohmann_json_schema_validator
29-
ifm3d::device
30-
)
40+
ods_config_lib
41+
ifm3d::device
42+
)
3143

32-
## Getting data example
3344
add_executable(ods_get_data ods_get_data.cpp)
3445
target_link_libraries(ods_get_data
35-
nlohmann_json_schema_validator
46+
ods_config_lib
3647
ifm3d::device
3748
ifm3d::framegrabber
3849
ifm3d::deserialize
39-
)
40-
50+
)
51+
4152
add_executable(diagnostic diagnostic.cpp)
4253
target_link_libraries(diagnostic
4354
ifm3d::device
4455
ifm3d::framegrabber
45-
)
46-
47-
add_executable(ods_demo ods_demo.cpp)
48-
target_link_libraries(ods_demo
49-
nlohmann_json_schema_validator
50-
ifm3d::device
51-
ifm3d::framegrabber
52-
)
56+
)
5357

5458
add_executable(bootup_monitor bootup_monitor.cpp)
5559
target_link_libraries(bootup_monitor
5660
ifm3d::device
57-
)
61+
)
62+
63+
add_executable(ods_demo ods_demo.cpp)
64+
target_link_libraries(ods_demo
65+
ods_config_lib
66+
ifm3d::device
67+
ifm3d::framegrabber
68+
)

ODS/Cpp/Examples/Configs/ods_two_apps_config.json ODS/Cpp/Examples/Configs/ods_changing_views_config.json

+10-19
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,29 @@
44
"app0": {
55
"class": "ods",
66
"configuration": {
7+
"activePorts": ["port2"],
78
"port2": {
89
"acquisition": {
9-
"channelValue": 0
10+
"channelValue": 20
1011
}
11-
}
12-
},
13-
"name": "app0",
14-
"ports": [
15-
"port2",
16-
"port6"
17-
],
18-
"state": "CONF"
19-
},
20-
"app1": {
21-
"class": "ods",
22-
"configuration": {
12+
},
2313
"port3": {
2414
"acquisition": {
2515
"channelValue": 30
2616
}
2717
},
28-
"vo": {
29-
"portNumber": 3
18+
"vo":{
19+
"voPorts": ["port2", "port3"]
3020
}
3121
},
32-
"name": "app1",
22+
"name": "app0",
3323
"ports": [
24+
"port2",
3425
"port3",
3526
"port6"
3627
],
3728
"state": "CONF"
3829
}
39-
}
40-
}
41-
}
30+
}
31+
}}
32+

ODS/Cpp/Examples/bootup_monitor.h

+24-49
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BootupMonitor{
1515
const std::string IP;
1616
const int wait_time; // in seconds
1717

18-
BootupMonitor(ifm3d::O3R::Ptr o3r_, int timeout_ = 25, int wait_time_ = 0.5 ) :
18+
BootupMonitor(ifm3d::O3R::Ptr o3r_, int timeout_ = 25, int wait_time_ = 1 ) :
1919
o3r(o3r_),
2020
timeout(timeout_),
2121
IP(o3r->IP()),
@@ -29,64 +29,39 @@ class BootupMonitor{
2929
auto start = std::chrono::steady_clock::now();
3030
ifm3d::json config;
3131
do{
32-
// This "ping" function will only work on linux-based systems.
33-
// Since this is simply an example on how to monitor the proper
34-
// bootup of an O3R, Windows users can skip this step.
35-
if (!Ping_()){
36-
std::clog << "Awaiting successful 'ping' from VPU" << std::endl;
32+
try{
33+
config = o3r->Get();
34+
std::clog << "Connected." << std::endl;
35+
}catch(ifm3d::Error& e){
36+
std::clog << "Awaiting data from VPU..." << std::endl;
3737
}
38-
else{
39-
try{
40-
config = o3r->Get();
41-
std::clog << "Connected." << std::endl;
42-
}catch(ifm3d::Error& e){
43-
std::clog << "Awaiting data from VPU..." << std::endl;
44-
}
45-
if (!config.empty()){
46-
std::clog << "Checking the init stages." << std::endl;
47-
auto conf_init_stages = config["/device/diagnostic/confInitStages"_json_pointer];
48-
std::clog << conf_init_stages << std::endl;
49-
for (auto it : conf_init_stages)
38+
if (!config.empty()){
39+
std::clog << "Checking the init stages." << std::endl;
40+
auto conf_init_stages = config["/device/diagnostic/confInitStages"_json_pointer];
41+
std::clog << conf_init_stages << std::endl;
42+
for (auto it : conf_init_stages)
43+
{
44+
if (it == "applications"){
45+
std::clog << "VPU fully booted." << std::endl;
46+
RetrieveBootDiagnostic_();
47+
return true;
48+
}
49+
if (it == "ports"){
50+
std::clog << "Ports recognized." << std::endl;
51+
}
52+
else if (it == "device")
5053
{
51-
if (it == "applications"){
52-
std::clog << "VPU fully booted." << std::endl;
53-
RetrieveBootDiagnostic_();
54-
return true;
55-
}
56-
if (it == "ports"){
57-
std::clog << "Ports recognized." << std::endl;
58-
}
59-
else if (it == "device")
60-
{
61-
std::clog << "Device recognized." << std::endl;
62-
}
54+
std::clog << "Device recognized." << std::endl;
6355
}
64-
std::this_thread::sleep_for(std::chrono::seconds(wait_time));
6556
}
66-
6757
}
58+
std::this_thread::sleep_for(std::chrono::seconds(wait_time));
59+
6860
}while(std::chrono::steady_clock::now() - start < std::chrono::seconds(timeout));
6961
throw std::runtime_error("Process timed out waiting for the VPU to boot.");
7062
}
7163

7264
private:
73-
// Linux-only pinging.
74-
bool Ping_()
75-
{
76-
char ip[IP.length() + 1];
77-
strcpy(ip, IP.c_str());
78-
char ping[100] = "ping -c1 -s1 ";
79-
char* command = std::strcat(ping, std::strcat(ip, " > /dev/null 2>&1"));
80-
81-
int x = std::system(command);
82-
if (x==0){
83-
std::clog << "Ping successful" << std::endl;
84-
return true;
85-
}else{
86-
std::clog <<"Ping failed" << std::endl;
87-
return false;
88-
}
89-
}
9065
void RetrieveBootDiagnostic_()
9166
{
9267
auto active_diag = o3r->GetDiagnosticFiltered(ifm3d::json::parse(R"({"state": "active"})"))["/events"_json_pointer];

ODS/Cpp/Examples/diagnostic.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ int main()
1616
// Examples on how to retrieve the diagnostic
1717
// active and/or dormant.
1818
////////////////////////////////////////////////
19-
19+
// Using ifm3d::json::object()
2020
std::clog << "All current diagnostics:\n"
21-
<< diagnostic.GetDiagnosticFiltered({}) << std::endl;
21+
<< diagnostic.GetDiagnosticFiltered(ifm3d::json::object()) << std::endl;
2222

2323
std::clog << "Active diagnostics:\n"
2424
<< diagnostic.GetDiagnosticFiltered(ifm3d::json::parse(R"({"state": "active"})"))

ODS/Cpp/Examples/ods_config.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int main()
5858
}
5959
catch (const ifm3d::Error& ex) {
6060
std::clog << "Caught exception: " << ex.message() << std::endl;
61+
std::clog << "This was expected. Continuing on with the tutorial." << std::endl;
6162
}
6263

6364
std::clog << "Finished getting configurations" << std::endl;
@@ -70,7 +71,8 @@ int main()
7071
// The user could directly use the ifm3d library
7172
// native calls to set the configuration.
7273
ODSConfig configurator(o3r);
73-
74+
75+
std::clog << "Setting test configurations:" << std::endl;
7476
configurator.SetConfigFromStr(R"(
7577
{"device": { "info": { "description": "I will use this O3R to change the world"}}})");
7678

@@ -100,8 +102,9 @@ int main()
100102
configurator.SetConfigFromFile("/non/existent/file.json");
101103
}
102104
catch (...) {
103-
// Failing silently to continue with the tutorial
104-
}
105+
std::clog << "Error caught while configuring from a non-existent file.\n"
106+
<< "This is expected, continuing with the example.";
107+
}
105108

106109
std::clog << "You are done with the configuration tutorial!" << std::endl;
107110

0 commit comments

Comments
 (0)