ldq.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // The MIT License (MIT)
  3. //
  4. // Copyright (c) 2019 Livox. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. // livox lidar data queue
  25. #ifndef LIVOX_ROS_DRIVER_LDQ_H_
  26. #define LIVOX_ROS_DRIVER_LDQ_H_
  27. #include <stdint.h>
  28. namespace livox_ros {
  29. const uint32_t KEthPacketMaxLength = 1500;
  30. #pragma pack(1)
  31. typedef struct {
  32. uint64_t time_rcv; /**< receive time when data arrive */
  33. uint32_t point_num;
  34. uint8_t raw_data[KEthPacketMaxLength];
  35. } StoragePacket;
  36. #pragma pack()
  37. typedef struct {
  38. StoragePacket *storage_packet;
  39. volatile uint32_t rd_idx;
  40. volatile uint32_t wr_idx;
  41. uint32_t mask;
  42. uint32_t size; /**< must be power of 2. */
  43. } LidarDataQueue;
  44. inline static bool IsPowerOf2(uint32_t size) {
  45. return (size != 0) && ((size & (size - 1)) == 0);
  46. }
  47. inline static uint32_t RoundupPowerOf2(uint32_t size) {
  48. uint32_t power2_val = 0;
  49. for (int i = 0; i < 32; i++) {
  50. power2_val = ((uint32_t)1) << i;
  51. if (size <= power2_val) {
  52. break;
  53. }
  54. }
  55. return power2_val;
  56. }
  57. /** queue operate function */
  58. int InitQueue(LidarDataQueue *queue, uint32_t queue_size);
  59. int DeInitQueue(LidarDataQueue *queue);
  60. void ResetQueue(LidarDataQueue *queue);
  61. void QueuePrePop(LidarDataQueue *queue, StoragePacket *storage_packet);
  62. void QueuePopUpdate(LidarDataQueue *queue);
  63. uint32_t QueuePop(LidarDataQueue *queue, StoragePacket *storage_packet);
  64. uint32_t QueueUsedSize(LidarDataQueue *queue);
  65. uint32_t QueueUnusedSize(LidarDataQueue *queue);
  66. uint32_t QueueIsFull(LidarDataQueue *queue);
  67. uint32_t QueueIsEmpty(LidarDataQueue *queue);
  68. uint32_t QueuePush(LidarDataQueue *queue, StoragePacket *storage_packet);
  69. uint32_t QueuePushAny(LidarDataQueue *queue, uint8_t *data, uint32_t length,
  70. uint64_t time_rcv, uint32_t point_num);
  71. } // namespace livox_ros
  72. #endif