wj_lidar_task.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "wj_lidar_task.h"
  2. /**
  3. * 无参构造
  4. * */
  5. Wj_lidar_Task::Wj_lidar_Task()
  6. {
  7. m_task_type = Task_type::WJ_TASK;
  8. mb_result_flag = false;
  9. }
  10. /**
  11. * 析构
  12. * */
  13. Wj_lidar_Task::~Wj_lidar_Task()
  14. {
  15. }
  16. /**
  17. * 子类初始化函数
  18. * */
  19. Error_manager Wj_lidar_Task::init()
  20. {
  21. memset(&m_command, 0, sizeof(wj_command));
  22. m_wj_measure_result.terminal_id = -1;
  23. m_wj_measure_result.correctness = false;
  24. m_wj_measure_result.x = 0;
  25. m_wj_measure_result.y = 0;
  26. m_wj_measure_result.angle = 0;
  27. m_wj_measure_result.length = 0;
  28. m_wj_measure_result.width = 0;
  29. m_wj_measure_result.height = 0;
  30. m_wj_measure_result.wheel_base = 0;
  31. m_task_statu = Task_statu::TASK_CREATED;
  32. return SUCCESS;
  33. }
  34. /**
  35. * 获取命令
  36. * */
  37. Error_manager Wj_lidar_Task::get_command(wj_command &command)
  38. {
  39. // 检查参数合法性
  40. // 若终端编号小于零,或当前时间已经超时,则返回参数非法指令
  41. if (m_command.terminal_id < 0 ||
  42. std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_command.command_time).count() >= m_command.timeout_in_milliseconds)
  43. {
  44. return Error_manager(Error_code::WJ_LIDAR_TASK_INVALID_TASK);
  45. }else{
  46. m_mutex.lock();
  47. memcpy(&command, &m_command, sizeof(wj_command));
  48. m_mutex.unlock();
  49. return Error_manager(Error_code::SUCCESS);
  50. }
  51. }
  52. /**
  53. * 设置测量命令
  54. * */
  55. Error_manager Wj_lidar_Task::set_command(wj_command command)
  56. {
  57. // 检查参数合法性
  58. // 若终端编号小于零,或当前时间已经超时,则返回参数非法指令
  59. if (command.terminal_id < 0 ||
  60. std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - command.command_time).count() >= command.timeout_in_milliseconds)
  61. {
  62. return Error_manager(Error_code::WJ_LIDAR_TASK_INVALID_TASK);
  63. }else{
  64. m_mutex.lock();
  65. memcpy(&m_command, &command, sizeof(wj_command));
  66. m_mutex.unlock();
  67. return Error_manager(Error_code::SUCCESS);
  68. }
  69. }
  70. /**
  71. * 获取万集测量结果
  72. * 若结果未被更新则返回 WJ_LIDAR_TASK_RESULT_EMPTY
  73. * */
  74. Error_manager Wj_lidar_Task::get_result(wj_measure_result &result)
  75. {
  76. if (mb_result_flag)
  77. {
  78. m_mutex.lock();
  79. memcpy(&result, &m_wj_measure_result, sizeof(m_wj_measure_result));
  80. m_mutex.unlock();
  81. return Error_manager(Error_code::SUCCESS);
  82. }
  83. else
  84. {
  85. return Error_manager(Error_code::WJ_LIDAR_TASK_EMPTY_RESULT);
  86. }
  87. }
  88. /**
  89. * 设置万集测量结果
  90. * */
  91. Error_manager Wj_lidar_Task::set_result(wj_measure_result result)
  92. {
  93. m_mutex.lock();
  94. memcpy(&m_wj_measure_result, &result, sizeof(result));
  95. m_mutex.unlock();
  96. mb_result_flag = true;
  97. return Error_manager(Error_code::SUCCESS);
  98. }
  99. bool Wj_lidar_Task::get_result_flag()
  100. {
  101. return mb_result_flag;
  102. }