mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-06-28 03:20:34 +08:00
ekf2: terrain flow - migrate to Symforce
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyright (c) 2023 PX4 Development Team
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. Neither the name PX4 nor the names of its contributors may be
|
||||
used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
File: derivation_terrain_estimator.py
|
||||
Description:
|
||||
"""
|
||||
|
||||
import symforce.symbolic as sf
|
||||
from derivation_utils import *
|
||||
|
||||
def predict_opt_flow(
|
||||
terrain_vpos: sf.Scalar,
|
||||
q_att: sf.V4,
|
||||
v: sf.V3,
|
||||
pos_z: sf.Scalar,
|
||||
epsilon : sf.Scalar
|
||||
):
|
||||
R_to_earth = quat_to_rot(q_att)
|
||||
flow_pred = sf.V2()
|
||||
dist = - (pos_z - terrain_vpos)
|
||||
dist = add_epsilon_sign(dist, dist, epsilon)
|
||||
flow_pred[0] = -v[1] / dist * R_to_earth[2, 2]
|
||||
flow_pred[1] = v[0] / dist * R_to_earth[2, 2]
|
||||
return flow_pred
|
||||
|
||||
def terr_est_compute_flow_xy_innov_var_and_hx(
|
||||
terrain_vpos: sf.Scalar,
|
||||
P: sf.Scalar,
|
||||
q_att: sf.V4,
|
||||
v: sf.V3,
|
||||
pos_z: sf.Scalar,
|
||||
R: sf.Scalar,
|
||||
epsilon : sf.Scalar
|
||||
):
|
||||
flow_pred = predict_opt_flow(terrain_vpos, q_att, v, pos_z, epsilon)
|
||||
Hx = sf.V1(flow_pred[0]).jacobian(terrain_vpos)
|
||||
Hy = sf.V1(flow_pred[1]).jacobian(terrain_vpos)
|
||||
|
||||
innov_var = sf.V2()
|
||||
innov_var[0] = (Hx * P * Hx.T + R)[0,0]
|
||||
innov_var[1] = (Hy * P * Hy.T + R)[0,0]
|
||||
|
||||
return (innov_var, Hx[0, 0])
|
||||
|
||||
def terr_est_compute_flow_y_innov_var_and_h(
|
||||
terrain_vpos: sf.Scalar,
|
||||
P: sf.Scalar,
|
||||
q_att: sf.V4,
|
||||
v: sf.V3,
|
||||
pos_z: sf.Scalar,
|
||||
R: sf.Scalar,
|
||||
epsilon : sf.Scalar
|
||||
):
|
||||
flow_pred = predict_opt_flow(terrain_vpos, q_att, v, pos_z, epsilon)
|
||||
Hy = sf.V1(flow_pred[1]).jacobian(terrain_vpos)
|
||||
|
||||
innov_var = (Hy * P * Hy.T + R)[0,0]
|
||||
|
||||
return (innov_var, Hy[0, 0])
|
||||
|
||||
print("Derive terrain estimator equations...")
|
||||
generate_px4_function(terr_est_compute_flow_xy_innov_var_and_hx, output_names=["innov_var", "H"])
|
||||
generate_px4_function(terr_est_compute_flow_y_innov_var_and_h, output_names=["innov_var", "H"])
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// This file was autogenerated by symforce from template:
|
||||
// backends/cpp/templates/function/FUNCTION.h.jinja
|
||||
// Do NOT modify by hand.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <matrix/math.hpp>
|
||||
|
||||
namespace sym {
|
||||
|
||||
/**
|
||||
* This function was autogenerated from a symbolic function. Do not modify by hand.
|
||||
*
|
||||
* Symbolic function: terr_est_compute_flow_xy_innov_var_and_hx
|
||||
*
|
||||
* Args:
|
||||
* terrain_vpos: Scalar
|
||||
* P: Scalar
|
||||
* q_att: Matrix41
|
||||
* v: Matrix31
|
||||
* pos_z: Scalar
|
||||
* R: Scalar
|
||||
* epsilon: Scalar
|
||||
*
|
||||
* Outputs:
|
||||
* innov_var: Matrix21
|
||||
* H: Scalar
|
||||
*/
|
||||
template <typename Scalar>
|
||||
void TerrEstComputeFlowXyInnovVarAndHx(const Scalar terrain_vpos, const Scalar P,
|
||||
const matrix::Matrix<Scalar, 4, 1>& q_att,
|
||||
const matrix::Matrix<Scalar, 3, 1>& v, const Scalar pos_z,
|
||||
const Scalar R, const Scalar epsilon,
|
||||
matrix::Matrix<Scalar, 2, 1>* const innov_var = nullptr,
|
||||
Scalar* const H = nullptr) {
|
||||
// Total ops: 28
|
||||
|
||||
// Input arrays
|
||||
|
||||
// Intermediate terms (4)
|
||||
const Scalar _tmp0 = std::pow(q_att(0, 0), Scalar(2)) - std::pow(q_att(1, 0), Scalar(2)) -
|
||||
std::pow(q_att(2, 0), Scalar(2)) + std::pow(q_att(3, 0), Scalar(2));
|
||||
const Scalar _tmp1 = pos_z - terrain_vpos;
|
||||
const Scalar _tmp2 =
|
||||
-_tmp1 + epsilon * (2 * math::min<Scalar>(0, -(((_tmp1) > 0) - ((_tmp1) < 0))) + 1);
|
||||
const Scalar _tmp3 = P * std::pow(_tmp0, Scalar(2)) / std::pow(_tmp2, Scalar(4));
|
||||
|
||||
// Output terms (2)
|
||||
if (innov_var != nullptr) {
|
||||
matrix::Matrix<Scalar, 2, 1>& _innov_var = (*innov_var);
|
||||
|
||||
_innov_var(0, 0) = R + _tmp3 * std::pow(v(1, 0), Scalar(2));
|
||||
_innov_var(1, 0) = R + _tmp3 * std::pow(v(0, 0), Scalar(2));
|
||||
}
|
||||
|
||||
if (H != nullptr) {
|
||||
Scalar& _H = (*H);
|
||||
|
||||
_H = _tmp0 * v(1, 0) / std::pow(_tmp2, Scalar(2));
|
||||
}
|
||||
} // NOLINT(readability/fn_size)
|
||||
|
||||
// NOLINTNEXTLINE(readability/fn_size)
|
||||
} // namespace sym
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// This file was autogenerated by symforce from template:
|
||||
// backends/cpp/templates/function/FUNCTION.h.jinja
|
||||
// Do NOT modify by hand.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <matrix/math.hpp>
|
||||
|
||||
namespace sym {
|
||||
|
||||
/**
|
||||
* This function was autogenerated from a symbolic function. Do not modify by hand.
|
||||
*
|
||||
* Symbolic function: terr_est_compute_flow_y_innov_var_and_h
|
||||
*
|
||||
* Args:
|
||||
* terrain_vpos: Scalar
|
||||
* P: Scalar
|
||||
* q_att: Matrix41
|
||||
* v: Matrix31
|
||||
* pos_z: Scalar
|
||||
* R: Scalar
|
||||
* epsilon: Scalar
|
||||
*
|
||||
* Outputs:
|
||||
* innov_var: Scalar
|
||||
* H: Scalar
|
||||
*/
|
||||
template <typename Scalar>
|
||||
void TerrEstComputeFlowYInnovVarAndH(const Scalar terrain_vpos, const Scalar P,
|
||||
const matrix::Matrix<Scalar, 4, 1>& q_att,
|
||||
const matrix::Matrix<Scalar, 3, 1>& v, const Scalar pos_z,
|
||||
const Scalar R, const Scalar epsilon,
|
||||
Scalar* const innov_var = nullptr, Scalar* const H = nullptr) {
|
||||
// Total ops: 26
|
||||
|
||||
// Input arrays
|
||||
|
||||
// Intermediate terms (3)
|
||||
const Scalar _tmp0 = std::pow(q_att(0, 0), Scalar(2)) - std::pow(q_att(1, 0), Scalar(2)) -
|
||||
std::pow(q_att(2, 0), Scalar(2)) + std::pow(q_att(3, 0), Scalar(2));
|
||||
const Scalar _tmp1 = pos_z - terrain_vpos;
|
||||
const Scalar _tmp2 =
|
||||
-_tmp1 + epsilon * (2 * math::min<Scalar>(0, -(((_tmp1) > 0) - ((_tmp1) < 0))) + 1);
|
||||
|
||||
// Output terms (2)
|
||||
if (innov_var != nullptr) {
|
||||
Scalar& _innov_var = (*innov_var);
|
||||
|
||||
_innov_var =
|
||||
P * std::pow(_tmp0, Scalar(2)) * std::pow(v(0, 0), Scalar(2)) / std::pow(_tmp2, Scalar(4)) +
|
||||
R;
|
||||
}
|
||||
|
||||
if (H != nullptr) {
|
||||
Scalar& _H = (*H);
|
||||
|
||||
_H = -_tmp0 * v(0, 0) / std::pow(_tmp2, Scalar(2));
|
||||
}
|
||||
} // NOLINT(readability/fn_size)
|
||||
|
||||
// NOLINTNEXTLINE(readability/fn_size)
|
||||
} // namespace sym
|
||||
@@ -1,97 +0,0 @@
|
||||
"""
|
||||
|
||||
This script calculates the observation scalars (H matrix) for fusing optical flow
|
||||
measurements for terrain estimation.
|
||||
|
||||
@author: roman
|
||||
"""
|
||||
|
||||
from sympy import *
|
||||
|
||||
# q: quaternion describing rotation from frame 1 to frame 2
|
||||
# returns a rotation matrix derived form q which describes the same
|
||||
# rotation
|
||||
def quat2Rot(q):
|
||||
q0 = q[0]
|
||||
q1 = q[1]
|
||||
q2 = q[2]
|
||||
q3 = q[3]
|
||||
|
||||
Rot = Matrix([[q0**2 + q1**2 - q2**2 - q3**2, 2*(q1*q2 - q0*q3), 2*(q1*q3 + q0*q2)],
|
||||
[2*(q1*q2 + q0*q3), q0**2 - q1**2 + q2**2 - q3**2, 2*(q2*q3 - q0*q1)],
|
||||
[2*(q1*q3-q0*q2), 2*(q2*q3 + q0*q1), q0**2 - q1**2 - q2**2 + q3**2]])
|
||||
|
||||
return Rot
|
||||
|
||||
# take an expression calculated by the cse() method and write the expression
|
||||
# into a text file in C format
|
||||
def write_simplified(P_touple, filename, out_name):
|
||||
subs = P_touple[0]
|
||||
P = Matrix(P_touple[1])
|
||||
fd = open(filename, 'a')
|
||||
|
||||
is_vector = P.shape[0] == 1 or P.shape[1] == 1
|
||||
|
||||
# write sub expressions
|
||||
for index, item in enumerate(subs):
|
||||
fd.write('float ' + str(item[0]) + ' = ' + str(item[1]) + ';\n')
|
||||
|
||||
# write actual matrix values
|
||||
fd.write('\n')
|
||||
|
||||
if not is_vector:
|
||||
iterator = range(0,sqrt(len(P)), 1)
|
||||
for row in iterator:
|
||||
for column in iterator:
|
||||
fd.write(out_name + '(' + str(row) + ',' + str(column) + ') = ' + str(P[row, column]) + ';\n')
|
||||
else:
|
||||
iterator = range(0, len(P), 1)
|
||||
|
||||
for item in iterator:
|
||||
fd.write(out_name + '(' + str(item) + ') = ' + str(P[item]) + ';\n')
|
||||
|
||||
fd.write('\n\n')
|
||||
fd.close()
|
||||
|
||||
########## Symbolic variable definition #######################################
|
||||
|
||||
|
||||
# vehicle velocity
|
||||
v_x = Symbol("v_x", real=True) # vehicle body x velocity
|
||||
v_y = Symbol("v_y", real=True) # vehicle body y velocity
|
||||
|
||||
# unit quaternion describing vehicle attitude, qw is real part
|
||||
qw = Symbol("q0", real=True)
|
||||
qx = Symbol("q1", real=True)
|
||||
qy = Symbol("q2", real=True)
|
||||
qz = Symbol("q3", real=True)
|
||||
q_att = Matrix([qw, qx, qy, qz])
|
||||
|
||||
# terrain vertial position in local NED frame
|
||||
_terrain_vpos = Symbol("_terrain_vpos", real=True)
|
||||
|
||||
_terrain_var = Symbol("_terrain_var", real=True)
|
||||
|
||||
# vehicle vertical position in local NED frame
|
||||
pos_z = Symbol("z", real=True)
|
||||
|
||||
R_body_to_earth = quat2Rot(q_att)
|
||||
|
||||
# Optical flow around x axis
|
||||
flow_x = -v_y / (_terrain_vpos - pos_z) * R_body_to_earth[2,2]
|
||||
|
||||
# Calculate observation scalar
|
||||
H_x = Matrix([flow_x]).jacobian(Matrix([_terrain_vpos]))
|
||||
|
||||
H_x_simple = cse(H_x, symbols('t0:30'))
|
||||
|
||||
# Optical flow around y axis
|
||||
flow_y = v_x / (_terrain_vpos - pos_z) * R_body_to_earth[2,2]
|
||||
|
||||
# Calculate observation scalar
|
||||
H_y = Matrix([flow_y]).jacobian(Matrix([_terrain_vpos]))
|
||||
|
||||
H_y_simple = cse(H_y, symbols('t0:30'))
|
||||
|
||||
write_simplified(H_x_simple, "flow_x_observation.txt", "Hx")
|
||||
write_simplified(H_y_simple, "flow_y_observation.txt", "Hy")
|
||||
Reference in New Issue
Block a user