Skip to content

Commit 06cfa57

Browse files
committed
SITL: add simulated tie-down clamp
1 parent b974da9 commit 06cfa57

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

libraries/SITL/SIM_Aircraft.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,45 @@ void Aircraft::extrapolate_sensors(float delta_time)
958958
velocity_air_bf = dcm.transposed() * velocity_air_ef;
959959
}
960960

961+
bool Aircraft::Clamp::clamped(Aircraft &aircraft, const struct sitl_input &input)
962+
{
963+
const auto clamp_ch = AP::sitl()->clamp_ch;
964+
if (clamp_ch < 1) {
965+
return false;
966+
}
967+
const uint32_t clamp_idx = clamp_ch - 1;
968+
if (clamp_idx > ARRAY_SIZE(input.servos)) {
969+
return false;
970+
}
971+
const uint16_t servo_pos = input.servos[clamp_idx];
972+
bool new_clamped = currently_clamped;
973+
if (servo_pos < 1200) {
974+
if (currently_clamped) {
975+
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "SITL: Clamp: released vehicle");
976+
new_clamped = false;
977+
}
978+
grab_attempted = false;
979+
} else {
980+
// re-clamp if < 10cm from home
981+
if (servo_pos > 1800 && !grab_attempted) {
982+
const Vector3d pos = aircraft.get_position_relhome();
983+
const float distance_from_home = pos.length();
984+
// GCS_SEND_TEXT(MAV_SEVERITY_INFO, "SITL: Clamp: dist=%f", distance_from_home);
985+
if (distance_from_home < 0.5) {
986+
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "SITL: Clamp: grabbed vehicle");
987+
new_clamped = true;
988+
} else if (!grab_attempted) {
989+
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "SITL: Clamp: missed vehicle");
990+
}
991+
grab_attempted = true;
992+
}
993+
}
994+
995+
currently_clamped = new_clamped;
996+
997+
return currently_clamped;
998+
}
999+
9611000
void Aircraft::update_external_payload(const struct sitl_input &input)
9621001
{
9631002
external_payload_mass = 0;

libraries/SITL/SIM_Aircraft.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ class Aircraft {
317317
// get local thermal updraft
318318
float get_local_updraft(const Vector3d &currentPos);
319319

320+
// clamp support
321+
class Clamp {
322+
public:
323+
bool clamped(class Aircraft&, const struct sitl_input &input); // true if the vehicle is currently clamped down
324+
private:
325+
bool currently_clamped;
326+
bool grab_attempted; // avoid warning multiple times about missed grab
327+
} clamp;
328+
320329
private:
321330
uint64_t last_time_us;
322331
uint32_t frame_counter;

libraries/SITL/SIM_Multicopter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ void MultiCopter::update(const struct sitl_input &input)
6161
Vector3f rot_accel;
6262

6363
calculate_forces(input, rot_accel, accel_body);
64+
// simulated clamp holding vehicle down
65+
if (clamp.clamped(*this, input)) {
66+
rot_accel.zero();
67+
accel_body.zero();
68+
}
6469

6570
// estimate voltage and current
6671
frame->current_and_voltage(battery_voltage, battery_current);

libraries/SITL/SITL.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,11 @@ const AP_Param::GroupInfo SIM::var_ins[] = {
10831083
// @Description: Allow relay output operation when running SIM-on-hardware
10841084
AP_GROUPINFO("OH_RELAY_MSK", 48, SIM, on_hardware_relay_enable_mask, SIM_DEFAULT_ENABLED_RELAY_CHANNELS),
10851085

1086+
// @Param: CLAMP_CH
1087+
// @DisplayName: Simulated Clamp Channel
1088+
// @Description: If non-zero the vehicle will be clamped in position until the value on this servo channel passes 1800PWM
1089+
AP_GROUPINFO("CLAMP_CH", 49, SIM, clamp_ch, 0),
1090+
10861091
// the IMUT parameters must be last due to the enable parameters
10871092
#if HAL_INS_TEMPERATURE_CAL_ENABLE
10881093
AP_SUBGROUPINFO(imu_tcal[0], "IMUT1_", 61, SIM, AP_InertialSensor_TCal),

libraries/SITL/SITL.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ class SIM {
527527
// Master instance to use servos from with slave instances
528528
AP_Int8 ride_along_master;
529529

530+
// clamp simulation - servo channel starting at offset 1 (usually ailerons)
531+
AP_Int8 clamp_ch;
532+
530533
#if AP_SIM_INS_FILE_ENABLED
531534
enum INSFileMode {
532535
INS_FILE_NONE = 0,

0 commit comments

Comments
 (0)