void Rover::send_rangefinder(mavlink_channel_t chan) { float distance_cm; float voltage; bool got_one = false; // report smaller distance of all rangefinders for (uint8_t i=0; i<rangefinder.num_sensors(); i++) { AP_RangeFinder_Backend *s = rangefinder.get_backend(i); if (s == nullptr) { continue; } if (!got_one || s->distance_cm() < distance_cm) { distance_cm = s->distance_cm(); voltage = s->voltage_mv(); got_one = true; } } if (!got_one) { // no relevant data found return; } mavlink_msg_rangefinder_send( chan, distance_cm * 0.01f, voltage); }
MAV_DISTANCE_SENSOR RangeFinder::get_mav_distance_sensor_type_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return MAV_DISTANCE_SENSOR_UNKNOWN; } return backend->get_mav_distance_sensor_type(); }
uint8_t RangeFinder::range_valid_count_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return 0; } return backend->range_valid_count(); }
const Vector3f &RangeFinder::get_pos_offset_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return pos_offset_zero; } return backend->get_pos_offset(); }
int16_t RangeFinder::ground_clearance_cm_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return 0; } return backend->ground_clearance_cm(); }
bool RangeFinder::has_data_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return false; } return backend->has_data(); }
uint16_t RangeFinder::voltage_mv_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return 0; } return backend->voltage_mv(); }
RangeFinder::RangeFinder_Status RangeFinder::status_orient(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return RangeFinder_NotConnected; } return backend->status(); }
uint32_t RangeFinder::last_reading_ms(enum Rotation orientation) const { AP_RangeFinder_Backend *backend = find_instance(orientation); if (backend == nullptr) { return 0; } return backend->last_reading_ms(); }
// find first range finder instance with the specified orientation AP_RangeFinder_Backend *RangeFinder::find_instance(enum Rotation orientation) const { for (uint8_t i=0; i<num_instances; i++) { AP_RangeFinder_Backend *backend = get_backend(i); if (backend == nullptr) { continue; } if (backend->orientation() != orientation) { continue; } return backend; } return nullptr; }
// update error mask of sensors and subsystems. The mask // uses the MAV_SYS_STATUS_* values from mavlink. If a bit is set // then it indicates that the sensor or subsystem is present but // not functioning correctly. void Rover::update_sensor_status_flags(void) { // default sensors present control_sensors_present = MAVLINK_SENSOR_PRESENT_DEFAULT; // first what sensors/controllers we have if (g.compass_enabled) { control_sensors_present |= MAV_SYS_STATUS_SENSOR_3D_MAG; // compass present } if (gps.status() > AP_GPS::NO_GPS) { control_sensors_present |= MAV_SYS_STATUS_SENSOR_GPS; } if (g2.visual_odom.enabled()) { control_sensors_present |= MAV_SYS_STATUS_SENSOR_VISION_POSITION; } if (rover.DataFlash.logging_present()) { // primary logging only (usually File) control_sensors_present |= MAV_SYS_STATUS_LOGGING; } if (rover.g2.proximity.get_status() > AP_Proximity::Proximity_NotConnected) { control_sensors_present |= MAV_SYS_STATUS_SENSOR_LASER_POSITION; } // all present sensors enabled by default except rate control, attitude stabilization, yaw, altitude, position control and motor output which we will set individually control_sensors_enabled = control_sensors_present & (~MAV_SYS_STATUS_SENSOR_ANGULAR_RATE_CONTROL & ~MAV_SYS_STATUS_SENSOR_ATTITUDE_STABILIZATION & ~MAV_SYS_STATUS_SENSOR_YAW_POSITION & ~MAV_SYS_STATUS_SENSOR_XY_POSITION_CONTROL & ~MAV_SYS_STATUS_SENSOR_MOTOR_OUTPUTS & ~MAV_SYS_STATUS_LOGGING & ~MAV_SYS_STATUS_SENSOR_BATTERY); if (control_mode->attitude_stabilized()) { control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_ANGULAR_RATE_CONTROL; // 3D angular rate control control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_ATTITUDE_STABILIZATION; // 3D angular rate control } if (control_mode->is_autopilot_mode()) { control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_YAW_POSITION; // yaw position control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_XY_POSITION_CONTROL; // X/Y position control } if (rover.DataFlash.logging_enabled()) { control_sensors_enabled |= MAV_SYS_STATUS_LOGGING; } // set motors outputs as enabled if safety switch is not disarmed (i.e. either NONE or ARMED) if (hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_DISARMED) { control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_MOTOR_OUTPUTS; } if (battery.num_instances() > 0) { control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_BATTERY; } // default to all healthy except compass and gps which we set individually control_sensors_health = control_sensors_present & (~MAV_SYS_STATUS_SENSOR_3D_MAG & ~MAV_SYS_STATUS_SENSOR_GPS); if (g.compass_enabled && compass.healthy(0) && ahrs.use_compass()) { control_sensors_health |= MAV_SYS_STATUS_SENSOR_3D_MAG; } if (gps.is_healthy()) { control_sensors_health |= MAV_SYS_STATUS_SENSOR_GPS; } if (g2.visual_odom.enabled() && !g2.visual_odom.healthy()) { control_sensors_health &= ~MAV_SYS_STATUS_SENSOR_VISION_POSITION; } if (!ins.get_gyro_health_all() || !ins.gyro_calibrated_ok_all()) { control_sensors_health &= ~MAV_SYS_STATUS_SENSOR_3D_GYRO; } if (!ins.get_accel_health_all()) { control_sensors_health &= ~MAV_SYS_STATUS_SENSOR_3D_ACCEL; } if (ahrs.initialised() && !ahrs.healthy()) { // AHRS subsystem is unhealthy control_sensors_health &= ~MAV_SYS_STATUS_AHRS; } if (rangefinder.num_sensors() > 0) { control_sensors_present |= MAV_SYS_STATUS_SENSOR_LASER_POSITION; if (g.rangefinder_trigger_cm > 0) { control_sensors_enabled |= MAV_SYS_STATUS_SENSOR_LASER_POSITION; } AP_RangeFinder_Backend *s = rangefinder.get_backend(0); if (s != nullptr && s->has_data()) { control_sensors_health |= MAV_SYS_STATUS_SENSOR_LASER_POSITION; } } if (rover.g2.proximity.get_status() < AP_Proximity::Proximity_Good) { control_sensors_health &= ~MAV_SYS_STATUS_SENSOR_LASER_POSITION; } if (rover.DataFlash.logging_failed()) { control_sensors_health &= ~MAV_SYS_STATUS_LOGGING; } if (!battery.healthy() || battery.has_failsafed()) { control_sensors_enabled &= ~MAV_SYS_STATUS_SENSOR_BATTERY; } if (!initialised || ins.calibrating()) { // while initialising the gyros and accels are not enabled control_sensors_enabled &= ~(MAV_SYS_STATUS_SENSOR_3D_GYRO | MAV_SYS_STATUS_SENSOR_3D_ACCEL); control_sensors_health &= ~(MAV_SYS_STATUS_SENSOR_3D_GYRO | MAV_SYS_STATUS_SENSOR_3D_ACCEL); } #if FRSKY_TELEM_ENABLED == ENABLED // give mask of error flags to Frsky_Telemetry frsky_telemetry.update_sensor_status_flags(~control_sensors_health & control_sensors_enabled & control_sensors_present); #endif }
// select the height measurement to be fused from the available baro, range finder and GPS sources void NavEKF2_core::selectHeightForFusion() { // Read range finder data and check for new data in the buffer // This data is used by both height and optical flow fusion processing readRangeFinder(); rangeDataToFuse = storedRange.recall(rangeDataDelayed,imuDataDelayed.time_ms); // correct range data for the body frame position offset relative to the IMU // the corrected reading is the reading that would have been taken if the sensor was // co-located with the IMU if (rangeDataToFuse) { AP_RangeFinder_Backend *sensor = frontend->_rng.get_backend(rangeDataDelayed.sensor_idx); if (sensor != nullptr) { Vector3f posOffsetBody = sensor->get_pos_offset() - accelPosOffset; if (!posOffsetBody.is_zero()) { Vector3f posOffsetEarth = prevTnb.mul_transpose(posOffsetBody); rangeDataDelayed.rng += posOffsetEarth.z / prevTnb.c.z; } } } // read baro height data from the sensor and check for new data in the buffer readBaroData(); baroDataToFuse = storedBaro.recall(baroDataDelayed, imuDataDelayed.time_ms); // select height source if (extNavUsedForPos) { // always use external vision as the hight source if using for position. activeHgtSource = HGT_SOURCE_EV; } else if (((frontend->_useRngSwHgt > 0) || (frontend->_altSource == 1)) && (imuSampleTime_ms - rngValidMeaTime_ms < 500)) { if (frontend->_altSource == 1) { // always use range finder activeHgtSource = HGT_SOURCE_RNG; } else { // determine if we are above or below the height switch region float rangeMaxUse = 1e-4f * (float)frontend->_rng.max_distance_cm_orient(ROTATION_PITCH_270) * (float)frontend->_useRngSwHgt; bool aboveUpperSwHgt = (terrainState - stateStruct.position.z) > rangeMaxUse; bool belowLowerSwHgt = (terrainState - stateStruct.position.z) < 0.7f * rangeMaxUse; // If the terrain height is consistent and we are moving slowly, then it can be // used as a height reference in combination with a range finder // apply a hysteresis to the speed check to prevent rapid switching float horizSpeed = norm(stateStruct.velocity.x, stateStruct.velocity.y); bool dontTrustTerrain = ((horizSpeed > frontend->_useRngSwSpd) && filterStatus.flags.horiz_vel) || !terrainHgtStable; float trust_spd_trigger = MAX((frontend->_useRngSwSpd - 1.0f),(frontend->_useRngSwSpd * 0.5f)); bool trustTerrain = (horizSpeed < trust_spd_trigger) && terrainHgtStable; /* * Switch between range finder and primary height source using height above ground and speed thresholds with * hysteresis to avoid rapid switching. Using range finder for height requires a consistent terrain height * which cannot be assumed if the vehicle is moving horizontally. */ if ((aboveUpperSwHgt || dontTrustTerrain) && (activeHgtSource == HGT_SOURCE_RNG)) { // cannot trust terrain or range finder so stop using range finder height if (frontend->_altSource == 0) { activeHgtSource = HGT_SOURCE_BARO; } else if (frontend->_altSource == 2) { activeHgtSource = HGT_SOURCE_GPS; } } else if (belowLowerSwHgt && trustTerrain && (activeHgtSource != HGT_SOURCE_RNG)) { // reliable terrain and range finder so start using range finder height activeHgtSource = HGT_SOURCE_RNG; } } } else if ((frontend->_altSource == 2) && ((imuSampleTime_ms - lastTimeGpsReceived_ms) < 500) && validOrigin && gpsAccuracyGood) { activeHgtSource = HGT_SOURCE_GPS; } else if ((frontend->_altSource == 3) && validOrigin && rngBcnGoodToAlign) { activeHgtSource = HGT_SOURCE_BCN; } else { activeHgtSource = HGT_SOURCE_BARO; } // Use Baro alt as a fallback if we lose range finder, GPS or external nav bool lostRngHgt = ((activeHgtSource == HGT_SOURCE_RNG) && ((imuSampleTime_ms - rngValidMeaTime_ms) > 500)); bool lostGpsHgt = ((activeHgtSource == HGT_SOURCE_GPS) && ((imuSampleTime_ms - lastTimeGpsReceived_ms) > 2000)); bool lostExtNavHgt = ((activeHgtSource == HGT_SOURCE_EV) && ((imuSampleTime_ms - extNavMeasTime_ms) > 2000)); if (lostRngHgt || lostGpsHgt || lostExtNavHgt) { activeHgtSource = HGT_SOURCE_BARO; } // if there is new baro data to fuse, calculate filtered baro data required by other processes if (baroDataToFuse) { // calculate offset to baro data that enables us to switch to Baro height use during operation if (activeHgtSource != HGT_SOURCE_BARO) { calcFiltBaroOffset(); } // filtered baro data used to provide a reference for takeoff // it is is reset to last height measurement on disarming in performArmingChecks() if (!getTakeoffExpected()) { const float gndHgtFiltTC = 0.5f; const float dtBaro = frontend->hgtAvg_ms*1.0e-3f; float alpha = constrain_float(dtBaro / (dtBaro+gndHgtFiltTC),0.0f,1.0f); meaHgtAtTakeOff += (baroDataDelayed.hgt-meaHgtAtTakeOff)*alpha; } } // If we are not using GPS as the primary height sensor, correct EKF origin height so that // combined local NED position height and origin height remains consistent with the GPS altitude // This also enables the GPS height to be used as a backup height source if (gpsDataToFuse && (((frontend->_originHgtMode & (1 << 0)) && (activeHgtSource == HGT_SOURCE_BARO)) || ((frontend->_originHgtMode & (1 << 1)) && (activeHgtSource == HGT_SOURCE_RNG))) ) { correctEkfOriginHeight(); } // Select the height measurement source if (extNavDataToFuse && (activeHgtSource == HGT_SOURCE_EV)) { hgtMea = -extNavDataDelayed.pos.z; posDownObsNoise = sq(constrain_float(extNavDataDelayed.posErr, 0.1f, 10.0f)); } else if (rangeDataToFuse && (activeHgtSource == HGT_SOURCE_RNG)) { // using range finder data // correct for tilt using a flat earth model if (prevTnb.c.z >= 0.7) { // calculate height above ground hgtMea = MAX(rangeDataDelayed.rng * prevTnb.c.z, rngOnGnd); // correct for terrain position relative to datum hgtMea -= terrainState; // enable fusion fuseHgtData = true; velPosObs[5] = -hgtMea; // set the observation noise posDownObsNoise = sq(constrain_float(frontend->_rngNoise, 0.1f, 10.0f)); // add uncertainty created by terrain gradient and vehicle tilt posDownObsNoise += sq(rangeDataDelayed.rng * frontend->_terrGradMax) * MAX(0.0f , (1.0f - sq(prevTnb.c.z))); } else { // disable fusion if tilted too far fuseHgtData = false; } } else if (gpsDataToFuse && (activeHgtSource == HGT_SOURCE_GPS)) { // using GPS data hgtMea = gpsDataDelayed.hgt; // enable fusion velPosObs[5] = -hgtMea; fuseHgtData = true; // set the observation noise using receiver reported accuracy or the horizontal noise scaled for typical VDOP/HDOP ratio if (gpsHgtAccuracy > 0.0f) { posDownObsNoise = sq(constrain_float(gpsHgtAccuracy, 1.5f * frontend->_gpsHorizPosNoise, 100.0f)); } else { posDownObsNoise = sq(constrain_float(1.5f * frontend->_gpsHorizPosNoise, 0.1f, 10.0f)); } } else if (baroDataToFuse && (activeHgtSource == HGT_SOURCE_BARO)) { // using Baro data hgtMea = baroDataDelayed.hgt - baroHgtOffset; // enable fusion velPosObs[5] = -hgtMea; fuseHgtData = true; // set the observation noise posDownObsNoise = sq(constrain_float(frontend->_baroAltNoise, 0.1f, 10.0f)); // reduce weighting (increase observation noise) on baro if we are likely to be in ground effect if (getTakeoffExpected() || getTouchdownExpected()) { posDownObsNoise *= frontend->gndEffectBaroScaler; } // If we are in takeoff mode, the height measurement is limited to be no less than the measurement at start of takeoff // This prevents negative baro disturbances due to copter downwash corrupting the EKF altitude during initial ascent if (motorsArmed && getTakeoffExpected()) { hgtMea = MAX(hgtMea, meaHgtAtTakeOff); } } else { fuseHgtData = false; } // If we haven't fused height data for a while, then declare the height data as being timed out // set timeout period based on whether we have vertical GPS velocity available to constrain drift hgtRetryTime_ms = (useGpsVertVel && !velTimeout) ? frontend->hgtRetryTimeMode0_ms : frontend->hgtRetryTimeMode12_ms; if (imuSampleTime_ms - lastHgtPassTime_ms > hgtRetryTime_ms) { hgtTimeout = true; } else { hgtTimeout = false; } }