PX4 Firmware
PX4 Autopilot Software http://px4.io
lidar.cpp
Go to the documentation of this file.
1 #include "../BlockLocalPositionEstimator.hpp"
3 #include <matrix/math.hpp>
4 
6 
7 // required number of samples for sensor
8 // to initialize
9 //
10 static const uint32_t REQ_LIDAR_INIT_COUNT = 10;
11 static const uint32_t LIDAR_TIMEOUT = 1000000; // 1.0 s
12 
14 {
15  // measure
17 
18  if (lidarMeasure(y) != OK) {
20  }
21 
22  // if finished
24  mavlink_and_console_log_info(&mavlink_log_pub, "[lpe] lidar init: "
25  "mean %d cm stddev %d cm",
26  int(100 * _lidarStats.getMean()(0)),
27  int(100 * _lidarStats.getStdDev()(0)));
30  }
31 }
32 
34 {
35  // measure
36  float d = _sub_lidar->get().current_distance;
37  float eps = 0.01f; // 1 cm
38  float min_dist = _sub_lidar->get().min_distance + eps;
39  float max_dist = _sub_lidar->get().max_distance - eps;
40 
41  // prevent driver from setting min dist below eps
42  if (min_dist < eps) {
43  min_dist = eps;
44  }
45 
46  // check for bad data
47  if (d > max_dist || d < min_dist) {
48  return -1;
49  }
50 
51  // update stats
54  y.setZero();
56  y(0) = (d + _param_lpe_ldr_off_z.get()) *
57  cosf(euler.phi()) *
58  cosf(euler.theta());
59  return OK;
60 }
61 
63 {
64  // measure lidar
66 
67  if (lidarMeasure(y) != OK) { return; }
68 
69  // measurement matrix
71  C.setZero();
72  // y = -(z - tz)
73  // TODO could add trig to make this an EKF correction
74  C(Y_lidar_z, X_z) = -1; // measured altitude, negative down dir.
75  C(Y_lidar_z, X_tz) = 1; // measured altitude, negative down dir.
76 
77  // use parameter covariance unless sensor provides reasonable value
79  R.setZero();
80  float cov = _sub_lidar->get().variance;
81 
82  if (cov < 1.0e-3f) {
83  R(0, 0) = _param_lpe_ldr_z.get() * _param_lpe_ldr_z.get();
84 
85  } else {
86  R(0, 0) = cov;
87  }
88 
89  // residual
90  Vector<float, n_y_lidar> r = y - C * _x;
91  // residual covariance
93 
94  // publish innovations
95  _pub_innov.get().hagl_innov = r(0);
96  _pub_innov.get().hagl_innov_var = S(0, 0);
97 
98  // residual covariance, (inverse)
99  Matrix<float, n_y_lidar, n_y_lidar> S_I = inv<float, n_y_lidar>(S);
100 
101  // fault detection
102  float beta = (r.transpose() * (S_I * r))(0, 0);
103 
104  if (beta > BETA_TABLE[n_y_lidar]) {
105  if (!(_sensorFault & SENSOR_LIDAR)) {
106  mavlink_and_console_log_info(&mavlink_log_pub, "[lpe] lidar fault, beta %5.2f", double(beta));
108  }
109 
110  // abort correction
111  return;
112 
113  } else if (_sensorFault & SENSOR_LIDAR) {
114  _sensorFault &= ~SENSOR_LIDAR;
115  mavlink_and_console_log_info(&mavlink_log_pub, "[lpe] lidar OK");
116  }
117 
118  // kalman filter correction always
120  Vector<float, n_x> dx = K * r;
121  _x += dx;
122  m_P -= K * C * m_P;
123 }
124 
126 {
128  if (!(_sensorTimeout & SENSOR_LIDAR)) {
130  _lidarStats.reset();
131  mavlink_and_console_log_info(&mavlink_log_pub, "[lpe] lidar timeout ");
132  }
133  }
134 }
static const float BETA_TABLE[7]
Type theta() const
Definition: Euler.hpp:132
BlockStats< float, n_y_lidar > _lidarStats
matrix::Vector< Type, M > getMean()
Definition: BlockStats.hpp:85
orb_advert_t mavlink_log_pub
uORB::SubscriptionData< distance_sensor_s > * _sub_lidar
Type phi() const
Definition: Euler.hpp:128
Quaternion class.
Definition: Dcm.hpp:24
Matrix< Type, N, M > transpose() const
Definition: Matrix.hpp:353
uORB::SubscriptionData< vehicle_attitude_s > _sub_att
static const uint32_t REQ_LIDAR_INIT_COUNT
Definition: lidar.cpp:10
Scalar< float > Scalarf
Definition: Scalar.hpp:53
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
static const uint32_t LIDAR_TIMEOUT
Definition: lidar.cpp:11
Euler angles class.
Definition: AxisAngle.hpp:18
int lidarMeasure(Vector< float, n_y_lidar > &y)
Definition: lidar.cpp:33
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
const T & get() const
matrix::Vector< Type, M > getStdDev()
Definition: BlockStats.hpp:90
#define OK
Definition: uavcan_main.cpp:71
uORB::PublicationData< ekf2_innovations_s > _pub_innov
void update(const matrix::Vector< Type, M > &u)
Definition: BlockStats.hpp:71