Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_DDS/AP_DDS_Client.h
Views: 1798
#pragma once12#include "AP_DDS_config.h"34#if AP_DDS_ENABLED56#include "uxr/client/client.h"7#include "ucdr/microcdr.h"89#if AP_DDS_GLOBAL_POS_CTRL_ENABLED10#include "ardupilot_msgs/msg/GlobalPosition.h"11#endif // AP_DDS_GLOBAL_POS_CTRL_ENABLED12#if AP_DDS_TIME_PUB_ENABLED13#include "builtin_interfaces/msg/Time.h"14#endif // AP_DDS_TIME_PUB_ENABLED15#if AP_DDS_NAVSATFIX_PUB_ENABLED16#include "sensor_msgs/msg/NavSatFix.h"17#endif // AP_DDS_NAVSATFIX_PUB_ENABLED18#if AP_DDS_NEEDS_TRANSFORMS19#include "tf2_msgs/msg/TFMessage.h"20#endif // AP_DDS_NEEDS_TRANSFORMS21#if AP_DDS_BATTERY_STATE_PUB_ENABLED22#include "sensor_msgs/msg/BatteryState.h"23#endif // AP_DDS_BATTERY_STATE_PUB_ENABLED24#if AP_DDS_IMU_PUB_ENABLED25#include "sensor_msgs/msg/Imu.h"26#endif // AP_DDS_IMU_PUB_ENABLED27#if AP_DDS_STATUS_PUB_ENABLED28#include "ardupilot_msgs/msg/Status.h"29#endif // AP_DDS_STATUS_PUB_ENABLED30#if AP_DDS_JOY_SUB_ENABLED31#include "sensor_msgs/msg/Joy.h"32#endif // AP_DDS_JOY_SUB_ENABLED33#if AP_DDS_LOCAL_POSE_PUB_ENABLED34#include "geometry_msgs/msg/PoseStamped.h"35#endif // AP_DDS_LOCAL_POSE_PUB_ENABLED36#if AP_DDS_NEEDS_TWIST37#include "geometry_msgs/msg/TwistStamped.h"38#endif // AP_DDS_NEEDS_TWIST39#if AP_DDS_GPS_GLOBAL_ORIGIN_PUB_ENABLED40#include "geographic_msgs/msg/GeoPointStamped.h"41#endif // AP_DDS_GPS_GLOBAL_ORIGIN_PUB_ENABLED42#if AP_DDS_AIRSPEED_PUB_ENABLED43#include "geometry_msgs/msg/Vector3Stamped.h"44#endif // AP_DDS_AIRSPEED_PUB_ENABLED45#if AP_DDS_GEOPOSE_PUB_ENABLED46#include "geographic_msgs/msg/GeoPoseStamped.h"47#endif // AP_DDS_GEOPOSE_PUB_ENABLED48#if AP_DDS_CLOCK_PUB_ENABLED49#include "rosgraph_msgs/msg/Clock.h"50#endif // AP_DDS_CLOCK_PUB_ENABLED51#if AP_DDS_PARAMETER_SERVER_ENABLED52#include "rcl_interfaces/srv/SetParameters.h"53#include "rcl_interfaces/msg/Parameter.h"54#include "rcl_interfaces/msg/SetParametersResult.h"55#include "rcl_interfaces/msg/ParameterValue.h"56#include "rcl_interfaces/msg/ParameterType.h"57#include "rcl_interfaces/srv/GetParameters.h"58#endif //AP_DDS_PARAMETER_SERVER_ENABLED5960#include <AP_HAL/AP_HAL.h>61#include <AP_HAL/Scheduler.h>62#include <AP_HAL/Semaphores.h>6364#include "fcntl.h"6566#include <AP_Param/AP_Param.h>6768#define DDS_MTU 51269#define DDS_STREAM_HISTORY 870#define DDS_BUFFER_SIZE DDS_MTU * DDS_STREAM_HISTORY7172#if AP_DDS_UDP_ENABLED73#include <AP_HAL/utility/Socket.h>74#include <AP_Networking/AP_Networking_address.h>75#endif7677extern const AP_HAL::HAL& hal;7879class AP_DDS_Client80{8182private:8384AP_Int8 enabled;8586// Serial Allocation87uxrSession session; //Session88bool is_using_serial; // true when using serial transport8990// input and output stream91uint8_t *input_reliable_stream;92uint8_t *output_reliable_stream;93uxrStreamId reliable_in;94uxrStreamId reliable_out;9596// Outgoing Sensor and AHRS data9798#if AP_DDS_TIME_PUB_ENABLED99builtin_interfaces_msg_Time time_topic;100// The last ms timestamp AP_DDS wrote a Time message101uint64_t last_time_time_ms;102//! @brief Serialize the current time state and publish to the IO stream(s)103void write_time_topic();104static void update_topic(builtin_interfaces_msg_Time& msg);105#endif // AP_DDS_TIME_PUB_ENABLED106107#if AP_DDS_GPS_GLOBAL_ORIGIN_PUB_ENABLED108geographic_msgs_msg_GeoPointStamped gps_global_origin_topic;109// The last ms timestamp AP_DDS wrote a gps global origin message110uint64_t last_gps_global_origin_time_ms;111//! @brief Serialize the current gps global origin and publish to the IO stream(s)112void write_gps_global_origin_topic();113static void update_topic(geographic_msgs_msg_GeoPointStamped& msg);114# endif // AP_DDS_GPS_GLOBAL_ORIGIN_PUB_ENABLED115116#if AP_DDS_GOAL_PUB_ENABLED117geographic_msgs_msg_GeoPointStamped goal_topic;118// The last ms timestamp AP_DDS wrote a goal message119uint64_t last_goal_time_ms;120//! @brief Serialize the current goal and publish to the IO stream(s)121void write_goal_topic();122bool update_topic_goal(geographic_msgs_msg_GeoPointStamped& msg);123geographic_msgs_msg_GeoPointStamped prev_goal_msg;124#endif // AP_DDS_GOAL_PUB_ENABLED125126#if AP_DDS_GEOPOSE_PUB_ENABLED127geographic_msgs_msg_GeoPoseStamped geo_pose_topic;128// The last ms timestamp AP_DDS wrote a GeoPose message129uint64_t last_geo_pose_time_ms;130//! @brief Serialize the current geo_pose and publish to the IO stream(s)131void write_geo_pose_topic();132static void update_topic(geographic_msgs_msg_GeoPoseStamped& msg);133#endif // AP_DDS_GEOPOSE_PUB_ENABLED134135#if AP_DDS_LOCAL_POSE_PUB_ENABLED136geometry_msgs_msg_PoseStamped local_pose_topic;137// The last ms timestamp AP_DDS wrote a Local Pose message138uint64_t last_local_pose_time_ms;139//! @brief Serialize the current local_pose and publish to the IO stream(s)140void write_local_pose_topic();141static void update_topic(geometry_msgs_msg_PoseStamped& msg);142#endif // AP_DDS_LOCAL_POSE_PUB_ENABLED143144#if AP_DDS_LOCAL_VEL_PUB_ENABLED145geometry_msgs_msg_TwistStamped tx_local_velocity_topic;146// The last ms timestamp AP_DDS wrote a Local Velocity message147uint64_t last_local_velocity_time_ms;148//! @brief Serialize the current local velocity and publish to the IO stream(s)149void write_tx_local_velocity_topic();150static void update_topic(geometry_msgs_msg_TwistStamped& msg);151#endif // AP_DDS_LOCAL_VEL_PUB_ENABLED152153#if AP_DDS_AIRSPEED_PUB_ENABLED154geometry_msgs_msg_Vector3Stamped tx_local_airspeed_topic;155// The last ms timestamp AP_DDS wrote a airspeed message156uint64_t last_airspeed_time_ms;157//! @brief Serialize the current local airspeed and publish to the IO stream(s)158void write_tx_local_airspeed_topic();159static bool update_topic(geometry_msgs_msg_Vector3Stamped& msg);160#endif //AP_DDS_AIRSPEED_PUB_ENABLED161162#if AP_DDS_BATTERY_STATE_PUB_ENABLED163sensor_msgs_msg_BatteryState battery_state_topic;164// The last ms timestamp AP_DDS wrote a BatteryState message165uint64_t last_battery_state_time_ms;166//! @brief Serialize the current nav_sat_fix state and publish it to the IO stream(s)167void write_battery_state_topic();168static void update_topic(sensor_msgs_msg_BatteryState& msg, const uint8_t instance);169#endif // AP_DDS_BATTERY_STATE_PUB_ENABLED170171#if AP_DDS_NAVSATFIX_PUB_ENABLED172sensor_msgs_msg_NavSatFix nav_sat_fix_topic;173// The last ms timestamp AP_DDS wrote a NavSatFix message174uint64_t last_nav_sat_fix_time_ms;175//! @brief Serialize the current nav_sat_fix state and publish to the IO stream(s)176void write_nav_sat_fix_topic();177bool update_topic(sensor_msgs_msg_NavSatFix& msg, const uint8_t instance) WARN_IF_UNUSED;178#endif // AP_DDS_NAVSATFIX_PUB_ENABLED179180#if AP_DDS_IMU_PUB_ENABLED181sensor_msgs_msg_Imu imu_topic;182// The last ms timestamp AP_DDS wrote an IMU message183uint64_t last_imu_time_ms;184static void update_topic(sensor_msgs_msg_Imu& msg);185//! @brief Serialize the current IMU data and publish to the IO stream(s)186void write_imu_topic();187#endif // AP_DDS_IMU_PUB_ENABLED188189#if AP_DDS_CLOCK_PUB_ENABLED190rosgraph_msgs_msg_Clock clock_topic;191// The last ms timestamp AP_DDS wrote a Clock message192uint64_t last_clock_time_ms;193//! @brief Serialize the current clock and publish to the IO stream(s)194void write_clock_topic();195static void update_topic(rosgraph_msgs_msg_Clock& msg);196#endif // AP_DDS_CLOCK_PUB_ENABLED197198#if AP_DDS_STATUS_PUB_ENABLED199ardupilot_msgs_msg_Status status_topic;200bool update_topic(ardupilot_msgs_msg_Status& msg);201// The last ms timestamp AP_DDS wrote/checked a status message202uint64_t last_status_check_time_ms;203// last status values;204ardupilot_msgs_msg_Status last_status_msg_;205//! @brief Serialize the current status and publish to the IO stream(s)206void write_status_topic();207#endif // AP_DDS_STATUS_PUB_ENABLED208209#if AP_DDS_STATIC_TF_PUB_ENABLED210// outgoing transforms211tf2_msgs_msg_TFMessage tx_static_transforms_topic;212//! @brief Serialize the static transforms and publish to the IO stream(s)213void write_static_transforms();214static void populate_static_transforms(tf2_msgs_msg_TFMessage& msg);215#endif // AP_DDS_STATIC_TF_PUB_ENABLED216217#if AP_DDS_JOY_SUB_ENABLED218// incoming joystick data219static sensor_msgs_msg_Joy rx_joy_topic;220#endif // AP_DDS_JOY_SUB_ENABLED221#if AP_DDS_VEL_CTRL_ENABLED222// incoming REP147 velocity control223static geometry_msgs_msg_TwistStamped rx_velocity_control_topic;224#endif // AP_DDS_VEL_CTRL_ENABLED225#if AP_DDS_GLOBAL_POS_CTRL_ENABLED226// incoming REP147 goal interface global position227static ardupilot_msgs_msg_GlobalPosition rx_global_position_control_topic;228#endif // AP_DDS_GLOBAL_POS_CTRL_ENABLED229#if AP_DDS_DYNAMIC_TF_SUB_ENABLED230// incoming transforms231static tf2_msgs_msg_TFMessage rx_dynamic_transforms_topic;232#endif // AP_DDS_DYNAMIC_TF_SUB_ENABLED233HAL_Semaphore csem;234235#if AP_DDS_PARAMETER_SERVER_ENABLED236static rcl_interfaces_srv_SetParameters_Request set_parameter_request;237static rcl_interfaces_srv_SetParameters_Response set_parameter_response;238static rcl_interfaces_srv_GetParameters_Request get_parameters_request;239static rcl_interfaces_srv_GetParameters_Response get_parameters_response;240static rcl_interfaces_msg_Parameter param;241#endif242243// connection parametrics244bool status_ok{false};245bool connected{false};246247// subscription callback function248static void on_topic_trampoline(uxrSession* session, uxrObjectId object_id, uint16_t request_id, uxrStreamId stream_id, struct ucdrBuffer* ub, uint16_t length, void* args);249void on_topic(uxrSession* session, uxrObjectId object_id, uint16_t request_id, uxrStreamId stream_id, struct ucdrBuffer* ub, uint16_t length);250251// service replier callback function252static void on_request_trampoline(uxrSession* session, uxrObjectId object_id, uint16_t request_id, SampleIdentity* sample_id, ucdrBuffer* ub, uint16_t length, void* args);253void on_request(uxrSession* session, uxrObjectId object_id, uint16_t request_id, SampleIdentity* sample_id, ucdrBuffer* ub, uint16_t length);254255// delivery control parameters256uxrDeliveryControl delivery_control {257.max_samples = UXR_MAX_SAMPLES_UNLIMITED,258.max_elapsed_time = 0,259.max_bytes_per_second = 0,260.min_pace_period = 0261};262263// functions for serial transport264bool ddsSerialInit();265static bool serial_transport_open(uxrCustomTransport* args);266static bool serial_transport_close(uxrCustomTransport* transport);267static size_t serial_transport_write(uxrCustomTransport* transport, const uint8_t* buf, size_t len, uint8_t* error);268static size_t serial_transport_read(uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* error);269struct {270AP_HAL::UARTDriver *port;271uxrCustomTransport transport;272} serial;273274#if AP_DDS_UDP_ENABLED275// functions for udp transport276bool ddsUdpInit();277static bool udp_transport_open(uxrCustomTransport* args);278static bool udp_transport_close(uxrCustomTransport* transport);279static size_t udp_transport_write(uxrCustomTransport* transport, const uint8_t* buf, size_t len, uint8_t* error);280static size_t udp_transport_read(uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* error);281282struct {283AP_Int32 port;284// UDP endpoint285AP_Networking_IPV4 ip{AP_DDS_DEFAULT_UDP_IP_ADDR};286// UDP Allocation287uxrCustomTransport transport;288SocketAPM *socket;289} udp;290#endif291// pointer to transport's communication structure292uxrCommunication *comm{nullptr};293294// client key we present295static constexpr uint32_t key = 0xAAAABBBB;296297298public:299~AP_DDS_Client();300301bool start(void);302void main_loop(void);303304//! @brief Initialize the client's transport305//! @return True on successful initialization, false on failure306bool init_transport() WARN_IF_UNUSED;307308//! @brief Initialize the client's uxr session and IO stream(s)309//! @return True on successful initialization, false on failure310bool init_session() WARN_IF_UNUSED;311312//! @brief Set up the client's participants, data read/writes,313// publishers, subscribers314//! @return True on successful creation, false on failure315bool create() WARN_IF_UNUSED;316317//! @brief Update the internally stored DDS messages with latest data318void update();319320//! @brief GCS message prefix321static constexpr const char* msg_prefix = "DDS:";322323//! @brief Parameter storage324static const struct AP_Param::GroupInfo var_info[];325326//! @brief ROS_DOMAIN_ID327AP_Int32 domain_id;328329//! @brief Timeout in milliseconds when pinging the XRCE agent330AP_Int32 ping_timeout_ms;331332//! @brief Maximum number of attempts to ping the XRCE agent before exiting333AP_Int8 ping_max_retry;334335//! @brief Enum used to mark a topic as a data reader or writer336enum class Topic_rw : uint8_t {337DataReader = 0,338DataWriter = 1,339};340341//! @brief Convenience grouping for a single "channel" of data342struct Topic_table {343const uint8_t topic_id;344const uint8_t pub_id;345const uint8_t sub_id; // added sub_id fields to avoid confusion346const uxrObjectId dw_id;347const uxrObjectId dr_id; // added dr_id fields to avoid confusion348const Topic_rw topic_rw;349const char* topic_name;350const char* type_name;351const uxrQoS_t qos;352};353static const struct Topic_table topics[];354355//! @brief Enum used to mark a service as a requester or replier356enum class Service_rr : uint8_t {357Requester = 0,358Replier = 1,359};360361//! @brief Convenience grouping for a single "channel" of services362struct Service_table {363//! @brief Request ID for the service364const uint8_t req_id;365366//! @brief Reply ID for the service367const uint8_t rep_id;368369//! @brief Service is requester or replier370const Service_rr service_rr;371372//! @brief Service name as it appears in ROS373const char* service_name;374375//! @brief Service requester message type376const char* request_type;377378//! @brief Service replier message type379const char* reply_type;380381//! @brief Service requester topic name382const char* request_topic_name;383384//! @brief Service replier topic name385const char* reply_topic_name;386387//! @brief QoS for the service388const uxrQoS_t qos;389};390static const struct Service_table services[];391};392393#endif // AP_DDS_ENABLED394395396397398