control_clean.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #include "Sub.h"
  2. //目标姿态
  3. float maxspeed = 41.0;
  4. int16_t minspeed = 31;
  5. int16_t maxerrorspeed = 31;
  6. //float turnspeed = maxspeed - maxerror_f;
  7. Quaternion _attitude_target_quat;
  8. extern mavlink_rov_state_monitoring_t rov_message;
  9. bool Sub::clean_init()
  10. {
  11. pos_control.set_alt_target(0);
  12. motor1_speed_target =startval;//1500;
  13. motor2_speed_target =startval;//
  14. float _head =0;
  15. _head = (float)ahrs.yaw_sensor/100;
  16. //track
  17. track_head_gd = constrain_float(_head,0.0,360.0);
  18. last_roll = 0;
  19. last_pitch = 0;
  20. last_yaw = ahrs.yaw_sensor;
  21. //记录进入stable的YAW的当前值
  22. //yaw_press = (int16_t)(ahrs.yaw_sensor/100);//记住方位
  23. last_input_ms = AP_HAL::millis();
  24. track_reset();
  25. return true;
  26. }
  27. void Sub::track_reset(void){
  28. autoclean_command = FALSE;//自动刷网
  29. clean_bottom_command =FALSE;
  30. clean_bottom_flag = FALSE;
  31. autoclean_flag = FALSE;
  32. handclean = TRUE;
  33. turn_angle = 16.0;//20210622
  34. //----------------------
  35. track_motor_arm = 1;//履带停机标志
  36. //履带的初始方向位置 默认为水平方向的yaw值
  37. track_head_gd = 0;
  38. //履带停
  39. motor1_speed_target =startval;
  40. motor2_speed_target =startval;
  41. motors.pwm_track[0] = 1500;
  42. motors.pwm_track[1] = 1500;
  43. clean_mode =0;
  44. attitude_control.relax_attitude_controllers();//外置九轴YAW更新四元数
  45. last_roll = 0;
  46. last_pitch = 0;
  47. last_yaw = ahrs.yaw_sensor;
  48. //yaw_press = (int16_t)ahrs.yaw_sensor/100;//记住方位
  49. ahrs.get_quat_body_to_ned(_attitude_target_quat);
  50. }
  51. extern mavlink_data64_t rov_message2;
  52. extern mavlink_rov_control_t rov_control;
  53. void Sub::clean_run()
  54. {
  55. // if not armed set throttle to zero and exit immediately
  56. if (!motors.armed()) {
  57. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
  58. attitude_control.set_throttle_out(0,true,g.throttle_filt);
  59. attitude_control.relax_attitude_controllers();//---------------------20210623
  60. PressLevel_f =5.0;//压力为0
  61. PressLevel = no;
  62. //-------------
  63. track_reset();
  64. return;
  65. }
  66. //推进器-------------------
  67. motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
  68. motors.set_throttle(1.0-(float)PressLevel_f*0.1);//压力等级
  69. rov_message.pressure_level = int(PressLevel);
  70. motors.set_forward(0.0); //右侧摇杆,前推为+,后推为- 右履带的前进后退
  71. motors.set_lateral(0.0);//右侧摇杆 左推-,右推+
  72. motors.set_yaw(0.0);//左侧摇杆左推- 右推+
  73. motors.set_roll(0.0);
  74. motors.set_pitch(0.0);
  75. motors.set_yaw(0.0);
  76. static int j = 0;
  77. j++;
  78. if(j>800)
  79. {
  80. // gcs().send_text(MAV_SEVERITY_INFO, " track_limit %f\n",(float)rov_control.track_limit);
  81. j=0;
  82. }
  83. //最大速度-------------
  84. int16_t maxspeed_up=100;//这里是百分比0.9
  85. maxspeed_up = SRV_Channels::srv_channel(15)->get_trim()/100;
  86. maxspeed = (float)rov_control.track_limit;
  87. /*if(maxspeed_up<maxspeed)
  88. {
  89. maxspeed = maxspeed_up;
  90. }*/
  91. if(maxspeed_up >100)
  92. {
  93. maxspeed_up = 100;
  94. }
  95. maxspeed = maxspeed_up;
  96. maxspeed = constrain_float(maxspeed,30.0,100.0);
  97. //----------------------
  98. maxerrorspeed =SRV_Channels::srv_channel(15)->get_output_min()/100;//两个履带最大差速 ----------最深距离 厘米
  99. maxerrorspeed = constrain_int16(maxerrorspeed,20,maxspeed);
  100. minspeed =SRV_Channels::srv_channel(15)->get_output_max()/100;// 暂时用来做最小速度 -----最小水深
  101. minspeed = constrain_int16(minspeed,20,maxspeed);
  102. autoclean_flag_chose();//自动洗网的状态切换
  103. clean_net_joystick();//默认手动洗网
  104. clean_sidenet_auto();//按一下自动洗网使能,再按一下失能,回到手动洗网状态
  105. }
  106. void Sub::autoclean_flag_chose(void){
  107. if (autoclean_command == TRUE)
  108. {
  109. autoclean_flag = TRUE;
  110. }
  111. else{
  112. autoclean_flag = FALSE;
  113. }
  114. if (autoclean_flag == FALSE && clean_bottom_flag == FALSE)
  115. {
  116. handclean = TRUE;
  117. }
  118. else{
  119. handclean = FALSE;
  120. }
  121. }
  122. void Sub::clean_net_joystick(void)
  123. {
  124. float left =startval;
  125. float _turn =startval;
  126. int16_t motors1 =startval;
  127. int16_t motors2 =startval;
  128. if(handclean == TRUE ){
  129. //计算电机的 手动转速
  130. left = Constrate1(channel_forward->norm_input());//
  131. _turn = Constrate1(channel_yaw->norm_input());//
  132. motors1 = (int16_t)((left*maxspeed +_turn*maxerrorspeed));//
  133. motors2 = (int16_t)((left*maxspeed -_turn*maxerrorspeed));//
  134. if(_turn>0.1)
  135. {
  136. _turn =1.0;
  137. rov_message.turn = 3;
  138. }else if(_turn<-0.1){
  139. _turn =-1.0;
  140. rov_message.turn = 2;
  141. }else{
  142. rov_message.turn = 1;
  143. }
  144. if (left>0.1)
  145. {
  146. //前进
  147. if (motors1<minspeed)
  148. {
  149. motors1 =minspeed;
  150. }
  151. if (motors2<minspeed)
  152. {
  153. motors2 =minspeed;
  154. }
  155. }
  156. else if (left<-0.1)
  157. {
  158. //后退
  159. if (motors1>-minspeed)
  160. {
  161. motors1 =-minspeed;
  162. }
  163. if (motors2>-minspeed)
  164. {
  165. motors2 =-minspeed;
  166. }
  167. }
  168. else{
  169. if(fabsf(_turn)<0.1)
  170. {
  171. motors1 =0;
  172. motors2 =0;
  173. }
  174. else{
  175. motors1 = _turn*maxerrorspeed;//
  176. motors2 = -_turn*maxerrorspeed;//
  177. }
  178. }
  179. motors1=constrain_int16(motors1,-(int16_t)(maxspeed*0.65),maxspeed);
  180. motors2=constrain_int16(motors2,-(int16_t)(maxspeed*0.65),maxspeed);
  181. static int f = 0;
  182. f++;
  183. if(f>800)
  184. {
  185. gcs().send_text(MAV_SEVERITY_INFO, "rov_message.left_brush %d %d,%f",(int)minspeed,(int)maxerrorspeed,maxspeed);
  186. f=0;
  187. }
  188. //左摇杆前进后退的指示---------------
  189. if(left>0.1){
  190. track_motor_arm = 2;//前进
  191. }
  192. else if(left<-0.1){
  193. track_motor_arm = 0;
  194. }
  195. else{
  196. track_motor_arm=1;
  197. }
  198. rov_message.forward = track_motor_arm;
  199. //---------------------------
  200. //右摇杆 转向的控制
  201. if(fabsf(_turn)>0.1 )// 防抖
  202. {
  203. //转弯中 手动控制
  204. track_head_gd = (float)ahrs.yaw_sensor/100;//to show 20210611
  205. slowly_speed1(motor1_speed_target,motors1,1,3);
  206. slowly_speed2(motor2_speed_target,motors2,1,3);
  207. track_motor_arm =3;//left or right
  208. }
  209. else{
  210. //纯手动控制
  211. if(clean_mode == 0){
  212. slowly_speed1(motor1_speed_target,motors1,1,3);
  213. slowly_speed2(motor2_speed_target,motors2,1,3);
  214. static int kn1 = 0;
  215. kn1++;
  216. if(kn1>800)
  217. {
  218. //gcs().send_text(MAV_SEVERITY_WARNING, "left %f, turn %f %f %f\n",left,_turn,left*Speedmax_hand,_turn*maxerror);
  219. gcs().send_text(MAV_SEVERITY_WARNING, " speed_target %f %d %d ",left,motor2_speed_target,motor1_speed_target);
  220. kn1=0;
  221. }
  222. }else {
  223. }
  224. }
  225. motors.pwm_track[0] = 1500 + 400*motor1_speed_target/100;
  226. motors.pwm_track[1] = 1500 + 400*motor2_speed_target/100;
  227. }
  228. }
  229. void Sub::slowly_speed2(int16_t &p1, int16_t p2,int16_t step,int16_t per)
  230. {
  231. static int16_t countper = 0;
  232. countper++;
  233. if(countper>per){
  234. countper = 0;
  235. if (p1 > p2)
  236. {
  237. p1 -=step;
  238. }else if(p1 < p2){
  239. p1 +=step;
  240. }
  241. if (p2==startval && fabsf(p1-p2)<=step )
  242. {
  243. p1 =startval;
  244. }
  245. }
  246. else{
  247. }
  248. //p1 = constrain_int16(p1, -(Speedmax_hand+maxerror), Speedmax_hand+maxerror);
  249. p1 = constrain_int16(p1, -((int16_t)maxspeed), 100);
  250. static int j = 0;
  251. j++;
  252. if(j>800)
  253. {
  254. // gcs().send_text(MAV_SEVERITY_WARNING, " thrust%d %f ,%d,%d\n",i,thrust_in,thrust,last_thrust_pwm[i]);
  255. j=0;
  256. }
  257. }
  258. void Sub::slowly_speed1(int16_t &p1, int16_t p2,int16_t step,int16_t per)
  259. {
  260. static int16_t countper = 0;
  261. countper++;
  262. if(countper>per){
  263. countper = 0;
  264. if (p1 > p2)
  265. {
  266. p1 -=step;
  267. }else if(p1 < p2){
  268. p1 +=step;
  269. }
  270. if (p2==startval && fabsf(p1-p2)<=step )
  271. {
  272. p1 =startval;
  273. }
  274. }
  275. else{
  276. }
  277. //p1 = constrain_int16(p1, -(Speedmax_hand+maxerror), Speedmax_hand+maxerror);
  278. p1 = constrain_int16(p1, -((int16_t)maxspeed), 100);
  279. static int j = 0;
  280. j++;
  281. if(j>800)
  282. {
  283. // gcs().send_text(MAV_SEVERITY_WARNING, " thrust%d %f ,%d,%d\n",i,thrust_in,thrust,last_thrust_pwm[i]);
  284. j=0;
  285. }
  286. }
  287. float Sub::Constrate1(float d1)
  288. {//摇杆死区设置 0.06= 30/500
  289. if (fabsf(d1)*100<6)
  290. {
  291. return 0.0;
  292. }
  293. else{
  294. return d1;
  295. }
  296. }
  297. //extern mavlink_motor_feedback_t motor_feedback;
  298. extern mavlink_motor_speed_t mav_motor_speed;
  299. extern mavlink_rov_state_monitoring_t rov_message;
  300. void Sub::clean_sidenet_auto(void)
  301. {
  302. }
  303. //提取朝向误差并返回
  304. float Sub::get_yaw_error(float yaw_heading){
  305. //目标四元数
  306. Quaternion error_head;
  307. error_head.from_axis_angle(Vector3f(0, 0, yaw_heading*RAD_TO_DEG));
  308. _attitude_target_quat = _attitude_target_quat*error_head;
  309. //当前姿态
  310. Quaternion attitude_vehicle_quat;
  311. ahrs.get_quat_body_to_ned(attitude_vehicle_quat);
  312. //当前姿态Z轴
  313. Matrix3f att_to_rot_matrix; // rotation from the target body frame to the inertial frame.
  314. attitude_vehicle_quat.rotation_matrix(att_to_rot_matrix);
  315. Vector3f att_to_thrust_vec = att_to_rot_matrix * Vector3f(0.0f, 0.0f, 1.0f);
  316. //目标姿态Z轴
  317. Matrix3f att_from_rot_matrix; // rotation from the current body frame to the inertial frame.
  318. _attitude_target_quat.rotation_matrix(att_from_rot_matrix);
  319. Vector3f att_from_thrust_vec = att_from_rot_matrix * Vector3f(0.0f, 0.0f, 1.0f);
  320. Vector3f thrust_vec_cross = att_from_thrust_vec % att_to_thrust_vec;//得到轴
  321. float thrust_vec_dot = acosf(constrain_float(att_from_thrust_vec * att_to_thrust_vec, -1.0f, 1.0f));//得到角
  322. float thrust_vector_length = thrust_vec_cross.length();
  323. if (is_zero(thrust_vector_length) || is_zero(thrust_vec_dot)) {
  324. thrust_vec_cross = Vector3f(0, 0, 1);
  325. thrust_vec_dot = 0.0f;
  326. } else {
  327. thrust_vec_cross /= thrust_vector_length;
  328. }
  329. Quaternion error_lean;
  330. error_lean.from_axis_angle(thrust_vec_cross, thrust_vec_dot);// 地系下的从obj到half态的误差四元数
  331. Quaternion error_lean_body= attitude_vehicle_quat.inverse() * error_lean * attitude_vehicle_quat;//b系下的从obj到half态的误差四元数
  332. Quaternion lean_earth= attitude_vehicle_quat * error_lean_body ;//half态下的四元数
  333. Quaternion error_yaw= lean_earth.inverse() * _attitude_target_quat ;//yaw误差四元数
  334. Vector3f rotation;
  335. error_yaw.to_axis_angle(rotation);//得到轴角
  336. float yaw_error = rotation.z*RAD_TO_DEG;//得到yaw方位的误差
  337. return yaw_error;
  338. }
  339. void Sub::clean_sidenet_state(void){
  340. //-------------机器人头朝上-自动洗网-
  341. min_depth =SRV_Channels::srv_channel(11)->get_output_min();// 上端距离 厘米
  342. max_depth = SRV_Channels::srv_channel(11)->get_output_max();//最深距离 厘米
  343. int16_t depth_now =fabsf((int16_t)barometer.get_altitude())*100;
  344. if(autoclean_flag == FALSE)
  345. {//没有自动洗网 保存深度值
  346. autoclean_orgin =depth_now;
  347. autoclean_step = Orign;
  348. }else{
  349. int16_t depth_down=min_depth;
  350. static int8_t delayCnt = 0;
  351. int16_t depth_up = max_depth;
  352. static int8_t delayCnt2 = 0;
  353. switch(autoclean_step){
  354. case Orign:
  355. track_head_gd = 0;
  356. //起始位置位于中上部,先往下走,如果起始位置在中下部 先往上走 假设头向上
  357. if(fabsf(autoclean_orgin-min_depth)>fabsf(autoclean_orgin-max_depth)){
  358. track_motor_arm =2;//向前走
  359. autoclean_step = foward;
  360. }else{
  361. track_motor_arm =0;//向后走
  362. autoclean_step = backward;
  363. }
  364. break;
  365. case foward:
  366. //向前走 2
  367. if (track_motor_arm !=0 && depth_now<=min_depth)
  368. {//转折处 前走 或者 停 但深度小于设置值
  369. track_head_gd = turn_angle;//10度 转10度
  370. track_motor_arm = 1;//先停
  371. delayCnt++;
  372. if (delayCnt>100)//停后延时,400 HZ 理论是0.25秒
  373. {
  374. track_motor_arm = 0;
  375. delayCnt =0;
  376. }
  377. }else{
  378. }
  379. if (track_motor_arm == 0 && depth_now -depth_down>100)//相反方向走了xx cm
  380. {//切换到向后走
  381. track_head_gd = 0.0;//0度
  382. autoclean_step =backward;
  383. }
  384. break;
  385. case backward:
  386. //向后走kk
  387. if (track_motor_arm !=2 && depth_now>=max_depth)
  388. {//转折处 向前走或者停 深度大于设置的最大深度
  389. track_head_gd = -turn_angle;//-10度
  390. track_motor_arm = 1;//2;
  391. delayCnt2++;
  392. if (delayCnt2>100)
  393. {
  394. delayCnt2=0;
  395. track_motor_arm=2;
  396. }
  397. }
  398. else{
  399. }
  400. if (track_motor_arm == 2 && depth_up-depth_now>100)////相反方向走了xx cm
  401. {//切换到向前走
  402. track_head_gd = 0.0;
  403. autoclean_step =foward;//切换状态
  404. }
  405. break;
  406. default:
  407. track_motor_arm =1;
  408. track_head_gd=0.0;
  409. break;
  410. }
  411. }
  412. }
  413. void Sub::clean_sidenet_run(void)//自动洗测网
  414. {
  415. //水平方向还没有自动洗网,没有测试
  416. static int16_t motors1=0;
  417. static int16_t motors2=0;
  418. if (autoclean_flag == FALSE)
  419. {//没有自动洗网返回
  420. motors1=0;
  421. motors2=0;
  422. return;
  423. }
  424. //PID设置
  425. trackpid.p1 = attitude_control._thr_mix_man;
  426. trackpid.p2 = attitude_control._thr_mix_man;
  427. trackpid.i1 = attitude_control._thr_mix_max;
  428. trackpid.i2 = attitude_control._thr_mix_max;
  429. trackpid.d1 = attitude_control._thr_mix_min;
  430. trackpid.d2 = attitude_control._thr_mix_min;
  431. //1000-1500向前走,左履带1,右履带2,方向 右歪增大
  432. static int8_t per = 0;//分频数
  433. if (per >3)
  434. {//50HZ控制频率
  435. per = 0;
  436. track_pidcontrol(track_head_gd,motors1,motors2);
  437. }
  438. per++;
  439. slowly_speed1(motor1_speed_target,motors1,1,3);
  440. slowly_speed2(motor2_speed_target,motors2,1,3);
  441. }
  442. // 不同方向上的履带的PID调用 以及缓加缓减
  443. void Sub::track_pidcontrol(float _targethead,int16_t &_motor1,int16_t &_motor2){
  444. int16_t motors1=startval;
  445. int16_t motors2 =startval;
  446. uint32_t nowtime=AP_HAL::micros();
  447. static uint32_t lasttime = nowtime;//用于换向延时防止抱死
  448. float error = get_yaw_error(_targethead);
  449. if (track_motor_arm ==0)
  450. {//后走
  451. if (nowtime - lasttime <500000)//500ms T1
  452. {//防抱死
  453. motors1 = startval;
  454. motors2 = startval;
  455. }
  456. else{
  457. motors1 = constrain_int16(-Speedmax+trackpid.updatePID1(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
  458. motors2 = constrain_int16(-Speedmax+trackpid.updatePID2(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
  459. }
  460. }
  461. else if (track_motor_arm ==1)
  462. {//停
  463. lasttime = AP_HAL::micros();
  464. motors1 = startval;
  465. motors2 =startval;
  466. }
  467. else if (track_motor_arm ==2)
  468. {//前走
  469. if (nowtime - lasttime <500000)//500ms T1
  470. {//防抱死
  471. motors1 =startval;
  472. motors2 = startval;
  473. }
  474. else{
  475. motors1 = constrain_int16(Speedmax+trackpid.updatePID1(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
  476. motors2 = constrain_int16(Speedmax+trackpid.updatePID2(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
  477. }
  478. }
  479. _motor1 = motors1;
  480. _motor2 = motors2;
  481. }