main.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 "remotectrl.pb.h"
  15. #include <thread>
  16. #ifdef Q_OS_LINUX
  17. #include <signal.h>
  18. #endif
  19. static void * gpacansend;
  20. static void * gpadecition;
  21. static void * gpachassis;
  22. static void * gparemote;
  23. static std::string gstrmemdecition;
  24. static std::string gstrmemcansend;
  25. static std::string gstrmemchassis;
  26. std::string gstrmemremote; //Remote Ctrl
  27. static bool gbSendRun = true;
  28. static bool gbChassisEPS = false;
  29. static iv::brain::decition gdecition_def;
  30. static iv::brain::decition gdecition;
  31. bool gbAllowRemote = false; //Default, Not Allow Remote
  32. qint64 gLastRemoteTime = 0;
  33. bool gbAutoDriving = true; //if true, Auto Driving, false, remote controll
  34. iv::brain::decition gdecition_remote;
  35. static QTime gTime;
  36. static int gnLastSendTime = 0;
  37. static int gnLastRecvDecTime = -1000;
  38. static int gnDecitionNum = 0; //when is zero,send default;
  39. const int gnDecitionNumMax = 100;
  40. static int gnIndex = 0;
  41. static bool gbHaveVehSpd = false;
  42. static double gfVehSpd = 0.0;
  43. static bool gbHaveWheelAngle = false;
  44. static double gfWheelAngle = 0.0;
  45. static double gfWheelSpeedLim = 300; //300 degrees per second
  46. static boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  47. static QMutex gMutex;
  48. static std::thread * gpsendthread = NULL;
  49. static double kp = 0.1;
  50. static double ki = 0.0;
  51. static double kd = 0.0;
  52. static double MAXACC = 3.0;
  53. static double gfVehAcc = 0.0;
  54. static int64_t gnLastAccUptime = 0;
  55. static bool gbUsePID = true;
  56. static bool gbPrintPIDOut = true;
  57. static bool gbAccValid = false;
  58. // signal: @ACC_LatAngReq //更改CANid
  59. #define ECU_1C4_ACC_LatAngReq_CovFactor (0.1)
  60. // conversion value to CAN signal
  61. #define ECU_1C4_ACC_LatAngReq_toS(x) ((int16_t)((x) / 0.1 + 7200))
  62. // signal: @ACC_MotorTorqueMaxLimitRequest
  63. #define ECU_1C4_ACC_MotorTorqueMaxLimitRequest_CovFactor (0.02)
  64. // conversion value to CAN signal
  65. #define ECU_1C4_ACC_MotorTorqueMaxLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  66. // signal: @ACC_MotorTorqueMinLimitRequest
  67. #define ECU_1C4_ACC_MotorTorqueMinLimitRequest_CovFactor (0.02)
  68. // conversion value to CAN signal
  69. #define ECU_1C4_ACC_MotorTorqueMinLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  70. typedef struct
  71. {
  72. int16_t ACC_LatAngReq;
  73. //uint8_t ADS_Reqmode; //20221102, 新车没有此信号
  74. int16_t ACC_MotorTorqueMaxLimitRequest;
  75. uint8_t ACC_LatAngReqActive;
  76. int16_t ACC_MotorTorqueMinLimitRequest;
  77. //uint8_t ACC_ADCReqType; //20221102, 新车没有此信号
  78. } ECU_1C4_t;
  79. // signal: @ACC_AccTrqReq
  80. #define ECU_24E_ACC_AccTrqReq_CovFactor (1)
  81. // conversion value to CAN signal
  82. #define ECU_24E_ACC_AccTrqReq_toS(x) ((int16_t)((x) + 16384))
  83. // signal: @ACC_ACCTargetAcceleration
  84. #define ECU_24E_ACC_ACCTargetAcceleration_CovFactor (0.05)
  85. // conversion value to CAN signal
  86. #define ECU_24E_ACC_ACCTargetAcceleration_toS(x) ((int16_t)((x) / 0.05 + 100))
  87. // signal: @ACC_AEBTargetDeceleration
  88. #define ECU_24E_ACC_AEBTargetDeceleration_CovFactor (0.0005)
  89. // conversion value to CAN signal
  90. #define ECU_24E_ACC_AEBTargetDeceleration_toS(x) ((int32_t)((x) / 0.0005 + 32000))
  91. static int gnState = 0; //0 not act 1 act
  92. typedef struct
  93. {
  94. int16_t ACC_AccTrqReq;
  95. uint8_t ACC_AccTrqReqActive;
  96. int16_t ACC_ACCTargetAcceleration;
  97. int32_t ACC_AEBTargetDeceleration;
  98. uint8_t ACC_AEBVehilceHoldReq;
  99. uint8_t ADCReqMode;
  100. uint8_t ACC_AEBActive;
  101. uint8_t ACC_Driveoff_Request;
  102. uint8_t ACC_DecToStop;
  103. uint8_t ACC_CDDActive;
  104. uint8_t ACC_ACCMode;
  105. } ECU_24E_t;
  106. typedef struct
  107. {
  108. uint8_t CdcDoor;
  109. uint8_t res1;
  110. uint8_t res2;
  111. uint8_t ADS_UDLCTurnLightReq;
  112. } ECU_25E_t; //zhuanxiangdeng IDgenghuan
  113. unsigned char byte_1C4[64];//byte_144[8];
  114. unsigned char byte_24E[64];
  115. unsigned char byte_25E[32];
  116. ECU_1C4_t _m1C4 = {0,0,0,0};
  117. ECU_24E_t _m24E = {0,0,0,0,0,0,0,0,0,0,0};
  118. ECU_25E_t _m25E = {0,0,0,0};
  119. void ExecSend();
  120. double PIDTorque(double torque)
  121. {
  122. if(gbUsePID == false)return torque;
  123. int nnow = std::chrono::system_clock::now().time_since_epoch().count();
  124. if((nnow - gnLastAccUptime)>1e9)return torque;
  125. static double error_previous = 0.0;
  126. double fVehWeight = 1800;
  127. double fRollForce = 50;
  128. const double fRatio = 2.5;
  129. double accAim = (torque * fRatio - fRollForce)/fVehWeight;
  130. double error = accAim - gfVehAcc;
  131. double dt = 0.01; //10ms
  132. double diff = (error - error_previous)/dt;
  133. double integral = integral + error * dt * ki;
  134. double output = error * kp + integral + diff * kd;
  135. double fAccAjust = accAim + output;
  136. if(fAccAjust>MAXACC)fAccAjust = MAXACC;
  137. if(fAccAjust<=0)fAccAjust = 0.0;
  138. double fNeed = fRollForce + fVehWeight*fAccAjust;
  139. double ftorqueAjust = fNeed/fRatio;
  140. if(gbPrintPIDOut)
  141. {
  142. int64_t timesecond = nnow/1e9;
  143. int64_t timepart = nnow - timesecond * 1e9;
  144. double fnow = timepart; fnow = fnow/1e9;
  145. fnow = fnow + timesecond;
  146. std::cout<<fnow<<"\t"<<accAim<<"\t"<<fAccAjust<<"\t"<<error<<"\t"<<torque<<"\t"<<ftorqueAjust<<std::endl;
  147. }
  148. return ftorqueAjust;
  149. }
  150. void executeDecition(const iv::brain::decition &decition)
  151. {
  152. static int xieya = 50;
  153. // std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  154. // std::cout<<"angle_mode is "<<decition.angle_mode()<<" angle_active is "<<decition.angle_active()<<std::endl;
  155. // std::cout<<"brake_type is "<<decition.brake_type()<<" acc_active is "<<decition.acc_active()<<std::endl;
  156. // std::cout<<"brake is "<<decition.brake()<<" brake_active is "<<decition.brake_active()<<std::endl;
  157. // std::cout<<"auto_mode is "<<decition.auto_mode()<<" rightlamp is "<<decition.rightlamp()<<std::endl;
  158. double fWheelAngleReq = decition.wheelangle();
  159. double fsendinter = 0.02;
  160. // if(fabs(fWheelAngleReq - gfWheelAngle)/fsendinter > gfWheelSpeedLim)
  161. // {
  162. // if(fWheelAngleReq > gfWheelAngle)
  163. // {
  164. // fWheelAngleReq = gfWheelAngle + fsendinter * gfWheelSpeedLim;
  165. // }
  166. // else
  167. // {
  168. // fWheelAngleReq = gfWheelAngle - fsendinter * gfWheelSpeedLim;
  169. // }
  170. // }
  171. // std::cout<<" wheel req: "<<decition.wheelangle()<<" real send : "<<fWheelAngleReq<<" real whhel:"<<gfWheelAngle<< std::endl;
  172. _m1C4.ACC_LatAngReq = ECU_1C4_ACC_LatAngReq_toS(fWheelAngleReq);
  173. //_m1C4.ADS_Reqmode = decition.angle_mode(); //20221102,新车没有此信号
  174. _m1C4.ACC_MotorTorqueMaxLimitRequest = ECU_1C4_ACC_MotorTorqueMaxLimitRequest_toS(10);
  175. _m1C4.ACC_LatAngReqActive = decition.angle_active();
  176. _m1C4.ACC_MotorTorqueMinLimitRequest = ECU_1C4_ACC_MotorTorqueMinLimitRequest_toS(-10);
  177. // _m144.ACC_ADCReqType = decition.brake_type();//ADC请求类型(制动时为1,其余情况为0)
  178. if(decition.brake()>(-0.0000001))
  179. {
  180. //_m144.ACC_ADCReqType = 0;//ADC请求类型(制动时为1,其余情况为0)//20221102,新车没有此信号
  181. // _m24B.ADCReqMode = 0;//20221102,新车没有此信号
  182. _m24E.ACC_DecToStop =0;
  183. }
  184. else
  185. {
  186. //_m144.ACC_ADCReqType = 1;//20221102,新车没有此信号
  187. // _m24B.ADCReqMode = 1;//20221102,新车没有此信号
  188. _m24E.ACC_DecToStop =1;
  189. }
  190. // std::cout<<" type: "<<(int)_m144.ACC_ADCReqType<<std::endl;
  191. /*制动过程用的减速度,加速用扭矩*/
  192. double ftorque = decition.torque();
  193. if(ftorque>0.1)ftorque = PIDTorque(ftorque);
  194. _m24E.ACC_AccTrqReq = ECU_24E_ACC_AccTrqReq_toS(decition.torque());
  195. _m24E.ACC_AccTrqReqActive = decition.acc_active();
  196. if(decition.brake()<(-5.0))
  197. {
  198. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(-5.0);
  199. }
  200. else
  201. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(decition.brake());
  202. // std::cout<<" brake : "<<decition.brake()<<std::endl;
  203. // std::cout<<"brake acctive. "<<decition.brake_active()<<std::endl;
  204. if(decition.brake()>(-0.0000001))
  205. {
  206. if(xieya > 0)
  207. {
  208. _m24E.ACC_CDDActive = 1;
  209. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0.5);
  210. _m24E.ACC_AccTrqReq = ECU_24E_ACC_AccTrqReq_toS(0);
  211. _m24E.ACC_Driveoff_Request = 1;
  212. //_m144.ACC_ADCReqType = 1;//20221102,新车没有此信号
  213. //_m24B.ADCReqMode = 1; //20221102,新车没有此信号
  214. _m24E.ACC_DecToStop =1;
  215. xieya--;
  216. std::cout<<" xieya. now veh speed: "<<gfVehSpd<<std::endl;
  217. }
  218. else
  219. {
  220. _m24E.ACC_CDDActive = 0;
  221. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0);
  222. _m24E.ACC_Driveoff_Request = 0;
  223. }
  224. }
  225. else
  226. {
  227. _m24E.ACC_CDDActive = 1;
  228. _m24E.ACC_Driveoff_Request = 0;
  229. }
  230. // _m24B.ACC_CDDActive = decition.brake_active();
  231. // std::cout<<" req mode: "<<_m144.ADS_Reqmode<<std::endl;
  232. //byte_144[0] = ((_m144.ACC_LatAngReq >> 9) & (0x1FU)) | ((_m144.ADS_Reqmode & (0x07U)) << 5);
  233. // qDebug(" req mode: %d byte: %02X ",_m144.ADS_Reqmode, byte_144[0]);
  234. // byte_144[1] = ((_m144.ACC_LatAngReq >> 1) & (0xFFU));
  235. //byte_144[2] = ((_m144.ACC_LatAngReq & (0x01U)) << 7) | ((_m144.ACC_MotorTorqueMaxLimitRequest >> 5) & (0x3FU)) | ((_m144.ACC_LatAngReqActive & (0x01U)) << 6);
  236. //byte_144[3] = ((_m144.ACC_MotorTorqueMaxLimitRequest & (0x1FU)) << 3) | ((_m144.ACC_MotorTorqueMinLimitRequest >> 8) & (0x07U));
  237. //byte_144[4] = (_m144.ACC_MotorTorqueMinLimitRequest & (0xFFU));
  238. //byte_144[5] = ((_m144.ACC_ADCReqType & (0x03U)) << 6);
  239. byte_1C4[0] = ((_m1C4.ACC_MotorTorqueMaxLimitRequest >> 3) & (0xFFU));
  240. // qDebug(" req mode: %d byte: %02X ",_m144.ADS_Reqmode, byte_144[0]);
  241. byte_1C4[1] = ((_m1C4.ACC_MotorTorqueMaxLimitRequest & (0x07U)) << 5) | ((_m1C4.ACC_MotorTorqueMinLimitRequest >> 6) & (0x1FU));
  242. byte_1C4[2] = ((_m1C4.ACC_MotorTorqueMinLimitRequest & (0x3FU)) << 2) | ((_m1C4.ACC_LatAngReq >> 12) & (0x03U));
  243. byte_1C4[3] = (((_m1C4.ACC_LatAngReq>>4 )& (0xFFU)));
  244. // byte_1C4[4] = (((_m1C4.ACC_LatAngReq<<4) & (0xF0U)))|((_m1C4.ACC_LatAngReqActive&(0x01U))<<3);
  245. byte_1C4[4] = (((_m1C4.ACC_LatAngReq)& (0x0fU))<<4)|((_m1C4.ACC_LatAngReqActive&(0x01U))<<3);
  246. //byte_1C4[5] = ((_m144.ACC_ADCReqType & (0x03U)) << 6);
  247. // _m24B.ACC_AEBTargetDeceleration = 0;
  248. // _m24B.ACC_AEBVehilceHoldReq = 0;
  249. // _m24B.ADCReqMode = 0;
  250. // _m24B.ACC_AEBActive = 0;
  251. // _m24B.ACC_Driveoff_Request = 0;
  252. // _m24B.ACC_DecToStop = 0;
  253. // std::cout<<" brake : "<<decition.brake()<<std::endl;
  254. // std::cout<<"brake acctive. "<<decition.brake_active()<<std::endl;
  255. if(decition.brake()>(-0.0000001))
  256. {
  257. _m24E.ACC_CDDActive = 0;
  258. _m24E.ACC_Driveoff_Request = 1;
  259. if(xieya > 0)
  260. {
  261. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0.5);
  262. xieya--;
  263. }
  264. else
  265. {
  266. _m24E.ACC_ACCTargetAcceleration = ECU_24E_ACC_ACCTargetAcceleration_toS(0);
  267. }
  268. _m24E.ACC_AccTrqReqActive = 1;
  269. _m24E.ACC_DecToStop = 0;
  270. }
  271. else
  272. {
  273. _m24E.ACC_CDDActive = 1;
  274. _m24E.ACC_Driveoff_Request = 0;
  275. if(gbHaveVehSpd)
  276. {
  277. if(gfVehSpd < 0.01)
  278. {
  279. if(xieya != 50)std::cout<<"need xieya. "<<std::endl;
  280. xieya = 50;
  281. }
  282. }
  283. _m24E.ACC_AccTrqReqActive = 0;
  284. _m24E.ACC_DecToStop = 1;
  285. }
  286. // _m24B.ACC_CDDActive = decition.brake_active();
  287. _m24E.ACC_ACCMode = decition.auto_mode();
  288. if(gnState == 0)
  289. {
  290. _m24E.ACC_CDDActive = 0;
  291. _m24E.ACC_Driveoff_Request = 0;
  292. _m24E.ACC_ACCMode = 0;
  293. _m24E.ACC_ACCTargetAcceleration = 0;
  294. _m24E.ACC_AccTrqReqActive = 0;
  295. _m24E.ACC_DecToStop = 0;
  296. }
  297. // std::cout<<"acc mode: "<<(int)_m24B.ACC_ACCMode<<std::endl;
  298. // byte_24B[0] = ((_m24B.ACC_AccTrqReq >> 7) & (0xFFU));
  299. //byte_24B[1] = ((_m24B.ACC_AccTrqReq & (0x7FU)) << 1) | (_m24B.ACC_AccTrqReqActive & (0x01U));
  300. //byte_24B[2] = ((_m24B.ACC_ACCTargetAcceleration >> 3) & (0x1FU));
  301. //byte_24B[3] = ((_m24B.ACC_ACCTargetAcceleration & (0x07U)) << 5) | ((_m24B.ACC_AEBTargetDeceleration >> 15) & (0x01U));
  302. //byte_24B[4] = ((_m24B.ACC_AEBTargetDeceleration >> 7) & (0xFFU));
  303. //byte_24B[5] = ((_m24B.ACC_AEBTargetDeceleration & (0x7FU)) << 1) | (_m24B.ACC_AEBVehilceHoldReq & (0x01U));
  304. //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);
  305. //byte_24B[7] = (_m24B.ACC_ACCMode & (0x07U));
  306. byte_24E[0] = ((_m24E.ACC_ACCTargetAcceleration) & (0xFFU));
  307. //byte_24E[1] = ((_m24B.ACC_AccTrqReq & (0x7FU)) << 1) | (_m24B.ACC_AccTrqReqActive & (0x01U));
  308. //byte_24E[2] = ((_m24B.ACC_ACCTargetAcceleration >> 3) & (0x1FU));
  309. byte_24E[3] = ((_m24E.ACC_DecToStop & (0x01U)) << 7);// | ((_m24B.ACC_AEBTargetDeceleration >> 15) & (0x01U));
  310. //byte_24E[4] = ((_m24B.ACC_AEBTargetDeceleration >> 7) & (0xFFU));
  311. byte_24E[5] = ((_m24E.ACC_CDDActive & (0x01U)) << 4);//((_m24E.ACC_AEBTargetDeceleration & (0x7FU)) << 1) | (_m24E.ACC_AEBVehilceHoldReq & (0x01U));
  312. 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);
  313. byte_24E[8] = ((_m24E.ACC_AEBTargetDeceleration>>8) & (0xFFU));
  314. byte_24E[9] = ((_m24E.ACC_AEBTargetDeceleration) & (0xFFU));
  315. byte_24E[10] = ((_m24E.ACC_AEBActive & (0x01U)) << 7);
  316. byte_24E[11] = ((_m24E.ACC_AccTrqReq>>13 )& (0x03U));
  317. byte_24E[12] = ((_m24E.ACC_AccTrqReq>>5 )& (0xFFU));
  318. //byte_24E[13] = ((_m24E.ACC_AccTrqReq & (0x1FU)<<3))| ((_m24E.ACC_AccTrqReqActive & (0x01U)) << 2);
  319. byte_24E[13] = (((_m24E.ACC_AccTrqReq & (0x1FU))<<3))| ((_m24E.ACC_AccTrqReqActive & (0x01U)) << 2);
  320. if(decition.leftlamp() == true && decition.rightlamp() == false)
  321. _m25E.ADS_UDLCTurnLightReq = 3;
  322. else if(decition.leftlamp() == false && decition.rightlamp() == true)
  323. _m25E.ADS_UDLCTurnLightReq = 4;
  324. else
  325. _m25E.ADS_UDLCTurnLightReq = 0;
  326. byte_25E[3] = ((_m25E.ADS_UDLCTurnLightReq & (0x07U)));
  327. }
  328. void Activate()
  329. {
  330. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  331. iv::brain::decition xdecition;
  332. xdecition.set_brake(0.0);
  333. xdecition.set_torque(0.0);
  334. // for(int j=0;j<100000;j++)
  335. // {
  336. std::cout<<" run "<<std::endl;
  337. for(int i = 0; i < 3; i++){
  338. xdecition.set_wheelangle(0.0);
  339. xdecition.set_angle_mode(0);
  340. xdecition.set_angle_active(0);
  341. xdecition.set_acc_active(0);
  342. xdecition.set_brake_active(0);
  343. // xdecition.set_brake_type(0);
  344. xdecition.set_auto_mode(0);
  345. gnState = 0;
  346. executeDecition(xdecition);
  347. ExecSend();
  348. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  349. }
  350. for(int i = 0; i < 3; i++){
  351. xdecition.set_wheelangle(0.0);
  352. xdecition.set_angle_mode(1);
  353. xdecition.set_angle_active(1);
  354. xdecition.set_acc_active(1);
  355. xdecition.set_brake_active(1);
  356. // xdecition.set_brake_type(1);
  357. xdecition.set_auto_mode(3);
  358. gnState = 1;
  359. executeDecition(xdecition);
  360. ExecSend();
  361. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  362. }
  363. // }
  364. }
  365. void UnAcitvate()
  366. {
  367. iv::brain::decition xdecition;
  368. xdecition.set_brake(0.0);
  369. xdecition.set_torque(0.0);
  370. for(int i = 0; i < 3; i++){
  371. xdecition.set_wheelangle(0);
  372. xdecition.set_angle_mode(1);
  373. xdecition.set_angle_active(1);
  374. xdecition.set_acc_active(1);
  375. xdecition.set_brake_active(1);
  376. // xdecition.set_brake_type(1);
  377. xdecition.set_auto_mode(3);
  378. gnState = 1;
  379. executeDecition(xdecition);
  380. ExecSend();
  381. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  382. }
  383. for(int i = 0; i < 3; i++){
  384. xdecition.set_wheelangle(0);
  385. xdecition.set_angle_mode(0);
  386. xdecition.set_angle_active(0);
  387. xdecition.set_acc_active(0);
  388. xdecition.set_brake_active(0);
  389. // xdecition.set_brake_type(0);
  390. gnState = 0;
  391. xdecition.set_auto_mode(0);
  392. executeDecition(xdecition);
  393. ExecSend();
  394. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  395. }
  396. }
  397. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  398. {
  399. (void)index;
  400. (void)dt;
  401. (void)strmemname;
  402. iv::chassis xchassis;
  403. // static int ncount = 0;
  404. if(!xchassis.ParseFromArray(strdata,nSize))
  405. {
  406. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  407. return;
  408. }
  409. if(xchassis.has_epsmode())
  410. {
  411. if(xchassis.epsmode() == 0)
  412. {
  413. gbChassisEPS = true;
  414. }
  415. }
  416. if(xchassis.has_vel())
  417. {
  418. gfVehSpd = xchassis.vel();
  419. gbHaveVehSpd = true;
  420. // std::cout<<" gf Veh speed : "<<gfVehSpd<<std::endl;
  421. }
  422. if(xchassis.has_angle_feedback())
  423. {
  424. gfWheelAngle = xchassis.angle_feedback();
  425. gbHaveWheelAngle = true;
  426. }
  427. }
  428. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  429. {
  430. (void)index;
  431. (void)dt;
  432. (void)strmemname;
  433. static qint64 oldtime = QDateTime::currentMSecsSinceEpoch();
  434. iv::brain::decition xdecition;
  435. if(!xdecition.ParseFromArray(strdata,nSize))
  436. {
  437. std::cout<<"ListenDecition parse error."<<std::endl;
  438. return;
  439. }
  440. // if(xdecition.gear() != 4)
  441. // {
  442. // qDebug("not D");
  443. // }
  444. xdecition.set_angle_mode(1);
  445. xdecition.set_angle_active(1);
  446. xdecition.set_acc_active(1);
  447. xdecition.set_brake_active(1);
  448. // xdecition.set_brake_type(1);
  449. xdecition.set_auto_mode(3);
  450. // xdecition.set_wheelangle(45.0);
  451. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  452. oldtime = QDateTime::currentMSecsSinceEpoch();
  453. gMutex.lock();
  454. gdecition.CopyFrom(xdecition);
  455. gMutex.unlock();
  456. gnDecitionNum = gnDecitionNumMax;
  457. gbChassisEPS = false;
  458. }
  459. void ListenRemotectrl(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  460. {
  461. (void)index; (void)dt; (void)strmemname;
  462. iv::brain::decition xdecition;
  463. iv::remotectrl xrc;
  464. if(!xrc.ParseFromArray(strdata,nSize))
  465. {
  466. std::cout<<"ListenRemotectrl parse error."<<std::endl;
  467. return;
  468. }
  469. if(xrc.ntype() == iv::remotectrl_CtrlType_AUTO)
  470. {
  471. gbAutoDriving = true;
  472. }
  473. else
  474. {
  475. gbAutoDriving = false;
  476. xdecition.set_accelerator(xrc.acc());
  477. xdecition.set_brake(xrc.brake());
  478. xdecition.set_wheelangle(xrc.wheel());
  479. if(xrc.acc()>0.0)xdecition.set_torque(xrc.acc() * 100);
  480. xdecition.set_angle_mode(1);
  481. xdecition.set_angle_active(1);
  482. xdecition.set_acc_active(1);
  483. xdecition.set_brake_active(1);
  484. // xdecition.set_brake_type(1);
  485. xdecition.set_auto_mode(3);
  486. gMutex.lock();
  487. gdecition_remote.CopyFrom(xdecition);
  488. gMutex.unlock();
  489. gLastRemoteTime = QDateTime::currentMSecsSinceEpoch();
  490. gnDecitionNum = gnDecitionNumMax;
  491. gbChassisEPS = false;
  492. }
  493. }
  494. void ExecSend()
  495. {
  496. static int nCount = 0;
  497. nCount++;
  498. iv::can::canmsg xmsg;
  499. iv::can::canraw xraw;
  500. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  501. // 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]);
  502. xraw.set_id(0x1C4);
  503. xraw.set_data(byte_1C4,32);
  504. xraw.set_bext(false);
  505. xraw.set_bremote(false);
  506. xraw.set_len(32);
  507. iv::can::canraw * pxraw1C4 = xmsg.add_rawmsg();
  508. pxraw1C4->CopyFrom(xraw);
  509. // qDebug(" 0x144: %02X %02X %02X %02X %02X %02X %02X %02X",byte_144[0],byte_144[1],byte_144[2],byte_144[3],
  510. // byte_144[4],byte_144[5],byte_144[6],byte_144[7]);
  511. xmsg.set_channel(0);
  512. xmsg.set_index(gnIndex);
  513. xraw.set_id(0x24E);
  514. xraw.set_data(byte_24E,64);
  515. xraw.set_bext(false);
  516. xraw.set_bremote(false);
  517. xraw.set_len(64);
  518. if(nCount%2 == 1)
  519. {
  520. iv::can::canraw * pxraw24E = xmsg.add_rawmsg();
  521. pxraw24E->CopyFrom(xraw);
  522. }
  523. xmsg.set_channel(0);
  524. xmsg.set_index(gnIndex);
  525. xraw.set_id(0x25E);
  526. xraw.set_data(byte_25E,32);
  527. xraw.set_bext(false);
  528. xraw.set_bremote(false);
  529. xraw.set_len(32);
  530. // if(nCount == 10)
  531. if(nCount%2 == 1) //25Ede zhouqi shi 20ms
  532. {
  533. iv::can::canraw * pxraw25E = xmsg.add_rawmsg();
  534. pxraw25E->CopyFrom(xraw);
  535. // nCount = 0;
  536. }
  537. xmsg.set_channel(0);
  538. xmsg.set_index(gnIndex);
  539. gnIndex++;
  540. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  541. int ndatasize = xmsg.ByteSize();
  542. char * strser = new char[ndatasize];
  543. std::shared_ptr<char> pstrser;
  544. pstrser.reset(strser);
  545. if(xmsg.SerializeToArray(strser,ndatasize))
  546. {
  547. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  548. }
  549. else
  550. {
  551. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  552. }
  553. }
  554. void initial()
  555. {
  556. for (uint8_t i = 0; i < 64; i++) //CAN to canfd
  557. {
  558. byte_1C4[i] = 0;
  559. byte_24E[i] = 0;
  560. //byte_36E[i] = 0;
  561. }
  562. }
  563. void sendthread()
  564. {
  565. initial();
  566. iv::brain::decition xdecition;
  567. UnAcitvate();
  568. // UnAcitvate();
  569. int nstate = 0; //0 Un 1 Activate
  570. // Activate();
  571. while(gbSendRun)
  572. {
  573. if(gbAutoDriving)
  574. {
  575. if(gnDecitionNum <= 0)
  576. {
  577. gMutex.lock();
  578. xdecition.CopyFrom(gdecition_def);
  579. gMutex.unlock();
  580. }
  581. else
  582. {
  583. xdecition.CopyFrom(gdecition);
  584. gnDecitionNum--;
  585. }
  586. }
  587. else
  588. {
  589. if((QDateTime::currentMSecsSinceEpoch() - gLastRemoteTime)> 1000)
  590. {
  591. gbAutoDriving = true;
  592. continue;
  593. }
  594. else
  595. {
  596. gMutex.lock();
  597. xdecition.CopyFrom(gdecition_remote);
  598. gMutex.unlock();
  599. }
  600. }
  601. if(gnDecitionNum <= 0)
  602. {
  603. if(nstate == 1)
  604. {
  605. UnAcitvate();
  606. nstate = 0;
  607. }
  608. xdecition.CopyFrom(gdecition_def);
  609. }
  610. else
  611. {
  612. if(nstate == 0)
  613. {
  614. Activate();
  615. nstate = 1;
  616. }
  617. if(gbAutoDriving)
  618. {
  619. gMutex.lock();
  620. xdecition.CopyFrom(gdecition);
  621. gMutex.unlock();
  622. }
  623. else
  624. {
  625. gMutex.lock();
  626. xdecition.CopyFrom(gdecition_remote);
  627. gMutex.unlock();
  628. }
  629. gnDecitionNum--;
  630. }
  631. #ifdef TORQUEBRAKETEST
  632. if(gnothavetb < 10)
  633. {
  634. iv::controller::torquebrake xtb;
  635. gMutextb.lock();
  636. xtb.CopyFrom(gtb);
  637. gMutextb.unlock();
  638. if(xtb.enable())
  639. {
  640. xdecition.set_torque(xtb.torque());
  641. xdecition.set_brake(xtb.brake());
  642. std::cout<<" use xtb. torque: "<<xtb.torque()<<" brake: "<<xtb.brake()<<std::endl;
  643. // gcontroller->control_torque(xtb.torque());
  644. // gcontroller->control_brake(xtb.brake());
  645. // qDebug("use tb value torque is %f brake is %f",xtb.torque(),xtb.brake());
  646. }
  647. else
  648. {
  649. // qDebug("torquebrake not enable.");
  650. }
  651. gnothavetb++;
  652. }
  653. #endif
  654. executeDecition(xdecition);
  655. if(gbChassisEPS == false) ExecSend();
  656. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  657. }
  658. UnAcitvate();
  659. }
  660. #ifdef Q_OS_LINUX
  661. void sig_int(int signo)
  662. {
  663. gbSendRun = false;
  664. gpsendthread->join();
  665. iv::modulecomm::Unregister(gpacansend);
  666. iv::modulecomm::Unregister(gpachassis);
  667. iv::modulecomm::Unregister(gpadecition);
  668. std::cout<<" controller exit."<<std::endl;
  669. exit(0);
  670. }
  671. #endif
  672. void Listencanrecv0()
  673. {
  674. void * pblambda = iv::modulecomm::RegisterRecv("canrecv0",[](const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname){
  675. (void)strdata;(void)nSize;(void)index;(void)dt;(void)strmemname;
  676. iv::can::canmsg xmsg;
  677. if(false == xmsg.ParseFromArray(strdata,nSize))
  678. {
  679. std::cout<<"controller Listencan0 fail."<<std::endl;
  680. return;
  681. }
  682. static double fLastVehSpeed = 0;
  683. static bool baccnotokcount = 0;
  684. int i;
  685. for(i=0;i<xmsg.rawmsg_size();i++)
  686. {
  687. iv::can::canraw * praw = xmsg.mutable_rawmsg(i);
  688. if(praw->id() == 0x1CC)
  689. {
  690. if(praw->len() == 64)
  691. {
  692. unsigned char xdata[64];
  693. memcpy(xdata,praw->data().data(),64);
  694. unsigned int value = xdata[10]; value = value<<7;
  695. value = value +((xdata[11]&0xFE)>>1);
  696. double facc = value;
  697. facc = facc * 0.01 - 32.0;
  698. // qDebug(" %02X %02X",xdata[10],xdata[11]);
  699. // gfVehAcc = facc; //ESP Acc not ok.
  700. value = xdata[12]&0x1f;value = value<<8;
  701. value = value + xdata[13];
  702. double fvehspeed = value;fvehspeed = fvehspeed * 0.05625;
  703. int64_t timediff = (std::chrono::system_clock::now().time_since_epoch().count() - gnLastAccUptime)/1e6;
  704. if(timediff<16)
  705. {
  706. double facccalc = ((fvehspeed - fLastVehSpeed)/3.6)/0.01;
  707. gfVehAcc = facccalc;
  708. gbAccValid = true;
  709. baccnotokcount = 0;
  710. }
  711. else
  712. {
  713. baccnotokcount++;
  714. if(baccnotokcount>3)gbAccValid = false;
  715. }
  716. fLastVehSpeed = fvehspeed;
  717. // std::cout<<" diff : "<<(std::chrono::system_clock::now().time_since_epoch().count() - gnLastAccUptime)/1e6<<std::endl;
  718. gnLastAccUptime = std::chrono::system_clock::now().time_since_epoch().count();
  719. std::cout<<" acc : "<<gfVehAcc<<std::endl;
  720. }
  721. }
  722. if(praw->id() == 0x3)
  723. if(praw->id() == 0x322)
  724. {
  725. if(praw->len() == 8)
  726. {
  727. unsigned char xdata[8];
  728. memcpy(xdata,praw->data().data(),8);
  729. unsigned int value = xdata[1]; value = value<<12;
  730. value = value +((xdata[2]&0xFF)<<4);
  731. value = value + xdata[3];
  732. double fmotorrpm = value - 20000;
  733. (void)fmotorrpm;
  734. // std::cout<<" motor speed : "<<fmotorrpm<<std::endl;
  735. }
  736. }
  737. }
  738. });
  739. (void)pblambda;
  740. }
  741. int main(int argc, char *argv[])
  742. {
  743. RegisterIVBackTrace();
  744. showversion("controller_changan_shenlan");
  745. QCoreApplication a(argc, argv);
  746. QString strpath = QCoreApplication::applicationDirPath();
  747. if(argc < 2)
  748. strpath = strpath + "/controller_changan_shenlan.xml";
  749. else
  750. strpath = argv[1];
  751. std::cout<<strpath.toStdString()<<std::endl;
  752. // gdecition_def.set_accelerator(-0.5);
  753. gdecition_def.set_brake(0);
  754. gdecition_def.set_rightlamp(false);
  755. gdecition_def.set_leftlamp(false);
  756. gdecition_def.set_wheelangle(0);
  757. gdecition_def.set_angle_mode(0);
  758. gdecition_def.set_angle_active(0);
  759. gdecition_def.set_acc_active(0);
  760. // gdecition_def.set_brake_active(1);
  761. gdecition_def.set_brake_type(0);
  762. gdecition_def.set_auto_mode(0);
  763. // gdecition_def.set_angle_mode(0);
  764. // gdecition_def.set_angle_active(0);
  765. // gdecition_def.set_acc_active(0);
  766. // gdecition_def.set_brake_active(0);
  767. // gdecition_def.set_brake_type(0);
  768. // gdecition_def.set_auto_mode(0);
  769. gTime.start();
  770. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  771. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  772. gstrmemcansend = xp.GetParam("cansend","cansend0");
  773. gstrmemdecition = xp.GetParam("dection","deciton");
  774. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  775. gstrmemremote = xp.GetParam("remotectrl","remotectrl");
  776. kp = xp.GetParam("kp",0.1);
  777. ki = xp.GetParam("ki",0.0);
  778. kd = xp.GetParam("kd",0.0);
  779. MAXACC = xp.GetParam("MAXACC",3.0);
  780. gbUsePID = xp.GetParam("UsePID",false);
  781. gbPrintPIDOut = xp.GetParam("PrintPIDOut",false);
  782. std::string strremote = xp.GetParam("allowremote","true");
  783. if(strremote == "true")
  784. {
  785. gbAllowRemote = true;
  786. }
  787. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  788. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  789. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  790. if(gbAllowRemote)
  791. {
  792. gparemote = iv::modulecomm::RegisterRecv(gstrmemremote.data(),ListenRemotectrl);
  793. }
  794. #ifdef TORQUEBRAKETEST
  795. EnableTorqueBrakeTest();
  796. #endif
  797. std::thread xthread(sendthread);
  798. Listencanrecv0();
  799. gpsendthread = &xthread;
  800. #ifdef Q_OS_LINUX
  801. signal(SIGINT, sig_int);
  802. signal(SIGTERM,sig_int);
  803. #endif
  804. return a.exec();
  805. }