main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //#include <QCoreApplication>
  2. #include <thread>
  3. #include <mutex>
  4. #include <iostream>
  5. #include "cyber/cyber.h"
  6. #include "cyber/time/rate.h"
  7. #include "cyber/time/time.h"
  8. #include "shenlanfd.h"
  9. #include <canmsg.pb.h>
  10. #include "adchassis.pb.h"
  11. #include "canmsg.pb.h"
  12. #include "modules/common_msgs/control_msgs/control_cmd.pb.h"
  13. #include "gflags/gflags.h"
  14. DECLARE_string(controller_msg_name);
  15. DEFINE_string(controller_msg_name, "/apollo/control",
  16. "apllo controller name "
  17. "set for recve.");
  18. static bool gbthreadrun = true;
  19. static std::thread * gpthreadsend;
  20. static Shenlanfd * gpShenlanfd;
  21. static std::shared_ptr<apollo::cyber::Writer<iv::can::canmsg>>
  22. can0send_writer_;
  23. static std::shared_ptr<apollo::cyber::Reader<iv::can::canmsg>>
  24. can0recv_reader_;
  25. static std::shared_ptr<apollo::cyber::Reader<iv::chassis>>
  26. chassis_reader_;
  27. static std::shared_ptr<apollo::cyber::Reader<apollo::control::ControlCommand>>
  28. controller_reader_;
  29. static void ExecSend()
  30. {
  31. static int nCount = 0;
  32. static int gnIndex = 0;
  33. nCount++;
  34. iv::can::canmsg xmsg;
  35. iv::can::canraw xraw;
  36. unsigned char byte_1C4[32];
  37. gpShenlanfd->Get1C4(byte_1C4);
  38. xraw.set_id(0x1C4);
  39. xraw.set_data(byte_1C4,32);
  40. xraw.set_bext(false);
  41. xraw.set_bremote(false);
  42. xraw.set_len(32);
  43. iv::can::canraw * pxraw1C4 = xmsg.add_rawmsg();
  44. pxraw1C4->CopyFrom(xraw);
  45. // qDebug(" 0x144: %02X %02X %02X %02X %02X %02X %02X %02X",byte_144[0],byte_144[1],byte_144[2],byte_144[3],
  46. // byte_144[4],byte_144[5],byte_144[6],byte_144[7]);
  47. xmsg.set_channel(0);
  48. xmsg.set_index(gnIndex);
  49. unsigned char byte_24E[64];
  50. gpShenlanfd->Get1C4(byte_24E);
  51. xraw.set_id(0x24E);
  52. xraw.set_data(byte_24E,64);
  53. xraw.set_bext(false);
  54. xraw.set_bremote(false);
  55. xraw.set_len(64);
  56. if(nCount%2 == 1)
  57. {
  58. iv::can::canraw * pxraw24E = xmsg.add_rawmsg();
  59. pxraw24E->CopyFrom(xraw);
  60. }
  61. xmsg.set_channel(0);
  62. xmsg.set_index(gnIndex);
  63. unsigned char byte_25E[32];
  64. gpShenlanfd->Get1C4(byte_25E);
  65. xraw.set_id(0x25E);
  66. xraw.set_data(byte_25E,32);
  67. xraw.set_bext(false);
  68. xraw.set_bremote(false);
  69. xraw.set_len(32);
  70. // if(nCount == 10)
  71. if(nCount%2 == 1) //25Ede zhouqi shi 20ms
  72. {
  73. iv::can::canraw * pxraw25E = xmsg.add_rawmsg();
  74. pxraw25E->CopyFrom(xraw);
  75. // nCount = 0;
  76. }
  77. xmsg.set_channel(0);
  78. xmsg.set_index(gnIndex);
  79. std::shared_ptr<iv::can::canmsg> canmsg_ptr;
  80. iv::can::canmsg * pcanmsg = new iv::can::canmsg();
  81. pcanmsg->CopyFrom(xmsg);
  82. canmsg_ptr.reset(pcanmsg);
  83. can0send_writer_->Write(canmsg_ptr);
  84. nCount++;
  85. }
  86. void threadsend()
  87. {
  88. int i;
  89. while(gbthreadrun)
  90. {
  91. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  92. ExecSend();
  93. }
  94. std::cout<<" threadsendrecv exit."<<std::endl;
  95. }
  96. void RecvCANMsg(const std::shared_ptr<iv::can::canmsg> &xmsg)
  97. {
  98. }
  99. void RecvChassis(const std::shared_ptr<iv::chassis> &xchassis)
  100. {
  101. gpShenlanfd->SetSpeed(xchassis->vel());
  102. }
  103. void RecvController(const std::shared_ptr<apollo::control::ControlCommand> &xcmd)
  104. {
  105. double facc = 0;
  106. double ftorque = 0;
  107. double fbrake = xcmd->brake();
  108. double fwheelangle = 0;
  109. facc = xcmd->acceleration();
  110. double fVehWeight = 1800;
  111. double fg = 9.8;
  112. double fRollForce = 50;
  113. const double fRatio = 2.5;
  114. double fNeed = fRollForce + fVehWeight*facc;
  115. ftorque = fNeed/fRatio;
  116. if(fbrake>0.0001)
  117. {
  118. fbrake = fbrake * (-5.0) /100.0;
  119. ftorque = 0;
  120. }
  121. if(ftorque<0)ftorque = 0;
  122. // facc = xctrlcmd.linear_acceleration();
  123. fwheelangle = xcmd->steering_target() * 430.0/100.0;
  124. if(fwheelangle>430)fwheelangle = 430;
  125. if(fwheelangle<-430)fwheelangle = -430;
  126. iv::dcs xdcs;
  127. xdcs.mfBrake = fbrake;
  128. xdcs.mfTorque = ftorque;
  129. xdcs.mfWheel = fwheelangle;
  130. xdcs.mblampleft = false; xdcs.mblampright = false;
  131. if(xcmd->has_signal())
  132. {
  133. if(xcmd->signal().has_turn_signal())
  134. {
  135. if(xcmd->signal().turn_signal() == apollo::common::VehicleSignal_TurnSignal::VehicleSignal_TurnSignal_TURN_LEFT)
  136. {
  137. xdcs.mblampleft = true;
  138. }
  139. if(xcmd->signal().turn_signal() == apollo::common::VehicleSignal_TurnSignal::VehicleSignal_TurnSignal_TURN_RIGHT)
  140. {
  141. xdcs.mblampright = true;
  142. }
  143. }
  144. }
  145. gpShenlanfd->SetDecision(xdcs);
  146. }
  147. void testthread()
  148. {
  149. while(gbthreadrun)
  150. {
  151. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  152. iv::dcs xdcs;
  153. xdcs.mfBrake = 0;
  154. xdcs.mfTorque = 0;
  155. xdcs.mfWheel = 300;
  156. xdcs.mblampleft = false; xdcs.mblampright = false;
  157. gpShenlanfd->SetDecision(xdcs);
  158. }
  159. }
  160. int main(int argc, char *argv[])
  161. {
  162. // QCoreApplication a(argc, argv);
  163. google::ParseCommandLineFlags(&argc, &argv, true);
  164. std::cout<<" msg name: "<<FLAGS_controller_msg_name<<std::endl;
  165. apollo::cyber::Init("apollocontroller_shenlanfd");
  166. std::unique_ptr<apollo::cyber::Node> pilot_node = apollo::cyber::CreateNode("apollocontroller_shenlanfd");
  167. can0send_writer_ = pilot_node->CreateWriter<iv::can::canmsg>("/adc/cansend0");
  168. can0recv_reader_ = pilot_node->CreateReader<iv::can::canmsg>(
  169. "/adc/canrecv0",
  170. [](const std::shared_ptr<iv::can::canmsg> &xmsg) {
  171. RecvCANMsg(xmsg);
  172. });
  173. chassis_reader_ = pilot_node->CreateReader<iv::chassis>(
  174. "/adc/chassis",
  175. [](const std::shared_ptr<iv::chassis> &xchassis){
  176. RecvChassis(xchassis);
  177. }
  178. );
  179. controller_reader_ = pilot_node->CreateReader<apollo::control::ControlCommand>(
  180. FLAGS_controller_msg_name,
  181. [](const std::shared_ptr<apollo::control::ControlCommand> &xcmd){
  182. RecvController(xcmd);
  183. }
  184. );
  185. gpShenlanfd = new Shenlanfd();
  186. gpthreadsend = new std::thread(threadsend);
  187. // std::thread * ptestthread = new std::thread(testthread);
  188. apollo::cyber::WaitForShutdown();
  189. gbthreadrun = false;
  190. gpthreadsend->join();
  191. // ptestthread->join();
  192. delete gpShenlanfd;
  193. std::cout<<" Shut Down."<<std::endl;
  194. return 0;
  195. }