flight_mode.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "flight_mode.h"
  2. #include "auto_pilot.h"
  3. #include "control_rate.h"
  4. #include "control_throttle.h"
  5. #include "mode_attitude.h"
  6. #include "mode_gcs_tax_launch_run.h"
  7. #include "mode_vehicle_land.h"
  8. #include "mode_vehicle_launch.h"
  9. #include "params.h"
  10. #include "soft_flash.h"
  11. #include "soft_gps.h"
  12. #include "soft_gs.h"
  13. #include "soft_imu.h"
  14. #include "soft_motor_output.h"
  15. #include "soft_rc_input.h"
  16. #include "soft_time.h"
  17. #include "soft_voltage.h"
  18. unsigned char control_mode = CONTROL_GCS;
  19. unsigned char flight_mode = MODE_DEFAULT;
  20. unsigned char flight_mode_flag = MODE_DEFAULT;
  21. // @brief 飞行模式切换
  22. void flight_mode_switch(void)
  23. {
  24. static short gcs_remote_switch = 1700;
  25. //=============== 如果是遥控器控制 ====================
  26. if (control_mode == CONTROL_REMOTE)
  27. {
  28. if (comp_rc_status == COMP_NORMAL &&
  29. rc_signal_health == RC_SIGNAL_HEALTH)
  30. {
  31. // 姿态模式
  32. if (rc_in[RC_CH5] >= MID_S && rc_in[RC_CH5] <= MAX_E)
  33. {
  34. control_mode = CONTROL_REMOTE;
  35. set_flight_mode(ATTITUDE);
  36. }
  37. // GPS模式
  38. else if (rc_in[RC_CH5] >= MIN_S && rc_in[RC_CH5] <= MIN_E)
  39. {
  40. if (ins.insgps_ok == INSGPS)
  41. {
  42. control_mode = CONTROL_GCS;
  43. flight_mode_flag = GCS_VEHICLE_LAUNCH;
  44. set_flight_mode(flight_mode_flag);
  45. __mode_state = __STATE_LAUNCH_GO;
  46. pid_m_alt.thr_hold == false;
  47. althold_state = NO_ALT_HOLD;
  48. record_target_alt = pid_m_alt.loc_c; // 需要修改
  49. }
  50. }
  51. }
  52. else
  53. {
  54. if (ground_air_status == ON_GROUND)
  55. {
  56. // 避免遥控器解锁然后接收机受干扰后飞机一直怠速转无法停转。
  57. if (thr_lock_status == UNLOCKED)
  58. {
  59. set_thr_lock_status(LOCKED, RCBADLOCK);
  60. }
  61. }
  62. }
  63. }
  64. // 如果是地面站控制
  65. else if (control_mode == CONTROL_GCS)
  66. {
  67. set_flight_mode(flight_mode_flag);
  68. // RC开并且RC状态健康
  69. if (comp_rc_status == COMP_NORMAL &&
  70. rc_signal_health == RC_SIGNAL_HEALTH)
  71. {
  72. // 地面站模式下 从GPS模式切到姿态模式时 遥控器重新获取控制权
  73. if (rc_in[RC_CH5] >= MIN_S && rc_in[RC_CH5] <= MIN_E)
  74. {
  75. gcs_remote_switch = rc_in[RC_CH5];
  76. }
  77. else if ((rc_in[RC_CH5] >= MAX_S && rc_in[RC_CH5] <= MAX_E) &&
  78. (gcs_remote_switch >= MIN_S && gcs_remote_switch <= MIN_E))
  79. {
  80. gcs_remote_switch = rc_in[RC_CH5];
  81. control_mode = CONTROL_REMOTE;
  82. set_flight_mode(ATTITUDE);
  83. }
  84. }
  85. }
  86. }
  87. // 电压低标志位
  88. bool battary_volt_islow = false;
  89. // 托盘摇杆是否有效
  90. comp_status rock_isenable = COMP_NOEXIST;
  91. // @brief 模式退出
  92. void exit_cur_mode(unsigned char cur_mode, unsigned char new_mode)
  93. {
  94. switch (cur_mode)
  95. {
  96. case ATTITUDE:
  97. attitude_exit();
  98. break;
  99. case GCS_VEHICLE_LAUNCH:
  100. gcs_tax_launch_exit();
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. // @brief 设置飞行模式
  107. bool set_flight_mode(unsigned char new_mode)
  108. {
  109. bool success = false;
  110. if (new_mode == flight_mode)
  111. {
  112. return true;
  113. }
  114. switch (new_mode)
  115. {
  116. case ATTITUDE:
  117. success = attitude_init();
  118. break;
  119. case GCS_VEHICLE_LAUNCH:
  120. success = gcs_tax_launch_init();
  121. break;
  122. default:
  123. success = false;
  124. break;
  125. }
  126. if (success)
  127. {
  128. exit_cur_mode(flight_mode, new_mode);
  129. flight_mode = new_mode;
  130. }
  131. return success;
  132. }
  133. // @brief 模式更新
  134. void update_flight_mode(void)
  135. {
  136. switch (flight_mode)
  137. {
  138. case ATTITUDE:
  139. attitude_run();
  140. break;
  141. case GCS_VEHICLE_LAUNCH:
  142. gcs_tax_launch_run(fast_loop_dt);
  143. break;
  144. default:
  145. break;
  146. }
  147. }