main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <QCoreApplication>
  2. #include <QTime>
  3. #include <QMutex>
  4. #include "control/control_status.h"
  5. #include "control/controller.h"
  6. #include "xmlparam.h"
  7. #include "modulecomm.h"
  8. #include "ivbacktrace.h"
  9. #include "ivversion.h"
  10. #include "canmsg.pb.h"
  11. #include "decition.pb.h"
  12. #include "chassis.pb.h"
  13. #include "torquebrake.h"
  14. #include <thread>
  15. void * gpacansend;
  16. void * gpadecition;
  17. void * gpachassis;
  18. std::string gstrmemdecition;
  19. std::string gstrmemcansend;
  20. std::string gstrmemchassis;
  21. bool gbSendRun = true;
  22. bool gbChassisEPS = false;
  23. iv::brain::decition gdecition_def;
  24. iv::brain::decition gdecition;
  25. QTime gTime;
  26. int gnLastSendTime = 0;
  27. int gnLastRecvDecTime = -1000;
  28. int gnDecitionNum = 0; //when is zero,send default;
  29. const int gnDecitionNumMax = 100;
  30. int gnIndex = 0;
  31. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  32. static QMutex gMutex;
  33. // signal: @ACC_LatAngReq
  34. #define ECU_144_ACC_LatAngReq_CovFactor (0.1)
  35. // conversion value to CAN signal
  36. #define ECU_144_ACC_LatAngReq_toS(x) ((int16_t)((x) / 0.1 + 7200))
  37. // signal: @ACC_MotorTorqueMaxLimitRequest
  38. #define ECU_144_ACC_MotorTorqueMaxLimitRequest_CovFactor (0.02)
  39. // conversion value to CAN signal
  40. #define ECU_144_ACC_MotorTorqueMaxLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  41. // signal: @ACC_MotorTorqueMinLimitRequest
  42. #define ECU_144_ACC_MotorTorqueMinLimitRequest_CovFactor (0.02)
  43. // conversion value to CAN signal
  44. #define ECU_144_ACC_MotorTorqueMinLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  45. typedef struct
  46. {
  47. int16_t ACC_LatAngReq;
  48. uint8_t ADS_Reqmode;
  49. int16_t ACC_MotorTorqueMaxLimitRequest;
  50. uint8_t ACC_LatAngReqActive;
  51. int16_t ACC_MotorTorqueMinLimitRequest;
  52. uint8_t ACC_ADCReqType;
  53. } ECU_144_t;
  54. // signal: @ACC_AccTrqReq
  55. #define ECU_24B_ACC_AccTrqReq_CovFactor (1)
  56. // conversion value to CAN signal
  57. #define ECU_24B_ACC_AccTrqReq_toS(x) ((int16_t)((x) + 16384))
  58. // signal: @ACC_ACCTargetAcceleration
  59. #define ECU_24B_ACC_ACCTargetAcceleration_CovFactor (0.05)
  60. // conversion value to CAN signal
  61. #define ECU_24B_ACC_ACCTargetAcceleration_toS(x) ((int16_t)((x) / 0.05 + 200))
  62. // signal: @ACC_AEBTargetDeceleration
  63. #define ECU_24B_ACC_AEBTargetDeceleration_CovFactor (0.0005)
  64. // conversion value to CAN signal
  65. #define ECU_24B_ACC_AEBTargetDeceleration_toS(x) ((int32_t)((x) / 0.0005 + 32000))
  66. typedef struct
  67. {
  68. int16_t ACC_AccTrqReq;
  69. uint8_t ACC_AccTrqReqActive;
  70. int16_t ACC_ACCTargetAcceleration;
  71. int32_t ACC_AEBTargetDeceleration;
  72. uint8_t ACC_AEBVehilceHoldReq;
  73. uint8_t ADCReqMode;
  74. uint8_t ACC_AEBActive;
  75. uint8_t ACC_Driveoff_Request;
  76. uint8_t ACC_DecToStop;
  77. uint8_t ACC_CDDActive;
  78. uint8_t ACC_ACCMode;
  79. } ECU_24B_t;
  80. typedef struct
  81. {
  82. uint8_t ADS_UDLCTurnLightReq;
  83. } ECU_36E_t;
  84. unsigned char byte_144[8];
  85. unsigned char byte_24B[8];
  86. unsigned char byte_36E[8];
  87. ECU_144_t* _m144;
  88. ECU_24B_t* _m24B;
  89. ECU_36E_t* _m36E;
  90. void executeDecition(const iv::brain::decition decition)
  91. {
  92. // std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  93. _m144->ACC_LatAngReq = ECU_144_ACC_LatAngReq_toS(decition.wheelangle());
  94. _m144->ADS_Reqmode = 2;
  95. _m144->ACC_MotorTorqueMaxLimitRequest = ECU_144_ACC_MotorTorqueMaxLimitRequest_toS(10);
  96. _m144->ACC_LatAngReqActive = 1;
  97. _m144->ACC_MotorTorqueMinLimitRequest = ECU_144_ACC_MotorTorqueMinLimitRequest_toS(-10);
  98. _m144->ACC_ADCReqType = decition.brake_type();//ADC请求类型(制动时为1,其余情况为0)
  99. byte_144[0] |= ((_m144->ACC_LatAngReq >> 9) & (0x1FU)) | ((_m144->ADS_Reqmode & (0x07U)) << 5);
  100. byte_144[1] |= ((_m144->ACC_LatAngReq >> 1) & (0xFFU));
  101. byte_144[2] |= ((_m144->ACC_LatAngReq & (0x01U)) << 7) | ((_m144->ACC_MotorTorqueMaxLimitRequest >> 5) & (0x3FU)) | ((_m144->ACC_LatAngReqActive & (0x01U)) << 6);
  102. byte_144[3] |= ((_m144->ACC_MotorTorqueMaxLimitRequest & (0x1FU)) << 3) | ((_m144->ACC_MotorTorqueMinLimitRequest >> 8) & (0x07U));
  103. byte_144[4] |= (_m144->ACC_MotorTorqueMinLimitRequest & (0xFFU));
  104. byte_144[5] |= ((_m144->ACC_ADCReqType & (0x03U)) << 6);
  105. /*制动过程用的减速度,加速用扭矩*/
  106. _m24B->ACC_AccTrqReq = decition.niuju_y();
  107. _m24B->ACC_AccTrqReqActive = decition.acc_active();
  108. _m24B->ACC_ACCTargetAcceleration = decition.accelerator();
  109. // _m24B->ACC_AEBTargetDeceleration = 0;
  110. // _m24B->ACC_AEBVehilceHoldReq = 0;
  111. // _m24B->ADCReqMode = 0;
  112. // _m24B->ACC_AEBActive = 0;
  113. // _m24B->ACC_Driveoff_Request = 0;
  114. // _m24B->ACC_DecToStop = 0;
  115. _m24B->ACC_CDDActive = decition.brake_active();
  116. _m24B->ACC_ACCMode = decition.auto_mode();
  117. byte_24B[0] |= ((_m24B->ACC_AccTrqReq >> 7) & (0xFFU));
  118. byte_24B[1] |= ((_m24B->ACC_AccTrqReq & (0x7FU)) << 1) | (_m24B->ACC_AccTrqReqActive & (0x01U));
  119. byte_24B[2] |= ((_m24B->ACC_ACCTargetAcceleration >> 3) & (0x1FU));
  120. byte_24B[3] |= ((_m24B->ACC_ACCTargetAcceleration & (0x07U)) << 5) | ((_m24B->ACC_AEBTargetDeceleration >> 15) & (0x01U));
  121. byte_24B[4] |= ((_m24B->ACC_AEBTargetDeceleration >> 7) & (0xFFU));
  122. byte_24B[5] |= ((_m24B->ACC_AEBTargetDeceleration & (0x7FU)) << 1) | (_m24B->ACC_AEBVehilceHoldReq & (0x01U));
  123. byte_24B[6] |= ((_m24B->ADCReqMode & (0x03U)) << 1) | ((_m24B->ACC_AEBActive & (0x01U)) << 3) | ((_m24B->ACC_Driveoff_Request & (0x01U)) << 5) | ((_m24B->ACC_DecToStop & (0x01U)) << 6) | ((_m24B->ACC_CDDActive & (0x01U)) << 7);
  124. byte_24B[7] |= (_m24B->ACC_ACCMode & (0x07U));
  125. if(decition.leftlamp() == true && decition.rightlamp() == false)
  126. _m36E->ADS_UDLCTurnLightReq = 3;
  127. else if(decition.leftlamp() == false && decition.rightlamp() == true)
  128. _m36E->ADS_UDLCTurnLightReq = 4;
  129. else
  130. _m36E->ADS_UDLCTurnLightReq = 0;
  131. byte_36E[0] |= ((_m36E->ADS_UDLCTurnLightReq & (0x07U)) << 5);
  132. }
  133. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  134. {
  135. (void)index;
  136. (void)dt;
  137. (void)strmemname;
  138. iv::chassis xchassis;
  139. // static int ncount = 0;
  140. if(!xchassis.ParseFromArray(strdata,nSize))
  141. {
  142. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  143. return;
  144. }
  145. if(xchassis.epsmode() == 0)
  146. {
  147. gbChassisEPS = true;
  148. }
  149. }
  150. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  151. {
  152. (void)index;
  153. (void)dt;
  154. (void)strmemname;
  155. static qint64 oldtime = QDateTime::currentMSecsSinceEpoch();
  156. iv::brain::decition xdecition;
  157. if(!xdecition.ParseFromArray(strdata,nSize))
  158. {
  159. std::cout<<"ListenDecition parse error."<<std::endl;
  160. return;
  161. }
  162. // if(xdecition.gear() != 4)
  163. // {
  164. // qDebug("not D");
  165. // }
  166. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  167. oldtime = QDateTime::currentMSecsSinceEpoch();
  168. gMutex.lock();
  169. gdecition.CopyFrom(xdecition);
  170. gMutex.unlock();
  171. gnDecitionNum = gnDecitionNumMax;
  172. gbChassisEPS = false;
  173. }
  174. void ExecSend()
  175. {
  176. iv::can::canmsg xmsg;
  177. iv::can::canraw xraw;
  178. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  179. // qDebug("%02x %02x %02x %02x %02x %02x %02x %02x",strp[0],strp[1],strp[2],strp[3],strp[4],strp[5],strp[6],strp[7]);
  180. xraw.set_id(0x144);
  181. xraw.set_data(byte_144,8);
  182. xraw.set_bext(false);
  183. xraw.set_bremote(false);
  184. xraw.set_len(8);
  185. iv::can::canraw * pxraw144 = xmsg.add_rawmsg();
  186. pxraw144->CopyFrom(xraw);
  187. xmsg.set_channel(0);
  188. xmsg.set_index(gnIndex);
  189. xraw.set_id(0x24B);
  190. xraw.set_data(byte_144,8);
  191. xraw.set_bext(false);
  192. xraw.set_bremote(false);
  193. xraw.set_len(8);
  194. iv::can::canraw * pxraw24B = xmsg.add_rawmsg();
  195. pxraw24B->CopyFrom(xraw);
  196. xmsg.set_channel(0);
  197. xmsg.set_index(gnIndex);
  198. xraw.set_id(0x36E);
  199. xraw.set_data(byte_144,8);
  200. xraw.set_bext(false);
  201. xraw.set_bremote(false);
  202. xraw.set_len(8);
  203. iv::can::canraw * pxraw36E = xmsg.add_rawmsg();
  204. pxraw36E->CopyFrom(xraw);
  205. xmsg.set_channel(0);
  206. xmsg.set_index(gnIndex);
  207. gnIndex++;
  208. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  209. int ndatasize = xmsg.ByteSize();
  210. char * strser = new char[ndatasize];
  211. std::shared_ptr<char> pstrser;
  212. pstrser.reset(strser);
  213. if(xmsg.SerializeToArray(strser,ndatasize))
  214. {
  215. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  216. }
  217. else
  218. {
  219. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  220. }
  221. }
  222. void initial()
  223. {
  224. for (uint8_t i = 0; i < 8; i++)
  225. {
  226. byte_144[i] = 0;
  227. byte_24B[i] = 0;
  228. byte_36E[i] = 0;
  229. }
  230. }
  231. void sendthread()
  232. {
  233. initial();
  234. iv::brain::decition xdecition;
  235. while(gbSendRun)
  236. {
  237. if(gnDecitionNum <= 0)
  238. {
  239. xdecition.CopyFrom(gdecition_def);
  240. }
  241. else
  242. {
  243. gMutex.lock();
  244. xdecition.CopyFrom(gdecition);
  245. gMutex.unlock();
  246. gnDecitionNum--;
  247. }
  248. executeDecition(xdecition);
  249. if(gbChassisEPS == false) ExecSend();
  250. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  251. }
  252. }
  253. int main(int argc, char *argv[])
  254. {
  255. RegisterIVBackTrace();
  256. showversion("controller_changan_shenlan");
  257. QCoreApplication a(argc, argv);
  258. QString strpath = QCoreApplication::applicationDirPath();
  259. if(argc < 2)
  260. strpath = strpath + "/controller_changan_shenlan.xml";
  261. else
  262. strpath = argv[1];
  263. std::cout<<strpath.toStdString()<<std::endl;
  264. gdecition_def.set_accelerator(-0.5);
  265. gdecition_def.set_rightlamp(true);
  266. gdecition_def.set_wheelangle(0);
  267. gdecition_def.set_angle_mode(0);
  268. gdecition_def.set_angle_active(0);
  269. gdecition_def.set_acc_active(0);
  270. gdecition_def.set_brake_active(0);
  271. gdecition_def.set_brake_type(0);
  272. gdecition_def.set_auto_mode(0);
  273. gTime.start();
  274. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  275. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  276. gstrmemcansend = xp.GetParam("cansend","cansend0");
  277. gstrmemdecition = xp.GetParam("dection","deciton");
  278. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  279. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  280. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  281. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  282. #ifdef TORQUEBRAKETEST
  283. EnableTorqueBrakeTest();
  284. #endif
  285. std::thread xthread(sendthread);
  286. return a.exec();
  287. }