Libmodal Pipe voxl-tflite-server aI_detection helper
-
Hey,
i'm trying to get the ai_detection_t published by voxl-flite-server, decoded in a libmodal pipe that is subscribing to the tflite_data pipe. I had a look at the examples from libmodal pipe and pipe_client_set_camera_helper_cb() from libmodal pipe.But i don't really understand how get the buffer to put it's contents into a ai_detection_t object. My attempt was the following:
Init:int ch = pipe_client_get_next_available_channel(); pipe_client_set_simple_helper_cb(ch, callback, 0); flag = CLIENT_FLAG_EN_SIMPLE_HELPER; pipe_client_open(ch,"tflite_data", "tflite_data-client", CLIENT_FLAG_EN_SIMPLE_HELPER, sizeof(ai_detection_t))
callback:
void callback(__attribute__((unused)) int ch, char* data, int bytes, void* context) { ai_detection_t detection; int bytes_read = read(bytes,(char*)&detection,sizeof(ai_detection_t)); spdlog::info("Bytes Read: {}",bytes_read); }
It's working but bytes read is always -1. Unforunately i'm not really familiar with buffers and how they work.Help would be appreciated.
-
Hi,
When you enable the simple helper you don't need to perform the reads yourself, the helper will read data from the pipe and pass it to the callback via the data pointer (the bytes field is the number of bytes the helper read). Also, while your operations in that callback will generally finish before a new detection arrives, we strongly recommend having the buffer for the pipe be larger than just 1 of the thing you're trying to deal with in case something gets backed up. We generally say to have an integer multiple of whatever you're trying to process i.e.
16*sizeof(ai_detection_t)
-
@Alex-Gardner Thanks for the answer. Is there a better way of putting the data into the ai_detection_t object?
void detection_handler(int ch, char* data, int bytes, void* context) { ai_detection_t detection; memcpy(&detection, data, bytes); }
-
When using the simple helper we usually just work with pointers and use the memory where it is:
void detection_handler(int ch, char* data, int bytes, void* context) { ai_detection_t* detections = (ai_detection_t*) data; }
Which would also let you dereference multiple if you know that there've been more than one:
void detection_handler(int ch, char* data, int bytes, void* context) { num_detections = bytes/sizeof(ai_detection_t); ai_detection_t* detections = (ai_detection_t*) data; for (int i = 0; i < num_detections; i++) { detections[i]. //Your code here } }
If you're heart set on copying it into stack memory, you can just do that with
void detection_handler(int ch, char* data, int bytes, void* context) { ai_detection_t detection = *((ai_detection_t*) data); }
-
Thank you for the Answer