PX4 Firmware
PX4 Autopilot Software http://px4.io
StraightLine.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file StraightLine.cpp
36  */
37 
38 #include "StraightLine.hpp"
39 #include <mathlib/mathlib.h>
40 #include <px4_platform_common/defines.h>
41 
42 using namespace matrix;
43 
44 void StraightLine::generateSetpoints(matrix::Vector3f &position_setpoint, matrix::Vector3f &velocity_setpoint)
45 {
46  if (isEndReached()) {
47  // Vehicle has reached target, lock position
48  position_setpoint = _end;
49  velocity_setpoint.setNaN();
50  return;
51  }
52 
53  Vector3f start_to_end = _end - _start;
54  float distance_start_to_end = start_to_end.norm();
55 
56  // capture progress as ratio between 0 and 1 of entire distance
57  Vector3f vehicle_to_end = _end - _position;
58  float distance_vehicle_to_end = vehicle_to_end.norm();
59  float distance_from_start = Vector3f(_start - _position).norm();
60  float progress = distance_from_start / (distance_from_start + distance_vehicle_to_end);
61 
62  float distance_from_boundary = 0.f;
63 
64  // calculate the distance to the closer boundary
65  if (progress < 0.5f) {
66  distance_from_boundary = (2 * progress) * distance_start_to_end;
67 
68  } else {
69  distance_from_boundary = (2 * (1 - progress)) * distance_start_to_end;
70  }
71 
72  // ramp velocity based on the distance to the boundary
73  float velocity = 0.5f + (distance_from_boundary / 4.f);
74  velocity = math::min(velocity, _speed);
75  velocity_setpoint = vehicle_to_end.unit_or_zero() * velocity;
76 
77  // check if we plan to go against the line direction which indicates we reached the goal
78  if (start_to_end * vehicle_to_end < 0) {
79  end_reached = true;
80  }
81 }
82 
84 {
85  if (PX4_ISFINITE(start.norm_squared()) && PX4_ISFINITE(end.norm_squared())) {
86  _start = start;
87  _end = end;
88  end_reached = false;
89  }
90 }
Type norm_squared() const
Definition: Vector.hpp:78
void setLineFromTo(const matrix::Vector3f &start, const matrix::Vector3f &end)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
constexpr _Tp min(_Tp a, _Tp b)
Definition: Limits.hpp:54
static int start()
Definition: dataman.cpp:1452
Vector3< float > Vector3f
Definition: Vector3.hpp:136
void setNaN()
Definition: Matrix.hpp:442
lib to return setpoints on a straight line
Type norm() const
Definition: Vector.hpp:73
Vector unit_or_zero(const Type eps=Type(1e-5)) const
Definition: Vector.hpp:95
void generateSetpoints(matrix::Vector3f &position_setpoint, matrix::Vector3f &velocity_setpoint)
Generate setpoints on a straight line according to parameters.