mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-02 03:49:12 +08:00
microRTPS_client: use updated uORB API; improve usage
This commit is contained in:
@@ -69,6 +69,8 @@ receive_base_types = [s.short_name for idx, s in enumerate(spec) if scope[idx] =
|
|||||||
#include <px4_time.h>
|
#include <px4_time.h>
|
||||||
#include <uORB/uORB.h>
|
#include <uORB/uORB.h>
|
||||||
|
|
||||||
|
#include <uORB/Publication.hpp>
|
||||||
|
#include <uORB/Subscription.hpp>
|
||||||
@[for topic in list(set(topic_names))]@
|
@[for topic in list(set(topic_names))]@
|
||||||
#include <uORB/topics/@(topic).h>
|
#include <uORB/topics/@(topic).h>
|
||||||
#include <uORB_microcdr/topics/@(topic).h>
|
#include <uORB_microcdr/topics/@(topic).h>
|
||||||
@@ -86,12 +88,8 @@ void* send(void* /*unused*/)
|
|||||||
uint16_t header_length = 0;
|
uint16_t header_length = 0;
|
||||||
|
|
||||||
/* subscribe to topics */
|
/* subscribe to topics */
|
||||||
int fds[@(len(send_topics))] = {};
|
|
||||||
|
|
||||||
// orb_set_interval statblish an update interval period in milliseconds.
|
|
||||||
@[for idx, topic in enumerate(send_topics)]@
|
@[for idx, topic in enumerate(send_topics)]@
|
||||||
fds[@(idx)] = orb_subscribe(ORB_ID(@(topic)));
|
uORB::Subscription @(topic)_sub{ORB_ID(@(topic))};
|
||||||
orb_set_interval(fds[@(idx)], _options.update_time_ms);
|
|
||||||
@[end for]@
|
@[end for]@
|
||||||
|
|
||||||
// ucdrBuffer to serialize using the user defined buffer
|
// ucdrBuffer to serialize using the user defined buffer
|
||||||
@@ -104,24 +102,18 @@ void* send(void* /*unused*/)
|
|||||||
|
|
||||||
while (!_should_exit_task)
|
while (!_should_exit_task)
|
||||||
{
|
{
|
||||||
bool updated;
|
|
||||||
@[for idx, topic in enumerate(send_topics)]@
|
@[for idx, topic in enumerate(send_topics)]@
|
||||||
orb_check(fds[@(idx)], &updated);
|
@(send_base_types[idx])_s @(topic)_data;
|
||||||
if (updated)
|
if (@(topic)_sub.update(&@(topic)_data)) {
|
||||||
{
|
// copy raw data into local buffer
|
||||||
// obtained data for the file descriptor
|
// payload is shifted by header length to make room for header
|
||||||
struct @(send_base_types[idx])_s data;
|
serialize_@(send_base_types[idx])(&writer, &@(topic)_data, &data_buffer[header_length], &length);
|
||||||
// copy raw data into local buffer
|
|
||||||
if (orb_copy(ORB_ID(@(topic)), fds[@(idx)], &data) == 0) {
|
|
||||||
/* payload is shifted by header length to make room for header*/
|
|
||||||
serialize_@(send_base_types[idx])(&writer, &data, &data_buffer[header_length], &length);
|
|
||||||
|
|
||||||
if (0 < (read = transport_node->write((char)@(rtps_message_id(ids, topic)), data_buffer, length)))
|
if (0 < (read = transport_node->write((char)@(rtps_message_id(ids, topic)), data_buffer, length)))
|
||||||
{
|
{
|
||||||
total_sent += read;
|
total_sent += read;
|
||||||
++sent;
|
++sent;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@[end for]@
|
@[end for]@
|
||||||
|
|
||||||
@@ -164,8 +156,8 @@ void micrortps_start_topics(struct timespec &begin, int &total_read, uint32_t &r
|
|||||||
|
|
||||||
// Declare received topics
|
// Declare received topics
|
||||||
@[for idx, topic in enumerate(recv_topics)]@
|
@[for idx, topic in enumerate(recv_topics)]@
|
||||||
struct @(receive_base_types[idx])_s @(topic)_data;
|
@(receive_base_types[idx])_s @(topic)_data;
|
||||||
orb_advert_t @(topic)_pub = nullptr;
|
uORB::Publication<@(receive_base_types[idx])_s> @(topic)_pub{ORB_ID(@(topic))};
|
||||||
@[end for]@
|
@[end for]@
|
||||||
|
|
||||||
// ucdrBuffer to deserialize using the user defined buffer
|
// ucdrBuffer to deserialize using the user defined buffer
|
||||||
@@ -194,11 +186,7 @@ void micrortps_start_topics(struct timespec &begin, int &total_read, uint32_t &r
|
|||||||
case @(rtps_message_id(ids, topic)):
|
case @(rtps_message_id(ids, topic)):
|
||||||
{
|
{
|
||||||
deserialize_@(receive_base_types[idx])(&reader, &@(topic)_data, data_buffer);
|
deserialize_@(receive_base_types[idx])(&reader, &@(topic)_data, data_buffer);
|
||||||
if (!@(topic)_pub) {
|
@(topic)_pub.publish(@(topic)_data);
|
||||||
@(topic)_pub = orb_advertise(ORB_ID(@(topic)), &@(topic)_data);
|
|
||||||
} else {
|
|
||||||
orb_publish(ORB_ID(@(topic)), @(topic)_pub, &@(topic)_data);
|
|
||||||
}
|
|
||||||
++received;
|
++received;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -74,11 +74,11 @@ struct options {
|
|||||||
};
|
};
|
||||||
eTransports transport = options::eTransports::UART;
|
eTransports transport = options::eTransports::UART;
|
||||||
char device[64] = DEVICE;
|
char device[64] = DEVICE;
|
||||||
int update_time_ms = UPDATE_TIME_MS;
|
uint32_t update_time_ms = UPDATE_TIME_MS;
|
||||||
int loops = LOOPS;
|
int loops = LOOPS;
|
||||||
int sleep_ms = SLEEP_MS;
|
uint32_t sleep_ms = SLEEP_MS;
|
||||||
uint32_t baudrate = BAUDRATE;
|
uint32_t baudrate = BAUDRATE;
|
||||||
int poll_ms = POLL_MS;
|
uint32_t poll_ms = POLL_MS;
|
||||||
char ip[16] = IP;
|
char ip[16] = IP;
|
||||||
uint16_t recv_port = DEFAULT_RECV_PORT;
|
uint16_t recv_port = DEFAULT_RECV_PORT;
|
||||||
uint16_t send_port = DEFAULT_SEND_PORT;
|
uint16_t send_port = DEFAULT_SEND_PORT;
|
||||||
|
|||||||
@@ -86,25 +86,23 @@ static int parse_options(int argc, char *argv[])
|
|||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 't': _options.transport = strcmp(myoptarg, "UDP") == 0 ?
|
case 't': _options.transport = strcmp(myoptarg, "UDP") == 0 ?
|
||||||
options::eTransports::UDP
|
options::eTransports::UDP
|
||||||
: options::eTransports::UART; break;
|
: options::eTransports::UART; break;
|
||||||
|
|
||||||
case 'd': if (nullptr != myoptarg) strcpy(_options.device, myoptarg); break;
|
case 'd': if (nullptr != myoptarg) strcpy(_options.device, myoptarg); break;
|
||||||
|
|
||||||
case 'u': _options.update_time_ms = strtol(myoptarg, nullptr, 10); break;
|
case 'u': _options.update_time_ms = strtoul(myoptarg, nullptr, 10); break;
|
||||||
|
|
||||||
case 'l': _options.loops = strtol(myoptarg, nullptr, 10); break;
|
case 'l': _options.loops = strtol(myoptarg, nullptr, 10); break;
|
||||||
|
|
||||||
case 'w': _options.sleep_ms = strtol(myoptarg, nullptr, 10); break;
|
case 'w': _options.sleep_ms = strtoul(myoptarg, nullptr, 10); break;
|
||||||
|
|
||||||
case 'b': _options.baudrate = strtoul(myoptarg, nullptr, 10); break;
|
case 'b': _options.baudrate = strtoul(myoptarg, nullptr, 10); break;
|
||||||
|
|
||||||
case 'p': _options.poll_ms = strtol(myoptarg, nullptr, 10); break;
|
|
||||||
|
|
||||||
case 'r': _options.recv_port = strtoul(myoptarg, nullptr, 10); break;
|
case 'r': _options.recv_port = strtoul(myoptarg, nullptr, 10); break;
|
||||||
|
|
||||||
case 's': _options.send_port = strtoul(myoptarg, nullptr, 10); break;
|
case 's': _options.send_port = strtoul(myoptarg, nullptr, 10); break;
|
||||||
|
|
||||||
case 'i': if (nullptr != myoptarg) strcpy(_options.ip, myoptarg); break;
|
case 'i': if (nullptr != myoptarg) strcpy(_options.ip, myoptarg); break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
usage(argv[1]);
|
usage(argv[1]);
|
||||||
@@ -112,11 +110,6 @@ static int parse_options(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_options.sleep_ms < 1) {
|
|
||||||
_options.sleep_ms = 1;
|
|
||||||
PX4_ERR("sleep time too low, using 1 ms");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_options.poll_ms < 1) {
|
if (_options.poll_ms < 1) {
|
||||||
_options.poll_ms = 1;
|
_options.poll_ms = 1;
|
||||||
PX4_ERR("poll timeout too low, using 1 ms");
|
PX4_ERR("poll timeout too low, using 1 ms");
|
||||||
|
|||||||
Reference in New Issue
Block a user