control_attitude.c 9.4 KB

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