|
| 1 | +""" |
| 2 | +Publish sensor data using DroneCAN |
| 3 | +""" |
| 4 | + |
| 5 | +import dronecan |
| 6 | +import threading |
| 7 | +import time |
| 8 | + |
| 9 | +GZ_VERSION_GARDEN = "garden" |
| 10 | +GZ_VERSION_HARMONIC = "harmonic" |
| 11 | +GZ_VERSION_IONIC = "ionic" |
| 12 | + |
| 13 | +GZ_VERSION = GZ_VERSION_HARMONIC |
| 14 | + |
| 15 | +if GZ_VERSION == GZ_VERSION_GARDEN: |
| 16 | + from gz.msgs9.fluid_pressure_pb2 import FluidPressure |
| 17 | + from gz.msgs9.air_speed_pb2 import AirSpeed |
| 18 | + from gz.msgs9.altimeter_pb2 import Altimeter |
| 19 | + from gz.msgs9.magnetometer_pb2 import Magnetometer |
| 20 | + from gz.msgs9.navsat_pb2 import NavSat |
| 21 | + from gz.msgs9.imu_pb2 import IMU |
| 22 | + |
| 23 | + from gz.transport12 import Node |
| 24 | + |
| 25 | +elif GZ_VERSION == GZ_VERSION_HARMONIC: |
| 26 | + from gz.msgs10.fluid_pressure_pb2 import FluidPressure |
| 27 | + from gz.msgs10.air_speed_pb2 import AirSpeed |
| 28 | + from gz.msgs10.altimeter_pb2 import Altimeter |
| 29 | + from gz.msgs10.magnetometer_pb2 import Magnetometer |
| 30 | + from gz.msgs10.navsat_pb2 import NavSat |
| 31 | + from gz.msgs10.imu_pb2 import IMU |
| 32 | + |
| 33 | + from gz.transport13 import Node |
| 34 | + |
| 35 | +elif GZ_VERSION == GZ_VERSION_IONIC: |
| 36 | + from gz.msgs11.fluid_pressure_pb2 import FluidPressure |
| 37 | + from gz.msgs11.air_speed_pb2 import AirSpeed |
| 38 | + from gz.msgs11.altimeter_pb2 import Altimeter |
| 39 | + from gz.msgs11.magnetometer_pb2 import Magnetometer |
| 40 | + from gz.msgs11.navsat_pb2 import NavSat |
| 41 | + from gz.msgs11.imu_pb2 import IMU |
| 42 | + |
| 43 | + from gz.transport14 import Node |
| 44 | + |
| 45 | + |
| 46 | +def sensor_topics(): |
| 47 | + world = "iris_runway" |
| 48 | + model = "iris_with_gimbal" |
| 49 | + sub_model = "iris_with_standoffs" |
| 50 | + link = "base_link" |
| 51 | + |
| 52 | + topic = ( |
| 53 | + f"/world/{world}/model/{model}" |
| 54 | + f"/model/{sub_model}/link/{link}" |
| 55 | + f"/sensor/air_pressure_sensor/air_pressure" |
| 56 | + ) |
| 57 | + |
| 58 | + topic = ( |
| 59 | + f"/world/{world}/model/{model}" |
| 60 | + f"/model/{sub_model}/link/base_link" |
| 61 | + f"/sensor/air_speed_sensor/air_speed" |
| 62 | + ) |
| 63 | + |
| 64 | + topic = ( |
| 65 | + f"/world/{world}/model/{model}" |
| 66 | + f"/model/{sub_model}/link/{link}" |
| 67 | + f"/sensor/altimeter_sensor/altimeter" |
| 68 | + ) |
| 69 | + |
| 70 | + topic = ( |
| 71 | + f"/world/{world}/model/{model}" |
| 72 | + f"/model/{sub_model}/link/{link}" |
| 73 | + f"/sensor/magnetometer_sensor/magnetometer" |
| 74 | + ) |
| 75 | + |
| 76 | + topic = ( |
| 77 | + f"/world/{world}/model/{model}" |
| 78 | + f"/model/{sub_model}/link/{link}" |
| 79 | + f"/sensor/navsat_sensor/navsat" |
| 80 | + ) |
| 81 | + |
| 82 | + link = "imu_link" |
| 83 | + |
| 84 | + topic = ( |
| 85 | + f"/world/{world}/model/{model}" |
| 86 | + f"/model/{sub_model}/link/{link}" |
| 87 | + f"/sensor/imu_sensor/imu" |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +class SensorSubscribers: |
| 92 | + def __init__(self): |
| 93 | + |
| 94 | + world = "iris_runway" |
| 95 | + model = "iris_with_gimbal" |
| 96 | + sub_model = "iris_with_standoffs" |
| 97 | + link = "base_link" |
| 98 | + |
| 99 | + self._lock = threading.Lock() |
| 100 | + self._node = Node() |
| 101 | + |
| 102 | + # Magnetometer |
| 103 | + self._sensor_do_print_msg = True |
| 104 | + self._sensor_msg = None |
| 105 | + self._sensor_topic = ( |
| 106 | + f"/world/{world}/model/{model}" |
| 107 | + f"/model/{sub_model}/link/{link}" |
| 108 | + f"/sensor/magnetometer_sensor/magnetometer" |
| 109 | + ) |
| 110 | + |
| 111 | + self._sensor_sub = self._node.subscribe( |
| 112 | + Magnetometer, self._sensor_topic, self._sensor_cb |
| 113 | + ) |
| 114 | + |
| 115 | + def _sensor_cb(self, msg): |
| 116 | + with self._lock: |
| 117 | + self._sensor_msg = msg |
| 118 | + do_print_msg = self._sensor_do_print_msg |
| 119 | + |
| 120 | + if do_print_msg: |
| 121 | + print(msg) |
| 122 | + |
| 123 | + |
| 124 | +class DroneCANNode(): |
| 125 | + def __init__(self): |
| 126 | + pass |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +def main(): |
| 133 | + sensor_topics() |
| 134 | + |
| 135 | + sensor_subs = SensorSubscribers() |
| 136 | + |
| 137 | + while True: |
| 138 | + time.sleep(0.01) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + main() |
0 commit comments