/
ros1_param.hpp
136 lines (112 loc) · 3.62 KB
/
ros1_param.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
#include <atomic>
#include <thread>
#include <xwidgets/xbox.hpp>
#include <xwidgets/xbutton.hpp>
#include <xwidgets/xlabel.hpp>
#include <xwidgets/xtext.hpp>
#include <xwidgets/xvalid.hpp>
#include "ros1_init.hpp"
struct ZeusParam {
private:
// widget objects
xw::valid valid;
xw::label lbl_param;
xw::text txt_param;
xw::label lbl_value;
xw::text txt_value;
xw::button btn_wait;
xw::button btn_set;
// widget layout
xw::hbox main_layout;
xw::vbox left_section;
// middle section is just the valid button, no need for layout
xw::vbox right_section;
xw::hbox label_txt_param_layout; // part of left_section
xw::hbox label_txt_value_layout; // part of left_section
// ros stuff
ros::NodeHandle nh;
std::atomic<bool> thr_running;
std::thread thr;
std::size_t sleep_usec;
public:
ZeusParam(const std::string& default_param_name = "", const std::string& default_param_value = "",
std::size_t sleep_usec = 100'000)
: valid()
, txt_param()
, btn_wait()
, txt_value()
, btn_set()
, main_layout()
, sleep_usec(sleep_usec) {
thr_running = false;
// init compontents
lbl_param.value = "param_name";
txt_param.value = default_param_name;
txt_param.on_submit([this]() { valid.value = nh.hasParam(txt_param.value); });
lbl_value.value = "param_value";
txt_value.value = default_param_value;
txt_value.on_submit(std::bind(&ZeusParam::set_param_value, this));
btn_wait.description = "Start Waiting";
btn_wait.on_click([this]() {
if (thr_running) {
stop_thread();
} else {
start_thread();
}
});
btn_set.description = "Set Param";
btn_set.on_click(std::bind(&ZeusParam::set_param_value, this));
// set up layout
// | lbl_param | txt_param | valid | btn_wait
// | lbl_value | txt_value | | btn_set
label_txt_param_layout.add(lbl_param);
label_txt_param_layout.add(txt_param);
label_txt_value_layout.add(lbl_value);
label_txt_value_layout.add(txt_value);
left_section.add(label_txt_param_layout);
left_section.add(label_txt_value_layout);
main_layout.add(left_section);
main_layout.add(valid);
right_section.add(btn_wait);
right_section.add(btn_set);
main_layout.add(right_section);
}
void display() { main_layout.display(); }
private:
void set_param_value() {
nh.setParam(txt_param.value, txt_value.value);
valid.value = nh.hasParam(txt_param.value);
}
void start_thread() {
thr_running = true;
txt_param.disabled = true;
txt_value.disabled = true;
btn_wait.description = "Stop Waiting";
thr = std::thread(&ZeusParam::thread_loop, this);
}
void thread_loop() {
while (thr_running) {
if (nh.hasParam(txt_param.value)) {
valid.value = true;
std::string tmp;
nh.getParam(txt_param.value, tmp);
txt_value.value = tmp;
} else {
valid.value = false;
txt_value.value = "";
}
usleep(sleep_usec);
}
}
void stop_thread() {
thr_running = false;
txt_param.disabled = false;
txt_value.disabled = false;
btn_wait.description = "Start Waiting";
usleep(1'000);
if (thr.joinable()) {
thr.join();
}
}
};