|
| 1 | +import numpy as np |
| 2 | +import rclpy |
| 3 | +import threading |
| 4 | +import time |
| 5 | +import sys |
| 6 | + |
| 7 | +from hal_interfaces.general.camera import CameraNode |
| 8 | +from jderobot_drones.drone_wrapper import DroneWrapper |
| 9 | + |
| 10 | +IMG_WIDTH = 320 |
| 11 | +IMG_HEIGHT = 240 |
| 12 | +freq = 30.0 |
| 13 | + |
| 14 | + |
| 15 | +# Mutes exceptions |
| 16 | +def custom_thread_excepthook(args): |
| 17 | + if "spin" in args.thread.name: |
| 18 | + return |
| 19 | + sys.__excepthook__(args.exc_type, args.exc_value, args.exc_traceback) |
| 20 | + |
| 21 | + |
| 22 | +threading.excepthook = custom_thread_excepthook |
| 23 | + |
| 24 | +### HAL INIT ### |
| 25 | + |
| 26 | +print("HAL initializing", flush=True) |
| 27 | +if not rclpy.ok(): |
| 28 | + rclpy.init() |
| 29 | + |
| 30 | + |
| 31 | +CAM_FRONTAL_TOPIC = "/" + "drone" + "/frontal_cam/image_raw" |
| 32 | +CAM_VENTRAL_TOPIC = "/" + "drone" + "/ventral_cam/image_raw" |
| 33 | + |
| 34 | +drone = DroneWrapper("drone") |
| 35 | +frontal_camera_node = CameraNode(CAM_FRONTAL_TOPIC) |
| 36 | +ventral_camera_node = CameraNode(CAM_VENTRAL_TOPIC) |
| 37 | + |
| 38 | +# Spin nodes so that subscription callbacks load topic data |
| 39 | +executor = rclpy.executors.MultiThreadedExecutor() |
| 40 | +executor.add_node(frontal_camera_node) |
| 41 | +executor.add_node(ventral_camera_node) |
| 42 | + |
| 43 | + |
| 44 | +def __auto_spin() -> None: |
| 45 | + while rclpy.ok(): |
| 46 | + try: |
| 47 | + executor.spin_once(timeout_sec=0) |
| 48 | + except Exception: |
| 49 | + pass |
| 50 | + time.sleep(1 / freq) |
| 51 | + |
| 52 | + |
| 53 | +executor_thread = threading.Thread(target=__auto_spin, daemon=True) |
| 54 | +executor_thread.start() |
| 55 | + |
| 56 | +### GETTERS ### |
| 57 | + |
| 58 | + |
| 59 | +def get_frontal_image(): |
| 60 | + image = frontal_camera_node.getImage() |
| 61 | + while image is None: |
| 62 | + image = frontal_camera_node.getImage() |
| 63 | + return image.data |
| 64 | + |
| 65 | + |
| 66 | +def get_ventral_image(): |
| 67 | + image = ventral_camera_node.getImage() |
| 68 | + while image is None: |
| 69 | + image = ventral_camera_node.getImage() |
| 70 | + return image.data |
| 71 | + |
| 72 | + |
| 73 | +def get_position(): |
| 74 | + pos = drone.get_position() |
| 75 | + return pos |
| 76 | + |
| 77 | + |
| 78 | +def get_velocity(): |
| 79 | + vel = drone.get_velocity() |
| 80 | + return vel |
| 81 | + |
| 82 | + |
| 83 | +def get_yaw_rate(): |
| 84 | + yaw_rate = drone.get_yaw_rate() |
| 85 | + return yaw_rate |
| 86 | + |
| 87 | + |
| 88 | +def get_orientation(): |
| 89 | + orientation = drone.get_orientation() |
| 90 | + return orientation |
| 91 | + |
| 92 | + |
| 93 | +def get_roll(): |
| 94 | + roll = drone.get_roll() |
| 95 | + return roll |
| 96 | + |
| 97 | + |
| 98 | +def get_pitch(): |
| 99 | + pitch = drone.get_pitch() |
| 100 | + return pitch |
| 101 | + |
| 102 | + |
| 103 | +def get_yaw(): |
| 104 | + yaw = drone.get_yaw() |
| 105 | + return yaw |
| 106 | + |
| 107 | + |
| 108 | +def get_landed_state(): |
| 109 | + state = drone.get_landed_state() |
| 110 | + return state |
| 111 | + |
| 112 | + |
| 113 | +### SETTERS ### |
| 114 | + |
| 115 | + |
| 116 | +def set_cmd_pos(x, y, z, az): |
| 117 | + drone.set_cmd_pos(x, y, z, az) |
| 118 | + |
| 119 | + |
| 120 | +def set_cmd_vel(vx, vy, vz, az): |
| 121 | + drone.set_cmd_vel(vx, vy, vz, az) |
| 122 | + |
| 123 | + |
| 124 | +def set_cmd_mix(vx, vy, z, az): |
| 125 | + drone.set_cmd_mix(vx, vy, z, az) |
| 126 | + |
| 127 | + |
| 128 | +def takeoff(h=3): |
| 129 | + drone.takeoff(h) |
| 130 | + |
| 131 | + |
| 132 | +def land(): |
| 133 | + drone.land() |
0 commit comments