forklift.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "space_lib/space_lib.h"
  2. #include "forklift/forklift.h"
  3. namespace forklift
  4. {
  5. FL::FL()
  6. {
  7. if (__readConf()) {
  8. sub_ = nh_.subscribe<std_msgs::String>("rudder_angle", 1, &FL::__rudderAngleCallback, this);
  9. }
  10. }
  11. FL::~FL()
  12. {}
  13. bool FL::__readConf()
  14. {
  15. common::Config conf = getConfig();
  16. std::string path = space::home() + "configure/nav_config.json";
  17. Json::Value c = space::readConfig(path);
  18. Json::Value coy = c;
  19. space::jsonValueTo(c["forklift"]["maxmal_next"], conf.maxmal_next) ?
  20. false : (c["forklift"]["maxmal_next"] = conf.maxmal_next = 0.25);
  21. space::jsonValueTo(c["forklift"]["minmal_next"], conf.minmal_next) ?
  22. false : (c["forklift"]["minmal_next"] = conf.minmal_next = 0.1);
  23. space::jsonValueTo(c["forklift"]["toler_start_theta"], conf.toler_start_theta) ?
  24. false : (c["forklift"]["toler_start_theta"] = conf.toler_start_theta = 0.1);
  25. space::jsonValueTo(c["forklift"]["toler_follow_theta"], conf.toler_follow_theta) ?
  26. false : (c["forklift"]["toler_follow_theta"] = conf.toler_follow_theta = 0.53);
  27. space::jsonValueTo(c["forklift"]["toler_theta"], conf.toler_theta) ?
  28. false : (c["forklift"]["toler_theta"] = conf.toler_theta = 0.01);
  29. space::jsonValueTo(c["forklift"]["angular_slow_p"], conf.angular_slow_p) ?
  30. false : (c["forklift"]["angular_slow_p"] = conf.angular_slow_p = 0.8);
  31. space::jsonValueTo(c["forklift"]["linear_slow_p"], conf.linear_slow_p) ?
  32. false : (c["forklift"]["linear_slow_p"] = conf.linear_slow_p = 0.3);
  33. bool ok = space::jsonValueTo(c["forklift"]["rudder_offset"], rudder_ant_.offset) &&
  34. space::jsonValueTo(c["forklift"]["rudder_rot_vel"], rudder_ant_.vel) &&
  35. space::jsonValueTo(c["forklift"]["max_rudder_angle"], rudder_ant_.max_rudder_angle) &&
  36. space::jsonValueTo(c["forklift"]["min_rudder_angle"], rudder_ant_.min_rudder_angle);
  37. space::jsonValueTo(c["forklift"]["p"], rudder_ant_.p) ?
  38. false : (c["forklift"]["p"] = rudder_ant_.p = 4.0);
  39. c["forklift"]["rudder_offset"] = rudder_ant_.offset;
  40. c["forklift"]["rudder_rot_vel"] = rudder_ant_.vel;
  41. c["forklift"]["max_rudder_angle"] = rudder_ant_.max_rudder_angle;
  42. c["forklift"]["min_rudder_angle"] = rudder_ant_.min_rudder_angle;
  43. c["forklift"]["toler_rudder_angle"] = rudder_ant_.toler_angle;
  44. rudder_ant_.angle = 0;
  45. rudder_ant_.t = ros::Time(0);
  46. if (coy != c) {
  47. space::writeConfig(path, c);
  48. }
  49. setConfig(conf);
  50. return ok;
  51. }
  52. common::NavState FL::computeVelocityCommands(common::Point& sta_pose, common::Twist& cmd)
  53. {
  54. RudderAnt ant = __getRudderAnt();
  55. common::Config conf = getConfig();
  56. if ((ros::Time::now() - ant.t).toSec() > 0.5) {
  57. return common::NavState::DATA_MISS;
  58. }
  59. common::NavState state = computeVelocity(sta_pose, cmd);
  60. float mo = rudder_ant_.offset * cmd.angular;
  61. float den = fabs(cmd.x);
  62. mo = cmd.x >= 0 ? mo : -mo;
  63. float trig = atan2(mo, den); // 预期舵轮角度
  64. float e = trig - ant.angle;
  65. float e_dis = fabs(e) + 1e-3; // 防止除0
  66. // 舵轮线速度v,角转速w,舵轮角度误差e,允许最大偏移d = 0.1
  67. // 为使车体不偏移 (e / w) * v * sin(e) < d
  68. // 则v < d / ((e / w) * sin(e)), 其中e > M_PI_2 则 e = M_PI_2
  69. float vel;
  70. if (fabs(trig) > 0.765) {
  71. vel = rudder_ant_.offset * cmd.angular / sin(trig);
  72. }
  73. else {
  74. vel = cmd.x / cos(trig);
  75. }
  76. float relia = vel * exp(-rudder_ant_.p * fabs(e));
  77. // if (fabs(e) > rudder_ant_.toler_angle) {
  78. // relia = 0;
  79. // }
  80. cmd.x = relia;// > fabs(cmd.x) ? cmd.x : (cmd.x < 0 ? -vel : vel);
  81. cmd.angular = trig;
  82. // cmd.angle = trig;
  83. cmd.x = fabs(cmd.x) < 0.5 ? cmd.x : (cmd.x > 0 ? 0.5 : -0.5);
  84. if (trig > rudder_ant_.max_rudder_angle) {
  85. cmd.angular = rudder_ant_.max_rudder_angle;
  86. // return common::NavState::HEAD_WARN;
  87. }
  88. if (trig < rudder_ant_.min_rudder_angle) {
  89. cmd.angular = rudder_ant_.min_rudder_angle;
  90. // return common::NavState::HEAD_WARN;
  91. }
  92. return state;
  93. }
  94. void FL::destroy()
  95. {
  96. sub_.shutdown();
  97. }
  98. void FL::__rudderAngleCallback(const std_msgs::StringConstPtr &msg)
  99. {
  100. RUN_LOCK()
  101. Json::Value value;
  102. space::stringTo(msg->data, value);
  103. rudder_ant_.angle = value["data"].asFloat();
  104. rudder_ant_.t = ros::Time::now();
  105. }
  106. RudderAnt FL::__getRudderAnt()
  107. {
  108. RUN_LOCK()
  109. return rudder_ant_;
  110. }
  111. }