Skip to content

Commit 41b25e9

Browse files
committed
Sensors: set dronecan node mode and health status
Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>
1 parent 5937cb1 commit 41b25e9

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

scripts/dronecan_sensor.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010

1111
from argparse import ArgumentParser
12+
from contextlib import closing
1213

1314
GZ_VERSION_GARDEN = "garden"
1415
GZ_VERSION_HARMONIC = "harmonic"
@@ -94,8 +95,9 @@ def __init__(self, uri, node_id, rate, debug):
9495

9596
self._rpm = [0, 0, 0, 0]
9697

97-
self._node = None
9898
self._lock = threading.Lock()
99+
self._node = None
100+
self._esc_pub = None
99101
self._task_thread = threading.Thread(target=self._run)
100102
self._task_thread.start()
101103

@@ -105,19 +107,35 @@ def _run(self):
105107
node_id = self._node_id
106108
rate = self._rate
107109

110+
node_info = dronecan.uavcan.protocol.GetNodeInfo.Response()
111+
node_info.name = "org.ardupilot.gazebo"
112+
node_info.software_version.major = 0
113+
node_info.hardware_version.unique_id = b"12345"
114+
# etc.
115+
108116
# Initialise a DroneCAN node instance.
109-
self._node = dronecan.make_node(uri, node_id=node_id, bitrate=1000000)
110-
111-
# Setup to publish sensor measurement
112-
self._node.periodic(1.0 / rate, self._pub_esc_status)
113-
114-
# Running the node
115-
while True:
116-
try:
117-
self._node.spin()
118-
except dronecan.transport.TransferError as ex:
119-
print(ex)
120-
pass
117+
with closing(
118+
dronecan.make_node(
119+
uri, node_id=node_id, bitrate=1000000, node_info=node_info
120+
)
121+
) as self._node:
122+
# Setup to publish sensor measurement
123+
self._esc_pub = self._node.periodic(1.0 / rate, self._pub_esc_status)
124+
125+
# Set mode and health status
126+
self._node.mode = dronecan.uavcan.protocol.NodeStatus().MODE_OPERATIONAL
127+
self._node.health = dronecan.uavcan.protocol.NodeStatus().HEALTH_OK
128+
# self._node.vendor_specific_status_code = 12345
129+
# etc.
130+
131+
# Run the node
132+
while True:
133+
try:
134+
self._node.spin()
135+
except dronecan.UAVCANException as ex:
136+
print(f"Node error: {ex}")
137+
except dronecan.transport.TransferError as ex:
138+
print(f"Node error: {ex}")
121139

122140
def _pub_esc_status(self):
123141
with self._lock:

0 commit comments

Comments
 (0)