HCV_example.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /////////////////////////////// rp动力学积分部分 //////////////////////////////
  2. // 动力学方程积分部分
  3. // 输入: StateVar: 状态变量
  4. // ControlVar:控制变量
  5. // auxdata: 其他参数
  6. // 输出: dState: 状态微分
  7. // integrand: 积分量
  8. // path: 路径约束
  9. #include <iostream>
  10. #include <armadillo>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "GPM.h"
  15. #include "HCV.h"
  16. using namespace std;
  17. using namespace arma;
  18. int Dynamics_HCV(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path)
  19. {
  20. // 动力学模型
  21. mat State = *StateVar;
  22. mat Control = *ControlVar;
  23. //禁飞区设置
  24. #ifndef LIULU20220629_PSLINUX //原书写方式在Linux下会以为double不能转换为int类型而报错
  25. int a[] = { 100,48,500000 };
  26. int b[] = { 90,43,360000 };
  27. int c[] = { 62,43,320000 };
  28. #else
  29. int a[] = { 100,48,500e3 };
  30. int b[] = { 90,43,360e3 };
  31. int c[] = { 62,43,320e3 };
  32. #endif
  33. // 常数
  34. double pi = datum::pi;
  35. double g = auxdata.g;
  36. double Re = auxdata.Re;
  37. double rad2deg = 180 / pi;
  38. double tc = sqrt(Re / g);
  39. // 状态量本地化
  40. vec phi = State.col(0);
  41. vec lam = State.col(1);
  42. vec v = State.col(2);
  43. vec sigma = State.col(3);
  44. vec m = State.col(4);
  45. // 控制量本地化
  46. vec phit = Control.col(0);
  47. vec alpha = Control.col(1);
  48. vec niu = Control.col(2);
  49. // 参量计算
  50. int n = size(v)(0);
  51. vec Rou = zeros(n, 1);
  52. vec H = 27000 * ones(n, 1) / auxdata.re;
  53. vec Ma = zeros(n, 1);
  54. vec L = zeros(n, 1);
  55. vec D = zeros(n, 1);
  56. vec IspDim = zeros(n, 1);
  57. vec CT = zeros(n, 1);
  58. vec Theta = zeros(n, 1);
  59. vec r = H + 1;
  60. Get_Rou_Mach(H*Re, v*sqrt(Re*g), &Rou, &Ma);
  61. alpha2LD_HCV(alpha, Rou, Ma, v*sqrt(Re*g), auxdata, &L, &D);
  62. vec temp = 0.5*Rou%v*sqrt(Re*g) % v*sqrt(Re*g) * 150 / 100000;
  63. L = temp%L / g;
  64. D = temp%D / g;
  65. // 微分方程
  66. calp(Ma, phit, alpha, &CT, &IspDim);
  67. vec T = 0.029*phit%IspDim%Rou%v*sqrt(Re*g) % CT*auxdata.Ac / auxdata.M0;
  68. vec Isp = IspDim / tc;
  69. // 加速度计算
  70. /*推力*/
  71. vec calpha = cos(alpha);
  72. vec salpha = sin(alpha);
  73. vec cniu = cos(niu);
  74. vec sniu = sin(niu);
  75. //cout << cniu << endl;
  76. mat acc_P[] = { calpha%T / m, salpha%cniu%T / m, salpha%sniu%T / m };
  77. mat acc_R[] = { -D / m, cniu%L / m, sniu%L / m };
  78. vec sphi = sin(phi);
  79. vec cphi = cos(phi);
  80. vec sTheta = sin(Theta);
  81. vec cTheta = cos(Theta);
  82. vec csigma = cos(sigma);
  83. vec ssigma = sin(sigma);
  84. mat gr = -auxdata.GMe / ((r*auxdata.Re) % (r*auxdata.Re)) % (1 + 1.5*auxdata.J2*(auxdata.ae / (r*auxdata.re)) % (auxdata.ae / (r*auxdata.re)) % (1 - 5 * sphi%sphi)); // gr[列向量]
  85. mat gw = -2 * auxdata.GMe / ((r*auxdata.re) % (r*auxdata.re))*1.5*auxdata.J2 % (auxdata.ae / (r*auxdata.re)) % (auxdata.ae / (r*auxdata.re)) % sphi;
  86. mat gr_Vec[] = { sTheta%gr, cTheta%gr,zeros(n,1) };
  87. mat gw_Vec[] = { gw % (csigma%cTheta%cphi + sTheta%sphi), gw % (cTheta%sphi - csigma%sTheta%cphi), gw%ssigma%cphi };
  88. mat acc_gr[] = { sTheta%gr / g, cTheta%gr / g,zeros(n,1) };
  89. mat acc_gw[] = { gw % (csigma%cTheta%cphi + sTheta%sphi) / g, gw % (cTheta%sphi - csigma%sTheta%cphi) / g, gw%ssigma%cphi / g };
  90. // 惯性力[无量纲]
  91. mat Iner_Vec[] = { cphi%cphi%sTheta % (auxdata.omige*auxdata.omige*r*auxdata.re) - cphi%sphi%csigma%cTheta % (auxdata.omige*auxdata.omige*r*auxdata.re), auxdata.omige*auxdata.omige*r*auxdata.re % (cphi%cphi%cTheta + cphi%sphi%csigma%sTheta), auxdata.omige*auxdata.omige*r*auxdata.re%cphi%sphi%ssigma };
  92. mat acc_Iner[] = { (cphi%cphi%sTheta % (auxdata.omige*auxdata.omige*r*auxdata.re) - cphi%sphi%csigma%cTheta % (auxdata.omige*auxdata.omige*r*auxdata.re)) / g, auxdata.omige*auxdata.omige*r*auxdata.re % (cphi%cphi%cTheta + cphi%sphi%csigma%sTheta) / g, auxdata.omige*auxdata.omige*auxdata.re*r%cphi%sphi%ssigma / g };
  93. // 科式力[无量纲]
  94. mat Cori_Vec[] = { zeros(n,1), 2 * auxdata.omige*v*sqrt(Re*g) % ssigma%cphi, 2 * auxdata.omige*v*sqrt(Re*g) % (csigma%sTheta / cTheta%cphi - sphi) };
  95. mat acc_Cori[] = { zeros(n,1), 2 * auxdata.omige*v*sqrt(Re*g) % ssigma%cphi / g, 2 * auxdata.omige*v*sqrt(Re*g) % (csigma%sTheta / cTheta%cphi - sphi) / g };
  96. // 状态量求导
  97. vec Hdot = v%sTheta;
  98. vec phidot = v%cTheta%csigma / r;
  99. vec lamdot = v%cTheta%ssigma / r / cphi;
  100. vec vdot = acc_P[0] + acc_R[0] + acc_gr[0] + acc_gw[0] + acc_Iner[0] + acc_Cori[0];
  101. vec Thetadot = acc_P[1] / v + acc_R[1] / v + acc_gr[1] / v + v%cTheta / r + acc_gw[1] / v + acc_Iner[1] / v + acc_Cori[1] / v;
  102. vec sigmadot = acc_P[2] / v / cTheta + acc_R[2] / v / cTheta + acc_gr[2] + v%sphi / cphi%cTheta%ssigma / r - acc_gw[2] / v / cTheta + acc_Iner[2] / v / cTheta - acc_Cori[2] / v;
  103. vec mdot = -T / Isp;
  104. vec hdot = v%sTheta;
  105. // 路径约束设置
  106. vec path1 = vdot;
  107. vec path2 = Thetadot;
  108. vec path3 = acos(cos(phi)*cos(a[1] / rad2deg) % cos(a[0] / rad2deg - lam) + sin(phi)*sin(a[1] / rad2deg));
  109. vec path4 = acos(cos(phi)*cos(b[1] / rad2deg) % cos(b[0] / rad2deg - lam) + sin(phi)*sin(b[1] / rad2deg));
  110. vec path5 = acos(cos(phi)*cos(c[1] / rad2deg) % cos(c[0] / rad2deg - lam) + sin(phi)*sin(c[1] / rad2deg));
  111. mat dState0(n, 5);
  112. dState0.col(0) = phidot;
  113. dState0.col(1) = lamdot;
  114. dState0.col(2) = vdot;
  115. dState0.col(3) = sigmadot;
  116. dState0.col(4) = mdot;
  117. mat path0(n, 5);
  118. path0.col(0) = path1;
  119. path0.col(1) = path2;
  120. path0.col(2) = path3;
  121. path0.col(3) = path4;
  122. path0.col(4) = path5;
  123. *dState = dState0;
  124. *path = path0;
  125. (*integrand).reset();
  126. int aaaa = 1;
  127. return 1;
  128. }
  129. int Objective_HCV(input0* input, double* output, mat* event) {
  130. double pi = datum::pi;
  131. double rad2deg = 180 / pi;
  132. int re = 6371004;
  133. //飞行器末端位置
  134. double state[] = { 116,47,500 };
  135. double is1 = (*input).phase.initialstate(0);
  136. double fs1 = (*input).phase.finalstate(0);
  137. double fs2 = (*input).phase.finalstate(1);
  138. (*output) = -(*input).phase.finalstate(4);
  139. mat event_temp;
  140. event_temp.reset();
  141. (*event) = event_temp;
  142. return 1;
  143. }