main.cpp 5.4 KB

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