main.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. static bool gbHaveVehSpd = false;
  32. static double gfVehSpd = 0.0;
  33. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  34. static QMutex gMutex;
  35. // signal: @ACC_LatAngReq //更改CANid
  36. #define ECU_1C4_ACC_LatAngReq_CovFactor (0.1)
  37. // conversion value to CAN signal
  38. #define ECU_1C4_ACC_LatAngReq_toS(x) ((int16_t)((x) / 0.1 + 7200))
  39. // signal: @ACC_MotorTorqueMaxLimitRequest
  40. #define ECU_1C4_ACC_MotorTorqueMaxLimitRequest_CovFactor (0.02)
  41. // conversion value to CAN signal
  42. #define ECU_1C4_ACC_MotorTorqueMaxLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  43. // signal: @ACC_MotorTorqueMinLimitRequest
  44. #define ECU_1C4_ACC_MotorTorqueMinLimitRequest_CovFactor (0.02)
  45. // conversion value to CAN signal
  46. #define ECU_1C4_ACC_MotorTorqueMinLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  47. typedef struct
  48. {
  49. int16_t ACC_LatAngReq;
  50. //uint8_t ADS_Reqmode; //20221102, 新车没有此信号
  51. int16_t ACC_MotorTorqueMaxLimitRequest;
  52. uint8_t ACC_LatAngReqActive;
  53. int16_t ACC_MotorTorqueMinLimitRequest;
  54. //uint8_t ACC_ADCReqType; //20221102, 新车没有此信号
  55. } ECU_1C4_t;
  56. // signal: @ACC_AccTrqReq
  57. #define ECU_24E_ACC_AccTrqReq_CovFactor (1)
  58. // conversion value to CAN signal
  59. #define ECU_24E_ACC_AccTrqReq_toS(x) ((int16_t)((x) + 16384))
  60. // signal: @ACC_ACCTargetAcceleration
  61. #define ECU_24E_ACC_ACCTargetAcceleration_CovFactor (0.05)
  62. // conversion value to CAN signal
  63. #define ECU_24E_ACC_ACCTargetAcceleration_toS(x) ((int16_t)((x) / 0.05 + 200))
  64. // signal: @ACC_AEBTargetDeceleration
  65. #define ECU_24E_ACC_AEBTargetDeceleration_CovFactor (0.0005)
  66. // conversion value to CAN signal
  67. #define ECU_24E_ACC_AEBTargetDeceleration_toS(x) ((int32_t)((x) / 0.0005 + 32000))
  68. static int gnState = 0; //0 not act 1 act
  69. typedef struct
  70. {
  71. int16_t ACC_AccTrqReq;
  72. uint8_t ACC_AccTrqReqActive;
  73. int16_t ACC_ACCTargetAcceleration;
  74. int32_t ACC_AEBTargetDeceleration;
  75. uint8_t ACC_AEBVehilceHoldReq;
  76. uint8_t ADCReqMode;
  77. uint8_t ACC_AEBActive;
  78. uint8_t ACC_Driveoff_Request;
  79. uint8_t ACC_DecToStop;
  80. uint8_t ACC_CDDActive;
  81. uint8_t ACC_ACCMode;
  82. } ECU_24E_t;
  83. typedef struct
  84. {
  85. uint8_t ADS_UDLCTurnLightReq;
  86. } ECU_36E_t;
  87. unsigned char byte_1C4[64];//byte_144[8];
  88. unsigned char byte_24E[64];
  89. unsigned char byte_36E[64];
  90. ECU_1C4_t _m1C4 = {0,0,0,0};
  91. ECU_24E_t _m24E = {0,0,0,0,0,0,0,0,0,0,0};
  92. ECU_36E_t _m36E = {0};
  93. void ExecSend();
  94. void executeDecition(const iv::brain::decition &decition)
  95. {
  96. static int xieya = 50;
  97. // std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  98. // std::cout<<"angle_mode is "<<decition.angle_mode()<<" angle_active is "<<decition.angle_active()<<std::endl;
  99. // std::cout<<"brake_type is "<<decition.brake_type()<<" acc_active is "<<decition.acc_active()<<std::endl;
  100. // std::cout<<"brake is "<<decition.brake()<<" brake_active is "<<decition.brake_active()<<std::endl;
  101. // std::cout<<"auto_mode is "<<decition.auto_mode()<<" rightlamp is "<<decition.rightlamp()<<std::endl;
  102. _m1C4.ACC_LatAngReq = ECU_1C4_ACC_LatAngReq_toS(decition.wheelangle());
  103. //_m1C4.ADS_Reqmode = decition.angle_mode(); //20221102,新车没有此信号
  104. _m1C4.ACC_MotorTorqueMaxLimitRequest = ECU_1C4_ACC_MotorTorqueMaxLimitRequest_toS(10);
  105. _m1C4.ACC_LatAngReqActive = decition.angle_active();
  106. _m1C4.ACC_MotorTorqueMinLimitRequest = ECU_1C4_ACC_MotorTorqueMinLimitRequest_toS(-10);
  107. // _m144.ACC_ADCReqType = decition.brake_type();//ADC请求类型(制动时为1,其余情况为0)
  108. if(decition.brake()>(-0.0000001))
  109. {
  110. //_m144.ACC_ADCReqType = 0;//ADC请求类型(制动时为1,其余情况为0)//20221102,新车没有此信号
  111. // _m24B.ADCReqMode = 0;//20221102,新车没有此信号
  112. _m24E.ACC_DecToStop =0;
  113. }
  114. else
  115. {
  116. //_m144.ACC_ADCReqType = 1;//20221102,新车没有此信号
  117. // _m24B.ADCReqMode = 1;//20221102,新车没有此信号
  118. _m24E.ACC_DecToStop =1;
  119. }
  120. // std::cout<<" type: "<<(int)_m144.ACC_ADCReqType<<std::endl;
  121. /*制动过程用的减速度,加速用扭矩*/
  122. _m24E.ACC_AccTrqReq = ECU_24E_ACC_AccTrqReq_toS(decition.torque());
  123. _m24E.ACC_AccTrqReqActive = decition.acc_active();
  124. if(decition.brake()<(-1.5))
  125. {
  126. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(-1.5);
  127. }
  128. else
  129. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(decition.brake());
  130. // std::cout<<" brake : "<<decition.brake()<<std::endl;
  131. // std::cout<<"brake acctive. "<<decition.brake_active()<<std::endl;
  132. if(decition.brake()>(-0.0000001))
  133. {
  134. if(xieya > 0)
  135. {
  136. _m24E.ACC_CDDActive = 1;
  137. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0.5);
  138. _m24E.ACC_AccTrqReq = ECU_24E_ACC_AccTrqReq_toS(0);
  139. _m24E.ACC_Driveoff_Request = 1;
  140. //_m144.ACC_ADCReqType = 1;//20221102,新车没有此信号
  141. //_m24B.ADCReqMode = 1; //20221102,新车没有此信号
  142. _m24E.ACC_DecToStop =1;
  143. xieya--;
  144. std::cout<<" xieya. now veh speed: "<<gfVehSpd<<std::endl;
  145. }
  146. else
  147. {
  148. _m24E.ACC_CDDActive = 0;
  149. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0);
  150. _m24E.ACC_Driveoff_Request = 0;
  151. }
  152. }
  153. else
  154. {
  155. _m24E.ACC_CDDActive = 1;
  156. _m24E.ACC_Driveoff_Request = 0;
  157. }
  158. // _m24B.ACC_CDDActive = decition.brake_active();
  159. // std::cout<<" req mode: "<<_m144.ADS_Reqmode<<std::endl;
  160. //byte_144[0] = ((_m144.ACC_LatAngReq >> 9) & (0x1FU)) | ((_m144.ADS_Reqmode & (0x07U)) << 5);
  161. // qDebug(" req mode: %d byte: %02X ",_m144.ADS_Reqmode, byte_144[0]);
  162. // byte_144[1] = ((_m144.ACC_LatAngReq >> 1) & (0xFFU));
  163. //byte_144[2] = ((_m144.ACC_LatAngReq & (0x01U)) << 7) | ((_m144.ACC_MotorTorqueMaxLimitRequest >> 5) & (0x3FU)) | ((_m144.ACC_LatAngReqActive & (0x01U)) << 6);
  164. //byte_144[3] = ((_m144.ACC_MotorTorqueMaxLimitRequest & (0x1FU)) << 3) | ((_m144.ACC_MotorTorqueMinLimitRequest >> 8) & (0x07U));
  165. //byte_144[4] = (_m144.ACC_MotorTorqueMinLimitRequest & (0xFFU));
  166. //byte_144[5] = ((_m144.ACC_ADCReqType & (0x03U)) << 6);
  167. byte_1C4[0] = ((_m1C4.ACC_MotorTorqueMaxLimitRequest >> 3) & (0xFFU));
  168. // qDebug(" req mode: %d byte: %02X ",_m144.ADS_Reqmode, byte_144[0]);
  169. byte_1C4[1] = ((_m1C4.ACC_MotorTorqueMaxLimitRequest & (0x07U)) << 5) | ((_m1C4.ACC_MotorTorqueMinLimitRequest >> 6) & (0x1FU));
  170. byte_1C4[2] = ((_m1C4.ACC_MotorTorqueMinLimitRequest & (0x3FU)) << 2) | ((_m1C4.ACC_LatAngReq >> 12) & (0x03U));
  171. byte_1C4[3] = (((_m1C4.ACC_LatAngReq>>4 )& (0xFFU)));
  172. // byte_1C4[4] = (((_m1C4.ACC_LatAngReq<<4) & (0xF0U)))|((_m1C4.ACC_LatAngReqActive&(0x01U))<<3);
  173. byte_1C4[4] = (((_m1C4.ACC_LatAngReq)& (0x0fU))<<4)|((_m1C4.ACC_LatAngReqActive&(0x01U))<<3);
  174. //byte_1C4[5] = ((_m144.ACC_ADCReqType & (0x03U)) << 6);
  175. // _m24B.ACC_AEBTargetDeceleration = 0;
  176. // _m24B.ACC_AEBVehilceHoldReq = 0;
  177. // _m24B.ADCReqMode = 0;
  178. // _m24B.ACC_AEBActive = 0;
  179. // _m24B.ACC_Driveoff_Request = 0;
  180. // _m24B.ACC_DecToStop = 0;
  181. // std::cout<<" brake : "<<decition.brake()<<std::endl;
  182. // std::cout<<"brake acctive. "<<decition.brake_active()<<std::endl;
  183. if(decition.brake()>(-0.0000001))
  184. {
  185. _m24E.ACC_CDDActive = 0;
  186. _m24E.ACC_Driveoff_Request = 1;
  187. if(xieya > 0)
  188. {
  189. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0.5);
  190. xieya--;
  191. }
  192. else
  193. {
  194. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0);
  195. }
  196. _m24E.ACC_AccTrqReqActive = 1;
  197. _m24E.ACC_DecToStop = 0;
  198. }
  199. else
  200. {
  201. _m24E.ACC_CDDActive = 1;
  202. _m24E.ACC_Driveoff_Request = 0;
  203. if(gbHaveVehSpd)
  204. {
  205. if(gfVehSpd < 0.01)
  206. {
  207. if(xieya != 50)std::cout<<"need xieya. "<<std::endl;
  208. xieya = 50;
  209. }
  210. }
  211. _m24E.ACC_AccTrqReqActive = 0;
  212. _m24E.ACC_DecToStop = 1;
  213. }
  214. // _m24B.ACC_CDDActive = decition.brake_active();
  215. _m24E.ACC_ACCMode = decition.auto_mode();
  216. if(gnState == 0)
  217. {
  218. _m24E.ACC_CDDActive = 0;
  219. _m24E.ACC_Driveoff_Request = 0;
  220. _m24E.ACC_ACCMode = 0;
  221. _m24E.ACC_ACCTargetAcceleration = 0;
  222. _m24E.ACC_AccTrqReqActive = 0;
  223. _m24E.ACC_DecToStop = 0;
  224. }
  225. // std::cout<<"acc mode: "<<(int)_m24B.ACC_ACCMode<<std::endl;
  226. // byte_24B[0] = ((_m24B.ACC_AccTrqReq >> 7) & (0xFFU));
  227. //byte_24B[1] = ((_m24B.ACC_AccTrqReq & (0x7FU)) << 1) | (_m24B.ACC_AccTrqReqActive & (0x01U));
  228. //byte_24B[2] = ((_m24B.ACC_ACCTargetAcceleration >> 3) & (0x1FU));
  229. //byte_24B[3] = ((_m24B.ACC_ACCTargetAcceleration & (0x07U)) << 5) | ((_m24B.ACC_AEBTargetDeceleration >> 15) & (0x01U));
  230. //byte_24B[4] = ((_m24B.ACC_AEBTargetDeceleration >> 7) & (0xFFU));
  231. //byte_24B[5] = ((_m24B.ACC_AEBTargetDeceleration & (0x7FU)) << 1) | (_m24B.ACC_AEBVehilceHoldReq & (0x01U));
  232. //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);
  233. //byte_24B[7] = (_m24B.ACC_ACCMode & (0x07U));
  234. byte_24E[0] = ((_m24E.ACC_ACCTargetAcceleration) & (0xFFU));
  235. //byte_24E[1] = ((_m24B.ACC_AccTrqReq & (0x7FU)) << 1) | (_m24B.ACC_AccTrqReqActive & (0x01U));
  236. //byte_24E[2] = ((_m24B.ACC_ACCTargetAcceleration >> 3) & (0x1FU));
  237. byte_24E[3] = ((_m24E.ACC_DecToStop & (0x01U)) << 7);// | ((_m24B.ACC_AEBTargetDeceleration >> 15) & (0x01U));
  238. //byte_24E[4] = ((_m24B.ACC_AEBTargetDeceleration >> 7) & (0xFFU));
  239. byte_24E[5] = ((_m24E.ACC_CDDActive & (0x01U)) << 4);//((_m24E.ACC_AEBTargetDeceleration & (0x7FU)) << 1) | (_m24E.ACC_AEBVehilceHoldReq & (0x01U));
  240. byte_24E[6] = ((_m24E.ACC_Driveoff_Request & (0x01U)) << 7)|((_m24E.ACC_ACCMode & (0x07U))<<4);//((_m24E.ADCReqMode & (0x03U)) << 1) | ((_m24E.ACC_AEBActive & (0x01U)) << 3) | ((_m24E.ACC_Driveoff_Request & (0x01U)) << 5) | ((_m24E.ACC_DecToStop & (0x01U)) << 6) | ((_m24E.ACC_CDDActive & (0x01U)) << 7);
  241. byte_24E[8] = ((_m24E.ACC_AEBTargetDeceleration>>8) & (0xFFU));
  242. byte_24E[9] = ((_m24E.ACC_AEBTargetDeceleration) & (0xFFU));
  243. byte_24E[10] = ((_m24E.ACC_AEBActive & (0x01U)) << 7);
  244. byte_24E[11] = ((_m24E.ACC_AccTrqReq>>13 )& (0x03U));
  245. byte_24E[12] = ((_m24E.ACC_AccTrqReq>>5 )& (0xFFU));
  246. //byte_24E[13] = ((_m24E.ACC_AccTrqReq & (0x1FU)<<3))| ((_m24E.ACC_AccTrqReqActive & (0x01U)) << 2);
  247. byte_24E[13] = (((_m24E.ACC_AccTrqReq & (0x1FU))<<3))| ((_m24E.ACC_AccTrqReqActive & (0x01U)) << 2);
  248. // if(decition.leftlamp() == true && decition.rightlamp() == false)
  249. // _m36E.ADS_UDLCTurnLightReq = 3;
  250. // else if(decition.leftlamp() == false && decition.rightlamp() == true)
  251. // _m36E.ADS_UDLCTurnLightReq = 4;
  252. // else
  253. // _m36E.ADS_UDLCTurnLightReq = 0;
  254. // byte_36E[0] = ((_m36E.ADS_UDLCTurnLightReq & (0x07U)) << 5);
  255. }
  256. void Activate()
  257. {
  258. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  259. iv::brain::decition xdecition;
  260. // for(int j=0;j<100000;j++)
  261. // {
  262. std::cout<<" run "<<std::endl;
  263. for(int i = 0; i < 10; i++){
  264. xdecition.set_wheelangle(0.0);
  265. xdecition.set_angle_mode(0);
  266. xdecition.set_angle_active(0);
  267. xdecition.set_acc_active(0);
  268. xdecition.set_brake_active(0);
  269. // xdecition.set_brake_type(0);
  270. xdecition.set_auto_mode(0);
  271. gnState = 0;
  272. executeDecition(xdecition);
  273. ExecSend();
  274. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  275. }
  276. for(int i = 0; i < 10; i++){
  277. xdecition.set_wheelangle(0.0);
  278. xdecition.set_angle_mode(1);
  279. xdecition.set_angle_active(1);
  280. xdecition.set_acc_active(1);
  281. xdecition.set_brake_active(1);
  282. // xdecition.set_brake_type(1);
  283. xdecition.set_auto_mode(3);
  284. gnState = 1;
  285. executeDecition(xdecition);
  286. ExecSend();
  287. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  288. }
  289. // }
  290. }
  291. void UnAcitvate()
  292. {
  293. iv::brain::decition xdecition;
  294. for(int i = 0; i < 10; i++){
  295. xdecition.set_wheelangle(0);
  296. xdecition.set_angle_mode(1);
  297. xdecition.set_angle_active(1);
  298. xdecition.set_acc_active(1);
  299. xdecition.set_brake_active(1);
  300. // xdecition.set_brake_type(1);
  301. xdecition.set_auto_mode(3);
  302. gnState = 1;
  303. executeDecition(xdecition);
  304. ExecSend();
  305. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  306. }
  307. for(int i = 0; i < 10; i++){
  308. xdecition.set_wheelangle(0);
  309. xdecition.set_angle_mode(0);
  310. xdecition.set_angle_active(0);
  311. xdecition.set_acc_active(0);
  312. xdecition.set_brake_active(0);
  313. // xdecition.set_brake_type(0);
  314. gnState = 0;
  315. xdecition.set_auto_mode(0);
  316. executeDecition(xdecition);
  317. ExecSend();
  318. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  319. }
  320. }
  321. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  322. {
  323. (void)index;
  324. (void)dt;
  325. (void)strmemname;
  326. iv::chassis xchassis;
  327. // static int ncount = 0;
  328. if(!xchassis.ParseFromArray(strdata,nSize))
  329. {
  330. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  331. return;
  332. }
  333. if(xchassis.has_epsmode())
  334. {
  335. if(xchassis.epsmode() == 0)
  336. {
  337. gbChassisEPS = true;
  338. }
  339. }
  340. if(xchassis.has_vel())
  341. {
  342. gfVehSpd = xchassis.vel();
  343. gbHaveVehSpd = true;
  344. // std::cout<<" gf Veh speed : "<<gfVehSpd<<std::endl;
  345. }
  346. }
  347. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  348. {
  349. (void)index;
  350. (void)dt;
  351. (void)strmemname;
  352. static qint64 oldtime = QDateTime::currentMSecsSinceEpoch();
  353. iv::brain::decition xdecition;
  354. if(!xdecition.ParseFromArray(strdata,nSize))
  355. {
  356. std::cout<<"ListenDecition parse error."<<std::endl;
  357. return;
  358. }
  359. // if(xdecition.gear() != 4)
  360. // {
  361. // qDebug("not D");
  362. // }
  363. xdecition.set_angle_mode(1);
  364. xdecition.set_angle_active(1);
  365. xdecition.set_acc_active(1);
  366. xdecition.set_brake_active(1);
  367. // xdecition.set_brake_type(1);
  368. xdecition.set_auto_mode(3);
  369. // xdecition.set_wheelangle(45.0);
  370. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  371. oldtime = QDateTime::currentMSecsSinceEpoch();
  372. gMutex.lock();
  373. gdecition.CopyFrom(xdecition);
  374. gMutex.unlock();
  375. gnDecitionNum = gnDecitionNumMax;
  376. gbChassisEPS = false;
  377. }
  378. void ExecSend()
  379. {
  380. static int nCount = 0;
  381. nCount++;
  382. iv::can::canmsg xmsg;
  383. iv::can::canraw xraw;
  384. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  385. // 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]);
  386. xraw.set_id(0x1C4);
  387. xraw.set_data(byte_1C4,32);
  388. xraw.set_bext(false);
  389. xraw.set_bremote(false);
  390. xraw.set_len(32);
  391. iv::can::canraw * pxraw1C4 = xmsg.add_rawmsg();
  392. pxraw1C4->CopyFrom(xraw);
  393. // qDebug(" 0x144: %02X %02X %02X %02X %02X %02X %02X %02X",byte_144[0],byte_144[1],byte_144[2],byte_144[3],
  394. // byte_144[4],byte_144[5],byte_144[6],byte_144[7]);
  395. xmsg.set_channel(0);
  396. xmsg.set_index(gnIndex);
  397. xraw.set_id(0x24E);
  398. xraw.set_data(byte_24E,64);
  399. xraw.set_bext(false);
  400. xraw.set_bremote(false);
  401. xraw.set_len(64);
  402. if(nCount%2 == 1)
  403. {
  404. iv::can::canraw * pxraw24E = xmsg.add_rawmsg();
  405. pxraw24E->CopyFrom(xraw);
  406. }
  407. xmsg.set_channel(0);
  408. xmsg.set_index(gnIndex);
  409. // xraw.set_id(0x36E);
  410. // xraw.set_data(byte_36E,8);
  411. // xraw.set_bext(false);
  412. // xraw.set_bremote(false);
  413. // xraw.set_len(8);
  414. // if(nCount == 10)
  415. // {
  416. // iv::can::canraw * pxraw36E = xmsg.add_rawmsg();
  417. // pxraw36E->CopyFrom(xraw);
  418. // nCount = 0;
  419. // }
  420. // xmsg.set_channel(0);
  421. // xmsg.set_index(gnIndex);
  422. gnIndex++;
  423. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  424. int ndatasize = xmsg.ByteSize();
  425. char * strser = new char[ndatasize];
  426. std::shared_ptr<char> pstrser;
  427. pstrser.reset(strser);
  428. if(xmsg.SerializeToArray(strser,ndatasize))
  429. {
  430. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  431. }
  432. else
  433. {
  434. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  435. }
  436. }
  437. void initial()
  438. {
  439. for (uint8_t i = 0; i < 64; i++) //CAN to canfd
  440. {
  441. byte_1C4[i] = 0;
  442. byte_24E[i] = 0;
  443. //byte_36E[i] = 0;
  444. }
  445. }
  446. void sendthread()
  447. {
  448. initial();
  449. iv::brain::decition xdecition;
  450. UnAcitvate();
  451. // UnAcitvate();
  452. int nstate = 0; //0 Un 1 Activate
  453. // Activate();
  454. while(gbSendRun)
  455. {
  456. if(gnDecitionNum <= 0)
  457. {
  458. if(nstate == 1)
  459. {
  460. UnAcitvate();
  461. nstate = 0;
  462. }
  463. xdecition.CopyFrom(gdecition_def);
  464. }
  465. else
  466. {
  467. if(nstate == 0)
  468. {
  469. Activate();
  470. nstate = 1;
  471. }
  472. gMutex.lock();
  473. xdecition.CopyFrom(gdecition);
  474. gMutex.unlock();
  475. gnDecitionNum--;
  476. }
  477. executeDecition(xdecition);
  478. if(gbChassisEPS == false) ExecSend();
  479. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  480. }
  481. }
  482. int main(int argc, char *argv[])
  483. {
  484. RegisterIVBackTrace();
  485. showversion("controller_changan_shenlan");
  486. QCoreApplication a(argc, argv);
  487. QString strpath = QCoreApplication::applicationDirPath();
  488. if(argc < 2)
  489. strpath = strpath + "/controller_changan_shenlan.xml";
  490. else
  491. strpath = argv[1];
  492. std::cout<<strpath.toStdString()<<std::endl;
  493. // gdecition_def.set_accelerator(-0.5);
  494. gdecition_def.set_brake(0);
  495. gdecition_def.set_rightlamp(false);
  496. gdecition_def.set_leftlamp(false);
  497. gdecition_def.set_wheelangle(0);
  498. gdecition_def.set_angle_mode(0);
  499. gdecition_def.set_angle_active(0);
  500. gdecition_def.set_acc_active(0);
  501. // gdecition_def.set_brake_active(1);
  502. gdecition_def.set_brake_type(0);
  503. gdecition_def.set_auto_mode(0);
  504. // gdecition_def.set_angle_mode(0);
  505. // gdecition_def.set_angle_active(0);
  506. // gdecition_def.set_acc_active(0);
  507. // gdecition_def.set_brake_active(0);
  508. // gdecition_def.set_brake_type(0);
  509. // gdecition_def.set_auto_mode(0);
  510. gTime.start();
  511. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  512. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  513. gstrmemcansend = xp.GetParam("cansend","cansend0");
  514. gstrmemdecition = xp.GetParam("dection","deciton");
  515. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  516. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  517. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  518. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  519. #ifdef TORQUEBRAKETEST
  520. EnableTorqueBrakeTest();
  521. #endif
  522. std::thread xthread(sendthread);
  523. return a.exec();
  524. }