AC_AttitudeControl.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. #include "AC_AttitudeControl.h"
  2. #include <AP_HAL/AP_HAL.h>
  3. extern const AP_HAL::HAL& hal;
  4. #if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
  5. // default gains for Plane
  6. # define AC_ATTITUDE_CONTROL_INPUT_TC_DEFAULT 0.2f // Soft
  7. #else
  8. // default gains for Copter and Sub
  9. # define AC_ATTITUDE_CONTROL_INPUT_TC_DEFAULT 0.15f // Medium
  10. #endif
  11. // table of user settable parameters
  12. const AP_Param::GroupInfo AC_AttitudeControl::var_info[] = {
  13. // 0, 1 were RATE_RP_MAX, RATE_Y_MAX
  14. // @Param: SLEW_YAW
  15. // @DisplayName: Yaw target slew rate
  16. // @Description: Maximum rate the yaw target can be updated in Loiter, RTL, Auto flight modes
  17. // @Units: cdeg/s
  18. // @Range: 500 18000
  19. // @Increment: 100
  20. // @User: Advanced
  21. AP_GROUPINFO("SLEW_YAW", 2, AC_AttitudeControl, _slew_yaw, AC_ATTITUDE_CONTROL_SLEW_YAW_DEFAULT_CDS),
  22. // 3 was for ACCEL_RP_MAX
  23. // @Param: ACCEL_Y_MAX
  24. // @DisplayName: Acceleration Max for Yaw
  25. // @Description: Maximum acceleration in yaw axis
  26. // @Units: cdeg/s/s
  27. // @Range: 0 72000
  28. // @Values: 0:Disabled, 9000:VerySlow, 18000:Slow, 36000:Medium, 54000:Fast
  29. // @Increment: 1000
  30. // @User: Advanced
  31. AP_GROUPINFO("ACCEL_Y_MAX", 4, AC_AttitudeControl, _accel_yaw_max, AC_ATTITUDE_CONTROL_ACCEL_Y_MAX_DEFAULT_CDSS),
  32. // @Param: RATE_FF_ENAB
  33. // @DisplayName: Rate Feedforward Enable
  34. // @Description: Controls whether body-frame rate feedfoward is enabled or disabled
  35. // @Values: 0:Disabled, 1:Enabled
  36. // @User: Advanced
  37. AP_GROUPINFO("RATE_FF_ENAB", 5, AC_AttitudeControl, _rate_bf_ff_enabled, AC_ATTITUDE_CONTROL_RATE_BF_FF_DEFAULT),
  38. // @Param: ACCEL_R_MAX
  39. // @DisplayName: Acceleration Max for Roll
  40. // @Description: Maximum acceleration in roll axis
  41. // @Units: cdeg/s/s
  42. // @Range: 0 180000
  43. // @Increment: 1000
  44. // @Values: 0:Disabled, 30000:VerySlow, 72000:Slow, 108000:Medium, 162000:Fast
  45. // @User: Advanced
  46. AP_GROUPINFO("ACCEL_R_MAX", 6, AC_AttitudeControl, _accel_roll_max, AC_ATTITUDE_CONTROL_ACCEL_RP_MAX_DEFAULT_CDSS),
  47. // @Param: ACCEL_P_MAX
  48. // @DisplayName: Acceleration Max for Pitch
  49. // @Description: Maximum acceleration in pitch axis
  50. // @Units: cdeg/s/s
  51. // @Range: 0 180000
  52. // @Increment: 1000
  53. // @Values: 0:Disabled, 30000:VerySlow, 72000:Slow, 108000:Medium, 162000:Fast
  54. // @User: Advanced
  55. AP_GROUPINFO("ACCEL_P_MAX", 7, AC_AttitudeControl, _accel_pitch_max, AC_ATTITUDE_CONTROL_ACCEL_RP_MAX_DEFAULT_CDSS),
  56. // IDs 8,9,10,11 RESERVED (in use on Solo)
  57. // @Param: ANGLE_BOOST
  58. // @DisplayName: Angle Boost
  59. // @Description: Angle Boost increases output throttle as the vehicle leans to reduce loss of altitude
  60. // @Values: 0:Disabled, 1:Enabled
  61. // @User: Advanced
  62. AP_GROUPINFO("ANGLE_BOOST", 12, AC_AttitudeControl, _angle_boost_enabled, 1),
  63. // @Param: ANG_RLL_P
  64. // @DisplayName: Roll axis angle controller P gain
  65. // @Description: Roll axis angle controller P gain. Converts the error between the desired roll angle and actual angle to a desired roll rate
  66. // @Range: 3.000 12.000
  67. // @Range{Sub}: 0.0 12.000
  68. // @User: Standard
  69. AP_SUBGROUPINFO(_p_angle_roll, "ANG_RLL_", 13, AC_AttitudeControl, AC_P),
  70. // @Param: ANG_PIT_P
  71. // @DisplayName: Pitch axis angle controller P gain
  72. // @Description: Pitch axis angle controller P gain. Converts the error between the desired pitch angle and actual angle to a desired pitch rate
  73. // @Range: 3.000 12.000
  74. // @Range{Sub}: 0.0 12.000
  75. // @User: Standard
  76. AP_SUBGROUPINFO(_p_angle_pitch, "ANG_PIT_", 14, AC_AttitudeControl, AC_P),
  77. // @Param: ANG_YAW_P
  78. // @DisplayName: Yaw axis angle controller P gain
  79. // @Description: Yaw axis angle controller P gain. Converts the error between the desired yaw angle and actual angle to a desired yaw rate
  80. // @Range: 3.000 6.000
  81. // @Range{Sub}: 0.0 6.000
  82. // @User: Standard
  83. AP_SUBGROUPINFO(_p_angle_yaw, "ANG_YAW_", 15, AC_AttitudeControl, AC_P),
  84. // @Param: ANG_LIM_TC
  85. // @DisplayName: Angle Limit (to maintain altitude) Time Constant
  86. // @Description: Angle Limit (to maintain altitude) Time Constant
  87. // @Range: 0.5 10.0
  88. // @User: Advanced
  89. AP_GROUPINFO("ANG_LIM_TC", 16, AC_AttitudeControl, _angle_limit_tc, AC_ATTITUDE_CONTROL_ANGLE_LIMIT_TC_DEFAULT),
  90. // @Param: RATE_R_MAX
  91. // @DisplayName: Angular Velocity Max for Roll
  92. // @Description: Maximum angular velocity in roll axis
  93. // @Units: deg/s
  94. // @Range: 0 1080
  95. // @Increment: 1
  96. // @Values: 0:Disabled, 360:Slow, 720:Medium, 1080:Fast
  97. // @User: Advanced
  98. AP_GROUPINFO("RATE_R_MAX", 17, AC_AttitudeControl, _ang_vel_roll_max, 0.0f),
  99. // @Param: RATE_P_MAX
  100. // @DisplayName: Angular Velocity Max for Pitch
  101. // @Description: Maximum angular velocity in pitch axis
  102. // @Units: deg/s
  103. // @Range: 0 1080
  104. // @Increment: 1
  105. // @Values: 0:Disabled, 360:Slow, 720:Medium, 1080:Fast
  106. // @User: Advanced
  107. AP_GROUPINFO("RATE_P_MAX", 18, AC_AttitudeControl, _ang_vel_pitch_max, 0.0f),
  108. // @Param: RATE_Y_MAX
  109. // @DisplayName: Angular Velocity Max for Yaw
  110. // @Description: Maximum angular velocity in yaw axis
  111. // @Units: deg/s
  112. // @Range: 0 1080
  113. // @Increment: 1
  114. // @Values: 0:Disabled, 360:Slow, 720:Medium, 1080:Fast
  115. // @User: Advanced
  116. AP_GROUPINFO("RATE_Y_MAX", 19, AC_AttitudeControl, _ang_vel_yaw_max, 0.0f),
  117. // @Param: INPUT_TC
  118. // @DisplayName: Attitude control input time constant
  119. // @Description: Attitude control input time constant. Low numbers lead to sharper response, higher numbers to softer response
  120. // @Units: s
  121. // @Range: 0 1
  122. // @Increment: 0.01
  123. // @Values: 0.5:Very Soft, 0.2:Soft, 0.15:Medium, 0.1:Crisp, 0.05:Very Crisp
  124. // @User: Standard
  125. AP_GROUPINFO("INPUT_TC", 20, AC_AttitudeControl, _input_tc, AC_ATTITUDE_CONTROL_INPUT_TC_DEFAULT),
  126. AP_GROUPEND
  127. };
  128. // Ensure attitude controller have zero errors to relax rate controller output
  129. void AC_AttitudeControl::relax_attitude_controllers()
  130. {
  131. // Initialize the attitude variables to the current attitude
  132. _ahrs.get_quat_body_to_ned(_attitude_target_quat);
  133. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  134. _attitude_ang_error.initialise();
  135. // Initialize the angular rate variables to the current rate
  136. _attitude_target_ang_vel = _ahrs.get_gyro();
  137. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  138. _rate_target_ang_vel = _ahrs.get_gyro();
  139. // Initialize remaining variables
  140. _thrust_error_angle = 0.0f;
  141. // Reset the PID filters
  142. get_rate_roll_pid().reset_filter();
  143. get_rate_pitch_pid().reset_filter();
  144. get_rate_yaw_pid().reset_filter();
  145. // Reset the I terms
  146. reset_rate_controller_I_terms();
  147. }
  148. void AC_AttitudeControl::reset_rate_controller_I_terms()
  149. {
  150. get_rate_roll_pid().reset_I();
  151. get_rate_pitch_pid().reset_I();
  152. get_rate_yaw_pid().reset_I();
  153. }
  154. // The attitude controller works around the concept of the desired attitude, target attitude
  155. // and measured attitude. The desired attitude is the attitude input into the attitude controller
  156. // that expresses where the higher level code would like the aircraft to move to. The target attitude is moved
  157. // to the desired attitude with jerk, acceleration, and velocity limits. The target angular velocities are fed
  158. // directly into the rate controllers. The angular error between the measured attitude and the target attitude is
  159. // fed into the angle controller and the output of the angle controller summed at the input of the rate controllers.
  160. // By feeding the target angular velocity directly into the rate controllers the measured and target attitudes
  161. // remain very close together.
  162. //
  163. // All input functions below follow the same procedure
  164. // 1. define the desired attitude the aircraft should attempt to achieve using the input variables
  165. // 2. using the desired attitude and input variables, define the target angular velocity so that it should
  166. // move the target attitude towards the desired attitude
  167. // 3. if _rate_bf_ff_enabled is not being used then make the target attitude
  168. // and target angular velocities equal to the desired attitude and desired angular velocities.
  169. // 4. ensure _attitude_target_quat, _attitude_target_euler_angle, _attitude_target_euler_rate and
  170. // _attitude_target_ang_vel have been defined. This ensures input modes can be changed without discontinuity.
  171. // 5. attitude_controller_run_quat is then run to pass the target angular velocities to the rate controllers and
  172. // integrate them into the target attitude. Any errors between the target attitude and the measured attitude are
  173. // corrected by first correcting the thrust vector until the angle between the target thrust vector measured
  174. // trust vector drops below 2*AC_ATTITUDE_THRUST_ERROR_ANGLE. At this point the heading is also corrected.
  175. // Command a Quaternion attitude with feedforward and smoothing
  176. void AC_AttitudeControl::input_quaternion(Quaternion attitude_desired_quat)
  177. {
  178. // calculate the attitude target euler angles
  179. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  180. Quaternion attitude_error_quat = _attitude_target_quat.inverse() * attitude_desired_quat;
  181. Vector3f attitude_error_angle;
  182. attitude_error_quat.to_axis_angle(attitude_error_angle);
  183. if (_rate_bf_ff_enabled) {
  184. // When acceleration limiting and feedforward are enabled, the sqrt controller is used to compute an euler
  185. // angular velocity that will cause the euler angle to smoothly stop at the input angle with limited deceleration
  186. // and an exponential decay specified by _input_tc at the end.
  187. _attitude_target_ang_vel.x = input_shaping_angle(wrap_PI(attitude_error_angle.x), _input_tc, get_accel_roll_max_radss(), _attitude_target_ang_vel.x, _dt);
  188. _attitude_target_ang_vel.y = input_shaping_angle(wrap_PI(attitude_error_angle.y), _input_tc, get_accel_pitch_max_radss(), _attitude_target_ang_vel.y, _dt);
  189. _attitude_target_ang_vel.z = input_shaping_angle(wrap_PI(attitude_error_angle.z), _input_tc, get_accel_yaw_max_radss(), _attitude_target_ang_vel.z, _dt);
  190. // Limit the angular velocity
  191. ang_vel_limit(_attitude_target_ang_vel, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), radians(_ang_vel_yaw_max));
  192. // Convert body-frame angular velocity into euler angle derivative of desired attitude
  193. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  194. } else {
  195. _attitude_target_quat = attitude_desired_quat;
  196. // Set rate feedforward requests to zero
  197. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  198. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  199. }
  200. // Call quaternion attitude controller
  201. attitude_controller_run_quat();
  202. }
  203. // Command an euler roll and pitch angle and an euler yaw rate with angular velocity feedforward and smoothing
  204. void AC_AttitudeControl::input_euler_angle_roll_pitch_euler_rate_yaw(float euler_roll_angle_cd, float euler_pitch_angle_cd, float euler_yaw_rate_cds)
  205. {
  206. // Convert from centidegrees on public interface to radians
  207. float euler_roll_angle = radians(euler_roll_angle_cd * 0.01f);
  208. float euler_pitch_angle = radians(euler_pitch_angle_cd * 0.01f);
  209. float euler_yaw_rate = radians(euler_yaw_rate_cds * 0.01f);
  210. // calculate the attitude target euler angles
  211. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  212. // Add roll trim to compensate tail rotor thrust in heli (will return zero on multirotors)
  213. euler_roll_angle += get_roll_trim_rad();
  214. if (_rate_bf_ff_enabled) {
  215. // translate the roll pitch and yaw acceleration limits to the euler axis
  216. Vector3f euler_accel = euler_accel_limit(_attitude_target_euler_angle, Vector3f(get_accel_roll_max_radss(), get_accel_pitch_max_radss(), get_accel_yaw_max_radss()));
  217. // When acceleration limiting and feedforward are enabled, the sqrt controller is used to compute an euler
  218. // angular velocity that will cause the euler angle to smoothly stop at the input angle with limited deceleration
  219. // and an exponential decay specified by smoothing_gain at the end.
  220. _attitude_target_euler_rate.x = input_shaping_angle(wrap_PI(euler_roll_angle - _attitude_target_euler_angle.x), _input_tc, euler_accel.x, _attitude_target_euler_rate.x, _dt);
  221. _attitude_target_euler_rate.y = input_shaping_angle(wrap_PI(euler_pitch_angle - _attitude_target_euler_angle.y), _input_tc, euler_accel.y, _attitude_target_euler_rate.y, _dt);
  222. // When yaw acceleration limiting is enabled, the yaw input shaper constrains angular acceleration about the yaw axis, slewing
  223. // the output rate towards the input rate.
  224. _attitude_target_euler_rate.z = input_shaping_ang_vel(_attitude_target_euler_rate.z, euler_yaw_rate, euler_accel.z, _dt);
  225. // Convert euler angle derivative of desired attitude into a body-frame angular velocity vector for feedforward
  226. euler_rate_to_ang_vel(_attitude_target_euler_angle, _attitude_target_euler_rate, _attitude_target_ang_vel);
  227. // Limit the angular velocity
  228. ang_vel_limit(_attitude_target_ang_vel, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), radians(_ang_vel_yaw_max));
  229. // Convert body-frame angular velocity into euler angle derivative of desired attitude
  230. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  231. } else {
  232. // When feedforward is not enabled, the target euler angle is input into the target and the feedforward rate is zeroed.
  233. _attitude_target_euler_angle.x = euler_roll_angle;
  234. _attitude_target_euler_angle.y = euler_pitch_angle;
  235. _attitude_target_euler_angle.z += euler_yaw_rate * _dt;
  236. // Compute quaternion target attitude
  237. _attitude_target_quat.from_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  238. // Set rate feedforward requests to zero
  239. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  240. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  241. }
  242. // Call quaternion attitude controller
  243. attitude_controller_run_quat();
  244. }
  245. void AC_AttitudeControl::input_euler_angle_roll_pitch_yaw_quat_control(float euler_roll_angle_cd, float euler_pitch_angle_cd, float euler_yaw_angle_cd, bool slew_yaw)
  246. {
  247. float euler_roll_angle = radians(euler_roll_angle_cd * 0.01f);
  248. float euler_pitch_angle = radians(euler_pitch_angle_cd * 0.01f);
  249. float euler_yaw_angle = radians(euler_yaw_angle_cd * 0.01f);
  250. //
  251. Quaternion input_quat;
  252. input_quat.from_euler(euler_roll_angle,euler_pitch_angle,euler_yaw_angle);//输入得欧拉角转换的4元数 e - b
  253. input_quat.normalize();//归一化
  254. /*static int i = 0;
  255. i++;
  256. if (i>500)
  257. {
  258. i=0;
  259. gcs().send_text(MAV_SEVERITY_INFO, "Qmatrix_1_1 %f %f %f %f",Qmatrix_1.a.M0,Qmatrix_1.a.M1,Qmatrix_1.a.M2,Qmatrix_1.a.M3);
  260. gcs().send_text(MAV_SEVERITY_INFO, "Qmatrix_1_2 %f %f %f %f",Qmatrix_1.b.M0,Qmatrix_1.b.M1,Qmatrix_1.b.M2,Qmatrix_1.b.M3);
  261. gcs().send_text(MAV_SEVERITY_INFO, "Qmatrix_1_3 %f %f %f %f",Qmatrix_1.c.M0,Qmatrix_1.c.M1,Qmatrix_1.c.M2,Qmatrix_1.c.M3);
  262. gcs().send_text(MAV_SEVERITY_INFO, "Qmatrix_1_4 %f %f %f %f",Qmatrix_1.d.M0,Qmatrix_1.d.M1,Qmatrix_1.d.M2,Qmatrix_1.d.M3);
  263. }*/
  264. if (_rate_bf_ff_enabled) {
  265. //方案1--------------------------------
  266. //q' = 1/2*Q*W
  267. //q(t+dt)-q(t) = 1/2*Q*W*dt
  268. //2*Q(-1)*(q(t+dt)-q(t)) = w*dt
  269. //W = [0,Wx,Wy,Wz]得到机体三轴速度 在期望机体坐标系
  270. /*Matrix4f Qmatrix(_attitude_target_quat.q1, -_attitude_target_quat.q2, -_attitude_target_quat.q3, -_attitude_target_quat.q4,
  271. _attitude_target_quat.q2, _attitude_target_quat.q1, -_attitude_target_quat.q4, _attitude_target_quat.q3,
  272. _attitude_target_quat.q3, _attitude_target_quat.q4, _attitude_target_quat.q1, -_attitude_target_quat.q2,
  273. _attitude_target_quat.q4, -_attitude_target_quat.q3, _attitude_target_quat.q2, _attitude_target_quat.q1);
  274. Matrix4f Qmatrix_1;
  275. Qmatrix.inverse(Qmatrix_1);
  276. Quaternion error_quat = input_quat - _attitude_target_quat;
  277. Vector4f error_vec(error_quat.q1,error_quat.q2,error_quat.q3,error_quat.q4);
  278. Vector4f w4 = Qmatrix_1*error_vec;
  279. Vector3f error_angle3(2*w4.M1,2*w4.M2,2*w4.M3);*/
  280. //方案2------------------------------
  281. //Q' = 1/2*W*Q 齐次方程的通解Q(k+1) = e(1/2*A*dt) * Q(k) (1/2*A*dt) 上标 ,
  282. //根据2阶泰勒展开 近似计算,经过化简
  283. //如下计算 error_angle.M0 =1-(M1^2+M2^2+M3^2)/8 这里是近似值
  284. Matrix4f Qmatrix(_attitude_target_quat.q1,-0.5*_attitude_target_quat.q2,-0.5*_attitude_target_quat.q3, -0.5*_attitude_target_quat.q4,
  285. _attitude_target_quat.q2,0.5*_attitude_target_quat.q1,-0.5*_attitude_target_quat.q4, 0.5*_attitude_target_quat.q3,
  286. _attitude_target_quat.q3,0.5*_attitude_target_quat.q4, 0.5*_attitude_target_quat.q1, -0.5*_attitude_target_quat.q2,
  287. _attitude_target_quat.q4,-0.5*_attitude_target_quat.q3, 0.5*_attitude_target_quat.q2, 0.5*_attitude_target_quat.q1);
  288. Matrix4f Qmatrix_1;
  289. Vector4f error_angle;
  290. Qmatrix.inverse(Qmatrix_1);
  291. Vector4f _attitude_target_vec(input_quat.q1,input_quat.q2,input_quat.q3,input_quat.q4);
  292. error_angle = Qmatrix_1*_attitude_target_vec;
  293. //Vector3f error_angle3(error_angle.M1,error_angle.M2,error_angle.M3);//wrap_PI
  294. Vector3f error_angle3(wrap_PI(error_angle.M1),wrap_PI(error_angle.M2),wrap_PI(error_angle.M3));
  295. //限幅
  296. //注意这里的限幅原来的定义都是对应对地的 不是对机体坐标系的,这里直接拿来用了
  297. //从角度误差计算角速度
  298. _attitude_target_ang_vel.x = input_shaping_angle(error_angle3.x, _input_tc, get_accel_roll_max_radss(), _attitude_target_ang_vel.x, _dt);
  299. _attitude_target_ang_vel.y = input_shaping_angle(error_angle3.y, _input_tc, get_accel_pitch_max_radss(), _attitude_target_ang_vel.y, _dt);
  300. _attitude_target_ang_vel.z = input_shaping_angle(error_angle3.z, _input_tc, get_accel_yaw_max_radss(), _attitude_target_ang_vel.z, _dt);
  301. if (slew_yaw) {
  302. _attitude_target_ang_vel.z = constrain_float(_attitude_target_ang_vel.z, -get_slew_yaw_rads(), get_slew_yaw_rads());
  303. }
  304. ang_vel_limit(_attitude_target_ang_vel, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), radians(_ang_vel_yaw_max));
  305. }else {
  306. if (slew_yaw){
  307. float angle_error = constrain_float(wrap_PI(euler_yaw_angle - _ahrs.yaw), -get_slew_yaw_rads() * _dt, get_slew_yaw_rads() * _dt);
  308. euler_yaw_angle = _ahrs.yaw + angle_error;
  309. }
  310. _attitude_target_quat.from_euler(euler_roll_angle, euler_pitch_angle, euler_yaw_angle);
  311. // Set rate feedforward requests to zero
  312. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  313. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  314. }
  315. attitude_controller_run_quat();
  316. }
  317. // Command an euler roll, pitch and yaw angle with angular velocity feedforward and smoothing
  318. void AC_AttitudeControl::input_euler_angle_roll_pitch_yaw(float euler_roll_angle_cd, float euler_pitch_angle_cd, float euler_yaw_angle_cd, bool slew_yaw)
  319. {
  320. // Convert from centidegrees on public interface to radians
  321. float euler_roll_angle = radians(euler_roll_angle_cd * 0.01f);
  322. float euler_pitch_angle = radians(euler_pitch_angle_cd * 0.01f);
  323. float euler_yaw_angle = radians(euler_yaw_angle_cd * 0.01f);
  324. // calculate the attitude target euler angles
  325. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  326. // Add roll trim to compensate tail rotor thrust in heli (will return zero on multirotors)
  327. euler_roll_angle += get_roll_trim_rad();
  328. if (_rate_bf_ff_enabled) {
  329. // translate the roll pitch and yaw acceleration limits to the euler axis
  330. Vector3f euler_accel = euler_accel_limit(_attitude_target_euler_angle, Vector3f(get_accel_roll_max_radss(), get_accel_pitch_max_radss(), get_accel_yaw_max_radss()));
  331. // When acceleration limiting and feedforward are enabled, the sqrt controller is used to compute an euler
  332. // angular velocity that will cause the euler angle to smoothly stop at the input angle with limited deceleration
  333. // and an exponential decay specified by _input_tc at the end.
  334. _attitude_target_euler_rate.x = input_shaping_angle(wrap_PI(euler_roll_angle - _attitude_target_euler_angle.x), _input_tc, euler_accel.x, _attitude_target_euler_rate.x, _dt);
  335. _attitude_target_euler_rate.y = input_shaping_angle(wrap_PI(euler_pitch_angle - _attitude_target_euler_angle.y), _input_tc, euler_accel.y, _attitude_target_euler_rate.y, _dt);
  336. _attitude_target_euler_rate.z = input_shaping_angle(wrap_PI(euler_yaw_angle - _attitude_target_euler_angle.z), _input_tc, euler_accel.z, _attitude_target_euler_rate.z, _dt);
  337. if (slew_yaw) {
  338. _attitude_target_euler_rate.z = constrain_float(_attitude_target_euler_rate.z, -get_slew_yaw_rads(), get_slew_yaw_rads());
  339. }
  340. // Convert euler angle derivative of desired attitude into a body-frame angular velocity vector for feedforward
  341. euler_rate_to_ang_vel(_attitude_target_euler_angle, _attitude_target_euler_rate, _attitude_target_ang_vel);
  342. // Limit the angular velocity
  343. ang_vel_limit(_attitude_target_ang_vel, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), radians(_ang_vel_yaw_max));
  344. // Convert body-frame angular velocity into euler angle derivative of desired attitude
  345. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  346. } else {
  347. // When feedforward is not enabled, the target euler angle is input into the target and the feedforward rate is zeroed.
  348. _attitude_target_euler_angle.x = euler_roll_angle;
  349. _attitude_target_euler_angle.y = euler_pitch_angle;
  350. if (slew_yaw) {
  351. // Compute constrained angle error
  352. float angle_error = constrain_float(wrap_PI(euler_yaw_angle - _attitude_target_euler_angle.z), -get_slew_yaw_rads() * _dt, get_slew_yaw_rads() * _dt);
  353. // Update attitude target from constrained angle error
  354. _attitude_target_euler_angle.z = wrap_PI(angle_error + _attitude_target_euler_angle.z);
  355. } else {
  356. _attitude_target_euler_angle.z = euler_yaw_angle;
  357. }
  358. // Compute quaternion target attitude
  359. _attitude_target_quat.from_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  360. // Set rate feedforward requests to zero
  361. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  362. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  363. }
  364. // Call quaternion attitude controller
  365. attitude_controller_run_quat();
  366. }
  367. // Command euler pitch and yaw angles and roll rate (used only by tailsitter quadplanes)
  368. // Multicopter style controls: roll stick is tailsitter bodyframe yaw in hover
  369. void AC_AttitudeControl::input_euler_rate_yaw_euler_angle_pitch_bf_roll_m(float euler_yaw_rate_cds, float euler_pitch_cd, float body_roll_cd)
  370. {
  371. // Convert from centidegrees on public interface to radians
  372. float euler_yaw_rate = radians(euler_yaw_rate_cds*0.01f);
  373. float euler_pitch = radians(constrain_float(euler_pitch_cd * 0.01f, -90.0f, 90.0f));
  374. float body_roll = radians(constrain_float(body_roll_cd * 0.01f, -90.0f, 90.0f));
  375. // Compute attitude error
  376. Quaternion attitude_vehicle_quat;
  377. Quaternion error_quat;
  378. attitude_vehicle_quat.from_rotation_matrix(_ahrs.get_rotation_body_to_ned());
  379. error_quat = attitude_vehicle_quat.inverse() * _attitude_target_quat;
  380. Vector3f att_error;
  381. error_quat.to_axis_angle(att_error);
  382. // limit yaw error
  383. if (fabsf(att_error.z) < AC_ATTITUDE_THRUST_ERROR_ANGLE) {
  384. // update heading
  385. _attitude_target_euler_angle.z = wrap_PI(_attitude_target_euler_angle.z + euler_yaw_rate * _dt);
  386. }
  387. // init attitude target to desired euler yaw and pitch with zero roll
  388. _attitude_target_quat.from_euler(0, euler_pitch, _attitude_target_euler_angle.z);
  389. const float cpitch = cosf(euler_pitch);
  390. const float spitch = fabsf(sinf(euler_pitch));
  391. // apply body-frame yaw/roll (this is roll/yaw for a tailsitter in forward flight)
  392. // rotate body_roll axis by |sin(pitch angle)|
  393. Quaternion bf_roll_Q;
  394. bf_roll_Q.from_axis_angle(Vector3f(0, 0, spitch * body_roll));
  395. // rotate body_yaw axis by cos(pitch angle)
  396. Quaternion bf_yaw_Q;
  397. bf_yaw_Q.from_axis_angle(Vector3f(-cpitch * body_roll, 0, 0));
  398. _attitude_target_quat = _attitude_target_quat * bf_roll_Q * bf_yaw_Q;
  399. // _attitude_target_euler_angle roll and pitch: Note: roll/yaw will be indeterminate when pitch is near +/-90
  400. // These should be used only for logging target eulers, with the caveat noted above.
  401. // Also note that _attitude_target_quat.from_euler() should only be used in special circumstances
  402. // such as when attitude is specified directly in terms of Euler angles.
  403. // _attitude_target_euler_angle.x = _attitude_target_quat.get_euler_roll();
  404. // _attitude_target_euler_angle.y = euler_pitch;
  405. // Set rate feedforward requests to zero
  406. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  407. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  408. // Compute attitude error
  409. error_quat = attitude_vehicle_quat.inverse() * _attitude_target_quat;
  410. error_quat.to_axis_angle(att_error);
  411. // Compute the angular velocity target from the attitude error
  412. _rate_target_ang_vel = update_ang_vel_target_from_att_error(att_error);
  413. }
  414. // Command euler pitch and yaw angles and roll rate (used only by tailsitter quadplanes)
  415. // Plane style controls: yaw stick is tailsitter bodyframe yaw in hover
  416. void AC_AttitudeControl::input_euler_rate_yaw_euler_angle_pitch_bf_roll_p(float euler_yaw_rate_cds, float euler_pitch_cd, float body_roll_cd)
  417. {
  418. // Convert from centidegrees on public interface to radians
  419. float euler_yaw_rate = radians(euler_yaw_rate_cds*0.01f);
  420. float euler_pitch = radians(constrain_float(euler_pitch_cd * 0.01f, -90.0f, 90.0f));
  421. float body_roll = radians(constrain_float(body_roll_cd * 0.01f, -90.0f, 90.0f));
  422. const float cpitch = cosf(euler_pitch);
  423. const float spitch = fabsf(sinf(euler_pitch));
  424. // Compute attitude error
  425. Quaternion attitude_vehicle_quat;
  426. Quaternion error_quat;
  427. attitude_vehicle_quat.from_rotation_matrix(_ahrs.get_rotation_body_to_ned());
  428. error_quat = attitude_vehicle_quat.inverse() * _attitude_target_quat;
  429. Vector3f att_error;
  430. error_quat.to_axis_angle(att_error);
  431. // limit yaw error
  432. if (fabsf(att_error.z) < AC_ATTITUDE_THRUST_ERROR_ANGLE) {
  433. // update heading
  434. float yaw_rate = euler_yaw_rate * spitch + body_roll * cpitch;
  435. _attitude_target_euler_angle.z = wrap_PI(_attitude_target_euler_angle.z + yaw_rate * _dt);
  436. }
  437. // init attitude target to desired euler yaw and pitch with zero roll
  438. _attitude_target_quat.from_euler(0, euler_pitch, _attitude_target_euler_angle.z);
  439. // apply body-frame yaw/roll (this is roll/yaw for a tailsitter in forward flight)
  440. // rotate body_roll axis by |sin(pitch angle)|
  441. Quaternion bf_roll_Q;
  442. bf_roll_Q.from_axis_angle(Vector3f(0, 0, spitch * body_roll));
  443. // rotate body_yaw axis by cos(pitch angle)
  444. Quaternion bf_yaw_Q;
  445. bf_yaw_Q.from_axis_angle(Vector3f(cpitch, 0, 0), euler_yaw_rate);
  446. _attitude_target_quat = _attitude_target_quat * bf_roll_Q * bf_yaw_Q;
  447. // _attitude_target_euler_angle roll and pitch: Note: roll/yaw will be indeterminate when pitch is near +/-90
  448. // These should be used only for logging target eulers, with the caveat noted above
  449. // Also note that _attitude_target_quat.from_euler() should only be used in special circumstances
  450. // such as when attitude is specified directly in terms of Euler angles.
  451. // _attitude_target_euler_angle.x = _attitude_target_quat.get_euler_roll();
  452. // _attitude_target_euler_angle.y = euler_pitch;
  453. // Set rate feedforward requests to zero
  454. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  455. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  456. // Compute attitude error
  457. error_quat = attitude_vehicle_quat.inverse() * _attitude_target_quat;
  458. error_quat.to_axis_angle(att_error);
  459. // Compute the angular velocity target from the attitude error
  460. _rate_target_ang_vel = update_ang_vel_target_from_att_error(att_error);
  461. }
  462. // Command an euler roll, pitch, and yaw rate with angular velocity feedforward and smoothing
  463. void AC_AttitudeControl::input_euler_rate_roll_pitch_yaw(float euler_roll_rate_cds, float euler_pitch_rate_cds, float euler_yaw_rate_cds)
  464. {
  465. // Convert from centidegrees on public interface to radians
  466. float euler_roll_rate = radians(euler_roll_rate_cds * 0.01f);
  467. float euler_pitch_rate = radians(euler_pitch_rate_cds * 0.01f);
  468. float euler_yaw_rate = radians(euler_yaw_rate_cds * 0.01f);
  469. // calculate the attitude target euler angles
  470. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  471. if (_rate_bf_ff_enabled) {
  472. // translate the roll pitch and yaw acceleration limits to the euler axis
  473. Vector3f euler_accel = euler_accel_limit(_attitude_target_euler_angle, Vector3f(get_accel_roll_max_radss(), get_accel_pitch_max_radss(), get_accel_yaw_max_radss()));
  474. // When acceleration limiting is enabled, the input shaper constrains angular acceleration, slewing
  475. // the output rate towards the input rate.
  476. _attitude_target_euler_rate.x = input_shaping_ang_vel(_attitude_target_euler_rate.x, euler_roll_rate, euler_accel.x, _dt);
  477. _attitude_target_euler_rate.y = input_shaping_ang_vel(_attitude_target_euler_rate.y, euler_pitch_rate, euler_accel.y, _dt);
  478. _attitude_target_euler_rate.z = input_shaping_ang_vel(_attitude_target_euler_rate.z, euler_yaw_rate, euler_accel.z, _dt);
  479. // Convert euler angle derivative of desired attitude into a body-frame angular velocity vector for feedforward
  480. euler_rate_to_ang_vel(_attitude_target_euler_angle, _attitude_target_euler_rate, _attitude_target_ang_vel);
  481. } else {
  482. // When feedforward is not enabled, the target euler angle is input into the target and the feedforward rate is zeroed.
  483. // Pitch angle is restricted to +- 85.0 degrees to avoid gimbal lock discontinuities.
  484. _attitude_target_euler_angle.x = wrap_PI(_attitude_target_euler_angle.x + euler_roll_rate * _dt);
  485. _attitude_target_euler_angle.y = constrain_float(_attitude_target_euler_angle.y + euler_pitch_rate * _dt, radians(-85.0f), radians(85.0f));
  486. _attitude_target_euler_angle.z = wrap_2PI(_attitude_target_euler_angle.z + euler_yaw_rate * _dt);
  487. // Set rate feedforward requests to zero
  488. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  489. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  490. // Compute quaternion target attitude
  491. _attitude_target_quat.from_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  492. }
  493. // Call quaternion attitude controller
  494. attitude_controller_run_quat();
  495. }
  496. // Command an angular velocity with angular velocity feedforward and smoothing
  497. void AC_AttitudeControl::input_rate_bf_roll_pitch_yaw(float roll_rate_bf_cds, float pitch_rate_bf_cds, float yaw_rate_bf_cds)
  498. {
  499. // Convert from centidegrees on public interface to radians
  500. float roll_rate_rads = radians(roll_rate_bf_cds * 0.01f);
  501. float pitch_rate_rads = radians(pitch_rate_bf_cds * 0.01f);
  502. float yaw_rate_rads = radians(yaw_rate_bf_cds * 0.01f);
  503. // calculate the attitude target euler angles
  504. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  505. if (_rate_bf_ff_enabled) {
  506. // Compute acceleration-limited body frame rates
  507. // When acceleration limiting is enabled, the input shaper constrains angular acceleration about the axis, slewing
  508. // the output rate towards the input rate.
  509. _attitude_target_ang_vel.x = input_shaping_ang_vel(_attitude_target_ang_vel.x, roll_rate_rads, get_accel_roll_max_radss(), _dt);
  510. _attitude_target_ang_vel.y = input_shaping_ang_vel(_attitude_target_ang_vel.y, pitch_rate_rads, get_accel_pitch_max_radss(), _dt);
  511. _attitude_target_ang_vel.z = input_shaping_ang_vel(_attitude_target_ang_vel.z, yaw_rate_rads, get_accel_yaw_max_radss(), _dt);
  512. // Convert body-frame angular velocity into euler angle derivative of desired attitude
  513. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  514. } else {
  515. // When feedforward is not enabled, the quaternion is calculated and is input into the target and the feedforward rate is zeroed.
  516. Quaternion attitude_target_update_quat;
  517. attitude_target_update_quat.from_axis_angle(Vector3f(roll_rate_rads * _dt, pitch_rate_rads * _dt, yaw_rate_rads * _dt));
  518. _attitude_target_quat = _attitude_target_quat * attitude_target_update_quat;
  519. _attitude_target_quat.normalize();
  520. // Set rate feedforward requests to zero
  521. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  522. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  523. }
  524. // Call quaternion attitude controller
  525. attitude_controller_run_quat();
  526. }
  527. // Command an angular velocity with angular velocity smoothing using rate loops only with no attitude loop stabilization
  528. void AC_AttitudeControl::input_rate_bf_roll_pitch_yaw_2(float roll_rate_bf_cds, float pitch_rate_bf_cds, float yaw_rate_bf_cds)
  529. {
  530. // Convert from centidegrees on public interface to radians
  531. float roll_rate_rads = radians(roll_rate_bf_cds * 0.01f);
  532. float pitch_rate_rads = radians(pitch_rate_bf_cds * 0.01f);
  533. float yaw_rate_rads = radians(yaw_rate_bf_cds * 0.01f);
  534. // Compute acceleration-limited body frame rates
  535. // When acceleration limiting is enabled, the input shaper constrains angular acceleration about the axis, slewing
  536. // the output rate towards the input rate.
  537. _attitude_target_ang_vel.x = input_shaping_ang_vel(_attitude_target_ang_vel.x, roll_rate_rads, get_accel_roll_max_radss(), _dt);
  538. _attitude_target_ang_vel.y = input_shaping_ang_vel(_attitude_target_ang_vel.y, pitch_rate_rads, get_accel_pitch_max_radss(), _dt);
  539. _attitude_target_ang_vel.z = input_shaping_ang_vel(_attitude_target_ang_vel.z, yaw_rate_rads, get_accel_yaw_max_radss(), _dt);
  540. // Update the unused targets attitude based on current attitude to condition mode change
  541. _ahrs.get_quat_body_to_ned(_attitude_target_quat);
  542. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  543. // Convert body-frame angular velocity into euler angle derivative of desired attitude
  544. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  545. _rate_target_ang_vel = _attitude_target_ang_vel;
  546. }
  547. // Command an angular velocity with angular velocity smoothing using rate loops only with integrated rate error stabilization
  548. void AC_AttitudeControl::input_rate_bf_roll_pitch_yaw_3(float roll_rate_bf_cds, float pitch_rate_bf_cds, float yaw_rate_bf_cds)
  549. {
  550. // Convert from centidegrees on public interface to radians
  551. float roll_rate_rads = radians(roll_rate_bf_cds * 0.01f);
  552. float pitch_rate_rads = radians(pitch_rate_bf_cds * 0.01f);
  553. float yaw_rate_rads = radians(yaw_rate_bf_cds * 0.01f);
  554. // Update attitude error
  555. Vector3f attitude_error_vector;
  556. _attitude_ang_error.to_axis_angle(attitude_error_vector);
  557. Quaternion attitude_ang_error_update_quat;
  558. // limit the integrated error angle
  559. float err_mag = attitude_error_vector.length();
  560. if (err_mag > AC_ATTITUDE_THRUST_ERROR_ANGLE) {
  561. attitude_error_vector *= AC_ATTITUDE_THRUST_ERROR_ANGLE / err_mag;
  562. _attitude_ang_error.from_axis_angle(attitude_error_vector);
  563. }
  564. Vector3f gyro_latest = _ahrs.get_gyro_latest();
  565. attitude_ang_error_update_quat.from_axis_angle(Vector3f((_attitude_target_ang_vel.x-gyro_latest.x) * _dt, (_attitude_target_ang_vel.y-gyro_latest.y) * _dt, (_attitude_target_ang_vel.z-gyro_latest.z) * _dt));
  566. _attitude_ang_error = attitude_ang_error_update_quat * _attitude_ang_error;
  567. // Compute acceleration-limited body frame rates
  568. // When acceleration limiting is enabled, the input shaper constrains angular acceleration about the axis, slewing
  569. // the output rate towards the input rate.
  570. _attitude_target_ang_vel.x = input_shaping_ang_vel(_attitude_target_ang_vel.x, roll_rate_rads, get_accel_roll_max_radss(), _dt);
  571. _attitude_target_ang_vel.y = input_shaping_ang_vel(_attitude_target_ang_vel.y, pitch_rate_rads, get_accel_pitch_max_radss(), _dt);
  572. _attitude_target_ang_vel.z = input_shaping_ang_vel(_attitude_target_ang_vel.z, yaw_rate_rads, get_accel_yaw_max_radss(), _dt);
  573. // Retrieve quaternion vehicle attitude
  574. Quaternion attitude_vehicle_quat;
  575. _ahrs.get_quat_body_to_ned(attitude_vehicle_quat);
  576. // Update the unused targets attitude based on current attitude to condition mode change
  577. _attitude_target_quat = attitude_vehicle_quat * _attitude_ang_error;
  578. // calculate the attitude target euler angles
  579. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  580. // Convert body-frame angular velocity into euler angle derivative of desired attitude
  581. ang_vel_to_euler_rate(_attitude_target_euler_angle, _attitude_target_ang_vel, _attitude_target_euler_rate);
  582. // Compute the angular velocity target from the integrated rate error
  583. _attitude_ang_error.to_axis_angle(attitude_error_vector);
  584. _rate_target_ang_vel = update_ang_vel_target_from_att_error(attitude_error_vector);
  585. _rate_target_ang_vel += _attitude_target_ang_vel;
  586. // ensure Quaternions stay normalized
  587. _attitude_ang_error.normalize();
  588. }
  589. // Command an angular step (i.e change) in body frame angle
  590. // Used to command a step in angle without exciting the orthogonal axis during autotune
  591. void AC_AttitudeControl::input_angle_step_bf_roll_pitch_yaw(float roll_angle_step_bf_cd, float pitch_angle_step_bf_cd, float yaw_angle_step_bf_cd)
  592. {
  593. // Convert from centidegrees on public interface to radians
  594. float roll_step_rads = radians(roll_angle_step_bf_cd * 0.01f);
  595. float pitch_step_rads = radians(pitch_angle_step_bf_cd * 0.01f);
  596. float yaw_step_rads = radians(yaw_angle_step_bf_cd * 0.01f);
  597. // rotate attitude target by desired step
  598. Quaternion attitude_target_update_quat;
  599. attitude_target_update_quat.from_axis_angle(Vector3f(roll_step_rads, pitch_step_rads, yaw_step_rads));
  600. _attitude_target_quat = _attitude_target_quat * attitude_target_update_quat;
  601. _attitude_target_quat.normalize();
  602. // calculate the attitude target euler angles
  603. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  604. // Set rate feedforward requests to zero
  605. _attitude_target_euler_rate = Vector3f(0.0f, 0.0f, 0.0f);
  606. _attitude_target_ang_vel = Vector3f(0.0f, 0.0f, 0.0f);
  607. // Call quaternion attitude controller
  608. attitude_controller_run_quat();
  609. }
  610. // Calculates the body frame angular velocities to follow the target attitude
  611. void AC_AttitudeControl::attitude_controller_run_quat()
  612. {
  613. // Retrieve quaternion vehicle attitude
  614. Quaternion attitude_vehicle_quat;
  615. _ahrs.get_quat_body_to_ned(attitude_vehicle_quat);
  616. // Compute attitude error
  617. Vector3f attitude_error_vector;
  618. thrust_heading_rotation_angles(_attitude_target_quat, attitude_vehicle_quat, attitude_error_vector, _thrust_error_angle);
  619. // Compute the angular velocity target from the attitude error
  620. _rate_target_ang_vel = update_ang_vel_target_from_att_error(attitude_error_vector);
  621. // Add feedforward term that attempts to ensure that roll and pitch errors rotate with the body frame rather than the reference frame.
  622. // todo: this should probably be a matrix that couples yaw as well.
  623. _rate_target_ang_vel.x += constrain_float(attitude_error_vector.y, -M_PI / 4, M_PI / 4) * _ahrs.get_gyro().z;
  624. _rate_target_ang_vel.y += -constrain_float(attitude_error_vector.x, -M_PI / 4, M_PI / 4) * _ahrs.get_gyro().z;
  625. ang_vel_limit(_rate_target_ang_vel, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), radians(_ang_vel_yaw_max));
  626. // Add the angular velocity feedforward, rotated into vehicle frame
  627. Quaternion attitude_target_ang_vel_quat = Quaternion(0.0f, _attitude_target_ang_vel.x, _attitude_target_ang_vel.y, _attitude_target_ang_vel.z);
  628. Quaternion to_to_from_quat = attitude_vehicle_quat.inverse() * _attitude_target_quat;
  629. Quaternion desired_ang_vel_quat = to_to_from_quat.inverse() * attitude_target_ang_vel_quat * to_to_from_quat;
  630. // Correct the thrust vector and smoothly add feedforward and yaw input
  631. if (_thrust_error_angle > AC_ATTITUDE_THRUST_ERROR_ANGLE * 2.0f) {
  632. _rate_target_ang_vel.z = _ahrs.get_gyro().z;
  633. } else if (_thrust_error_angle > AC_ATTITUDE_THRUST_ERROR_ANGLE) {
  634. float feedforward_scalar = (1.0f - (_thrust_error_angle - AC_ATTITUDE_THRUST_ERROR_ANGLE) / AC_ATTITUDE_THRUST_ERROR_ANGLE);
  635. _rate_target_ang_vel.x += desired_ang_vel_quat.q2 * feedforward_scalar;
  636. _rate_target_ang_vel.y += desired_ang_vel_quat.q3 * feedforward_scalar;
  637. _rate_target_ang_vel.z += desired_ang_vel_quat.q4;
  638. _rate_target_ang_vel.z = _ahrs.get_gyro().z * (1.0 - feedforward_scalar) + _rate_target_ang_vel.z * feedforward_scalar;
  639. } else {
  640. _rate_target_ang_vel.x += desired_ang_vel_quat.q2;
  641. _rate_target_ang_vel.y += desired_ang_vel_quat.q3;
  642. _rate_target_ang_vel.z += desired_ang_vel_quat.q4;
  643. }
  644. if (_rate_bf_ff_enabled) {
  645. // rotate target and normalize
  646. Quaternion attitude_target_update_quat;
  647. attitude_target_update_quat.from_axis_angle(Vector3f(_attitude_target_ang_vel.x * _dt, _attitude_target_ang_vel.y * _dt, _attitude_target_ang_vel.z * _dt));
  648. _attitude_target_quat = _attitude_target_quat * attitude_target_update_quat;
  649. _attitude_target_quat.normalize();
  650. }
  651. // ensure Quaternions stay normalized
  652. _attitude_target_quat.normalize();
  653. // Record error to handle EKF resets
  654. _attitude_ang_error = attitude_vehicle_quat.inverse() * _attitude_target_quat;
  655. }
  656. // thrust_heading_rotation_angles - calculates two ordered rotations to move the att_from_quat quaternion to the att_to_quat quaternion.
  657. // The first rotation corrects the thrust vector and the second rotation corrects the heading vector.
  658. void AC_AttitudeControl::thrust_heading_rotation_angles(Quaternion& att_to_quat, const Quaternion& att_from_quat, Vector3f& att_diff_angle, float& thrust_vec_dot)
  659. {
  660. Matrix3f att_to_rot_matrix; // rotation from the target body frame to the inertial frame.
  661. att_to_quat.rotation_matrix(att_to_rot_matrix);
  662. Vector3f att_to_thrust_vec = att_to_rot_matrix * Vector3f(0.0f, 0.0f, 1.0f);
  663. Matrix3f att_from_rot_matrix; // rotation from the current body frame to the inertial frame.
  664. att_from_quat.rotation_matrix(att_from_rot_matrix);
  665. Vector3f att_from_thrust_vec = att_from_rot_matrix * Vector3f(0.0f, 0.0f, 1.0f);
  666. // the cross product of the desired and target thrust vector defines the rotation vector
  667. Vector3f thrust_vec_cross = att_from_thrust_vec % att_to_thrust_vec;
  668. // the dot product is used to calculate the angle between the target and desired thrust vectors
  669. thrust_vec_dot = acosf(constrain_float(att_from_thrust_vec * att_to_thrust_vec, -1.0f, 1.0f));
  670. // Normalize the thrust rotation vector
  671. float thrust_vector_length = thrust_vec_cross.length();
  672. if (is_zero(thrust_vector_length) || is_zero(thrust_vec_dot)) {
  673. thrust_vec_cross = Vector3f(0, 0, 1);
  674. thrust_vec_dot = 0.0f;
  675. } else {
  676. thrust_vec_cross /= thrust_vector_length;
  677. }
  678. Quaternion thrust_vec_correction_quat;
  679. thrust_vec_correction_quat.from_axis_angle(thrust_vec_cross, thrust_vec_dot);
  680. // Rotate thrust_vec_correction_quat to the att_from frame
  681. thrust_vec_correction_quat = att_from_quat.inverse() * thrust_vec_correction_quat * att_from_quat;
  682. // calculate the remaining rotation required after thrust vector is rotated transformed to the att_from frame
  683. Quaternion yaw_vec_correction_quat = thrust_vec_correction_quat.inverse() * att_from_quat.inverse() * att_to_quat;
  684. // calculate the angle error in x and y.
  685. Vector3f rotation;
  686. thrust_vec_correction_quat.to_axis_angle(rotation);
  687. att_diff_angle.x = rotation.x;
  688. att_diff_angle.y = rotation.y;
  689. // calculate the angle error in z (x and y should be zero here).
  690. yaw_vec_correction_quat.to_axis_angle(rotation);
  691. att_diff_angle.z = rotation.z;
  692. // Todo: Limit roll an pitch error based on output saturation and maximum error.
  693. // Limit Yaw Error based on maximum acceleration - Update to include output saturation and maximum error.
  694. // Currently the limit is based on the maximum acceleration using the linear part of the SQRT controller.
  695. // This should be updated to be based on an angle limit, saturation, or unlimited based on user defined parameters.
  696. if (!is_zero(_p_angle_yaw.kP()) && fabsf(att_diff_angle.z) > AC_ATTITUDE_ACCEL_Y_CONTROLLER_MAX_RADSS / _p_angle_yaw.kP()) {
  697. att_diff_angle.z = constrain_float(wrap_PI(att_diff_angle.z), -AC_ATTITUDE_ACCEL_Y_CONTROLLER_MAX_RADSS / _p_angle_yaw.kP(), AC_ATTITUDE_ACCEL_Y_CONTROLLER_MAX_RADSS / _p_angle_yaw.kP());
  698. yaw_vec_correction_quat.from_axis_angle(Vector3f(0.0f, 0.0f, att_diff_angle.z));
  699. att_to_quat = att_from_quat * thrust_vec_correction_quat * yaw_vec_correction_quat;
  700. }
  701. }
  702. // calculates the velocity correction from an angle error. The angular velocity has acceleration and
  703. // deceleration limits including basic jerk limiting using _input_tc
  704. float AC_AttitudeControl::input_shaping_angle(float error_angle, float input_tc, float accel_max, float target_ang_vel, float dt)
  705. {
  706. // Calculate the velocity as error approaches zero with acceleration limited by accel_max_radss
  707. float desired_ang_vel = sqrt_controller(error_angle, 1.0f / MAX(input_tc, 0.01f), accel_max, dt);
  708. // Acceleration is limited directly to smooth the beginning of the curve.
  709. return input_shaping_ang_vel(target_ang_vel, desired_ang_vel, accel_max, dt);
  710. }
  711. // limits the acceleration and deceleration of a velocity request
  712. float AC_AttitudeControl::input_shaping_ang_vel(float target_ang_vel, float desired_ang_vel, float accel_max, float dt)
  713. {
  714. // Acceleration is limited directly to smooth the beginning of the curve.
  715. if (is_positive(accel_max)) {
  716. float delta_ang_vel = accel_max * dt;
  717. return constrain_float(desired_ang_vel, target_ang_vel - delta_ang_vel, target_ang_vel + delta_ang_vel);
  718. } else {
  719. return desired_ang_vel;
  720. }
  721. }
  722. // calculates the expected angular velocity correction from an angle error based on the AC_AttitudeControl settings.
  723. // This function can be used to predict the delay associated with angle requests.
  724. void AC_AttitudeControl::input_shaping_rate_predictor(const Vector2f &error_angle, Vector2f& target_ang_vel, float dt) const
  725. {
  726. if (_rate_bf_ff_enabled) {
  727. // translate the roll pitch and yaw acceleration limits to the euler axis
  728. target_ang_vel.x = input_shaping_angle(wrap_PI(error_angle.x), _input_tc, get_accel_roll_max_radss(), target_ang_vel.x, dt);
  729. target_ang_vel.y = input_shaping_angle(wrap_PI(error_angle.y), _input_tc, get_accel_pitch_max_radss(), target_ang_vel.y, dt);
  730. } else {
  731. target_ang_vel.x = _p_angle_roll.get_p(wrap_PI(error_angle.x));
  732. target_ang_vel.y = _p_angle_pitch.get_p(wrap_PI(error_angle.y));
  733. }
  734. // Limit the angular velocity correction
  735. Vector3f ang_vel(target_ang_vel.x, target_ang_vel.y, 0.0f);
  736. ang_vel_limit(ang_vel, radians(_ang_vel_roll_max), radians(_ang_vel_pitch_max), 0.0f);
  737. target_ang_vel.x = ang_vel.x;
  738. target_ang_vel.y = ang_vel.y;
  739. }
  740. // limits angular velocity
  741. void AC_AttitudeControl::ang_vel_limit(Vector3f& euler_rad, float ang_vel_roll_max, float ang_vel_pitch_max, float ang_vel_yaw_max) const
  742. {
  743. if (is_zero(ang_vel_roll_max) || is_zero(ang_vel_pitch_max)) {
  744. if (!is_zero(ang_vel_roll_max)) {
  745. euler_rad.x = constrain_float(euler_rad.x, -ang_vel_roll_max, ang_vel_roll_max);
  746. }
  747. if (!is_zero(ang_vel_pitch_max)) {
  748. euler_rad.y = constrain_float(euler_rad.y, -ang_vel_pitch_max, ang_vel_pitch_max);
  749. }
  750. } else {
  751. Vector2f thrust_vector_ang_vel(euler_rad.x / ang_vel_roll_max, euler_rad.y / ang_vel_pitch_max);
  752. float thrust_vector_length = thrust_vector_ang_vel.length();
  753. if (thrust_vector_length > 1.0f) {
  754. euler_rad.x = thrust_vector_ang_vel.x * ang_vel_roll_max / thrust_vector_length;
  755. euler_rad.y = thrust_vector_ang_vel.y * ang_vel_pitch_max / thrust_vector_length;
  756. }
  757. }
  758. if (!is_zero(ang_vel_yaw_max)) {
  759. euler_rad.z = constrain_float(euler_rad.z, -ang_vel_yaw_max, ang_vel_yaw_max);
  760. }
  761. }
  762. // translates body frame acceleration limits to the euler axis
  763. Vector3f AC_AttitudeControl::euler_accel_limit(const Vector3f &euler_rad, const Vector3f &euler_accel)
  764. {
  765. float sin_phi = constrain_float(fabsf(sinf(euler_rad.x)), 0.1f, 1.0f);
  766. float cos_phi = constrain_float(fabsf(cosf(euler_rad.x)), 0.1f, 1.0f);
  767. float sin_theta = constrain_float(fabsf(sinf(euler_rad.y)), 0.1f, 1.0f);
  768. Vector3f rot_accel;
  769. if (is_zero(euler_accel.x) || is_zero(euler_accel.y) || is_zero(euler_accel.z) || is_negative(euler_accel.x) || is_negative(euler_accel.y) || is_negative(euler_accel.z)) {
  770. rot_accel.x = euler_accel.x;
  771. rot_accel.y = euler_accel.y;
  772. rot_accel.z = euler_accel.z;
  773. } else {
  774. rot_accel.x = euler_accel.x;
  775. rot_accel.y = MIN(euler_accel.y / cos_phi, euler_accel.z / sin_phi);
  776. rot_accel.z = MIN(MIN(euler_accel.x / sin_theta, euler_accel.y / sin_phi), euler_accel.z / cos_phi);
  777. }
  778. return rot_accel;
  779. }
  780. // Shifts earth frame yaw target by yaw_shift_cd. yaw_shift_cd should be in centidegrees and is added to the current target heading
  781. void AC_AttitudeControl::shift_ef_yaw_target(float yaw_shift_cd)
  782. {
  783. float yaw_shift = radians(yaw_shift_cd * 0.01f);
  784. Quaternion _attitude_target_update_quat;
  785. _attitude_target_update_quat.from_axis_angle(Vector3f(0.0f, 0.0f, yaw_shift));
  786. _attitude_target_quat = _attitude_target_update_quat * _attitude_target_quat;
  787. }
  788. // Shifts earth frame yaw target by yaw_shift_cd. yaw_shift_cd should be in centidegrees and is added to the current target heading
  789. void AC_AttitudeControl::inertial_frame_reset()
  790. {
  791. // Retrieve quaternion vehicle attitude
  792. Quaternion attitude_vehicle_quat;
  793. _ahrs.get_quat_body_to_ned(attitude_vehicle_quat);
  794. // Recalculate the target quaternion
  795. _attitude_target_quat = attitude_vehicle_quat * _attitude_ang_error;
  796. // calculate the attitude target euler angles
  797. _attitude_target_quat.to_euler(_attitude_target_euler_angle.x, _attitude_target_euler_angle.y, _attitude_target_euler_angle.z);
  798. }
  799. // Convert a 321-intrinsic euler angle derivative to an angular velocity vector
  800. void AC_AttitudeControl::euler_rate_to_ang_vel(const Vector3f& euler_rad, const Vector3f& euler_rate_rads, Vector3f& ang_vel_rads)
  801. {
  802. float sin_theta = sinf(euler_rad.y);
  803. float cos_theta = cosf(euler_rad.y);
  804. float sin_phi = sinf(euler_rad.x);
  805. float cos_phi = cosf(euler_rad.x);
  806. ang_vel_rads.x = euler_rate_rads.x - sin_theta * euler_rate_rads.z;
  807. ang_vel_rads.y = cos_phi * euler_rate_rads.y + sin_phi * cos_theta * euler_rate_rads.z;
  808. ang_vel_rads.z = -sin_phi * euler_rate_rads.y + cos_theta * cos_phi * euler_rate_rads.z;
  809. }
  810. // Convert an angular velocity vector to a 321-intrinsic euler angle derivative
  811. // Returns false if the vehicle is pitched 90 degrees up or down
  812. bool AC_AttitudeControl::ang_vel_to_euler_rate(const Vector3f& euler_rad, const Vector3f& ang_vel_rads, Vector3f& euler_rate_rads)
  813. {
  814. float sin_theta = sinf(euler_rad.y);
  815. float cos_theta = cosf(euler_rad.y);
  816. float sin_phi = sinf(euler_rad.x);
  817. float cos_phi = cosf(euler_rad.x);
  818. // When the vehicle pitches all the way up or all the way down, the euler angles become discontinuous. In this case, we just return false.
  819. if (is_zero(cos_theta)) {
  820. return false;
  821. }
  822. euler_rate_rads.x = ang_vel_rads.x + sin_phi * (sin_theta / cos_theta) * ang_vel_rads.y + cos_phi * (sin_theta / cos_theta) * ang_vel_rads.z;
  823. euler_rate_rads.y = cos_phi * ang_vel_rads.y - sin_phi * ang_vel_rads.z;
  824. euler_rate_rads.z = (sin_phi / cos_theta) * ang_vel_rads.y + (cos_phi / cos_theta) * ang_vel_rads.z;
  825. return true;
  826. }
  827. // Update rate_target_ang_vel using attitude_error_rot_vec_rad
  828. Vector3f AC_AttitudeControl::update_ang_vel_target_from_att_error(const Vector3f &attitude_error_rot_vec_rad)
  829. {
  830. Vector3f rate_target_ang_vel;
  831. // Compute the roll angular velocity demand from the roll angle error
  832. if (_use_sqrt_controller) {
  833. rate_target_ang_vel.x = sqrt_controller(attitude_error_rot_vec_rad.x, _p_angle_roll.kP(), constrain_float(get_accel_roll_max_radss() / 2.0f, AC_ATTITUDE_ACCEL_RP_CONTROLLER_MIN_RADSS, AC_ATTITUDE_ACCEL_RP_CONTROLLER_MAX_RADSS), _dt);
  834. } else {
  835. rate_target_ang_vel.x = _p_angle_roll.kP() * attitude_error_rot_vec_rad.x;
  836. }
  837. // todo: Add Angular Velocity Limit
  838. // Compute the pitch angular velocity demand from the pitch angle error
  839. if (_use_sqrt_controller) {
  840. rate_target_ang_vel.y = sqrt_controller(attitude_error_rot_vec_rad.y, _p_angle_pitch.kP(), constrain_float(get_accel_pitch_max_radss() / 2.0f, AC_ATTITUDE_ACCEL_RP_CONTROLLER_MIN_RADSS, AC_ATTITUDE_ACCEL_RP_CONTROLLER_MAX_RADSS), _dt);
  841. } else {
  842. rate_target_ang_vel.y = _p_angle_pitch.kP() * attitude_error_rot_vec_rad.y;
  843. }
  844. // todo: Add Angular Velocity Limit
  845. // Compute the yaw angular velocity demand from the yaw angle error
  846. if (_use_sqrt_controller) {
  847. rate_target_ang_vel.z = sqrt_controller(attitude_error_rot_vec_rad.z, _p_angle_yaw.kP(), constrain_float(get_accel_yaw_max_radss() / 2.0f, AC_ATTITUDE_ACCEL_Y_CONTROLLER_MIN_RADSS, AC_ATTITUDE_ACCEL_Y_CONTROLLER_MAX_RADSS), _dt);
  848. } else {
  849. rate_target_ang_vel.z = _p_angle_yaw.kP() * attitude_error_rot_vec_rad.z;
  850. }
  851. // todo: Add Angular Velocity Limit
  852. return rate_target_ang_vel;
  853. }
  854. // Enable or disable body-frame feed forward
  855. void AC_AttitudeControl::accel_limiting(bool enable_limits)
  856. {
  857. if (enable_limits) {
  858. // If enabling limits, reload from eeprom or set to defaults
  859. if (is_zero(_accel_roll_max)) {
  860. _accel_roll_max.load();
  861. }
  862. if (is_zero(_accel_pitch_max)) {
  863. _accel_pitch_max.load();
  864. }
  865. if (is_zero(_accel_yaw_max)) {
  866. _accel_yaw_max.load();
  867. }
  868. } else {
  869. _accel_roll_max = 0.0f;
  870. _accel_pitch_max = 0.0f;
  871. _accel_yaw_max = 0.0f;
  872. }
  873. }
  874. // Return tilt angle limit for pilot input that prioritises altitude hold over lean angle
  875. float AC_AttitudeControl::get_althold_lean_angle_max() const
  876. {
  877. // convert to centi-degrees for public interface
  878. return MAX(ToDeg(_althold_lean_angle_max), AC_ATTITUDE_CONTROL_ANGLE_LIMIT_MIN) * 100.0f;
  879. }
  880. // Proportional controller with piecewise sqrt sections to constrain second derivative
  881. float AC_AttitudeControl::sqrt_controller(float error, float p, float second_ord_lim, float dt)
  882. {
  883. float correction_rate;
  884. if (is_negative(second_ord_lim) || is_zero(second_ord_lim)) {
  885. // second order limit is zero or negative.
  886. correction_rate = error * p;
  887. } else if (is_zero(p)) {
  888. // P term is zero but we have a second order limit.
  889. if (is_positive(error)) {
  890. correction_rate = safe_sqrt(2.0f * second_ord_lim * (error));
  891. } else if (is_negative(error)) {
  892. correction_rate = -safe_sqrt(2.0f * second_ord_lim * (-error));
  893. } else {
  894. correction_rate = 0.0f;
  895. }
  896. } else {
  897. // Both the P and second order limit have been defined.
  898. float linear_dist = second_ord_lim / sq(p);
  899. if (error > linear_dist) {
  900. correction_rate = safe_sqrt(2.0f * second_ord_lim * (error - (linear_dist / 2.0f)));
  901. } else if (error < -linear_dist) {
  902. correction_rate = -safe_sqrt(2.0f * second_ord_lim * (-error - (linear_dist / 2.0f)));
  903. } else {
  904. correction_rate = error * p;
  905. }
  906. }
  907. if (!is_zero(dt)) {
  908. // this ensures we do not get small oscillations by over shooting the error correction in the last time step.
  909. return constrain_float(correction_rate, -fabsf(error) / dt, fabsf(error) / dt);
  910. } else {
  911. return correction_rate;
  912. }
  913. }
  914. // Inverse proportional controller with piecewise sqrt sections to constrain second derivative
  915. float AC_AttitudeControl::stopping_point(float first_ord_mag, float p, float second_ord_lim)
  916. {
  917. if (is_positive(second_ord_lim) && !is_zero(second_ord_lim) && is_zero(p)) {
  918. return (first_ord_mag * first_ord_mag) / (2.0f * second_ord_lim);
  919. } else if ((is_negative(second_ord_lim) || is_zero(second_ord_lim)) && !is_zero(p)) {
  920. return first_ord_mag / p;
  921. } else if ((is_negative(second_ord_lim) || is_zero(second_ord_lim)) && is_zero(p)) {
  922. return 0.0f;
  923. }
  924. // calculate the velocity at which we switch from calculating the stopping point using a linear function to a sqrt function
  925. float linear_velocity = second_ord_lim / p;
  926. if (fabsf(first_ord_mag) < linear_velocity) {
  927. // if our current velocity is below the cross-over point we use a linear function
  928. return first_ord_mag / p;
  929. } else {
  930. float linear_dist = second_ord_lim / sq(p);
  931. float overshoot = (linear_dist * 0.5f) + sq(first_ord_mag) / (2.0f * second_ord_lim);
  932. if (is_positive(first_ord_mag)) {
  933. return overshoot;
  934. } else {
  935. return -overshoot;
  936. }
  937. }
  938. }
  939. // Return roll rate step size in centidegrees/s that results in maximum output after 4 time steps
  940. float AC_AttitudeControl::max_rate_step_bf_roll()
  941. {
  942. float alpha = MIN(get_rate_roll_pid().get_filt_E_alpha(), get_rate_roll_pid().get_filt_D_alpha());
  943. float alpha_remaining = 1 - alpha;
  944. // todo: When a thrust_max is available we should replace 0.5f with 0.5f * _motors.thrust_max
  945. float throttle_hover = constrain_float(_motors.get_throttle_hover(), 0.1f, 0.5f);
  946. return 2.0f * throttle_hover * AC_ATTITUDE_RATE_RP_CONTROLLER_OUT_MAX / ((alpha_remaining * alpha_remaining * alpha_remaining * alpha * get_rate_roll_pid().kD()) / _dt + get_rate_roll_pid().kP());
  947. }
  948. // Return pitch rate step size in centidegrees/s that results in maximum output after 4 time steps
  949. float AC_AttitudeControl::max_rate_step_bf_pitch()
  950. {
  951. float alpha = MIN(get_rate_pitch_pid().get_filt_E_alpha(), get_rate_pitch_pid().get_filt_D_alpha());
  952. float alpha_remaining = 1 - alpha;
  953. // todo: When a thrust_max is available we should replace 0.5f with 0.5f * _motors.thrust_max
  954. float throttle_hover = constrain_float(_motors.get_throttle_hover(), 0.1f, 0.5f);
  955. return 2.0f * throttle_hover * AC_ATTITUDE_RATE_RP_CONTROLLER_OUT_MAX / ((alpha_remaining * alpha_remaining * alpha_remaining * alpha * get_rate_pitch_pid().kD()) / _dt + get_rate_pitch_pid().kP());
  956. }
  957. // Return yaw rate step size in centidegrees/s that results in maximum output after 4 time steps
  958. float AC_AttitudeControl::max_rate_step_bf_yaw()
  959. {
  960. float alpha = MIN(get_rate_yaw_pid().get_filt_E_alpha(), get_rate_yaw_pid().get_filt_D_alpha());
  961. float alpha_remaining = 1 - alpha;
  962. // todo: When a thrust_max is available we should replace 0.5f with 0.5f * _motors.thrust_max
  963. float throttle_hover = constrain_float(_motors.get_throttle_hover(), 0.1f, 0.5f);
  964. return 2.0f * throttle_hover * AC_ATTITUDE_RATE_YAW_CONTROLLER_OUT_MAX / ((alpha_remaining * alpha_remaining * alpha_remaining * alpha * get_rate_yaw_pid().kD()) / _dt + get_rate_yaw_pid().kP());
  965. }
  966. bool AC_AttitudeControl::pre_arm_checks(const char *param_prefix,
  967. char *failure_msg,
  968. const uint8_t failure_msg_len)
  969. {
  970. // validate AC_P members:
  971. const struct {
  972. const char *pid_name;
  973. AC_P &p;
  974. } ps[] = {
  975. { "ANG_PIT", get_angle_pitch_p() },
  976. { "ANG_RLL", get_angle_roll_p() },
  977. { "ANG_YAW", get_angle_yaw_p() }
  978. };
  979. for (uint8_t i=0; i<ARRAY_SIZE(ps); i++) {
  980. // all AC_P's must have a positive P value:
  981. if (!is_positive(ps[i].p.kP())) {
  982. hal.util->snprintf(failure_msg, failure_msg_len, "%s_%s_P must be > 0", param_prefix, ps[i].pid_name);
  983. return false;
  984. }
  985. }
  986. // validate AC_PID members:
  987. const struct {
  988. const char *pid_name;
  989. AC_PID &pid;
  990. } pids[] = {
  991. { "RAT_RLL", get_rate_roll_pid() },
  992. { "RAT_PIT", get_rate_pitch_pid() },
  993. { "RAT_YAW", get_rate_yaw_pid() },
  994. };
  995. for (uint8_t i=0; i<ARRAY_SIZE(pids); i++) {
  996. // if the PID has a positive FF then we just ensure kP and
  997. // kI aren't negative
  998. AC_PID &pid = pids[i].pid;
  999. const char *pid_name = pids[i].pid_name;
  1000. if (is_positive(pid.ff())) {
  1001. // kP and kI must be non-negative:
  1002. if (is_negative(pid.kP())) {
  1003. hal.util->snprintf(failure_msg, failure_msg_len, "%s_%s_P must be >= 0", param_prefix, pid_name);
  1004. return false;
  1005. }
  1006. if (is_negative(pid.kI())) {
  1007. hal.util->snprintf(failure_msg, failure_msg_len, "%s_%s_I must be >= 0", param_prefix, pid_name);
  1008. return false;
  1009. }
  1010. } else {
  1011. // kP and kI must be positive:
  1012. if (!is_positive(pid.kP())) {
  1013. hal.util->snprintf(failure_msg, failure_msg_len, "%s_%s_P must be > 0", param_prefix, pid_name);
  1014. return false;
  1015. }
  1016. if (!is_positive(pid.kI())) {
  1017. hal.util->snprintf(failure_msg, failure_msg_len, "%s_%s_I must be > 0", param_prefix, pid_name);
  1018. return false;
  1019. }
  1020. }
  1021. // never allow a negative D term (but zero is allowed)
  1022. if (is_negative(pid.kD())) {
  1023. hal.util->snprintf(failure_msg, failure_msg_len, "%s_%s_D must be >= 0", param_prefix, pid_name);
  1024. return false;
  1025. }
  1026. }
  1027. return true;
  1028. }