main.cpp 20 KB

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