123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- #include "Sub.h"
- float maxspeed = 41.0;
- int16_t minspeed = 31;
- int16_t maxerrorspeed = 31;
- Quaternion _attitude_target_quat;
- extern mavlink_rov_state_monitoring_t rov_message;
- bool Sub::clean_init()
- {
- pos_control.set_alt_target(0);
- hal.rcout->write(10,1500);
- motor1_speed_target =startval;
- motor2_speed_target =startval;
- float _head =0;
- _head = (float)ahrs.yaw_sensor/100;
-
- track_head_gd = constrain_float(_head,0.0,360.0);
-
- last_roll = 0;
- last_pitch = 0;
- last_yaw = ahrs.yaw_sensor;
-
- yaw_press = (int16_t)(ahrs.yaw_sensor/100);
- last_input_ms = AP_HAL::millis();
- track_reset();
-
- return true;
- }
- void Sub::track_reset(void){
- autoclean_command = FALSE;
- clean_bottom_command =FALSE;
- clean_bottom_flag = FALSE;
- autoclean_flag = FALSE;
- handclean = TRUE;
-
- turn_angle = 16.0;
-
- track_motor_arm = 1;
-
- track_head_gd = 0;
-
- motor1_speed_target =startval;
- motor2_speed_target =startval;
- clean_mode =0;
- attitude_control.relax_attitude_controllers();
- last_roll = 0;
- last_pitch = 0;
- last_yaw = ahrs.yaw_sensor;
- yaw_press = (int16_t)ahrs.yaw_sensor/100;
- ahrs.get_quat_body_to_ned(_attitude_target_quat);
-
- }
- extern mavlink_data64_t rov_message2;
- extern mavlink_rov_control_t rov_control;
- void Sub::clean_run()
- {
-
- if (!motors.armed()) {
- motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
- attitude_control.set_throttle_out(0,true,g.throttle_filt);
- attitude_control.relax_attitude_controllers();
-
- PressLevel_f =5.0;
- PressLevel = no;
-
- track_reset();
- return;
-
- }
-
- motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
- motors.set_throttle(1.0-(float)PressLevel_f*0.1);
-
- rov_message.pressure_level = int(PressLevel);
-
- motors.set_forward(0.0);
- motors.set_lateral(0.0);
- motors.set_yaw(0.0);
- motors.set_roll(0.0);
- motors.set_pitch(0.0);
-
-
- motors.set_yaw(0.0);
- static int j = 0;
- j++;
- if(j>800)
- {
-
- j=0;
- }
- maxspeed = (float)rov_control.track_limit;
-
- if (maxspeed<31.0)
- {
- maxspeed = 31.0;
- }
- if(maxspeed>91.0)
- {
- maxspeed = 91.0;
- }
-
-
- autoclean_flag_chose();
- clean_net_joystick();
-
- clean_sidenet_auto();
- }
- void Sub::autoclean_flag_chose(void){
- if (autoclean_command == TRUE)
- {
- autoclean_flag = TRUE;
- }
- else{
- autoclean_flag = FALSE;
- }
- if (autoclean_flag == FALSE && clean_bottom_flag == FALSE)
- {
- handclean = TRUE;
- ahrs.get_quat_body_to_ned(_attitude_target_quat);
- }
- else{
- handclean = FALSE;
- }
- }
- void Sub::clean_net_joystick(void)
- {
-
- float left =startval;
- float _turn =startval;
- int16_t motors1 =startval;
- int16_t motors2 =startval;
-
- minspeed =SRV_Channels::srv_channel(11)->get_output_min();
- maxerrorspeed =SRV_Channels::srv_channel(11)->get_output_max();
-
- if(handclean == TRUE ){
-
-
-
-
-
- left = Constrate1(channel_forward->norm_input());
- _turn = Constrate1(channel_yaw->norm_input());
- motors1 = (int16_t)((left*maxspeed +_turn*maxerrorspeed));
- motors2 = (int16_t)((left*maxspeed -_turn*maxerrorspeed));
- if(_turn>0.1)
- {
- _turn =1.0;
- rov_message.turn = 3;
- }else if(_turn<-0.1){
- _turn =-1.0;
- rov_message.turn = 2;
- }else{
- rov_message.turn = 1;
-
- }
- if (left>0.1)
- {
-
- if (motors1<minspeed)
- {
- motors1 =minspeed;
- }
- if (motors2<minspeed)
- {
- motors2 =minspeed;
- }
- }
- else if (left<-0.1)
- {
-
-
- if (motors1>-minspeed)
- {
- motors1 =-minspeed;
- }
- if (motors2>-minspeed)
- {
- motors2 =-minspeed;
- }
- }
- else{
- if(fabsf(_turn)<0.1)
- {
- motors1 =0;
- motors2 =0;
- }
- else{
-
-
- motors1 = _turn*maxerrorspeed;
- motors2 = -_turn*maxerrorspeed;
- }
- }
-
-
-
-
- motors1=constrain_int16(motors1,-((int16_t)(maxspeed)/2),(int16_t)((int16_t)maxspeed+maxerror));
- motors2=constrain_int16(motors2,-((int16_t)(maxspeed)/2),(int16_t)((int16_t)maxspeed+maxerror));
-
-
- static int f = 0;
- f++;
- if(f>800)
- {
-
-
- f=0;
- }
-
- if(left>0.1){
- track_motor_arm = 2;
- }
- else if(left<-0.1){
- track_motor_arm = 0;
- }
- else{
- track_motor_arm=1;
-
- }
- rov_message.forward = track_motor_arm;
-
-
-
-
- if(fabsf(_turn)>0.1 )
- {
-
- track_head_gd = (float)ahrs.yaw_sensor/100;
-
-
- slowly_speed1(motor1_speed_target,motors1,1,3);
- slowly_speed2(motor2_speed_target,motors2,1,3);
- track_motor_arm =3;
-
- }
- else{
-
- if(clean_mode == 0){
- slowly_speed1(motor1_speed_target,motors1,1,3);
- slowly_speed2(motor2_speed_target,motors2,1,3);
- static int kn1 = 0;
- kn1++;
- if(kn1>800)
- {
-
- gcs().send_text(MAV_SEVERITY_WARNING, " speed_target %f %d %d ",left,motor2_speed_target,motor1_speed_target);
- kn1=0;
- }
- }else {
-
- }
- }
- }
- }
- void Sub::slowly_speed2(int16_t &p1, int16_t p2,int16_t step,int16_t per)
- {
- static int16_t countper = 0;
- countper++;
- if(countper>per){
- countper = 0;
- if (p1 > p2)
- {
- p1 -=step;
- }else if(p1 < p2){
- p1 +=step;
- }
- if (p2==startval && fabsf(p1-p2)<=step )
- {
- p1 =startval;
- }
- }
- else{
-
- }
-
-
-
-
-
- p1 = constrain_int16(p1, -((int16_t)maxspeed+maxerror), (int16_t)maxspeed+maxerror);
- static int j = 0;
- j++;
- if(j>800)
- {
-
-
- j=0;
- }
-
-
-
- }
- void Sub::slowly_speed1(int16_t &p1, int16_t p2,int16_t step,int16_t per)
- {
- static int16_t countper = 0;
- countper++;
- if(countper>per){
- countper = 0;
- if (p1 > p2)
- {
- p1 -=step;
- }else if(p1 < p2){
- p1 +=step;
- }
- if (p2==startval && fabsf(p1-p2)<=step )
- {
- p1 =startval;
- }
- }
- else{
-
- }
-
-
-
-
-
- p1 = constrain_int16(p1, -((int16_t)maxspeed+maxerror), (int16_t)maxspeed+maxerror);
- static int j = 0;
- j++;
- if(j>800)
- {
-
-
- j=0;
- }
-
-
-
- }
- float Sub::Constrate1(float d1)
- {
- if (fabsf(d1)*100<6)
- {
- return 0.0;
- }
- else{
- return d1;
- }
- }
- extern mavlink_motor_speed_t mav_motor_speed;
- extern mavlink_rov_state_monitoring_t rov_message;
- void Sub::motor_toCan(void)
- {
-
-
- if(mav_motor_speed.motorTest == 1)
- {
- motors.motor_to_can[8] = mav_motor_speed.Ltrack;
- motors.motor_to_can[9] = mav_motor_speed.Rtrack;
- }else{
- if(control_mode == CLEAN){
- motors.motor_to_can[8] = motor1_speed_target;
- motors.motor_to_can[9] = motor2_speed_target;
- }else{
- motors.motor_to_can[8] = 0;
- motors.motor_to_can[9] = 0;
- }
- }
-
- static int j = 0;
- j++;
- if(j>400)
- {
-
-
- j=0;
- }
- }
- void Sub::clean_sidenet_auto(void)
- {
- }
- float Sub::get_yaw_error(float yaw_heading){
-
- Quaternion error_head;
- error_head.from_axis_angle(Vector3f(0, 0, yaw_heading*RAD_TO_DEG));
- _attitude_target_quat = _attitude_target_quat*error_head;
-
-
- Quaternion attitude_vehicle_quat;
- ahrs.get_quat_body_to_ned(attitude_vehicle_quat);
-
-
-
- Matrix3f att_to_rot_matrix;
- attitude_vehicle_quat.rotation_matrix(att_to_rot_matrix);
- Vector3f att_to_thrust_vec = att_to_rot_matrix * Vector3f(0.0f, 0.0f, 1.0f);
-
-
- Matrix3f att_from_rot_matrix;
- _attitude_target_quat.rotation_matrix(att_from_rot_matrix);
- Vector3f att_from_thrust_vec = att_from_rot_matrix * Vector3f(0.0f, 0.0f, 1.0f);
- Vector3f thrust_vec_cross = att_from_thrust_vec % att_to_thrust_vec;
- float thrust_vec_dot = acosf(constrain_float(att_from_thrust_vec * att_to_thrust_vec, -1.0f, 1.0f));
-
- float thrust_vector_length = thrust_vec_cross.length();
- if (is_zero(thrust_vector_length) || is_zero(thrust_vec_dot)) {
- thrust_vec_cross = Vector3f(0, 0, 1);
- thrust_vec_dot = 0.0f;
- } else {
- thrust_vec_cross /= thrust_vector_length;
- }
-
- Quaternion error_lean;
- error_lean.from_axis_angle(thrust_vec_cross, thrust_vec_dot);
-
- Quaternion error_lean_body= attitude_vehicle_quat.inverse() * error_lean * attitude_vehicle_quat;
- Quaternion lean_earth= attitude_vehicle_quat * error_lean_body ;
- Quaternion error_yaw= lean_earth.inverse() * _attitude_target_quat ;
-
- Vector3f rotation;
- error_yaw.to_axis_angle(rotation);
- float yaw_error = rotation.z*RAD_TO_DEG;
- return yaw_error;
- }
- void Sub::clean_sidenet_state(void){
-
- min_depth =SRV_Channels::srv_channel(11)->get_output_min();
- max_depth = SRV_Channels::srv_channel(11)->get_output_max();
- int16_t depth_now =fabsf((int16_t)barometer.get_altitude())*100;
- if(autoclean_flag == FALSE)
- {
- autoclean_orgin =depth_now;
- autoclean_step = Orign;
-
- }else{
- int16_t depth_down=min_depth;
- static int8_t delayCnt = 0;
- int16_t depth_up = max_depth;
- static int8_t delayCnt2 = 0;
- switch(autoclean_step){
- case Orign:
- track_head_gd = 0;
-
- if(fabsf(autoclean_orgin-min_depth)>fabsf(autoclean_orgin-max_depth)){
- track_motor_arm =2;
- autoclean_step = foward;
- }else{
- track_motor_arm =0;
- autoclean_step = backward;
- }
- break;
- case foward:
-
- if (track_motor_arm !=0 && depth_now<=min_depth)
- {
- track_head_gd = turn_angle;
- track_motor_arm = 1;
- delayCnt++;
- if (delayCnt>100)
- {
- track_motor_arm = 0;
- delayCnt =0;
- }
- }else{
- }
- if (track_motor_arm == 0 && depth_now -depth_down>100)
- {
- track_head_gd = 0.0;
- autoclean_step =backward;
- }
- break;
- case backward:
-
- if (track_motor_arm !=2 && depth_now>=max_depth)
- {
- track_head_gd = -turn_angle;
- track_motor_arm = 1;
- delayCnt2++;
- if (delayCnt2>100)
- {
- delayCnt2=0;
- track_motor_arm=2;
- }
- }
- else{
- }
- if (track_motor_arm == 2 && depth_up-depth_now>100)
- {
- track_head_gd = 0.0;
- autoclean_step =foward;
- }
- break;
- default:
- track_motor_arm =1;
- track_head_gd=0.0;
- break;
- }
- }
- }
- void Sub::clean_sidenet_run(void)
- {
- static int16_t motors1=0;
- static int16_t motors2=0;
- if (autoclean_flag == FALSE)
- {
- motors1=0;
- motors2=0;
- return;
- }
-
- trackpid.p1 = attitude_control._thr_mix_man;
- trackpid.p2 = attitude_control._thr_mix_man;
- trackpid.i1 = attitude_control._thr_mix_max;
- trackpid.i2 = attitude_control._thr_mix_max;
- trackpid.d1 = attitude_control._thr_mix_min;
- trackpid.d2 = attitude_control._thr_mix_min;
-
- static int8_t per = 0;
- if (per >3)
- {
- per = 0;
- track_pidcontrol(track_head_gd,motors1,motors2);
- }
- per++;
- slowly_speed1(motor1_speed_target,motors1,1,3);
- slowly_speed2(motor2_speed_target,motors2,1,3);
- }
- void Sub::track_pidcontrol(float _targethead,int16_t &_motor1,int16_t &_motor2){
- int16_t motors1=startval;
- int16_t motors2 =startval;
- uint32_t nowtime=AP_HAL::micros();
- static uint32_t lasttime = nowtime;
- float error = get_yaw_error(_targethead);
- if (track_motor_arm ==0)
- {
- if (nowtime - lasttime <500000)
- {
- motors1 = startval;
- motors2 = startval;
- }
- else{
-
- motors1 = constrain_int16(-Speedmax+trackpid.updatePID1(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
- motors2 = constrain_int16(-Speedmax+trackpid.updatePID2(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
-
- }
- }
- else if (track_motor_arm ==1)
- {
- lasttime = AP_HAL::micros();
- motors1 = startval;
- motors2 =startval;
-
- }
- else if (track_motor_arm ==2)
- {
-
- if (nowtime - lasttime <500000)
- {
- motors1 =startval;
- motors2 = startval;
- }
- else{
- motors1 = constrain_int16(Speedmax+trackpid.updatePID1(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
- motors2 = constrain_int16(Speedmax+trackpid.updatePID2(0,0,error,(float)Speedmax),-Speedmax,Speedmax);
-
- }
-
- }
-
- _motor1 = motors1;
- _motor2 = motors2;
-
- }
-
|