Skip to content

Commit 2b564eb

Browse files
committed
Fix lowpass & refactor filter structure
- Fixed second order lowpass - Use the filtered signal for PID calculations - Add a derivative filter
1 parent 0601627 commit 2b564eb

File tree

11 files changed

+436
-59
lines changed

11 files changed

+436
-59
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ add_library(${PROJECT_NAME}
6565
src/${PROJECT_NAME}_local_planner.cpp
6666
src/controller.cpp
6767
src/calculations.cpp
68-
src/visualization.cpp
68+
src/details/derivative.cpp
6969
src/details/second_order_lowpass.cpp
70+
src/visualization.cpp
7071
)
7172
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS} ${PROJECT_NAME}_gencfg)
7273
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES})
@@ -109,8 +110,10 @@ install(
109110
if(CATKIN_ENABLE_TESTING)
110111
add_rostest(test/test_path_tracking_pid.test ARGS rviz:=false reconfigure:=false)
111112
catkin_add_gtest(unittests
113+
test/unittests/second_order_lowpass.cpp
112114
test/unittests/test_main.cpp
113115
test/unittests/test_fifo_array.cpp
114-
test/unittests/test_calculations.cpp)
116+
test/unittests/test_calculations.cpp
117+
)
115118
target_link_libraries(unittests ${catkin_LIBRARIES} ${PROJECT_NAME})
116119
endif()

cfg/Pid.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
PACKAGE = "path_tracking_pid"
33

44
from dynamic_reconfigure.parameter_generator_catkin import ParameterGenerator, bool_t, double_t, int_t
5+
from math import sqrt
56

67
gen = ParameterGenerator()
78

@@ -33,6 +34,9 @@ gen.add("Kp_ang", double_t, 0, "Kp Angular", 1, 0, 10)
3334
gen.add("Ki_ang", double_t, 0, "Ki Angular", 0, 0, 2)
3435
gen.add("Kd_ang", double_t, 0, "Kd Angular", 0.3, 0, 10)
3536

37+
gen.add("lowpass_cutoff", double_t, 0, "Lowpass cutoff (Hz)", 20, 0, 1000)
38+
gen.add("lowpass_damping", double_t, 0, "Lowpass damping", sqrt(2), 0, 10)
39+
3640
gen.add("feedback_lat", bool_t, 0, "Enable lateral feedback?", True)
3741
gen.add("feedback_ang", bool_t, 0, "Enable angular feedback?", False)
3842

include/path_tracking_pid/controller.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <array>
1111
#include <boost/noncopyable.hpp>
12+
#include <path_tracking_pid/details/derivative.hpp>
1213
#include <path_tracking_pid/details/fifo_array.hpp>
1314
#include <path_tracking_pid/details/second_order_lowpass.hpp>
1415
#include <vector>
@@ -40,9 +41,9 @@ struct ControllerState
4041
double tracking_error_ang = 0.0;
4142
// Errors with little history
4243
details::SecondOrderLowpass error_lat;
43-
details::SecondOrderLowpass error_deriv_lat;
44+
details::Derivative error_deriv_lat;
4445
details::SecondOrderLowpass error_ang;
45-
details::SecondOrderLowpass error_deriv_ang;
46+
details::Derivative error_deriv_ang;
4647
};
4748

4849
class Controller : private boost::noncopyable
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <path_tracking_pid/details/fifo_array.hpp>
4+
5+
namespace path_tracking_pid::details
6+
{
7+
/**
8+
* @brief discrete time derivative filter
9+
*/
10+
class Derivative
11+
{
12+
public:
13+
/**
14+
* @brief Construct a Derivative instance
15+
*/
16+
Derivative();
17+
18+
/**
19+
* @brief filter one sample of a signal
20+
* @param u signal to be filtered
21+
* @param step_size
22+
* @return derivative of the signal
23+
*/
24+
double filter(double u, double step_size);
25+
26+
void reset();
27+
28+
private:
29+
FifoArray<double, 2> u_ = {};
30+
};
31+
32+
} // namespace path_tracking_pid::details

include/path_tracking_pid/details/fifo_array.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class FifoArray
2424
// Read-only access to the element at the given index.
2525
constexpr const value_type & operator[](std::size_t index) const { return data_[index]; }
2626

27+
// Read-write access to the element at the given index.
28+
value_type & operator[](std::size_t index) { return data_[index]; }
29+
2730
// Read-only access to the element at the given index (with compile-time range check).
2831
template <std::size_t index>
2932
constexpr const value_type & at() const

include/path_tracking_pid/details/second_order_lowpass.hpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,41 @@
44

55
namespace path_tracking_pid::details
66
{
7-
// Error tracker for the last 3 error and filtered error values.
7+
/**
8+
* @brief discrete time second order lowpass filter
9+
*/
810
class SecondOrderLowpass
911
{
1012
public:
11-
// Pushes the given value to the errors FIFO buffer. A corresponding filtered error value is calculated and pushed
12-
// to the filtered errors FIFO buffer.
13-
void push(double value);
13+
/**
14+
* @brief Construct a SecondOrderLowpass instance with NaNs
15+
*/
16+
SecondOrderLowpass();
1417

15-
// Resets both errors and filtered errors FIFO buffers.
16-
void reset();
18+
/**
19+
* @brief Construct a SecondOrderLowpass instance
20+
* @param fden frequency in Hz
21+
* @param bden frequency in Hz
22+
*/
23+
SecondOrderLowpass(double fden, double bden);
24+
25+
void configure(double fden, double bden);
1726

18-
// Read-only access to the errors FIFO buffer.
19-
const FifoArray<double, 3> & errors() const;
27+
/**
28+
* @brief filter one sample of a signal
29+
* @param u signal to be filtered
30+
* @param step_size
31+
* @return lowpass-filtered signal
32+
*/
33+
double filter(double u, double step_size);
2034

21-
// Read-only access to the filtered errors FIFO buffer.
22-
const FifoArray<double, 3> & filtered_errors() const;
35+
void reset();
2336

2437
private:
25-
FifoArray<double, 3> errors_;
26-
FifoArray<double, 3> filtered_errors_;
38+
FifoArray<double, 3> u_ = {};
39+
FifoArray<double, 3> y_ = {};
40+
double fden_;
41+
double bden_;
2742
};
2843

2944
} // namespace path_tracking_pid::details

0 commit comments

Comments
 (0)