RobotMonitorNode.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Created by zx on 23-3-14.
  3. //
  4. #include "RobotMonitorNode.h"
  5. #include <unistd.h>
  6. RobotMonitorNode::RobotMonitorNode(std::string nodeId,std::string pubTopic,std::string subTopic)
  7. :client_(0),thread_(0),nodeId_(nodeId),pubTopic_(pubTopic),subTopic_(subTopic),exit_(true)
  8. {
  9. pose_=Pose2d(0,0,0);
  10. v_=0;
  11. vth_=0;
  12. }
  13. RobotMonitorNode::~RobotMonitorNode()
  14. {
  15. if(thread_!= nullptr)
  16. {
  17. if(thread_->joinable())
  18. {
  19. exit_=true;
  20. thread_->join();
  21. }
  22. delete thread_;
  23. thread_= nullptr;
  24. }
  25. if(client_!= nullptr){
  26. client_->disconnect();
  27. delete client_;
  28. client_= nullptr;
  29. }
  30. }
  31. bool RobotMonitorNode::Connect(std::string ip,int port)
  32. {
  33. if(client_!= nullptr)
  34. {
  35. client_->disconnect();
  36. delete client_;
  37. }
  38. client_=new Paho_client(nodeId_);
  39. bool ret= client_->connect(ip,port);
  40. if(ret)
  41. {
  42. while(!client_->isconnected()) usleep(1000);
  43. client_->subcribe(subTopic_,1,SpeedChangeCallback,this);
  44. if(thread_!= nullptr)
  45. {
  46. exit_=true;
  47. if(thread_->joinable())
  48. thread_->join();
  49. delete thread_;
  50. }
  51. exit_=false;
  52. thread_=new std::thread(&RobotMonitorNode::update,this);
  53. }
  54. return ret;
  55. }
  56. void RobotMonitorNode::update()
  57. {
  58. auto last=std::chrono::steady_clock::now();
  59. while(1)
  60. {
  61. auto now=std::chrono::steady_clock::now();
  62. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - last);
  63. double time = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  64. float add_line=v_*time;
  65. float add_angular=vth_*time;
  66. Pose2d add_p(add_line,0,0);
  67. Pose2d addxy=add_p.rotate(pose_.theta());
  68. pose_=Pose2d(addxy.x()+pose_.x(),addxy.y()+pose_.y(),pose_.theta()+add_angular);
  69. if(client_!= nullptr)
  70. {
  71. MqttMsg msg;
  72. msg.fromStatu(pose_.x(), pose_.y(), pose_.theta(),v_,vth_);
  73. client_->publish(pubTopic_,1,msg);
  74. //printf("pub topic(%s):%s length:%d\n",pubTopic_.c_str(),msg.data(),msg.length());
  75. }
  76. last=now;
  77. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  78. }
  79. }
  80. void RobotMonitorNode::ChangeSpeed(double v ,double vth)
  81. {
  82. printf(" node:%s change speed :(%.5f,%.5f)\n",nodeId_.c_str(),v,vth);
  83. v_=v;
  84. vth_=vth;
  85. }
  86. void RobotMonitorNode::SpeedChangeCallback(std::string topic,int QOS,MqttMsg& msg,void* context)
  87. {
  88. RobotMonitorNode* robot=(RobotMonitorNode*)context;
  89. double v=0,vth=0;
  90. msg.toSpeed(v,vth);
  91. robot->ChangeSpeed(v,vth);
  92. }