pilot_navigation.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "pilot_navigation.h"
  2. #include "hpm_math.h"
  3. #include "auto_pilot.h"
  4. #include "common.h"
  5. #include "control_attitude.h"
  6. #include "control_throttle.h"
  7. #include "flight_mode.h"
  8. #include "geomatry.h"
  9. #include "helpler_funtions.h"
  10. #include "lowpass_filter.h"
  11. #include "my_math.h"
  12. #include "params.h"
  13. #include "soft_flash.h"
  14. #include "soft_gs.h"
  15. #include "soft_imu.h"
  16. #include "soft_motor_output.h"
  17. #include "soft_port_uart4.h"
  18. #include "soft_rc_input.h"
  19. #include "soft_time.h"
  20. #include "ver_config.h"
  21. #include <stdlib.h>
  22. struct rec_pos home_position;
  23. struct rec_pos takeoff_position;
  24. struct rec_pos poshold_position;
  25. struct rec_pos wpphoto_position;
  26. struct rec_pos circlecenter_position;
  27. struct rec_pos target_yaw_lock_position;
  28. // 航线圈数
  29. uint16_t wp_cycle_times = 1;
  30. uint16_t wp_have_cycle_times = 0;
  31. // 机头到达位置的时间
  32. uint32_t wp_reach_yaw_time;
  33. void record_position(int *r_lng, int *r_lat, int lng, int lat)
  34. {
  35. *r_lng = lng;
  36. *r_lat = lat;
  37. }
  38. // @brief 根究纬度计算余弦因子
  39. float earth_longitude_scale(int lat)
  40. {
  41. float earth_lngscale = cosf(fabsf(
  42. (lat / 10000000.0f) * DEG_TO_RAD)); // 1角度 = PI/180 = 0.0174532925弧度
  43. return earth_lngscale;
  44. }
  45. // @brief 计算 wgs48 经纬度两点的距离
  46. // @note 使用纬度余弦因子近似弥补误差
  47. int point_to_point_distance(int lon1, int lat1, int lon2, int lat2)
  48. {
  49. float dist = 0;
  50. double cmLat = 0.0f;
  51. double cmLon = 0.0f;
  52. cmLat = wrap_double((double)(lat2) - (double)(lat1), -180 * 1e7, 180 * 1e7);
  53. cmLon = wrap_double((double)(lon2) - (double)(lon1), -180 * 1e7, 180 * 1e7) * earth_longitude_scale(lat2);
  54. dist= hpm_dsp_sqrt_f32(sq(cmLat) + sq(cmLon));
  55. dist *= LONLAT_TO_CM;
  56. return dist;
  57. }
  58. float cal_tar_vel_z(int t_alt, int c_alt, int acc_up, int acc_down)
  59. {
  60. float tar_vel = pid_v_alt.dist_p * (t_alt - c_alt);
  61. tar_vel = constrain_float(
  62. tar_vel, -1.0f * parinf._par_max_approach_rate_automode * 10.0f,
  63. parinf._par_max_climb_rate_automode * 10.0f);
  64. float delt_vel_up_max = acc_up / 200.0f;
  65. float delt_vel_down_max = acc_down / 200.0f;
  66. if (althold_state == NO_ALT_HOLD)
  67. {
  68. // 如果飞机低于目标高度 1m 外,则慢加快减
  69. if (t_alt - c_alt > 100)
  70. {
  71. if (tar_vel - pid_m_alt.vel_t > 1.0f)
  72. {
  73. tar_vel = pid_m_alt.vel_t + 1.0f;
  74. }
  75. else if (tar_vel - pid_m_alt.vel_t < -delt_vel_down_max)
  76. {
  77. tar_vel = pid_m_alt.vel_t - delt_vel_down_max;
  78. }
  79. }
  80. // 如果飞机高于目标高度 1m 外,则快加慢减
  81. else if (t_alt - c_alt < -100)
  82. {
  83. if (tar_vel - pid_m_alt.vel_t > delt_vel_up_max)
  84. {
  85. tar_vel = pid_m_alt.vel_t + delt_vel_up_max;
  86. }
  87. else if (tar_vel - pid_m_alt.vel_t < -1.0f)
  88. {
  89. tar_vel = pid_m_alt.vel_t - 1.0f;
  90. }
  91. }
  92. // 如果飞机高度在目标高度 1m 内,则按照正常的逻辑计算目标垂直速度
  93. // 如果距离目标高度在 10 m 内,则注意速度不要超过 0.6 * 高度误差
  94. if (abs(t_alt - c_alt) < 1000)
  95. {
  96. float tmp_velz = fabsf((t_alt - c_alt) * 0.6f);
  97. float min_target_velz = (tmp_velz < 100 ? 100 : tmp_velz);
  98. if (fabsf(tar_vel) > min_target_velz)
  99. {
  100. tar_vel = min_target_velz * tar_vel / fabsf(tar_vel);
  101. }
  102. }
  103. }
  104. return tar_vel;
  105. }
  106. bool reset_wp_start_time_flag = false;
  107. uint32_t start_vel_time = 0, time_period = 0;
  108. #define WP_ACC 100.0f
  109. /**
  110. * @brief 根据时间计算起停速度
  111. */
  112. int cal_tar_vel_xy_unac(int h_dist, int r_dist, int min_vel, int max_vel)
  113. {
  114. // 测试方法三:按照固定加速度执行。
  115. float start_vel, stop_vel;
  116. static int init_vel = 0.0f;
  117. if (r_dist < 0)
  118. r_dist = 0;
  119. if (max_vel < 50)
  120. max_vel = 50;
  121. if (reset_wp_start_time_flag == true)
  122. {
  123. start_vel_time = micros();
  124. time_period = 0;
  125. init_vel = ins.horz_vel;
  126. reset_wp_start_time_flag = false;
  127. }
  128. time_period += micros() - start_vel_time;
  129. start_vel_time = micros();
  130. start_vel = init_vel + WP_ACC * time_period / 1000000.0f;
  131. start_vel = constrain_int32(start_vel, 0, max_vel);
  132. if (r_dist > 500)
  133. {
  134. stop_vel = hpm_dsp_sqrt_f32(2 * 0.8f * WP_ACC * (r_dist - 500));
  135. stop_vel = constrain_int32(stop_vel, min_vel, max_vel);
  136. }
  137. else
  138. stop_vel = min_vel;
  139. return min_int32(start_vel, stop_vel);
  140. }
  141. // 偏航距的PID,输出一个速度的角度差
  142. float drift_crosstrack_p = 0.0f,
  143. drift_crosstrack_d = 0.0f, drift_crosstrack_d_last = 0.0f;
  144. float drift_crosstrack_vel_integ = 0.0f;
  145. // 航点总数
  146. unsigned short waypoint_totalnum = 0;
  147. // 目标航点序号
  148. unsigned short tar_wp_no = 0;
  149. // 前一个点到目标点的距离 cm
  150. int wp_prevtotar_distance = 0;
  151. // 前一个点到目标点的方位角 deg
  152. float wp_prevtotar_bearing = 0.0f;
  153. // 前一个点到当前点的距离 cm
  154. int wp_prevtocur_distance = 0;
  155. // 前一个点到当前点的方位角 deg
  156. float wp_prevtocur_bearing = 0.0f;
  157. // 当前点到目标点的的距离 cm
  158. int wp_curtotar_distance = 0;
  159. // 当前点到目标点的的方位角 deg
  160. float wp_curtotar_bearing = 0.0f;
  161. // 开始点到当前点的垂线距离
  162. int wp_prevtocur_verdistance = 0;
  163. // 当前点到目标点的垂线距离,距离有正负
  164. int wp_curtotar_verdistance = 0;
  165. // 当前点到下条航线的垂直距离
  166. int wp_cur_to_next_wpline_verdistance = 0;
  167. bool fly_point_flag = false, update_point_flag = false,
  168. execute_command_flag = false;
  169. /**************************实现函数********************************************
  170. *函数原型: void update_nav_point(void);
  171. *功  能: 更新航点的信息
  172. *******************************************************************************/
  173. // 到达目标航点高度标志位
  174. bool tarpoint_alt_isarrive = false;
  175. // 到达目标航点位置标志位
  176. bool tarpoint_pos_isarrive = false;
  177. // U 型转弯航向转动标志
  178. bool coordinatemode_yaw_turn = false;
  179. float wp_curtotar_bearing_last = 0.0f;
  180. float wp_prevtotar_bearing_last = 0.0f;
  181. unsigned int arrive_point_time = 0;
  182. // 航线目标水平速度
  183. static int desire_vel_xy = 0; // CM/S
  184. // 航线目标垂直速度
  185. static int desire_vel_z = 0; // CM/S