Simon Shi的小站

人工智能,机器学习, 强化学习,大模型,自动驾驶

0%

ROS2

install ROS2

1
2
3
4
5
6
7
sudo apt update && sudo apt install curl gnupg2 lsb-release
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
sudo apt update
sudo apt install ros-foxy-desktop
echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
source ~/.bashrc
1
2
3
# sudo apt install colcon-common-extensions

sudo apt install python3-colcon-common-extensions

Build

1
2
3
4
5
6
colcon build --packages-select mbot_description
colcon build --packages-select mbot_bringup
colcon build --packages-select sllidar_ros2

# ros2 launch mbot_description gazebo.launch
source ~/ros2_ws/install/setup.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ubuntu@ubuntu:~/ros2_ws$ colcon build --packages-select sllidar_ros2
[3.464s] WARNING:colcon.colcon_cmake.package_identification.cmake:Ignoring 'src/CMakeLists.txt' since it seems to be a toplevel CMake file generated by 'catkin_make'
Starting >>> sllidar_ros2
Finished <<< sllidar_ros2 [2.22s]

Summary: 1 package finished [4.26s]
ubuntu@ubuntu:~/ros2_ws$
ubuntu@ubuntu:~/ros2_ws$ colcon build --packages-select mbot_description
[3.425s] WARNING:colcon.colcon_cmake.package_identification.cmake:Ignoring 'src/CMakeLists.txt' since it seems to be a toplevel CMake file generated by 'catkin_make'
Starting >>> mbot_description
Finished <<< mbot_description [10.8s]

Summary: 1 package finished [12.8s]
ubuntu@ubuntu:~/ros2_ws$
1
2
3
4
5
6
sudo apt install software-properties-common
sudo add-apt-repository universe
# "deb http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main"
sudo add-apt-repository "deb http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main"
sudo apt install curl
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

BUG 思路 (foxy)

1、报错日志,百度+通义千问,给出了解决方案(但是代码实现跟本地不一致,硬套失败)

2、AI给的解决方案,都是成员函数的bind,没转换过来思路(总以为是参数的原因)

  • cmdCallback3函数不是一个类成员函数,因此不能使用std::bindthis结合的方式绑定到订阅器上。如果cmdCallback3确实是类的成员函数,那么它应该被声明为该类的一个成员,并且需要使用正确的绑定方式。
  • 实际上,不需要bind,就是原本的参数方式!!!!!!

3、阅读源码,(确认了calback3的参数与回调函数的模板一致)
/opt/ros/foxy/include/rclcpp/rclcpp.hpp

1
2
3
4
* - Subscription
* - rclcpp::Node::create_subscription()
* - rclcpp::Subscription
* - rclcpp/subscription.hpp

查到实现subscribe的代码:

/opt/ros/foxy/include/rclcpp/subscription.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// Default constructor.
/**
* The constructor for a subscription is almost never called directly.
* Instead, subscriptions should be instantiated through the function
* rclcpp::create_subscription().
*/
///
Subscription(
rclcpp::node_interfaces::NodeBaseInterface * node_base,
const rosidl_message_type_support_t & type_support_handle,
const std::string & topic_name,
const rclcpp::QoS & qos,
AnySubscriptionCallback<CallbackMessageT, AllocatorT> callback,
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options,
typename MessageMemoryStrategyT::SharedPtr message_memory_strategy,
SubscriptionTopicStatisticsSharedPtr subscription_topic_statistics = nullptr)

确认回调入口:AnySubscriptionCallback

AnySubscriptionCallback<CallbackMessageT, AllocatorT> any_callback_;

    vim /opt/ros/foxy/include/rclcpp/any_subscription_callback.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
AnySubscriptionCallback


using SharedPtrCallback = std::function<void (const std::shared_ptr<MessageT>)>;
using SharedPtrWithInfoCallback =
std::function<void (const std::shared_ptr<MessageT>, const rclcpp::MessageInfo &)>;
// const 单个参数的,就这个了!!!!!!!!
using ConstSharedPtrCallback = std::function<void (const std::shared_ptr<const MessageT>)>;
using ConstSharedPtrWithInfoCallback =
std::function<void (const std::shared_ptr<const MessageT>, const rclcpp::MessageInfo &)>;
using UniquePtrCallback = std::function<void (MessageUniquePtr)>;
using UniquePtrWithInfoCallback =
std::function<void (MessageUniquePtr, const rclcpp::MessageInfo &)>;

4、Finally

Build PASS

RUN

1
$ros2 launch mbot_bringup mbot_with_lidar_launch.py