ros.connector
The ROS connector module provides an interface for communication with ROS2 topics.
It defines the ROSConnector class, which handles communication with ROS2 through multiprocessing to ensure non-blocking operation. The module is responsible for:
- Providing real-time state estimation (position, orientation, velocity, ...) for drones
- Tracking objects (gates, obstacles) through ROS /tf
- Publishing commands to ROS topics asynchronously for estimator nodes and logging
The main objective for the ROSConnector is to interface with ROS2 messages with minimal latency. It uses a multiprocessing architecture to prevent ROS callbacks from blocking the main application thread. Data is shared between processes using synchronized shared memory arrays, allowing for efficient, thread-safe data access without copying.
ROSConnector(tf_names=None, estimator_names=None, cmd_topic=None, timeout=1.0, outdated_after=0.1)
¶
A non-blocking interface for ROS2 communication using multiprocessing.
The ROSConnector class provides a thread-safe way to receive data from ROS2 topics and publish commands without blocking the main application thread. It uses separate processes for subscribing to topics and publishing commands, with data shared through synchronized memory arrays.
The class supports two types of tracking:
- Raw pose tracking from
/tftopics, available intf_posandtf_quat - Full state estimation including velocity, angular velocity, and disturbance forces from an
estimator node, available in
est_pos,est_quat,est_vel, etc.
An object can be tracked both ways at the same time.
Data is accessed through property methods that convert the shared memory arrays into dictionaries keyed by object names.
Example
Track drone position and state from estimator¶
conn = ROSConnector(estimator_names=["cf1"], cmd_topic="/drones/cf1/cmd", timeout=1.0)
Access the latest position data¶
drone_pos = conn.est_pos["cf1"]
Publish a command (non-blocking)¶
conn.publish_cmd(np.array([1.0, 0.0, 0.0, 0.0]))
Clean up when done¶
conn.close()
Initialize the ROSConnector with the specified tracking objects and command topic.
Note
It is strongly recommended to use the timeout parameter to ensure that the connector has valid data for all objects. Otherwise, data may contain NaN values until the first update has been received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tf_names
|
list[str] | None
|
Names of objects to track with their raw poses from the |
None
|
estimator_names
|
list[str] | None
|
Names of objects to track with full state estimation. Can overlap with tf_names. If None, nothing will be tracked. |
None
|
cmd_topic
|
str | None
|
Topic name for publishing commands. If None, command publishing is disabled. |
None
|
timeout
|
float
|
If greater than 0, wait for position updates of all tracked objects before returning. |
1.0
|
outdated_after
|
float
|
Time after which measurements are considered outdated and property is_outdated will return true |
0.1
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If ROS is not initialized before creating the connector. |
TimeoutError
|
If updates for all tracked objects aren't received within the specified timeout period. |
Source code in drone_estimators/ros/connector.py
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 | |
active
property
¶
Check if each object has received an update already.
est_ang_vel
property
¶
The latest angular velocity data for all tracked objects.
est_disturbance
property
¶
The latest disturbance data for all tracked objects.
est_pos
property
¶
The latest estimated position data for all objects in estimator_names.
est_quat
property
¶
The latest estimated orientation data for all objects in estimator_names.
est_rotor_vel
property
¶
The latest estimated rotor velocities for all objects in estimator_names.
est_time
property
¶
The times of the last received estimates for all objects in estimator_names.
est_vel
property
¶
The latest velocity data for all tracked objects.
is_outdated
property
¶
Whether the raw pose or the estimate of each tracked object is outdated.
tf_pos
property
¶
The latest raw position data from /tf for all objects in tf_names.
tf_quat
property
¶
The latest raw orientation data from /tf for all objects in tf_names.
tf_time
property
¶
The times of the last received raw poses for all objects in tf_names.
close()
¶
Unregister the ROS subscriptions and shut down the node.
Source code in drone_estimators/ros/connector.py
publish_cmd(value)
¶
Publish a command to ROS.
This is a non-blocking function that puts the command into a multiprocessing queue. Commands are picked up by the command publisher process and published to the ROS topic asynchronously.
Note
The command publisher topic must be set on initialization. Otherwise this function will raise an error.
Source code in drone_estimators/ros/connector.py
command_publisher(queue, cmd_topic, shutdown)
¶
Publish commands from a queue to a ROS topic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queue
|
Queue
|
The queue containing commands to publish. |
required |
cmd_topic
|
str
|
The topic name. |
required |
shutdown
|
Event
|
The event to signal shutdown. |
required |
Source code in drone_estimators/ros/connector.py
disturbance_callback(data, idx, disturbance)
¶
Save the disturbance force and torque from the estimator node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
WrenchStamped
|
The WrenchStamped message. |
required |
idx
|
int
|
The index of the object the force is referring to. |
required |
disturbance
|
SynchronizedArray
|
The shared memory array for the disturbances. |
required |
Source code in drone_estimators/ros/connector.py
estimate_update(names, pos, quat, vel, ang_vel, disturbance, rotor_vel, times, shutdown)
¶
Update the shared memory arrays with the latest pose data from the estimator node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
list[str]
|
The names of the objects being tracked by the estimator. |
required |
pos
|
list[SynchronizedArray]
|
The shared memory arrays for the position data. |
required |
quat
|
list[SynchronizedArray]
|
The shared memory arrays for the orientation data. |
required |
vel
|
list[SynchronizedArray]
|
The shared memory arrays for the velocity data. |
required |
ang_vel
|
list[SynchronizedArray]
|
The shared memory arrays for the angular velocity data. |
required |
disturbance
|
list[SynchronizedArray]
|
The shared memory arrays for the disturbance data. |
required |
rotor_vel
|
list[SynchronizedArray]
|
The shared memory arrays for the rotor velocity data. |
required |
times
|
list[Synchronized]
|
The shared memory arrays for the time data. |
required |
shutdown
|
Event
|
The event to signal shutdown. |
required |
Source code in drone_estimators/ros/connector.py
pose_callback(data, idx, pos, quat, times)
¶
Save the drone pose from the estimator node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PoseStamped
|
The PoseStamped message. |
required |
idx
|
int
|
The index of the object the pose is referring to. |
required |
pos
|
SynchronizedArray
|
The shared memory array for the position values. |
required |
quat
|
SynchronizedArray
|
The shared memory array for the orientation values. |
required |
times
|
Synchronized
|
The shared memory array for the time values. |
required |
Source code in drone_estimators/ros/connector.py
rotor_vel_callback(data, idx, rotor_vel)
¶
Save the estimated rotor velocities from the estimator node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Float64MultiArray
|
The Float64MultiArray message. |
required |
idx
|
int
|
The index of the object the force is referring to. |
required |
rotor_vel
|
SynchronizedArray
|
The shared memory array for the rotor velocities. |
required |
Source code in drone_estimators/ros/connector.py
tf_callback(data, names, pos, quat, times)
¶
Save the pose data from the /tf topic into shared memory arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TFMessage
|
The TFMessage message. |
required |
names
|
list[str]
|
The names of the objects to track. |
required |
pos
|
SynchronizedArray
|
The shared memory array for the position values. |
required |
quat
|
SynchronizedArray
|
The shared memory array for the orientation values. |
required |
times
|
SynchronizedArray
|
The shared memory array for the time values. |
required |
Source code in drone_estimators/ros/connector.py
tf_update(names, pos, quat, times, shutdown)
¶
Update the shared memory arrays with the latest position and orientation data from /tf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
list[str]
|
The names of the objects to track. |
required |
pos
|
list[SynchronizedArray]
|
The shared memory arrays for the position data. |
required |
quat
|
list[SynchronizedArray]
|
The shared memory arrays for the orientation data. |
required |
times
|
list[Synchronized]
|
The shared memory arrays for the time data. |
required |
shutdown
|
Event
|
The event to signal shutdown. |
required |
Source code in drone_estimators/ros/connector.py
twist_callback(data, idx, vel, ang_vel)
¶
Save the drone twist (velocity and angular velocity) from the estimator node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TwistStamped
|
The TwistStamped message. |
required |
idx
|
int
|
The index of the object the velocity is referring to. |
required |
vel
|
SynchronizedArray
|
The shared memory array for the velocity values. |
required |
ang_vel
|
SynchronizedArray
|
The shared memory array for the angular velocity values. |
required |