-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathexample_parameter_server.c
154 lines (129 loc) · 4.64 KB
/
example_parameter_server.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2021 - for information on the respective copyright owner
// see the NOTICE file and/or the repository https://github.com/ros2/rclc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <unistd.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <rclc_parameter/rclc_parameter.h>
rclc_parameter_server_t param_server;
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
(void) timer;
(void) last_call_time;
int64_t value;
rclc_parameter_get_int(¶m_server, "param2", &value);
value++;
rclc_parameter_set_int(¶m_server, "param2", value);
}
bool on_parameter_changed(const Parameter * old_param, const Parameter * new_param, void * context)
{
(void) context;
if (old_param == NULL && new_param == NULL) {
printf("Callback error, both parameters are NULL\n");
return false;
}
if (old_param == NULL) {
printf("Creating new parameter %s\n", new_param->name.data);
} else if (new_param == NULL) {
printf("Deleting parameter %s\n", old_param->name.data);
} else {
printf("Parameter %s modified.", old_param->name.data);
switch (old_param->value.type) {
case RCLC_PARAMETER_BOOL:
printf(
" Old value: %d, New value: %d (bool)", old_param->value.bool_value,
new_param->value.bool_value);
break;
case RCLC_PARAMETER_INT:
printf(
" Old value: %ld, New value: %ld (int)", old_param->value.integer_value,
new_param->value.integer_value);
break;
case RCLC_PARAMETER_DOUBLE:
printf(
" Old value: %f, New value: %f (double)", old_param->value.double_value,
new_param->value.double_value);
break;
default:
break;
}
printf("\n");
}
return true;
}
int main()
{
rcl_ret_t rc;
rcl_allocator_t allocator = rcl_get_default_allocator();
// Create init_options
rclc_support_t support;
rclc_support_init(&support, 0, NULL, &allocator);
// Create node
rcl_node_t node;
rclc_node_init_default(&node, "demo_param_node", "", &support);
// Create parameter service
rclc_parameter_server_init_default(¶m_server, &node);
// create timer,
rcl_timer_t timer;
rclc_timer_init_default2(
&timer,
&support,
RCL_MS_TO_NS(1000),
timer_callback,
true);
// Create executor
// Note:
// If you need more than the default number of publisher/subscribers, etc., you
// need to configure the micro-ROS middleware also!
// See documentation in the executor.h at the function rclc_executor_init()
// for more details.
rclc_executor_t executor;
rclc_executor_init(
&executor, &support.context, RCLC_EXECUTOR_PARAMETER_SERVER_HANDLES + 1,
&allocator);
rclc_executor_add_parameter_server(&executor, ¶m_server, on_parameter_changed);
rclc_executor_add_timer(&executor, &timer);
// Add parameters
rclc_add_parameter(¶m_server, "param1", RCLC_PARAMETER_BOOL);
rclc_add_parameter(¶m_server, "param2", RCLC_PARAMETER_INT);
rclc_add_parameter(¶m_server, "param3", RCLC_PARAMETER_DOUBLE);
rclc_parameter_set_bool(¶m_server, "param1", false);
rclc_parameter_set_int(¶m_server, "param2", 10);
rclc_parameter_set_double(¶m_server, "param3", 0.01);
// Add parameters constraints
rclc_add_parameter_description(¶m_server, "param2", "Second parameter", "Only even numbers");
rclc_add_parameter_constraint_integer(¶m_server, "param2", -10, 120, 2);
rclc_add_parameter_description(¶m_server, "param3", "Third parameter", "");
rclc_set_parameter_read_only(¶m_server, "param3", true);
bool param1;
int64_t param2;
double param3;
rclc_parameter_get_bool(¶m_server, "param1", ¶m1);
rclc_parameter_get_int(¶m_server, "param2", ¶m2);
rclc_parameter_get_double(¶m_server, "param3", ¶m3);
// Start Executor
rclc_executor_spin(&executor);
// clean up
rc = rclc_executor_fini(&executor);
rc += rclc_parameter_server_fini(¶m_server, &node);
rc += rcl_node_fini(&node);
if (rc != RCL_RET_OK) {
printf("Error while cleaning up!\n");
return -1;
}
return 0;
}