|
@@ -1,25 +1,46 @@
|
|
#include "Sub.h"
|
|
#include "Sub.h"
|
|
|
|
+//#include "/AP_Math/AP_Math.h"
|
|
|
|
+
|
|
extern mavlink_rov_state_monitoring_t rov_message;
|
|
extern mavlink_rov_state_monitoring_t rov_message;
|
|
|
|
+Quaternion attitude_desired_quat_;
|
|
|
|
+
|
|
|
|
|
|
|
|
+/*
|
|
|
|
+ * control_althold.pde - init and run calls for althold, flight mode
|
|
|
|
+ */
|
|
|
|
|
|
-// stabilize_init - initialise stabilize controller
|
|
|
|
|
|
+// althold_init - initialise althold controller
|
|
bool Sub::sport_init()
|
|
bool Sub::sport_init()
|
|
{
|
|
{
|
|
- // set target altitude to zero for reporting
|
|
|
|
- pos_control.set_alt_target(0);
|
|
|
|
- if (prev_control_mode == ALT_HOLD) {
|
|
|
|
- last_roll = ahrs.roll_sensor;
|
|
|
|
- last_pitch = ahrs.pitch_sensor;
|
|
|
|
- } else {
|
|
|
|
- last_roll = 0;
|
|
|
|
- last_pitch = 0;
|
|
|
|
|
|
+ if(!control_check_barometer()) {
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // initialize vertical speeds and leash lengths
|
|
|
|
+ // sets the maximum speed up and down returned by position controller
|
|
|
|
+ pos_control.set_max_speed_z(-get_pilot_speed_dn(), g.pilot_speed_up);
|
|
|
|
+ pos_control.set_max_accel_z(g.pilot_accel_z);
|
|
|
|
+ pos_control.relax_alt_hold_controllers();
|
|
|
|
+ //pos_control.set_alt_target(-20);//cm
|
|
|
|
+ pos_control.set_target_to_stopping_point_z();
|
|
|
|
+ holding_depth = true;
|
|
|
|
+ ahrs.get_quat_body_to_ned(attitude_desired_quat_);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ last_roll = 0;
|
|
|
|
+ last_pitch = 0;
|
|
|
|
+
|
|
|
|
+
|
|
last_yaw = ahrs.yaw_sensor;
|
|
last_yaw = ahrs.yaw_sensor;
|
|
- pitch_input_inc=0;
|
|
|
|
- yaw_press = (int16_t)(ahrs.yaw_sensor/100);//记住方位
|
|
|
|
last_input_ms = AP_HAL::millis();
|
|
last_input_ms = AP_HAL::millis();
|
|
|
|
+ last_input_ms_stable = AP_HAL::millis();
|
|
|
|
+
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " last_attitudeinit %d %d,%d ",(int)last_roll_s,(int)pitch_input_inc,(int)last_yaw_s);
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " init Z target %f %f",(float)pos_control.get_pos_target().z,barometer.get_altitude());
|
|
|
|
+
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
+
|
|
void Sub::handle_attitude_sport(){
|
|
void Sub::handle_attitude_sport(){
|
|
uint32_t tnow = AP_HAL::millis();
|
|
uint32_t tnow = AP_HAL::millis();
|
|
|
|
|
|
@@ -51,15 +72,17 @@ void Sub::handle_attitude_sport(){
|
|
|
|
|
|
target_yaw = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
|
target_yaw = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
|
|
if (abs(target_roll) > 300 || abs(target_pitch) > 300) {
|
|
if (abs(target_roll) > 300 || abs(target_pitch) > 300) {
|
|
- last_roll = ahrs.roll_sensor;
|
|
|
|
- last_pitch = ahrs.pitch_sensor;
|
|
|
|
- last_yaw = ahrs.yaw_sensor;
|
|
|
|
|
|
+ //last_roll = ahrs.roll_sensor;
|
|
|
|
+ //last_pitch = ahrs.pitch_sensor;
|
|
|
|
+ //last_yaw = ahrs.yaw_sensor;
|
|
|
|
+ ahrs.get_quat_body_to_ned(attitude_desired_quat_);
|
|
last_input_ms = tnow;
|
|
last_input_ms = tnow;
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(target_roll, target_pitch, target_yaw);
|
|
} else if (abs(target_yaw) > 300) {
|
|
} else if (abs(target_yaw) > 300) {
|
|
// if only yaw is being controlled, don't update pitch and roll
|
|
// if only yaw is being controlled, don't update pitch and roll
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, target_yaw);
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, target_yaw);
|
|
- last_yaw = ahrs.yaw_sensor;
|
|
|
|
|
|
+ //last_yaw = ahrs.yaw_sensor;
|
|
|
|
+ ahrs.get_quat_body_to_ned(attitude_desired_quat_);
|
|
last_input_ms = tnow;
|
|
last_input_ms = tnow;
|
|
} else if (tnow < last_input_ms + 250) {
|
|
} else if (tnow < last_input_ms + 250) {
|
|
// just brake for a few mooments so we don't bounce
|
|
// just brake for a few mooments so we don't bounce
|
|
@@ -67,57 +90,106 @@ void Sub::handle_attitude_sport(){
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, 0);
|
|
attitude_control.input_rate_bf_roll_pitch_yaw(0, 0, 0);
|
|
} else {
|
|
} else {
|
|
// Lock attitude
|
|
// Lock attitude
|
|
- attitude_control.input_euler_angle_roll_pitch_yaw(last_roll, last_pitch, last_yaw, true);
|
|
|
|
|
|
+ //attitude_control.input_euler_angle_roll_pitch_yaw(last_roll, last_pitch, last_yaw, true);
|
|
|
|
+ attitude_control.input_quaternion(attitude_desired_quat_);
|
|
}
|
|
}
|
|
|
|
+ static int f = 0;
|
|
|
|
+ f++;
|
|
|
|
+ if(f>800)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " sportrpy %f %f,%f ",(float)target_roll,(float)target_pitch,(float)target_yaw);
|
|
|
|
+
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " sportatd %f %f,%f ",(float)last_roll,(float)last_pitch,(float)last_yaw);
|
|
|
|
+ f=0;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-// stabilize_run - runs the main stabilize controller
|
|
|
|
|
|
+
|
|
|
|
+// althold_run - runs the althold controller
|
|
// should be called at 100hz or more
|
|
// should be called at 100hz or more
|
|
void Sub::sport_run()
|
|
void Sub::sport_run()
|
|
{
|
|
{
|
|
- // if not armed set throttle to zero and exit immediately
|
|
|
|
|
|
+ // When unarmed, disable motors and stabilization
|
|
if (!motors.armed()) {
|
|
if (!motors.armed()) {
|
|
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
|
|
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
|
|
- attitude_control.set_throttle_out(0,true,g.throttle_filt);
|
|
|
|
|
|
+ // Sub vehicles do not stabilize roll/pitch/yaw when not auto-armed (i.e. on the ground, pilot has never raised throttle)
|
|
|
|
+ attitude_control.set_throttle_out(0.5 ,true, g.throttle_filt);
|
|
attitude_control.relax_attitude_controllers();
|
|
attitude_control.relax_attitude_controllers();
|
|
|
|
+ pos_control.relax_alt_hold_controllers();
|
|
|
|
+ //pos_control.set_alt_target(-20);//cm
|
|
last_roll = 0;
|
|
last_roll = 0;
|
|
last_pitch = 0;
|
|
last_pitch = 0;
|
|
last_yaw = ahrs.yaw_sensor;
|
|
last_yaw = ahrs.yaw_sensor;
|
|
- yaw_press = (int16_t)(ahrs.yaw_sensor/100);//记住方位
|
|
|
|
|
|
+ holding_depth = false;
|
|
|
|
+ ahrs.get_quat_body_to_ned(attitude_desired_quat_);
|
|
|
|
+
|
|
|
|
+ static int f = 0;
|
|
|
|
+ f++;
|
|
|
|
+ if(f>800)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " disarm Z target %f %f %f",(float)pos_control.get_pos_target().z,pos_control.get_alt(),barometer.get_altitude());
|
|
|
|
+ f=0;
|
|
|
|
+ }
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Vehicle is armed, motors are free to run
|
|
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
|
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::THROTTLE_UNLIMITED);
|
|
|
|
+
|
|
|
|
+ handle_attitude_sport();
|
|
|
|
+
|
|
|
|
+ //handle_attitude();
|
|
|
|
|
|
- handle_attitude_sport();
|
|
|
|
|
|
|
|
- // output pilot's throttle
|
|
|
|
- //attitude_control.set_throttle_out(channel_throttle->norm_input(), false, g.throttle_filt);
|
|
|
|
- attitude_control.set_throttle_out(1.0-(float)PressLevel_f*0.1, false, g.throttle_filt);//压力分级
|
|
|
|
|
|
+ pos_control.update_z_controller();
|
|
|
|
+
|
|
|
|
+ // Read the output of the z controller and rotate it so it always points up
|
|
|
|
+ Vector3f throttle_vehicle_frame = ahrs.get_rotation_body_to_ned().transposed() * Vector3f(0, 0, motors.get_throttle_in_bidirectional());
|
|
|
|
+ // Output the Z controller + pilot input to all motors.
|
|
|
|
+
|
|
|
|
+ //TODO: scale throttle with the ammount of thrusters in the given direction
|
|
|
|
+ // motors.set_throttle(0.5+throttle_vehicle_frame.z + channel_throttle->norm_input()-0.5);
|
|
|
|
+ motors.set_throttle(throttle_vehicle_frame.z + 1.0-(float)PressLevel_f*0.1);
|
|
rov_message.pressure_level = int(PressLevel);
|
|
rov_message.pressure_level = int(PressLevel);
|
|
|
|
+ motors.set_forward(-throttle_vehicle_frame.x + channel_forward->norm_input());
|
|
|
|
+ motors.set_lateral(-throttle_vehicle_frame.y + channel_lateral->norm_input());
|
|
|
|
|
|
- //control_in is range -1000-1000
|
|
|
|
- //radio_in is raw pwm value
|
|
|
|
- motors.set_forward(channel_forward->norm_input());
|
|
|
|
- motors.set_lateral(channel_lateral->norm_input());
|
|
|
|
-
|
|
|
|
- //motors.set_lateral(0);
|
|
|
|
-
|
|
|
|
- static int j = 0;
|
|
|
|
- j++;
|
|
|
|
- if(j>800)
|
|
|
|
- {
|
|
|
|
- gcs().send_text(MAV_SEVERITY_INFO, " roll p y %d %d %d\n",(int16_t)(channel_lateral->norm_input()*5700),(int16_t)((0.5-channel_throttle->norm_input())*5700*2),channel_yaw->get_control_in());
|
|
|
|
- //gcs().send_text(MAV_SEVERITY_INFO, " throttle forward lateral %f %f %f \n",(float)PressLevel_f*0.1,channel_forward->norm_input());
|
|
|
|
- //gcs().send_text(MAV_SEVERITY_INFO, " channel_roll p y %d %d %d\n",channel_roll->get_control_in(),channel_pitch->get_control_in(),channel_yaw->get_control_in());
|
|
|
|
- //gcs().send_text(MAV_SEVERITY_INFO, " throttle forward lateral %f %f %f \n",channel_throttle->norm_input(),channel_forward->norm_input(),channel_lateral->norm_input());
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ // We rotate the RC inputs to the earth frame to check if the user is giving an input that would change the depth.
|
|
|
|
+ //Vector3f earth_frame_rc_inputs = ahrs.get_rotation_body_to_ned() * Vector3f(channel_forward->norm_input(), channel_lateral->norm_input(), (2.0f*(-0.5f+channel_throttle->norm_input())));
|
|
|
|
+ Vector3f earth_frame_rc_inputs = ahrs.get_rotation_body_to_ned() * Vector3f(channel_forward->norm_input(), channel_lateral->norm_input(), (2.0*(0.5-PressLevel_f*0.1)));
|
|
|
|
+
|
|
|
|
+ if (fabsf(earth_frame_rc_inputs.z) > 0.05f) { // Throttle input above 5%
|
|
|
|
+ // reset z targets to current values
|
|
|
|
+ holding_depth = false;
|
|
|
|
+ pos_control.relax_alt_hold_controllers();
|
|
|
|
+ //pos_control.set_alt_target(barometer.get_altitude()*100);//cm
|
|
|
|
+ } else { // hold z
|
|
|
|
+ if (ap.at_surface) { //最大油门不能向上动
|
|
|
|
+ pos_control.set_alt_target(g.surface_depth - 10.0f); // set target to 5cm below surface level
|
|
|
|
+ holding_depth = true;
|
|
|
|
+ } else if (ap.at_bottom) {//最大油门不能向下动
|
|
|
|
+ pos_control.set_alt_target(inertial_nav.get_altitude() + 20.0f); // set target to 10 cm above bottom
|
|
|
|
+ //pos_control.set_alt_target(barometer.get_altitude()*100 +20);//cm
|
|
|
|
+ holding_depth = true;
|
|
|
|
+ } else if (!holding_depth) {
|
|
|
|
+ pos_control.set_target_to_stopping_point_z();
|
|
|
|
+ holding_depth = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ static int f = 0;
|
|
|
|
+ f++;
|
|
|
|
+ if(f>800)
|
|
|
|
+ {
|
|
|
|
|
|
- j=0;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " Z target %f %f %f",(float)pos_control.get_pos_target().z,pos_control.get_alt(),barometer.get_altitude());
|
|
|
|
|
|
|
|
+ gcs().send_text(MAV_SEVERITY_INFO, " surface %d at_bottom %d ",ap.at_surface,ap.at_bottom);
|
|
|
|
+ f=0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|