PX4 Firmware
PX4 Autopilot Software http://px4.io
test_velocity_smoothing.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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  * Test code for the Velocity Smoothing library
36  * Build and run using: make && ./test_velocity_smoothing
37  */
38 
39 #include "VelocitySmoothing.hpp"
40 #include <cstdio>
41 #include <matrix/matrix/math.hpp>
42 
43 int main(int argc, char *argv[])
44 {
45  VelocitySmoothing trajectory[3];
46 
47  float a0[3] = {0.f, 0.f, 0.f};
48  float v0[3] = {0.f, 0.f, 0.f};
49  float x0[3] = {0.f, 0.f, 0.f};
50 
51  float j_max = 55.2f;
52  float a_max = 6.f;
53  float v_max = 6.f;
54 
55  for (int i = 0; i < 3; i++) {
56  trajectory[i].setMaxJerk(j_max);
57  trajectory[i].setMaxAccel(a_max);
58  trajectory[i].setMaxVel(v_max);
59  trajectory[i].setCurrentAcceleration(a0[i]);
60  trajectory[i].setCurrentVelocity(v0[i]);
61  }
62 
63  const float dt = 0.01f;
64 
65  float velocity_setpoint[3] = {1.f, 0.f, -1.f};
66 
67  for (int i = 0; i < 3; i++) {
68  trajectory[i].updateDurations(velocity_setpoint[i]);
69  }
70 
71  float t123 = trajectory[0].getTotalTime();
72  int nb_steps = ceil(t123 / dt);
73  printf("Nb steps = %d\n", nb_steps);
74 
75  for (int i = 0; i < nb_steps; i++) {
76  for (int i = 0; i < 3; i++) {
77  trajectory[i].updateTraj(dt);
78  }
79 
80  for (int i = 0; i < 3; i++) {
81  trajectory[i].updateDurations(velocity_setpoint[i]);
82  }
83 
85 
86  for (int i = 0; i < 1; i++) {
87  printf("Traj[%d]\n", i);
88  printf("jerk = %.3f\taccel = %.3f\tvel = %.3f\tpos = %.3f\n",
89  trajectory[i].getCurrentJerk(),
90  trajectory[i].getCurrentAcceleration(),
91  trajectory[i].getCurrentVelocity(),
92  trajectory[i].getCurrentPosition());
93  printf("T1 = %.3f\tT2 = %.3f\tT3 = %.3f\n", trajectory[i].getT1(), trajectory[i].getT2(), trajectory[i].getT3());
94  printf("\n");
95  }
96  }
97 
98  return 0;
99 }
int main(int argc, char *argv[])
Test code for the Velocity Smoothing library Build and run using: make && .
float t123
Definition: LOSX.c:122
Dual< Scalar, N > ceil(const Dual< Scalar, N > &a)
Definition: Dual.hpp:203
void setMaxJerk(float max_jerk)
void setCurrentVelocity(const float vel)
TODO: document the algorithm |T1| T2 |T3| __| |____ __ Jerk |_| / \ Acceleration ___/ ___ ;" / / V...
void updateTraj(float dt, float time_stretch=1.f)
Generate the trajectory (acceleration, velocity and position) by integrating the current jerk...
float getTotalTime() const
void updateDurations(float vel_setpoint)
Compute T1, T2, T3 depending on the current state and velocity setpoint.
float dt
Definition: px4io.c:73
static void timeSynchronization(VelocitySmoothing *traj, int n_traj)
Synchronize several trajectories to have the same total time.
void setMaxVel(float max_vel)
void setMaxAccel(float max_accel)
void setCurrentAcceleration(const float accel)