Skip to content

Commit 8553806

Browse files
committed
Port controller and state_manager to ROS2, build passing
1 parent 23f0786 commit 8553806

File tree

12 files changed

+571
-544
lines changed

12 files changed

+571
-544
lines changed

lqg_controller/CMakeLists.txt

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,52 @@
1-
cmake_minimum_required(VERSION 2.8.3)
2-
#cmake_minimum_required(VERSION 3.1.0)
1+
cmake_minimum_required(VERSION 3.8)
32
project(lqg_control)
4-
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O3 -ffast-math -Wall" )
3+
add_compile_options( -std=c++14 -O3 -ffast-math -Wall )
54

65

7-
find_package(catkin REQUIRED COMPONENTS
8-
message_generation
9-
roscpp
10-
freyja_msgs
11-
cmake_modules
12-
)
6+
find_package(ament_cmake REQUIRED)
137
#pkg_search_module( Eigen3 REQUIRED eigen3 )
14-
find_package( Eigen REQUIRED )
8+
#find_package( Eigen REQUIRED )
9+
10+
11+
find_package(rclcpp REQUIRED)
12+
find_package(std_msgs REQUIRED)
13+
find_package(std_srvs REQUIRED)
14+
find_package(freyja_msgs REQUIRED)
15+
find_package(geometry_msgs REQUIRED)
16+
find_package(Eigen3 REQUIRED)
1517
include_directories(${EIGEN3_INCLUDE_DIR})
1618
add_definitions( ${EIGEN3_DEFINITIONS} )
1719

18-
if( NOT EIGEN3_FOUND )
19-
# resort to cmake_modules
20-
find_package( cmake_modules REQUIRED )
21-
find_package(Eigen REQUIRED)
22-
set( EIGEN3_INCLUDE_DIRS ${EIGEN_INCLUDE_DIRS} )
23-
set( EIGEN3_LIBRARIES ${EIGEN_LIBRARIES} )
24-
else()
25-
set( EIGEN3_INCLUDE_DIRS ${EIGEN3_INCLUDE_DIR} )
26-
endif()
27-
28-
29-
catkin_package(
30-
INCLUDE_DIRS include
31-
# LIBRARIES lqg_control
32-
CATKIN_DEPENDS Eigen3 message_generation roscpp
33-
DEPENDS Eigen
34-
)
35-
3620
###########
3721
## Build ##
3822
###########
3923

4024
include_directories(include)
4125
include_directories(
42-
${catkin_INCLUDE_DIRS}
26+
# ${catkin_INCLUDE_DIRS}
4327
${Eigen_INCLUDE_DIRS}
4428
)
4529

