pid_controller.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include <adc_controller/pid_controller.h>
  2. #include <common/constants.h>
  3. #include <common/car_status.h>
  4. #include <math.h>
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8. iv::decition::PIDController::PIDController(){
  9. controller_id = 0;
  10. controller_name="pid";
  11. }
  12. iv::decition::PIDController::~PIDController(){
  13. }
  14. /**
  15. * @brief getControlDecition
  16. * pid方式计算横向方向盘和纵向加速度
  17. *
  18. * @param now_gps_ins 实时gps信息
  19. * @param path 目标路径
  20. * @param dSpeed 决策速度
  21. * @param ObsDistacne 障碍物距离
  22. * @param ObsSpeed 障碍物速度
  23. * @param computeAngle 是否要计算方向盘角度
  24. * @param computeAcc 是否要计算纵向加速度
  25. * @param decition 决策信息结构体的指针
  26. * @return iv::decition::Decition
  27. */
  28. iv::decition::Decition iv::decition::PIDController::getControlDecition(GPS_INS now_gps_ins, std::vector<Point2D> path, float dSpeed, float obsDistacne, float obsSpeed,
  29. bool computeAngle, bool computeAcc, Decition *decition){
  30. float realSpeed= now_gps_ins.speed;
  31. int roadMode = now_gps_ins.roadMode;
  32. if(computeAngle){
  33. (*decition)->wheel_angle=getPidAngle( realSpeed, path,roadMode);
  34. }
  35. if(computeAcc){
  36. (*decition)->accelerator=getPidAcc( now_gps_ins, dSpeed, obsDistacne, obsSpeed);
  37. }
  38. return *decition;
  39. }
  40. float iv::decition::PIDController::getPidAngle(float realSpeed, std::vector<Point2D> trace, int roadMode){
  41. double ang = 0;
  42. double EPos = 0, EAng = 0;
  43. double KEang = 14, KEPos = 10, DEang = 0, DEPos = 0,IEpos=0,IEang=0;
  44. bool turning=false;
  45. if(roadMode==14||roadMode==15||roadMode==16||roadMode==17){
  46. turning=true;
  47. }
  48. double PreviewDistance;//预瞄距离
  49. if(ServiceCarStatus.msysparam.mvehtype=="ge3" || ServiceCarStatus.msysparam.mvehtype=="vv7"){
  50. realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(4.0, realSpeed *0.3);
  51. }else if(ServiceCarStatus.msysparam.mvehtype=="hapo"){
  52. if(turning){
  53. realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(4.0, realSpeed *0.3);
  54. }else{
  55. realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(6.0, realSpeed *0.5);
  56. }
  57. } else{
  58. realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(6.0, realSpeed *0.5);
  59. }
  60. // if(ServiceCarStatus.msysparam.mvehtype=="bus"){
  61. // KEang = 14, KEPos = 7, DEang = 0, DEPos = 0,IEpos=0,IEang=0;
  62. // realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(6.0, realSpeed *0.5);
  63. // }
  64. // if(realSpeed <15){
  65. // PreviewDistance = max(4.0, realSpeed *0.4) ;
  66. // }
  67. // if(realSpeed <10){
  68. // PreviewDistance = max(3.0, realSpeed *0.3) ;
  69. // }
  70. double sumdis = 0;
  71. int gpsIndex = 0;
  72. std::vector<Point2D> farTrace;
  73. int nsize = trace.size();
  74. for (int i = 1; i < nsize-1; i++)
  75. {
  76. sumdis += GetDistance(trace[i - 1], trace[i]);
  77. if (sumdis > PreviewDistance)
  78. {
  79. gpsIndex = i;
  80. break;
  81. }
  82. }
  83. EPos = trace[gpsIndex].x;
  84. for (unsigned int i = max(0, gpsIndex - 3); i < min((size_t)(gpsIndex + 3), trace.size()); i++) {
  85. farTrace.push_back(trace[gpsIndex]);
  86. }
  87. if (farTrace.size() == 0) {
  88. EAng = 0;
  89. }
  90. else {
  91. EAng = getAveDef(farTrace);
  92. }
  93. if(abs(realSpeed)<3){
  94. eAngSum=0;
  95. ePosSum=0;
  96. }else{
  97. eAngSum=eAngSum*0.8+EAng;
  98. ePosSum=ePosSum*0.8+EPos;
  99. }
  100. ang = KEang * EAng + KEPos * EPos + DEang * (EAng - lastEA) + DEPos * (EPos - lastEP)
  101. +IEang*eAngSum + IEpos*ePosSum;
  102. lastEA = EAng;
  103. lastEP = EPos;
  104. // if (ang > angleLimit) {
  105. // ang = angleLimit;
  106. // }
  107. // else if (ang < -angleLimit) {
  108. // ang = -angleLimit;
  109. // }
  110. if (lastAng >-3000&&lastAng<3000) {
  111. ang = 0.2 * lastAng + 0.8 * ang;
  112. }
  113. if(ang >-3000&&ang<3000){
  114. lastAng = ang;
  115. }else{
  116. ang=lastAng;
  117. }
  118. return ang;
  119. }
  120. float iv::decition::PIDController::getAveDef(std::vector<Point2D> farTrace){
  121. double sum_x = 0;
  122. double sum_y = 0;
  123. for (int i = 0; i < min(5, (int)farTrace.size()); i++)
  124. {
  125. sum_x += farTrace[i].x;
  126. sum_y += abs(farTrace[i].y);
  127. }
  128. double average_y = sum_y / min(5, (int)farTrace.size());
  129. double average_x = sum_x / min(5, (int)farTrace.size());
  130. return atan(average_x / average_y) / M_PI * 180;
  131. }
  132. float iv::decition::PIDController::getPidAcc (GPS_INS gpsIns,float dSpeed, float obsDistance, float obsSpeed){
  133. std::cout << "\n计算用obs速度:%f\n" << obsSpeed << std::endl;
  134. std::cout << "\n计算用obs距离:%f\n" << obsDistance << std::endl;
  135. if(ServiceCarStatus.msysparam.mvehtype=="hapo")
  136. {
  137. static int obs_line=0;
  138. if(obsDistance<6 && obsDistance>0){
  139. dSpeed=0;
  140. obs_line=1;
  141. }
  142. if(obs_line==1)
  143. {
  144. if(obsDistance<8 && obsDistance>0){
  145. dSpeed=0;
  146. }else{
  147. obs_line=0;
  148. }
  149. }
  150. }else if(obsDistance<10 && obsDistance>0){
  151. dSpeed=0;
  152. }
  153. float dSecSpeed = dSpeed / 3.6;
  154. float realSpeed=gpsIns.speed;
  155. float secSpeed =realSpeed/3.6;
  156. double vt = dSecSpeed;
  157. double vs = secSpeed;
  158. if (abs(vs) < 0.05) vs = 0;
  159. if (abs(obsSpeed) < 0.05) obsSpeed = 0;
  160. double vl = vs + obsSpeed;
  161. double vh = vs;
  162. double dmax = 150;
  163. //车距滤波
  164. if (obsDistance < 0||obsDistance>100) {
  165. obsDistance = 500;
  166. obsSpeed=0;
  167. }
  168. if (obsDistance > 150) vl = 25; //25m/s
  169. //TTC计算
  170. double ds = 0.2566 * vl * vl + 5;
  171. double ttcr = (vh - vl) / obsDistance;
  172. double ttc = 15;
  173. if (15 * (vh - vl) > obsDistance)
  174. ttc = obsDistance / (vh - vl);
  175. ServiceCarStatus.mfttc = ttc;
  176. if (obsDistance <= dmax)
  177. {
  178. if (ttc > 10)
  179. {
  180. if (obsDistance > ds + 5)
  181. {
  182. double dds = min(30.0, obsDistance - (ds + 5));
  183. vt = vt * dds / 30 + vl * (1 - dds / 30);
  184. }
  185. else
  186. {
  187. vt = min(vt, vl);
  188. }
  189. }
  190. else
  191. {
  192. vt = min(vt, vl);
  193. }
  194. }else{
  195. vt=dSecSpeed;
  196. }
  197. vt=min((float)vt,dSecSpeed);
  198. std::cout << "\nvt:%f\n" << vt << std::endl;
  199. //计算加速度
  200. float u =computeU(obsDistance,ds,vs,vt,vh,vl,ttc,ttcr);
  201. //u 限值
  202. u=limiteU(u,ttc);
  203. lastVt = vt;
  204. lastU = u;
  205. float acc=0-u;
  206. return acc;
  207. }
  208. float iv::decition::PIDController::computeU(float obsDistance, float ds,float vs,float vt,float vh,float vl,float ttc,float ttcr){
  209. double Id = 0;
  210. double ed = ds - obsDistance;
  211. if (obsDistance > 150) ed = 0;
  212. double ev = vs - vt;
  213. double u = 0;
  214. if (ttc>10)
  215. {
  216. double kp = 0.5; //double kp = 0.5;
  217. double kd = 0.5; //kd = 0.5
  218. // double k11 = 0.025;
  219. // double k12 = 0.000125;
  220. double dev = (ev - lastEv) / 0.1;
  221. Iv = 0.925 * Iv + ev;
  222. Id = 0.85 * Id + ed;
  223. if (abs(vh) < 2.0&& abs(vl) < 2.0)
  224. {
  225. Iv = 0.0; Id = 0.0;
  226. }
  227. //u = kp * ev + kd * dev + k11 * Iv + k12 * Id;
  228. u = kp * ev + kd * dev;
  229. }
  230. else if (ttc < 1.6 || (ttc <= 10 && obsDistance <= 0.75 * ds))
  231. {
  232. //AEB控制
  233. Id = 0; Iv = 0;
  234. u = 0;
  235. if (ttc < 0.8) u = 7;
  236. else if (ttc < 1.6) u = 3 + 5 * (1.6 - ttc);
  237. else
  238. {
  239. u = (10 - ttc) * (10 - ttc) / 23.52;
  240. }
  241. }
  242. else
  243. {
  244. //制动控制
  245. Id = 0; Iv = 0;
  246. if (obsDistance > 1.25 * ds)
  247. {
  248. double rev_ed = 1 / ed;
  249. u = -0.5 * obsDistance * obsDistance * rev_ed * ttcr * ttcr;
  250. }
  251. else
  252. {
  253. if (abs(vl) > 2.0)
  254. u = 0;
  255. else
  256. u = max(lastU, 1.0f);
  257. }
  258. }
  259. if (abs(vl) < 1.0 && abs(vh) < 1.0
  260. && obsDistance < 2 * ds)
  261. {
  262. u = 0.5;
  263. }
  264. lastEv = ev;
  265. return u;
  266. }
  267. float iv::decition::PIDController::limiteU(float u,float ttc){
  268. if(ttc<3 && u<-0.2){
  269. u=-0.2;
  270. }
  271. else
  272. {
  273. if (u < -1.5) u = -1.5;
  274. }
  275. if (u >= 0 && lastU <= 0)
  276. {
  277. if (u > 0.5) u = 0.5;
  278. }
  279. else if (u >= 0 && lastU >= 0)
  280. {
  281. if (u > lastU + 0.5) u = lastU + 0.5;
  282. }
  283. else if (u <= 0 && lastU >= 0)
  284. {
  285. if (u < -0.04) u = -0.04;
  286. }
  287. else if (u <= 0 && lastU <= 0)
  288. {
  289. if (u < lastU - 0.04) u = lastU - 0.04;
  290. }
  291. return u;
  292. }