def send_request(self, class_name, request_name, callback, **request_extra_data): """ Send a data request message and passes the result directly to the callback method. :return: Number of clients this message was sent to. :rtype: int :param class_name: Message class, the same as :ref:`PprzMessage.__init__` :param request_name: Request name (without the _REQ suffix) :param callback: Callback function that accepts two parameters: 1. aircraft id as int 2. The response message :param request_extra_data: Payload that will be sent with the request if any :type class_name: str :type request_name: str :type callback: Callable[[str, PprzMessage], Any] :type request_extra_data: Dict[str, Any] :raises: ValueError: if msg was invalid or `sender_id` not provided for telemetry messages :raises: RuntimeError: if the server is not running """ new_id = RequestUIDFactory.generate_uid() regex = r"^((\S*\s*)?%s %s %s( .*|$))" % (new_id, class_name, request_name) def data_request_callback(ac_id, request_id, msg): try: callback(ac_id, msg) except Exception as e: raise e finally: self.unsubscribe(binding_id) binding_id = self.subscribe(data_request_callback, regex) request_message = PprzMessage(class_name, "%s_REQ" % request_name) for k, v in request_extra_data.items(): request_message.set_value_by_name(k, v) data_request_message = ' '.join( (self.agent_name, new_id, request_message.name, request_message.payload_to_ivy_string())) return self.send(data_request_message)
def worldenv_cb(ac_id, msg): """ Callback for paparazzi WORLD_ENV requests """ # request location (in meters) east, north, up = float(msg.get_field(3)),\ float(msg.get_field(4)),\ float(msg.get_field(5)) up *= -1 # convert in km + translation with mesoNH origin weast, wnorth, wup = get_wind(east, north, up) print("wind_est:") print(weast) print(wnorth) print(wup) msg_back = PprzMessage("ground", "WORLD_ENV") msg_back.set_value_by_name("wind_east", weast) msg_back.set_value_by_name("wind_north", wnorth) msg_back.set_value_by_name("wind_up", wup) msg_back.set_value_by_name("ir_contrast", 400) msg_back.set_value_by_name("time_scale", 1) msg_back.set_value_by_name("gps_availability", 1) ivy.send(msg_back, None)
def worldenv_cb(ac_id, msg): """ Callback for paparazzi WORLD_ENV requests """ # request location (in meters) east, north, up = float(msg.get_field(3)),\ float(msg.get_field(4)),\ float(msg.get_field(5)) up *= -1 # convert in km + translation with mesoNH origin weast, wnorth, wup = get_wind(east, north, up) print("wind_est:") print(weast) print(wnorth) print(wup) msg_back=PprzMessage("ground", "WORLD_ENV") msg_back.set_value_by_name("wind_east",weast) msg_back.set_value_by_name("wind_north",wnorth) msg_back.set_value_by_name("wind_up",wup) msg_back.set_value_by_name("ir_contrast",400) msg_back.set_value_by_name("time_scale",1) msg_back.set_value_by_name("gps_availability",1) ivy.send(msg_back,None)