4630
# lqr with bias estimator
4731
add_executable(lqg_control_node src/lqr_control_bias.cpp src/bias_estimator.cpp)
48-
add_dependencies(lqg_control_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
49-
target_link_libraries(lqg_control_node
50-
${catkin_LIBRARIES}
51-
)
32+
target_include_directories( lqg_control_node PUBLIC
33+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
34+
$<INSTALL_INTERFACE:include>
35+
)
36+
ament_target_dependencies( lqg_control_node
37+
rclcpp
38+
std_msgs
39+
freyja_msgs
40+
geometry_msgs
41+
std_srvs
42+
)
43+
44+
install(TARGETS
45+
lqg_control_node
46+
DESTINATION lib/${PROJECT_NAME})
5247

5348
# lqr with only velocity control
54-
add_executable(lqr_vel_ctrl_node src/lqr_vel_control.cpp)
55-
add_dependencies(lqr_vel_ctrl_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
56-
target_link_libraries(lqr_vel_ctrl_node
57-
${catkin_LIBRARIES}
58-
)
49+
#add_executable(lqr_vel_ctrl_node src/lqr_vel_control.cpp)
50+
#add_dependencies(lqr_vel_ctrl_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
51+
52+
ament_package()

lqg_controller/include/bias_estimator.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,28 @@
1212
#include <mutex>
1313
#include <thread>
1414
#include <chrono>
15+
#include <memory>
1516

16-
#include <ros/ros.h>
17+
#include "rclcpp/rclcpp.hpp"
1718
#include <eigen3/Eigen/Dense>
1819

19-
#include <freyja_msgs/CurrentStateBiasEst.h>
20+
#include "freyja_msgs/msg/current_state_bias_est.hpp"
2021

2122
typedef std::chrono::microseconds uSeconds;
2223
typedef std::chrono::high_resolution_clock ClockTime;
2324
typedef std::chrono::time_point<ClockTime> ClockTimePoint;
2425

26+
typedef freyja_msgs::msg::CurrentStateBiasEst EstimatedState;
27+
2528
const int nStates = 9;
2629
const int nCtrl = 3;
2730
const int nMeas = 6;
2831

29-
class BiasEstimator
32+
class BiasEstimator : public rclcpp::Node
3033
{
3134
friend class LQRController;
3235

33-
ros::NodeHandle nh_, priv_nh_;
36+
//ros::NodeHandle nh_, priv_nh_;
3437
Eigen::Matrix<double, nStates, nStates> sys_A_, sys_A_t_;
3538
Eigen::Matrix<double, nStates, nCtrl> sys_B_;
3639

@@ -75,12 +78,12 @@ class BiasEstimator
7578
std::chrono::duration<double> tc_interval_;
7679

7780
/* final ros data published */
78-
freyja_msgs::CurrentStateBiasEst state_msg_;
81+
EstimatedState state_msg_;
7982
public:
8083
BiasEstimator();
8184
~BiasEstimator();
8285

83-
ros::Publisher bias_pub_;
86+
rclcpp::Publisher<EstimatedState>::SharedPtr bias_pub_;
8487

8588
/* State propagation - runs at a fixed rate */
8689
__attribute__((optimize("unroll-loops")))

lqg_controller/include/lqr_control_bias.h

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,38 @@
1313
-- aj / 17th Nov, 2017.
1414
*/
1515
#include <mutex>
16+
#include <memory>
17+
#include <chrono>
1618

17-
#include <ros/ros.h>
18-
#include <geometry_msgs/TransformStamped.h>
19-
#include <std_srvs/SetBool.h>
20-
#include <std_msgs/Float32.h>
19+
#include "rclcpp/rclcpp.hpp"
20+
//#include "geometry_msgs/msg/TransformStamped.hpp"
21+
#include "std_srvs/srv/set_bool.hpp"
22+
#include "std_msgs/msg/float32.hpp"
2123

22-
#include <freyja_msgs/CurrentState.h>
23-
#include <freyja_msgs/CtrlCommand.h>
24-
#include <freyja_msgs/ControllerDebug.h>
25-
#include <freyja_msgs/ReferenceState.h>
24+
#include "freyja_msgs/msg/current_state.hpp"
25+
#include "freyja_msgs/msg/ctrl_command.hpp"
26+
#include <freyja_msgs/msg/controller_debug.hpp>
27+
#include <freyja_msgs/msg/reference_state.hpp>
2628

2729
#include <eigen3/Eigen/Dense>
2830

2931
#include "bias_estimator.h"
3032

31-
typedef freyja_msgs::ReferenceState TrajRef;
32-
typedef std_srvs::SetBool::Request BoolServReq;
33-
typedef std_srvs::SetBool::Response BoolServRsp;
33+
typedef freyja_msgs::msg::ReferenceState TrajRef;
34+
typedef freyja_msgs::msg::CurrentState CurrentState;
35+
typedef std_srvs::srv::SetBool BoolServ;
36+
typedef freyja_msgs::msg::CtrlCommand RPYT_Command;
37+
typedef freyja_msgs::msg::ControllerDebug CTRL_Debug;
38+
3439
typedef Eigen::Matrix<double, 6, 1> PosVelNED;
40+
typedef Eigen::Matrix<double, 4, 1> Vector4d;
41+
42+
using std::placeholders::_1;
43+
using std::placeholders::_2;
3544

36-
class LQRController
45+
class LQRController : public rclcpp::Node
3746
{
38-
ros::NodeHandle nh_, priv_nh_;
39-
freyja_msgs::CurrentState state_vector_;
47+
CurrentState state_vector_;
4048
Eigen::Matrix<double, 7, 1> reduced_state_;
4149

4250
/* Reference state vector */
@@ -61,7 +69,7 @@ class LQRController
6169
Eigen::Matrix<double, 3, 3> rot_yaw_;
6270

6371
float STATEFB_MISSING_INTRV_;
64-
ros::Time last_state_update_t_;
72+
rclcpp::Time last_state_update_t_;
6573
bool have_state_update_;
6674
bool have_reference_update_;
6775

@@ -82,25 +90,26 @@ class LQRController
8290
LQRController( BiasEstimator & );
8391
void initLqrSystem();
8492

85-
ros::Subscriber state_sub_;
86-
void stateCallback( const freyja_msgs::CurrentState::ConstPtr & ) __attribute__((hot));
93+
rclcpp::Subscription<CurrentState>::SharedPtr state_sub_;
94+
void stateCallback( const CurrentState::ConstSharedPtr ) __attribute__((hot));
8795

88-
ros::ServiceServer bias_enable_serv_;
89-
bool biasEnableServer( BoolServReq&, BoolServRsp& );
96+
rclcpp::Service<BoolServ>::SharedPtr bias_enable_serv_;
97+
void biasEnableServer( const BoolServ::Request::SharedPtr,
98+
const BoolServ::Response::SharedPtr );
9099

91-
ros::Publisher atti_cmd_pub_;
92-
ros::Publisher controller_debug_pub_;
100+
rclcpp::Publisher<RPYT_Command>::SharedPtr atti_cmd_pub_;
101+
rclcpp::Publisher<CTRL_Debug>::SharedPtr controller_debug_pub_;
93102

94-
ros::Timer controller_timer_;
95-
void computeFeedback( const ros::TimerEvent & ) __attribute__((optimize("unroll-loops")));
103+
rclcpp::TimerBase::SharedPtr controller_timer_;
104+
void computeFeedback( ) __attribute__((optimize("unroll-loops")));
96105

97-
ros::Subscriber reference_sub_;
98-
void trajectoryReferenceCallback( const TrajRef::ConstPtr & );
106+
rclcpp::Subscription<TrajRef>::SharedPtr reference_sub_;
107+
void trajectoryReferenceCallback( const TrajRef::ConstSharedPtr );
99108

100109
/* helper function to calculate yaw error */
101110
static constexpr inline double calcYawError( const double&, const double& ) __attribute__((always_inline));
102111

103112
/* estimate actual mass in flight */
104-
void estimateMass( const Eigen::Matrix<double, 4, 1> &, ros::Time & );
105-
ros::Publisher est_mass_pub_;
113+
void estimateMass( const Eigen::Matrix<double, 4, 1> &, rclcpp::Time & );
114+
rclcpp::Publisher<std_msgs::msg::Float32>::SharedPtr est_mass_pub_;
106115
};

lqg_controller/package.xml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
<?xml version="1.0"?>
2-
<package>
2+
<package format="3">
33
<name>lqg_control</name>
4-
<version>0.1.0</version>
4+
<version>2.0.0</version>
55
<description>The lqg_control package</description>
66

77
<maintainer email="ashankar@cse.unl.edu">aj</maintainer>
88
<license>GPLv3</license>
99

10-
<buildtool_depend>catkin</buildtool_depend>
11-
<build_depend>message_generation</build_depend>
12-
<build_depend>roscpp</build_depend>
13-
<build_depend>freyja_msgs</build_depend>
14-
<build_depend>cmake_modules</build_depend>
10+
<buildtool_depend>ament_cmake</buildtool_depend>
11+
12+
<depend>rclcpp</depend>
13+
<depend>std_msgs</depend>
14+
<depend>std_srvs</depend>
15+
<depend>geometry_msgs</depend>
16+
<depend>freyja_msgs</depend>
17+
1518
<build_depend>Eigen3</build_depend>
1619

17-
<run_depend>roscpp</run_depend>
18-
<run_depend>message_runtime</run_depend>
19-
<run_depend>message_generation</run_depend>
20-
<run_depend>Eigen3</run_depend>
20+
<export>
21+
<build_type>ament_cmake</build_type>
22+
</export>
2123

2224
</package>

lqg_controller/src/bias_estimator.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
#define ZERO3x3 Eigen::MatrixXd::Zero(3,3)
1616
#define IDEN3x3 Eigen::MatrixXd::Identity(3,3)
1717

18-
BiasEstimator::BiasEstimator() : nh_(), priv_nh_("~")
18+
BiasEstimator::BiasEstimator() : Node( ROS_NODE_NAME )
1919
{
20+
21+
declare_parameter( "estimator_rate", int(10) );
22+
23+
2024
/* Used for debug and such */
21-
std::string output_topic;
22-
priv_nh_.param( "estimator_debug_topic", output_topic,
23-
std::string("/bias_estimates") );
24-
bias_pub_ = nh_.advertise <freyja_msgs::CurrentStateBiasEst>
25-
( output_topic, 1, true );
25+
bias_pub_ = create_publisher <EstimatedState>
26+
( "/bias_estimates", 1 );
2627

2728
/* Parameters for thread */
28-
int estimator_rate_default = 10;
29-
priv_nh_.param( "estimator_rate", estimator_rate_, estimator_rate_default );
29+
get_parameter( "estimator_rate", estimator_rate_ );
3030
estimator_period_us_ = std::round( (1.0/estimator_rate_)*1000*1000 );
3131

3232
/* Time constant factor */
@@ -110,7 +110,7 @@ void BiasEstimator::state_propagation( )
110110
input_shaping << 0.95, 0.95, 0.95;
111111
Eigen::Matrix<double, 3, 1> BIAS_LIM_ABS; // clip limits (acceleration)
112112
BIAS_LIM_ABS << 2.0, 2.0, 3.0;
113-
while( ros::ok() )
113+
while( rclcpp::ok() )
114114
{
115115
/* Mark start(ts) and end(te) times for precise timing */
116116
ts = std::chrono::high_resolution_clock::now();
@@ -154,7 +154,7 @@ void BiasEstimator::state_propagation( )
154154
spmtx.unlock();
155155

156156
// publish
157-
state_msg_.header.stamp = ros::Time::now();
157+
state_msg_.header.stamp = now();
158158

159159
last_prop_t_ = te = std::chrono::high_resolution_clock::now();
160160
int dt = std::chrono::duration_cast<uSeconds>(te-ts).count();
@@ -164,7 +164,7 @@ void BiasEstimator::state_propagation( )
164164
state_msg_.state_vector[10] = delta_t;
165165
//state_msg_.state_vector[10] = state_cov_P_.diagonal().norm();
166166
state_msg_.state_vector[11] = ctrl_input_u_.norm();
167-
bias_pub_.publish( state_msg_ );
167+
bias_pub_ -> publish( state_msg_ );
168168

169169
std::this_thread::sleep_for( uSeconds(estimator_period_us_ - dt) );
170170
}

0 commit comments

Comments
 (0)