├── CMakeLists.txt
├── LICENSE
├── README.md
├── asset
└── demo_video.gif
├── include
└── nav2_mpc_controller
│ ├── mpc_controller.hpp
│ └── mpc_core.hpp
├── nav2_mpc_controller.xml
├── package.xml
└── src
├── mpc_controller.cpp
└── mpc_core.cpp
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.5)
2 | project(nav2_mpc_controller)
3 |
4 | # Nav2_Mpc_Controller
5 | find_package(ament_cmake REQUIRED)
6 | find_package(nav2_common REQUIRED)
7 | find_package(nav2_core REQUIRED)
8 | find_package(nav2_costmap_2d REQUIRED)
9 | find_package(nav2_util REQUIRED)
10 | find_package(rclcpp REQUIRED)
11 | find_package(geometry_msgs REQUIRED)
12 | find_package(nav_msgs REQUIRED)
13 | find_package(pluginlib REQUIRED)
14 | find_package(tf2 REQUIRED)
15 |
16 | # MPC_CORE
17 | find_package(Eigen3 REQUIRED)
18 |
19 | nav2_package()
20 | set(CMAKE_CXX_STANDARD 17)
21 |
22 | include_directories(
23 | include
24 | )
25 |
26 | set(dependencies
27 | rclcpp
28 | geometry_msgs
29 | nav2_costmap_2d
30 | pluginlib
31 | nav_msgs
32 | nav2_util
33 | nav2_core
34 | tf2
35 | Eigen3
36 | )
37 |
38 | # MPC_CORE
39 | add_library(mpc_core src/mpc_core.cpp)
40 | target_link_libraries(mpc_core ipopt)
41 | include_directories(${Eigen3_INCLUDE_DIRS})
42 |
43 | # Nav2_Mpc_controller
44 | set(library_name nav2_mpc_controller)
45 | add_library(${library_name} SHARED
46 | src/mpc_controller.cpp)
47 | target_link_libraries(${library_name} mpc_core)
48 |
49 | # prevent pluginlib from using boost
50 | target_compile_definitions(${library_name} PUBLIC "PLUGINLIB__DISABLE_BOOST_FUNCTIONS")
51 |
52 | ament_target_dependencies(${library_name}
53 | ${dependencies}
54 | )
55 |
56 | install(TARGETS ${library_name}
57 | ARCHIVE DESTINATION lib
58 | LIBRARY DESTINATION lib
59 | RUNTIME DESTINATION bin
60 | )
61 |
62 | install(DIRECTORY include/
63 | DESTINATION include/
64 | )
65 |
66 | # if(BUILD_TESTING)
67 | # find_package(ament_lint_auto REQUIRED)
68 | # # the following line skips the linter which checks for copyrights
69 | # set(ament_cmake_copyright_FOUND TRUE)
70 | # ament_lint_auto_find_test_dependencies()
71 | # add_subdirectory(test)
72 | # endif()
73 |
74 | ament_export_include_directories(include)
75 | ament_export_libraries(${library_name})
76 | ament_export_dependencies(${dependencies})
77 |
78 | pluginlib_export_plugin_description_file(nav2_core nav2_mpc_controller.xml)
79 |
80 | ament_package()
81 |
82 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > This is not official package for [**navigation2**](https://github.com/ros-planning/navigation2)
2 |
3 | # Nav2_MPC_Controller Plugin
4 |
5 | This is ros2 package and plugin for controller interface of Navigation2.You can use this plugin with controller server of navigation2 without any special implementation.
6 |
7 | 
8 |
9 | ### Installation
10 |
11 | Follow the official [documentation](https://navigation.ros.org/build_instructions/index.html#install) below to install.
12 | Requires Navigation2 and ROS2 (Default Foxy) installation.
13 |
14 | ### How to use
15 |
16 | This is an implementation of the plugin defined by Nav2.
17 | `bringup` package of Navigation2 package is a basic execution example.
18 | There are two main settings to run this plug-in.
19 |
20 | * Dependency Settings on `package.xml`
21 | Register this repository(pakcage).
22 | ```
23 | "in package.xml"
24 | ...
25 | nav2_mpc_controller
26 | ...
27 | ```
28 |
29 | * Loading Params on `.py` from launch files
30 | Load params .yaml file inclued this plugin configuration.
31 |
32 | ```
33 | "in tb3_simulation_with_mpc_launch.py"
34 | ...
35 | declare_params_file_cmd = DeclareLaunchArgument(
36 | 'params_file',
37 | default_value=os.path.join(bringup_dir, 'params', 'nav2_mpc_params.yaml'),
38 | description='Full path to the ROS2 parameters file to use for all launched nodes')
39 | ...
40 | ```
41 |
42 | Need to create new param file like `nav2_mpc_params.yaml`.
43 | It should include the configuration for this plugin like below.
44 |
45 | ```
46 | ...
47 | FollowPath:
48 | plugin: "nav2_mpc_controller::MPCController"
49 | ...
50 | ```
51 |
--------------------------------------------------------------------------------
/asset/demo_video.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harderthan/nav2_mpc_controller/825990a3e5c008bdde4329cbe465c94ccb19f556/asset/demo_video.gif
--------------------------------------------------------------------------------
/include/nav2_mpc_controller/mpc_controller.hpp:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2021 Harderthan
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | #ifndef NAV2_MPC_CONTROLLER__MPC_CONTROLLER_HPP_
16 | #define NAV2_MPC_CONTROLLER__MPC_CONTROLLER_HPP_
17 |
18 | #include
19 | #include
20 | #include
21 | #include
22 |
23 | #include "geometry_msgs/msg/pose2_d.hpp"
24 | #include "nav2_core/controller.hpp"
25 | #include "nav2_util/odometry_utils.hpp"
26 | #include "pluginlib/class_list_macros.hpp"
27 | #include "pluginlib/class_loader.hpp"
28 | #include "rclcpp/rclcpp.hpp"
29 |
30 | #include "mpc_core.hpp"
31 | #include
32 | #include
33 |
34 | namespace nav2_mpc_controller {
35 | /**
36 | * @class nav2_mpc_controller::MPCController
37 | * @brief mpc controller plugin
38 | */
39 | class MPCController : public nav2_core::Controller {
40 | public:
41 | /**
42 | * @brief Constructor for nav2_mpc_controller::MPCController
43 | */
44 | MPCController() = default;
45 |
46 | /**
47 | * @brief Destrructor for nav2_mpc_controller::MPCController
48 | */
49 | ~MPCController() override = default;
50 |
51 | /**
52 | * @brief Configure controller state machine
53 | * @param parent WeakPtr to node
54 | * @param name Name of plugin
55 | * @param tf TF buffer
56 | * @param costmap_ros Costmap2DROS object of environment
57 | */
58 | void configure(const rclcpp_lifecycle::LifecycleNode::SharedPtr &parent,
59 | std::string name, const std::shared_ptr &tf,
60 | const std::shared_ptr
61 | &costmap_ros) override;
62 |
63 | /**
64 | * @brief Cleanup controller state machine
65 | */
66 | void cleanup() override;
67 |
68 | /**
69 | * @brief Activate controller state machine
70 | */
71 | void activate() override;
72 |
73 | /**
74 | * @brief Deactivate controller state machine
75 | */
76 | void deactivate() override;
77 |
78 | /**
79 | * @brief Compute the best command given the current pose and velocity, with
80 | * possible debug information
81 | *
82 | * Same as above computeVelocityCommands, but with debug results.
83 | * If the results pointer is not null, additional information about the twists
84 | * evaluated will be in results after the call.
85 | *
86 | * @param pose Current robot pose
87 | * @param velocity Current robot velocity
88 | * @param results Output param, if not NULL, will be filled in with full
89 | * evaluation results
90 | * @return Best command
91 | */
92 | geometry_msgs::msg::TwistStamped
93 | computeVelocityCommands(const geometry_msgs::msg::PoseStamped &pose,
94 | const geometry_msgs::msg::Twist &velocity) override;
95 |
96 | /**
97 | * @brief nav2_core setPlan - Sets the global plan
98 | * @param path The global plan
99 | */
100 | void setPlan(const nav_msgs::msg::Path &path) override;
101 |
102 | protected:
103 | Eigen::VectorXd& impCoefficients(const geometry_msgs::msg::PoseStamped &pose, const nav_msgs::msg::Path& global_plan);
104 | double impThetaError(double theta, const Eigen::VectorXd& coeffs,
105 | int sample_size, int sample_ratio);
106 | void convertCoordinateSystem(Eigen::VectorXd& x_veh, Eigen::VectorXd y_veh);
107 | double polyeval(Eigen::VectorXd coeffs, double x);
108 | Eigen::VectorXd polyfit(Eigen::VectorXd xvals, Eigen::VectorXd yvals,
109 | int order);
110 |
111 | std::shared_ptr tf_;
112 | std::string plugin_name_;
113 | std::shared_ptr costmap_ros_;
114 | nav2_costmap_2d::Costmap2D *costmap_;
115 | rclcpp::Logger logger_{rclcpp::get_logger("MPCController")};
116 |
117 | MPCCore _mpc;
118 | map _mpc_params;
119 | double mpc_steps_, ref_cte_, ref_etheta_, ref_vel_, w_cte_, w_etheta_, w_vel_,
120 | w_angvel_, w_accel_, w_angvel_d_, w_accel_d_, max_angvel_, max_throttle_,
121 | bound_value_;
122 |
123 | double dt_, w_, throttle_, speed_, max_speed_;
124 | double pathLength_, goalRadius_, waypointsDist_;
125 | int controller_freq_, downSampling_, thread_numbers_;
126 | bool goal_received_, goal_reached_, path_computed_, pub_twist_flag_,
127 | debug_info_, delay_mode_;
128 |
129 | tf2::Duration transform_tolerance_;
130 | double control_duration_;
131 | bool use_cost_regulated_linear_velocity_scaling_;
132 | double inflation_cost_scaling_factor_;
133 |
134 | nav_msgs::msg::Path global_plan_;
135 | std::shared_ptr>
136 | global_path_pub_;
137 | std::shared_ptr<
138 | rclcpp_lifecycle::LifecyclePublisher>
139 | carrot_pub_;
140 | std::shared_ptr>
141 | carrot_arc_pub_;
142 | };
143 | } // namespace nav2_mpc_controller
144 |
145 | #endif // NAV2_MPC_CONTROLLER__MPC_CONTROLLER_HPP_
146 |
--------------------------------------------------------------------------------
/include/nav2_mpc_controller/mpc_core.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | # Copyright 2021 Harderthan
3 | # Original Code is from https://github.com/Geonhee-LEE/mpc_ros
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | */
17 |
18 | #ifndef MPC_H
19 | #define MPC_H
20 |
21 | #include
22 | #include