control_attitude.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "control_attitude.h"
  2. #include "hpm_math.h"
  3. #include "auto_pilot.h"
  4. #include "common.h"
  5. #include "control_throttle.h"
  6. #include "flight_mode.h"
  7. #include "helpler_funtions.h"
  8. #include "lowpass_filter.h"
  9. #include "my_math.h"
  10. #include "params.h"
  11. #include "pilot_navigation.h"
  12. #include "soft_flash.h"
  13. #include "soft_imu.h"
  14. #include "soft_motor_output.h"
  15. #include "soft_rc_input.h"
  16. // 自动模式下机头方向
  17. static float target_wpt_bearing = 0.0f;
  18. float t_roll_last = 0.0f, t_pitch_last = 0.0f;
  19. /**
  20. * @brief 设置自动模式下机头指向
  21. * @param yaw_deg 机头指向
  22. * @return true 设置成功
  23. * @return false 设置失败,机头指向被锁定为指向某点
  24. */
  25. bool set_automode_target_yaw(float yaw_deg)
  26. {
  27. target_wpt_bearing = constrain_float(yaw_deg, 0.0f, 360.0f);
  28. return true;
  29. }
  30. // convert pilot input to lean angles
  31. // To-Do: convert get_pilot_desired_lean_angles to return angles as floats
  32. void get_pilot_desired_lean_angles(short roll_in, short pitch_in)
  33. {
  34. float t_roll = 0.0f, t_pitch = 0.0f, t_total = 0.0f;
  35. float ratio = 0.0f;
  36. float scaler = (float)params_get_value(ParamNum_APMaxTilteAngleDeg) / (float)(500 - RC_DEAD_ZONE);
  37. if (rc_in[RC_CH3] < 1050 && rc_in[RC_CH3] >= 1000)
  38. {
  39. roll_in = 1500;
  40. pitch_in = 1500;
  41. }
  42. // 将杆量转换为目标姿态角
  43. if ((roll_in - 1500) > RC_DEAD_ZONE)
  44. {
  45. t_roll = (roll_in - 1500 - RC_DEAD_ZONE) * scaler;
  46. }
  47. else if ((roll_in - 1500) < -RC_DEAD_ZONE)
  48. {
  49. t_roll = (roll_in - 1500 + RC_DEAD_ZONE) * scaler;
  50. }
  51. else
  52. {
  53. t_roll = 0.0f;
  54. }
  55. if ((pitch_in - 1500) > RC_DEAD_ZONE)
  56. {
  57. t_pitch = (pitch_in - 1500 - RC_DEAD_ZONE) * scaler;
  58. }
  59. else if ((pitch_in - 1500) < -RC_DEAD_ZONE)
  60. {
  61. t_pitch = (pitch_in - 1500 + RC_DEAD_ZONE) * scaler;
  62. }
  63. else
  64. {
  65. t_pitch = 0.0f;
  66. }
  67. t_total = get_norm(t_roll, t_pitch);
  68. // do circular limit
  69. if (t_total > params_get_value(ParamNum_APMaxTilteAngleDeg))
  70. {
  71. ratio = params_get_value(ParamNum_APMaxTilteAngleDeg) / t_total;
  72. t_roll *= ratio;
  73. t_pitch *= ratio;
  74. }
  75. pid_m_roll.angle_t = apply(t_roll, t_roll_last, 4.0f, fast_loop_dt);
  76. pid_m_pitch.angle_t = apply(t_pitch, t_pitch_last, 4.0f, fast_loop_dt);
  77. t_roll_last = pid_m_roll.angle_t;
  78. t_pitch_last = pid_m_pitch.angle_t;
  79. }
  80. // get_pilot_desired_heading - transform pilot's yaw input into a desired yaw
  81. // rate
  82. // returns desired yaw rate in centi-degrees per second
  83. void get_pilot_desired_yaw_angle_fromrc(short yaw_in, float dt)
  84. {
  85. float rc_yaw_rate = 0.0f;
  86. bool yaw_motion = false;
  87. static bool yaw_lock = true;
  88. float scaler = (float)YAW_RATE_MAX / (float)(500 - RC_DEAD_ZONE);
  89. if (rc_in[RC_CH3] < 1050 && rc_in[RC_CH3] >= 1000)
  90. yaw_in = 1500;
  91. //推杆启动慢,默认20启动,导致想小幅度转机头比较难实现
  92. if ((yaw_in - 1500) > RC_DEAD_ZONE)
  93. {
  94. rc_yaw_rate = (yaw_in - 1500 - RC_DEAD_ZONE) * scaler;
  95. yaw_motion = true;
  96. }
  97. else if ((yaw_in - 1500) < -RC_DEAD_ZONE)
  98. {
  99. rc_yaw_rate = (yaw_in - 1500 + RC_DEAD_ZONE) * scaler;
  100. yaw_motion = true;
  101. }
  102. else
  103. {
  104. yaw_motion = false;
  105. rc_yaw_rate = 0.0f;
  106. }
  107. // angle_p有值是才更新目标角度。避免调试时Kp为零,打杆飞机不动,但是angle_t在更新。
  108. if (pid_v_yaw.angle_p > 0)
  109. {
  110. if (yaw_motion == true)
  111. {
  112. yaw_lock = false;
  113. switch (flight_mode)
  114. {
  115. case ATTITUDE:
  116. // rc_yaw_rate 方向是右打为正,和角度相同
  117. pid_m_yaw.angle_t = wrap_360(attitude_head + rc_yaw_rate / pid_v_yaw.angle_p);
  118. break;
  119. default:
  120. pid_m_yaw.angle_t = wrap_360(pid_m_yaw.angle_c + rc_yaw_rate / pid_v_yaw.angle_p);
  121. break;
  122. }
  123. }
  124. else
  125. {
  126. if (yaw_lock == false)
  127. {
  128. yaw_lock = true;
  129. switch (flight_mode)
  130. {
  131. case ATTITUDE:
  132. // gyro_c 的方向是北偏西正,和角度相反
  133. pid_m_yaw.angle_t = wrap_360(attitude_head - 1.0f * pid_m_yaw.gyro_c / pid_v_yaw.angle_p);
  134. break;
  135. default:
  136. pid_m_yaw.angle_t = wrap_360(pid_m_yaw.angle_c - 1.0f * pid_m_yaw.gyro_c / pid_v_yaw.angle_p);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. attitude_head += (-pid_m_yaw.gyro_c * dt);
  143. attitude_head = wrap_360(attitude_head);
  144. if (ground_air_status == ON_GROUND)
  145. {
  146. attitude_head = pid_m_yaw.angle_c;
  147. if (abs_int16(yaw_in - 1500) < RC_DEAD_ZONE)
  148. {
  149. pid_m_yaw.angle_t = pid_m_yaw.angle_c;
  150. }
  151. }
  152. }
  153. /*
  154. 姿态控制部分,角度差装换为期望角速度
  155. */
  156. float attitude_pid_ctl_rp(struct pid_method_rpy *method,
  157. struct pid_value_rpy *value)
  158. {
  159. method->angle_e = method->angle_t - method->angle_c;
  160. // ================ 计算P比例量 ==================
  161. method->angle_p_item = method->angle_e * value->angle_p; //当前值为4.5
  162. method->angle_pid_out = method->angle_p_item;
  163. method->gyro_t = constrain_float(method->angle_pid_out, -80.0f, +80.0f);
  164. /*
  165. 测试快速打杆超调问题,加入目标角速度的单次变化限幅。有明显改善。
  166. 测试期间查看rate_p<±100左右,变化迅速>,rate_i<±30左右,变化缓慢>,rate_d<±100左右,变化迅速>
  167. 测试rate_d的低通系数,系数过低时<5HZ>延时过大,导致很容易超调。
  168. */
  169. method->gyro_t = method->gyro_t_last + constrain_float(method->gyro_t - method->gyro_t_last,
  170. -pid_v_pos.brake_gyro, pid_v_pos.brake_gyro);
  171. method->gyro_t_last = method->gyro_t;
  172. return method->gyro_t;
  173. }
  174. /**************************实现函数********************************************
  175. *函数原型: float Get_Yaw_Error(float set,float currt)
  176. *功  能: 计算航向差
  177. *******************************************************************************/
  178. float get_yaw_error(float target, float currt)
  179. {
  180. float temp;
  181. temp = target - currt;
  182. if (temp < 0)
  183. temp += 360.0f;
  184. if ((temp >= 0.0f) && (temp <= 180.0f))
  185. return temp;
  186. else
  187. return (temp - 360.0f);
  188. }
  189. /*
  190. 姿态控制部分,角度差装换为期望角速度
  191. */
  192. float attitude_pid_ctl_yaw(struct pid_method_rpy *method,
  193. struct pid_value_rpy *value)
  194. {
  195. if (flight_mode == ATTITUDE)
  196. {
  197. // 如果电机输出饱和,触发了航向限制,则赋值一下angle_t
  198. if (MotorOutput_GetYawRestrictionStatus())
  199. pid_m_yaw.angle_t = attitude_head;
  200. method->angle_e = get_yaw_error(method->angle_t, attitude_head);
  201. if (pid_v_yaw.angle_p > 0 &&
  202. fabsf(method->angle_e) > YAW_RATE_MAX / pid_v_yaw.angle_p)
  203. {
  204. method->angle_e = method->angle_e / fabsf(method->angle_e) *
  205. YAW_RATE_MAX / pid_v_yaw.angle_p;
  206. method->angle_t = wrap_360(attitude_head + method->angle_e);
  207. }
  208. }
  209. else
  210. {
  211. // 如果电机输出饱和,触发了航向限制,则赋值一下angle_t
  212. if (MotorOutput_GetYawRestrictionStatus())
  213. pid_m_yaw.angle_t = pid_m_yaw.angle_c;
  214. method->angle_e = get_yaw_error(method->angle_t, method->angle_c);
  215. if (pid_v_yaw.angle_p > 0 &&
  216. fabsf(method->angle_e) > YAW_RATE_MAX / pid_v_yaw.angle_p)
  217. {
  218. method->angle_e = method->angle_e / fabsf(method->angle_e) *
  219. YAW_RATE_MAX / pid_v_yaw.angle_p;
  220. method->angle_t = wrap_360(method->angle_c + method->angle_e);
  221. }
  222. }
  223. method->angle_p_item =
  224. method->angle_e * pid_v_yaw.angle_p * -1.0f; //当前值为4.5
  225. method->gyro_t = method->angle_p_item;
  226. return method->gyro_t;
  227. }
  228. /**
  229. * @brief 在不同的模式平滑计算目标杆量
  230. * 由于SD卡存在延迟现象,此函数应
  231. */
  232. void get_target_yaw_by_flight_mode(unsigned char flight_mode, float dt)
  233. {
  234. if (pilot_mode == PILOT_NORMAL)
  235. {
  236. switch (flight_mode)
  237. {
  238. case ATTITUDE:
  239. get_pilot_desired_yaw_angle_fromrc(rc_in[RC_YAW], dt);
  240. set_automode_target_yaw(pid_m_yaw.angle_t);
  241. break;
  242. case GCS_VEHICLE_LAUNCH:
  243. get_smooth_target_yaw(target_wpt_bearing, dt);
  244. break;
  245. default:
  246. get_smooth_target_yaw(target_wpt_bearing, dt);
  247. break;
  248. }
  249. }
  250. }
  251. /*********************************************************************
  252. 参 数 : argTargetYaw - 航线的方向
  253. 返回值 : 传给航向内环的目标航向
  254. 功 能 : 通过航线的方向计算出传递给内环的目标航向
  255. *********************************************************************/
  256. void get_smooth_target_yaw(float argTargetYaw, float dt)
  257. {
  258. float yawError = wrap_180(argTargetYaw - pid_m_yaw.angle_t);
  259. if (yawError > parinf._par_maxyawrate * dt)
  260. {
  261. pid_m_yaw.angle_t += (parinf._par_maxyawrate * dt);
  262. }
  263. else if (yawError < -1.0f * parinf._par_maxyawrate * dt)
  264. {
  265. pid_m_yaw.angle_t += (-1.0f * parinf._par_maxyawrate * dt);
  266. }
  267. else
  268. {
  269. pid_m_yaw.angle_t = argTargetYaw;
  270. }
  271. pid_m_yaw.angle_t = wrap_360(pid_m_yaw.angle_t);
  272. }