FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
microcontroller/src/implementation.cpp at main · openvmp/microcontroller · GitHub
openvmp
/
microcontroller
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
14
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
microcontroller
/
src
/
implementation.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
343 lines (299 loc) · 13.3 KB
Breadcrumbs
microcontroller
/
src
/
implementation.cpp
Copy path
File metadata and controls
343 lines (299 loc) · 13.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* OpenVMP, 2023
*
* Author: Roman Kuzmenko
* Created: 2023-03-26
*
* Licensed under Apache License, Version 2.0.
*/
#
include
"
remote_microcontroller/implementation.hpp
"
#
include
<
yaml.h
>
#
include
<
ament_index_cpp/get_package_share_directory.hpp
>
#
include
<
charconv
>
#
include
<
locale
>
#
include
"
remote_microcontroller/config.hpp
"
#
include
"
remote_microcontroller/gpio_switch.hpp
"
#
include
"
remote_microcontroller/interface.hpp
"
#
include
"
remote_microcontroller/proto_mgmt.hpp
"
#
include
"
remote_microcontroller/proto_service.hpp
"
#
include
"
remote_microcontroller/puldir_stepper_driver.hpp
"
#
include
"
remote_microcontroller/pwm_actuator_position.hpp
"
#
include
"
remote_microcontroller/pwm_actuator_velocity.hpp
"
#
include
"
remote_microcontroller/uart.hpp
"
#
include
"
remote_serial/factory.hpp
"
#
include
"
remote_serial/utils.hpp
"
namespace
remote_microcontroller
{
Implementation::Implementation
(
rclcpp::Node *node,
std::shared_ptr<rclcpp::executors::MultiThreadedExecutor> exec)
: Interface{node}, initialized_{
false
}, exec_{exec} {
auto
prefix =
get_prefix_
();
std::string ns = node_->
get_namespace
();
prov_ =
remote_serial::Factory::New
(node);
prov_->
register_input_cb
(&Implementation::input_cb_,
this
);
srv_reset_ = node_->
create_service
<srv::Reset>(
prefix +
MICROCONTROLLER_SERVICE_RESET
,
std::bind
(&Implementation::reset_handler_,
this
, std::placeholders::_1,
std::placeholders::_2),
::rmw_qos_profile_default, callback_group_);
node->
declare_parameter
(
"
microcontroller_config
"
,
"
config/microcontroller.yaml
"
);
node->
get_parameter
(
"
microcontroller_config
"
, param_config_);
//
Static part is done, now dynamic behavior driven by the config
auto
config_filename = param_config_.
as_string
();
auto
config_file =
fopen
(config_filename.
c_str
(),
"
r
"
);
yaml_parser_t
parser;
yaml_document_t
doc;
yaml_parser_initialize
(&parser);
yaml_parser_set_input_file
(&parser, config_file);
if
(
yaml_parser_load
(&parser, &doc)) {
RCLCPP_DEBUG
(node->
get_logger
(),
"
YAML file is loaded
"
);
yaml_node_t
*root =
yaml_document_get_root_node
(&doc);
if
(root && root->
type
==
YAML_MAPPING_NODE
) {
for
(
auto
root_item = root->
data
.
mapping
.
pairs
.
start
;
root_item < root->
data
.
mapping
.
pairs
.
end
; root_item++) {
RCLCPP_DEBUG
(node->
get_logger
(),
"
root item is found
"
);
auto
root_key =
yaml_document_get_node
(&doc, root_item->
key
);
if
(!root_key || root_key->
type
!=
YAML_SCALAR_NODE
)
continue
;
auto
accessories =
yaml_document_get_node
(&doc, root_item->
value
);
if
(!accessories || accessories->
type
!=
YAML_SEQUENCE_NODE
)
continue
;
RCLCPP_DEBUG
(node->
get_logger
(),
"
root item is a sequence
"
);
std::string
chapter
((
char
*)root_key->
data
.
scalar
.
value
);
int
i;
yaml_node_item_t
*accessory_item;
for
(accessory_item = accessories->
data
.
sequence
.
items
.
start
, i =
0
;
accessory_item < accessories->
data
.
sequence
.
items
.
top
;
++accessory_item, ++i) {
RCLCPP_DEBUG
(node->
get_logger
(),
"
accessory item is found
"
);
auto
accessory =
yaml_document_get_node
(&doc, *accessory_item);
if
(!accessory || accessory->
type
!=
YAML_MAPPING_NODE
)
continue
;
RCLCPP_DEBUG
(node->
get_logger
(),
"
accessory item is a map
"
);
std::string node_name;
std::string node_prefix;
int
channel = i;
int
dir_channel = -
1
;
//
used by puldir only
std::string type;
//
used by pwm only
auto
node_options = rclcpp::NodeOptions{};
for
(
auto
param_item = accessory->
data
.
mapping
.
pairs
.
start
;
param_item < accessory->
data
.
mapping
.
pairs
.
end
; param_item++) {
RCLCPP_DEBUG
(node->
get_logger
(),
"
param item is found: %d
"
,
param_item->
key
);
auto
param_key =
yaml_document_get_node
(&doc, param_item->
key
);
if
(!param_key || param_key->
type
!=
YAML_SCALAR_NODE
)
continue
;
RCLCPP_DEBUG
(node->
get_logger
(),
"
param item is a scalar
"
);
auto
key =
std::string
((
char
*)param_key->
data
.
scalar
.
value
);
RCLCPP_DEBUG
(node->
get_logger
(),
"
param item is %s
"
,
(
char
*)param_key->
data
.
scalar
.
value
);
auto
param_value =
yaml_document_get_node
(&doc, param_item->
value
);
if
(!param_value || param_value->
type
!=
YAML_SCALAR_NODE
)
continue
;
RCLCPP_DEBUG
(node->
get_logger
(),
"
param value is a scalar
"
);
if
(key ==
"
type
"
) {
type =
std::string
((
char
*)param_value->
data
.
scalar
.
value
);
}
else
if
(key ==
"
name
"
) {
node_name =
"
driver_microcontroller_
"
+
std::string
((
char
*)param_value->
data
.
scalar
.
value
);
}
else
if
(key ==
"
prefix
"
) {
node_prefix =
std::string
((
char
*)param_value->
data
.
scalar
.
value
);
}
else
if
(key ==
"
channel
"
) {
channel =
std::atoi
((
char
*)param_value->
data
.
scalar
.
value
);
}
else
if
(key ==
"
dir_channel
"
) {
dir_channel =
std::atoi
((
char
*)param_value->
data
.
scalar
.
value
);
}
else
{
std::string
string_value
((
char
*)param_value->
data
.
scalar
.
value
);
const
char
*start = string_value.
c_str
();
const
char
*end = start +
strlen
(start);
int
int_value;
auto
res =
std::from_chars
(start, end, int_value);
if
(res.
ec
== std::errc{} && !*res.
ptr
) {
node_options.
parameter_overrides
().
push_back
({key, int_value});
}
else
{
double
double_value;
#
ifdef
__clang__
if
(
sscanf
(start,
"
%lf
"
, &double_value) ==
1
)
#
else
res =
std::from_chars
(start, end, double_value);
if
(res.
ec
== std::errc{} && !*res.
ptr
)
#
endif
{
node_options.
parameter_overrides
().
push_back
(
{key, double_value});
}
else
{
node_options.
parameter_overrides
().
push_back
(
{key, string_value});
}
}
}
}
if
(node_name ==
"
"
) {
node_name =
"
driver_microcontroller_
"
+ chapter +
std::to_string
(channel);
}
//
Create the node
RCLCPP_DEBUG
(node->
get_logger
(),
"
creating the node
"
);
node_options.
use_intra_process_comms
(
true
);
auto
accessory_node =
std::make_shared<rclcpp::Node>(node_name, ns, node_options);
exec->
add_node
(accessory_node);
//
Instantiate the accessory
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiating the accessory
"
);
std::shared_ptr<Accessory> ptr;
if
(chapter ==
MICROCONTROLLER_CONFIG_CHAPTER_GPIO
) {
ptr = std::make_shared<GPIOSwitch>(accessory_node.
get
(),
this
,
channel, node_prefix);
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiated GPIO
"
);
}
else
if
(chapter ==
MICROCONTROLLER_CONFIG_CHAPTER_PUL
) {
ptr = std::make_shared<PulDirStepperDriver>(
accessory_node.
get
(),
this
, channel, dir_channel, node_prefix);
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiated PulDirStepperDriver
"
);
}
else
if
(chapter ==
MICROCONTROLLER_CONFIG_CHAPTER_PWM
) {
if
(type ==
"
actuator_position
"
) {
RCLCPP_DEBUG
(node_->
get_logger
(),
"
Found a position actuator entry
"
);
ptr = std::make_shared<PWMActuatorVelocity>(
accessory_node.
get
(),
this
, channel, node_prefix);
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiated PWMActuatorVelocity
"
);
}
else
if
(type ==
"
actuator_position
"
) {
RCLCPP_DEBUG
(node_->
get_logger
(),
"
Found a velocity actuator entry
"
);
ptr = std::make_shared<PWMActuatorPosition>(
accessory_node.
get
(),
this
, channel, node_prefix);
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiated PWMActuatorPosition
"
);
}
else
{
RCLCPP_DEBUG
(node_->
get_logger
(),
"
Found a simple pwm entry
"
);
ptr = std::make_shared<
PWM
>(accessory_node.
get
(),
this
, channel,
node_prefix);
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiated PWM
"
);
}
}
else
if
(chapter ==
MICROCONTROLLER_CONFIG_CHAPTER_UART
) {
ptr = std::make_shared<
UART
>(accessory_node.
get
(),
this
, channel,
node_prefix);
RCLCPP_DEBUG
(node->
get_logger
(),
"
instantiated UART
"
);
}
//
Store the accessory in the collection
accessories_.
insert
({ptr->
get_addr
(), ptr});
}
}
}
RCLCPP_DEBUG
(node->
get_logger
(),
"
Destroying the document
"
);
yaml_document_delete
(&doc);
}
RCLCPP_DEBUG
(node->
get_logger
(),
"
Destroying the parser
"
);
yaml_parser_delete
(&parser);
}
bool
Implementation::reset_
(
bool
hard,
bool
reflash) {
(
void
)hard;
(
void
)reflash;
//
TODO(clairbee): implement reset
return
false
;
}
void
Implementation::write
(
uint16_t
addr,
uint16_t
value) {
uint8_t
cmd[
7
];
rm_mgmt_pack7_write
(addr, value, cmd);
prov_->
output
(
std::string
((
char
*)&cmd[
0
],
sizeof
(cmd)));
}
void
Implementation::read
(
uint16_t
addr) {
uint8_t
cmd[
5
];
rm_mgmt_pack5_read
(addr, cmd);
prov_->
output
(
std::string
((
char
*)&cmd[
0
],
sizeof
(cmd)));
}
void
Implementation::stream
(
uint16_t
addr,
const
std::string &value) {
uint8_t
cmd[
6
];
rm_mgmt_pack6_stream
(addr, value.
length
(), cmd);
prov_->
output
(
std::string
((
char
*)&cmd[
0
],
sizeof
(cmd)) + value);
}
/*
static
*/
void
Implementation::input_cb_
(
const
std::string &msg,
void
*user_data) {
(
void
)msg;
(
void
)user_data;
Implementation *that = (Implementation *)user_data;
that->
input_cb_real_
(msg);
}
void
Implementation::input_cb_real_
(
const
std::string &msg) {
RCLCPP_DEBUG
(node_->
get_logger
(),
"
Received data: %s
"
,
(
remote_serial::utils::bin2hex
(msg)).
c_str
());
input_queue_mutex_.
lock
();
input_queue_ += msg;
//
TODO(clairbee): optimize it to reduce extra copying
RCLCPP_DEBUG
(node_->
get_logger
(),
"
Queued data: %s
"
,
(
remote_serial::utils::bin2hex
(input_queue_)).
c_str
());
while
(input_queue_.
length
() >=
LENGTH_CMD_MIN
) {
if
((
uint8_t
)input_queue_[
0
] !=
HEADER_1
) {
input_queue_ = input_queue_.
substr
(
1
);
continue
;
}
if
((
uint8_t
)input_queue_[
1
] !=
HEADER_2
) {
input_queue_ = input_queue_.
substr
(
2
);
continue
;
}
//
With some level of confidence we can assume that we are receiving data
//
from a properly programmed controller.
//
Now it's time to initialize all accessories if it has not been done yet.
if
(!initialized_) {
for
(
auto
&acc_it : accessories_) {
acc_it.
second
->
init
();
}
initialized_ =
true
;
}
if
(input_queue_[
2
] ==
COMMAND_READ
) {
if
(input_queue_.
length
() <
LENGTH_READ_RESP
) {
//
Not enough data yet
break
;
}
uint16_t
addr = ((
uint8_t
)input_queue_[
3
]) <<
8
;
addr += ((
uint8_t
)input_queue_[
4
]);
if
(addr >=
ADDR_SERVICE_MIN
&& addr <=
ADDR_SERVICE_MAX
) {
//
Handle service traffic within this module.
//
TODO(clairbee): handle ping messages
}
else
{
//
Treat everything else as traffic related to one of the
//
accessories.
auto
acc_it = accessories_.
find
(addr);
if
(acc_it == accessories_.
end
()) {
RCLCPP_ERROR
(node_->
get_logger
(),
"
Received READ from an unknown accessory: %d
"
, addr);
}
else
{
uint16_t
value = ((
uint8_t
)input_queue_[
5
]) <<
8
;
value += ((
uint8_t
)input_queue_[
6
]);
acc_it->
second
->
read_cb
(value);
}
}
input_queue_ = input_queue_.
substr
(
LENGTH_READ_RESP
);
}
else
if
(input_queue_[
2
] ==
COMMAND_STREAM
) {
if
(input_queue_.
length
() <
LENGTH_STREAM_MIN
) {
//
Not enough data yet
break
;
}
uint8_t
len = ((
uint8_t
)input_queue_[
5
]);
if
((
int
)input_queue_.
length
() <
LENGTH_STREAM_MIN
+ len) {
//
Not enough data yet
break
;
}
uint16_t
addr = ((
uint8_t
)input_queue_[
3
]) <<
8
;
addr += ((
uint8_t
)input_queue_[
4
]);
auto
acc_it = accessories_.
find
(addr);
if
(acc_it == accessories_.
end
()) {
RCLCPP_ERROR
(node_->
get_logger
(),
"
Received STREAM from an unknown accessory: %d
"
, addr);
}
else
{
acc_it->
second
->
stream_cb
(
std::string
((
char
*)&input_queue_[
LENGTH_STREAM_MIN
], len));
}
input_queue_ = input_queue_.
substr
(
LENGTH_STREAM_MIN
+ len);
}
else
{
RCLCPP_ERROR
(node_->
get_logger
(),
"
Received unknown command from an unknown accessory: %d
"
,
input_queue_[
2
]);
input_queue_ = input_queue_.
substr
(
2
);
//
remove the header only
}
}
input_queue_mutex_.
unlock
();
}
rclcpp::FutureReturnCode
Implementation::reset_handler_
(
const
std::shared_ptr<srv::Reset::Request> request,
std::shared_ptr<srv::Reset::Response> response) {
response->
success
=
reset_
(request->
hard
, request->
reflash
);
return
rclcpp::FutureReturnCode::
SUCCESS
;
}
}
//
namespace remote_microcontroller
Back
|
FazBrowse Home
|
New Git URL