版权声明:本文为博主原创博文,未经允许不得转载,若要转载,请说明出处并给出博文链接
上一篇博客《ArduCopter——ArduPilot——航点导航WPNav(一)》大致讲解了直线航点的实现,如何从origin一步一步通过推进中间航点,其中要受制于leash,最终到达destination。
本篇博客从博客名也可以了解,讲解Spline Navigation,即曲线导航,这种导航方式可能更有利于航拍
这个Spline曲线到底是如何得到的?在代码中是如何实现的?其背后的数学含义又是什么?相信你看了本篇博客会大致有一个比较清晰的理解。
①通过origin与destination,以及二者的速度,来求取这里的hermite_spline。
/// update_spline_solution - recalculates hermite_spline_solution grid
/// relies on _spline_origin_vel, _spline_destination_vel and _origin and _destination
void AC_WPNav::update_spline_solution(const Vector3f& origin, const Vector3f& dest, const Vector3f& origin_vel, const Vector3f& dest_vel)
{
_hermite_spline_solution[0] = origin;
_hermite_spline_solution[1] = origin_vel;
_hermite_spline_solution[2] = -origin*3.0f -origin_vel*2.0f + dest*3.0f - dest_vel;
_hermite_spline_solution[3] = origin*2.0f + origin_vel -dest*2.0f + dest_vel;
}
②这里运用上边①得到的_hermite_spline_solution数组,通过复杂的数学公式得到飞机的目标位置和速度。
// calc_spline_pos_vel_accel - calculates target position, velocity and acceleration for the given "spline_time"
/// relies on update_spline_solution being called when the segment's origin and destination were set
void AC_WPNav::calc_spline_pos_vel(float spline_time, Vector3f& position, Vector3f& velocity)
{
float spline_time_sqrd = spline_time * spline_time;
float spline_time_cubed = spline_time_sqrd * spline_time;
position = _hermite_spline_solution[0] + \
_hermite_spline_solution[1] * spline_time + \
_hermite_spline_solution[2] * spline_time_sqrd + \
_hermite_spline_solution[3] * spline_time_cubed;
velocity = _hermite_spline_solution[1] + \
_hermite_spline_solution[2] * 2.0f * spline_time + \
_hermite_spline_solution[3] * 3.0f * spline_time_sqrd;
}
看到这里,数学一般或者较差的同学可能很是懵比,和博主一样。可能 数学学到比较好的同学了解hermite这个人,了解hermite矩阵。我查阅了很多网上的资料,也没有找到比较直接的对应的资料供学习。
后来在Google的时候发现了,这里的spline navigation 其实就是运用的三次多项式曲线(cubic polynomial curve),这就让我联想到之前搜索到的三次埃尔米特样条(cubic hermite spline),这下就找到了它的数学依托——就是三次埃尔米特插值,这好像是《数值分析》必讲内容,我的已经还给老师了。
翻了翻课本,重新学习一下吧!!!
埃尔米特插值方法可以处理插值条件中含有导数值的插值问题,即知道插值节点处的函数值以及导数值,求插值多项式的插值问题。
这里插入一张维基百科的图片。其中p1和m1都是p0和m0的tangent切线,这里可以大致近似理解为是p0和m0的一阶导数。可以将t=0,1带入上公式,得到p(0)=p0;p(1)=p1;以及对上公式求导后,讲t=0,1带入公式后,可以得到p'(0)=m0;p'(1)=m1;
所以结合代码,就可以这样理解: p0——origin,p1—destination,m0—origin_vel, m1—destination_vel
那么 p(t) 是三次埃尔米特插值多项式,其实就是 ②中position,不信的话,拿出笔和纸,把图片中p(t)写成 p(t) = ?+ ? * t + ? * t² + ?* t³ 格式。
是不是一模一样?????
当然,不难看出②中的velocity就是position的微分!这就很符合我们的认知啦
③那么疑问来了,origin和destination以及他们的微分_velocity速度是如何得到的呢?
下面的这段代码告诉我们:
destination的速度会分三种情况SEGMENT_END_STOP、SEGMENT_END_STRAIGHT、SEGMENT_END_SPLINE,分别意味着destination为终止点、destination到next_destination是直线、destination到next_destination是曲线。
origin的速度是存在一些逻辑设定的,受制于上一段是直线还是Spline,当前的origin速度可以等价于上一段的destination速度,详见代码
/// set_spline_origin_and_destination - set origin and destination waypoints using position vectors (distance from home in cm)
/// terrain_alt should be true if origin.z and destination.z are desired altitudes above terrain (false if desired altitudes above ekf origin)
/// seg_type should be calculated by calling function based on the mission
bool AC_WPNav::set_spline_origin_and_destination(const Vector3f& origin, const Vector3f& destination, bool terrain_alt, bool stopped_at_start, spline_segment_end_type seg_end_type, const Vector3f& next_destination)
{
// mission is "active" if wpnav has been called recently and vehicle reached the previous waypoint
bool prev_segment_exists = (_flags.reached_destination && ((AP_HAL::millis() - _wp_last_update) < 1000));
float dt = _pos_control.time_since_last_xy_update();
if (dt >= 0.2f) {
dt = 0.0f;
}
// check _wp_accel_cmss is reasonable to avoid divide by zero
if (_wp_accel_cmss <= 0) {
_wp_accel_cmss.set_and_save(WPNAV_ACCELERATION);
}
// segment start types
// stop - vehicle is not moving at origin
// straight-fast - vehicle is moving, previous segment is straight. vehicle will fly straight through the waypoint before beginning it's spline path to the next wp
// _flag.segment_type holds whether prev segment is straight vs spline but we don't know if it has a delay
// spline-fast - vehicle is moving, previous segment is splined, vehicle will fly through waypoint but previous segment should have it flying in the correct direction (i.e. exactly parallel to position difference vector from previous segment's origin to this segment's destination)
// calculate spline velocity at origin
if (stopped_at_start || !prev_segment_exists) {
// if vehicle is stopped at the origin, set origin velocity to 0.02 * distance vector from origin to destination
_spline_origin_vel = (destination - origin) * dt;
_spline_time = 0.0f;
_spline_vel_scaler = 0.0f;
}else{
// look at previous segment to determine velocity at origin
if (_flags.segment_type == SEGMENT_STRAIGHT) {
// previous segment is straight, vehicle is moving so vehicle should fly straight through the origin
// before beginning it's spline path to the next waypoint. Note: we are using the previous segment's origin and destination
_spline_origin_vel = (_destination - _origin);
_spline_time = 0.0f; // To-Do: this should be set based on how much overrun there was from straight segment?
_spline_vel_scaler = _pos_control.get_vel_target().length(); // start velocity target from current target velocity
}else{
// previous segment is splined, vehicle will fly through origin
// we can use the previous segment's destination velocity as this segment's origin velocity
// Note: previous segment will leave destination velocity parallel to position difference vector
// from previous segment's origin to this segment's destination)
_spline_origin_vel = _spline_destination_vel;
if (_spline_time > 1.0f && _spline_time < 1.1f) { // To-Do: remove hard coded 1.1f
_spline_time -= 1.0f;
}else{
_spline_time = 0.0f;
}
// Note: we leave _spline_vel_scaler as it was from end of previous segment
}
}
// calculate spline velocity at destination
switch (seg_end_type) {
case SEGMENT_END_STOP:
// if vehicle stops at the destination set destination velocity to 0.02 * distance vector from origin to destination
_spline_destination_vel = (destination - origin) * dt;
_flags.fast_waypoint = false;
break;
case SEGMENT_END_STRAIGHT:
// if next segment is straight, vehicle's final velocity should face along the next segment's position
_spline_destination_vel = (next_destination - destination);
_flags.fast_waypoint = true;
break;
case SEGMENT_END_SPLINE:
// if next segment is splined, vehicle's final velocity should face parallel to the line from the origin to the next destination
_spline_destination_vel = (next_destination - origin);
_flags.fast_waypoint = true;
break;
}
// code below ensures we don't get too much overshoot when the next segment is short
float vel_len = _spline_origin_vel.length() + _spline_destination_vel.length();
float pos_len = (destination - origin).length() * 4.0f;
if (vel_len > pos_len) {
// if total start+stop velocity is more than twice position difference
// use a scaled down start and stop velocityscale the start and stop velocities down
float vel_scaling = pos_len / vel_len;
// update spline calculator
update_spline_solution(origin, destination, _spline_origin_vel * vel_scaling, _spline_destination_vel * vel_scaling);
}else{
// update spline calculator
update_spline_solution(origin, destination, _spline_origin_vel, _spline_destination_vel);
}
// store origin and destination locations
_origin = origin;
_destination = destination;
_terrain_alt = terrain_alt;
// calculate slow down distance
calc_slow_down_distance(_wp_speed_cms, _wp_accel_cmss);
// get alt-above-terrain
float terr_offset = 0.0f;
if (terrain_alt) {
if (!get_terrain_offset(terr_offset)) {
return false;
}
}
// initialise intermediate point to the origin
_pos_control.set_pos_target(origin + Vector3f(0,0,terr_offset));
_flags.reached_destination = false;
_flags.segment_type = SEGMENT_SPLINE;
_flags.new_wp_destination = true; // flag new waypoint so we can freeze the pos controller's feed forward and smooth the transition
_flags.wp_yaw_set = false;
// initialise yaw related variables
_track_length_xy = safe_sqrt(sq(_destination.x - _origin.x)+sq(_destination.y - _origin.y)); // horizontal track length (used to decide if we should update yaw)
return true;
}
/// set_spline_destination waypoint using position vector (distance from home in cm)
/// returns false if conversion from location to vector from ekf origin cannot be calculated
/// terrain_alt should be true if destination.z is a desired altitudes above terrain (false if its desired altitudes above ekf origin)
/// stopped_at_start should be set to true if vehicle is stopped at the origin
/// seg_end_type should be set to stopped, straight or spline depending upon the next segment's type
/// next_destination should be set to the next segment's destination if the seg_end_type is SEGMENT_END_STRAIGHT or SEGMENT_END_SPLINE
bool AC_WPNav::set_spline_destination(const Vector3f& destination, bool terrain_alt, bool stopped_at_start, spline_segment_end_type seg_end_type, const Vector3f& next_destination)
{
Vector3f origin;
// if waypoint controller is active and copter has reached the previous waypoint use current pos target as the origin
if ((AP_HAL::millis() - _wp_last_update) < 1000) {
origin = _pos_control.get_pos_target();
}else{
// otherwise calculate origin from the current position and velocity
_pos_control.get_stopping_point_xy(origin);
_pos_control.get_stopping_point_z(origin);
}
// convert origin to alt-above-terrain
if (terrain_alt) {
float terr_offset;
if (!get_terrain_offset(terr_offset)) {
return false;
}
origin.z -= terr_offset;
}
// set origin and destination
return set_spline_origin_and_destination(origin, destination, terrain_alt, stopped_at_start, seg_end_type, next_destination);
}
上面的代码段就是获取最新的 origin,这里的类似普通航点导航里面对origin的计算,destination当然是地面站设定的喽。
④接下来,就是具体的代码调用与实现了,spline navigation 核心函数 advance_spline_target_along_track(),以及如何被调用
/// advance_spline_target_along_track - move target location along track from origin to destination
bool AC_WPNav::advance_spline_target_along_track(float dt)
{
if (!_flags.reached_destination) {
Vector3f target_pos, target_vel;
// update target position and velocity from spline calculator
calc_spline_pos_vel(_spline_time, target_pos, target_vel);
// if target velocity is zero the origin and destination must be the same
// so flag reached destination (and protect against divide by zero)
float target_vel_length = target_vel.length();
if (is_zero(target_vel_length)) {
_flags.reached_destination = true;
return true;
}
_pos_delta_unit = target_vel / target_vel_length;
calculate_wp_leash_length();
// get current location
Vector3f curr_pos = _inav.get_position();
// get terrain altitude offset for origin and current position (i.e. change in terrain altitude from a position vs ekf origin)
float terr_offset = 0.0f;
if (_terrain_alt && !get_terrain_offset(terr_offset)) {
return false;
}
// calculate position error
Vector3f track_error = curr_pos - target_pos;
track_error.z -= terr_offset;
// calculate the horizontal error
_track_error_xy = norm(track_error.x, track_error.y);
// calculate the vertical error
float track_error_z = fabsf(track_error.z);
// get position control leash lengths
float leash_xy = _pos_control.get_leash_xy();
float leash_z;
if (track_error.z >= 0) {
leash_z = _pos_control.get_leash_up_z();
}else{
leash_z = _pos_control.get_leash_down_z();
}
// calculate how far along the track we could move the intermediate target before reaching the end of the leash
float track_leash_slack = MIN(_track_leash_length*(leash_z-track_error_z)/leash_z, _track_leash_length*(leash_xy-_track_error_xy)/leash_xy);
if (track_leash_slack < 0.0f) {
track_leash_slack = 0.0f;
}
// update velocity
float spline_dist_to_wp = (_destination - target_pos).length();
float vel_limit = _wp_speed_cms;
if (!is_zero(dt)) {
vel_limit = MIN(vel_limit, track_leash_slack/dt);
}
// if within the stopping distance from destination, set target velocity to sqrt of distance * 2 * acceleration
if (!_flags.fast_waypoint && spline_dist_to_wp < _slow_down_dist) {
_spline_vel_scaler = safe_sqrt(spline_dist_to_wp * 2.0f * _wp_accel_cmss);
}else if(_spline_vel_scaler < vel_limit) {
// increase velocity using acceleration
_spline_vel_scaler += _wp_accel_cmss * dt;
}
// constrain target velocity
_spline_vel_scaler = constrain_float(_spline_vel_scaler, 0.0f, vel_limit);
// scale the spline_time by the velocity we've calculated vs the velocity that came out of the spline calculator
_spline_time_scale = _spline_vel_scaler / target_vel_length;
// update target position
target_pos.z += terr_offset;
_pos_control.set_pos_target(target_pos);
// update the target yaw if origin and destination are at least 2m apart horizontally
if (_track_length_xy >= WPNAV_YAW_DIST_MIN) {
if (_pos_control.get_leash_xy() < WPNAV_YAW_DIST_MIN) {
// if the leash is very short (i.e. flying at low speed) use the target point's velocity along the track
if (!is_zero(target_vel.x) && !is_zero(target_vel.y)) {
set_yaw_cd(RadiansToCentiDegrees(atan2f(target_vel.y,target_vel.x)));
}
} else {
// point vehicle along the leash (i.e. point vehicle towards target point on the segment from origin to destination)
float track_error_xy_length = safe_sqrt(sq(track_error.x)+sq(track_error.y));
if (track_error_xy_length > MIN(WPNAV_YAW_DIST_MIN, _pos_control.get_leash_xy()*WPNAV_YAW_LEASH_PCT_MIN)) {
// To-Do: why is track_error sign reversed?
set_yaw_cd(RadiansToCentiDegrees(atan2f(-track_error.y,-track_error.x)));
}
}
}
// advance spline time to next step
_spline_time += _spline_time_scale*dt;
// we will reach the next waypoint in the next step so set reached_destination flag
// To-Do: is this one step too early?
if (_spline_time >= 1.0f) {
_flags.reached_destination = true;
}
}
return true;
}
/// update_spline - update spline controller
bool AC_WPNav::update_spline()
{
// exit immediately if this is not a spline segment
if (_flags.segment_type != SEGMENT_SPLINE) {
return false;
}
bool ret = true;
// calculate dt
float dt = _pos_control.time_since_last_xy_update();
if (dt >= 0.2f) {
dt = 0.0f;
}
// advance the target if necessary
if (!advance_spline_target_along_track(dt)) {
// To-Do: handle failure to advance along track (due to missing terrain data)
ret = false;
}
// freeze feedforwards during known discontinuities
if (_flags.new_wp_destination) {
_flags.new_wp_destination = false;
_pos_control.freeze_ff_z();
}
// run horizontal position controller
_pos_control.update_xy_controller(1.0f);
_wp_last_update = AP_HAL::millis();
return ret;
}
// auto_spline_run - runs the auto spline controller
// called by auto_run at 100hz or more
void Copter::ModeAuto::spline_run()
{
// if not auto armed or motor interlock not enabled set throttle to zero and exit immediately
if (!motors->armed() || !ap.auto_armed || !motors->get_interlock()) {
// To-Do: reset waypoint origin to current location because copter is probably on the ground so we don't want it lurching left or right on take-off
// (of course it would be better if people just used take-off)
zero_throttle_and_relax_ac();
// clear i term when we're taking off
set_throttle_takeoff();
return;
}
// process pilot's yaw input
float target_yaw_rate = 0;
if (!copter.failsafe.radio) {
// get pilot's desired yaw rat
target_yaw_rate = get_pilot_desired_yaw_rate(channel_yaw->get_control_in());
if (!is_zero(target_yaw_rate)) {
auto_yaw.set_mode(AUTO_YAW_HOLD);
}
}
// set motors to full range
motors->set_desired_spool_state(AP_Motors::DESIRED_THROTTLE_UNLIMITED);
// run waypoint controller
wp_nav->update_spline();
// call z-axis position controller (wpnav should have already updated it's alt target)
pos_control->update_z_controller();
// call attitude controller
if (auto_yaw.mode() == AUTO_YAW_HOLD) {
// roll & pitch from waypoint controller, yaw rate from pilot
attitude_control->input_euler_angle_roll_pitch_euler_rate_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), target_yaw_rate);
} else {
// roll, pitch from waypoint controller, yaw heading from auto_heading()
attitude_control->input_euler_angle_roll_pitch_yaw(wp_nav->get_roll(), wp_nav->get_pitch(), auto_yaw.yaw(), true);
}
}