ros2_can_interface
is a ROS 2 package designed to facilitate seamless communication between ROS 2 topics and Controller Area Network (CAN) messages. It provides two primary nodes:
-
ROS2 to CAN Publisher (
ros2can
): Subscribes to specified ROS 2 topics, serializes the messages, segments them if necessary, and transmits them over the CAN bus. -
CAN to ROS2 Subscriber (
can2ros
): Listens for CAN messages, reassembles segmented messages, deserializes them, and publishes the data to corresponding ROS 2 topics.
This package is particularly useful in robotics and automotive applications where integrating ROS 2 systems with CAN-based hardware is required.
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Testing
- Troubleshooting
- Package Structure
- Dependencies
- Contributing
- License
- Acknowledgments
- Bidirectional Communication: Convert ROS 2 messages to CAN frames and vice versa.
- Custom Segmentation: Automatically segment large messages into multiple CAN frames based on configuration.
- Flexible Configuration: Define mappings between ROS 2 topics and CAN messages using a YAML configuration file.
- Threaded CAN Listener: Efficiently listen to CAN bus messages without blocking ROS 2 operations.
- Logging: Comprehensive logging for monitoring and debugging.
- Operating System: Ubuntu 20.04 or later (compatible with ROS 2 distributions like Foxy, Galactic, etc.)
- ROS 2: Installed and properly sourced.
- Python 3: Version 3.8 or later.
- CAN Interface Hardware: Such as USB-CAN adapters. Alternatively, use a virtual CAN interface (
vcan
) for testing.
-
Set Up Your ROS 2 Workspace:
cd ~/psd_ws/src
-
Clone the Package:
git clone https://github.com/rimaturus/ros2_can_interface.git
-
Install Python Dependencies:
pip install python-can pyyaml ament_index_python --break-system-packages
Achtung! [WIP cleaner fix]
-
Build the Package:
cd ~/psd_ws/ colcon build --packages-select ros2_can_interface
-
Source the Workspace:
source install/setup.bash
The mapping.yaml
file defines how ROS 2 topics map to CAN messages and vice versa. It specifies CAN IDs, message fields, data types, and segmentation parameters.
Location: ros2_can_interface/config/mapping.yaml
Sample mapping.yaml
:
ros_to_can:
/psd_vehicle/pose:
can_id_base: 0x200
fields:
car_x:
start_bit: 0
length: 32
type: float
car_y:
start_bit: 32
length: 32
type: float
car_yaw:
start_bit: 64
length: 32
type: float
segmentation:
max_payload: 8 # bytes per CAN frame
segments:
- offset: 0 # bytes
- offset: 4
- offset: 8
can_to_ros:
/actuator/command:
can_id_base: 0x200
fields:
command:
start_bit: 0
length: 16
type: int
value:
start_bit: 16
length: 32
type: float
segmentation:
max_payload: 8 # bytes per CAN frame
segments:
- offset: 0
- offset: 4
If you've made changes to the package or added new dependencies, rebuild the workspace:
cd ~/psd_ws/
colcon build --packages-select ros2_can_interface
source install/setup.bash
This node subscribes to ROS 2 topics and publishes corresponding CAN messages.
ros2 run ros2_can_interface ros2can
This node listens to CAN messages and publishes corresponding ROS 2 topics.
ros2 run ros2_can_interface can2ros
-
Set Up Virtual CAN Interface:
sudo modprobe vcan sudo ip link add dev vcan0 type vcan sudo ip link set up vcan0
-
Monitor CAN Traffic:
Use
candump
to monitor CAN messages:candump vcan0
-
Send Test CAN Messages:
Use
cansend
to send test messages:cansend vcan0 100#1122334455667788
-
Publish Test ROS 2 Messages:
In a new terminal, publish a test message:
ros2 topic pub /psd_vehicle/pose std_msgs/msg/Float32MultiArray "data: [25.5, 3.2, -1.1]"
-
Verify Reception:
Echo the ROS 2 subscriber topic:
ros2 topic echo /actuator/command
-
ModuleNotFoundError: No module named 'ros2_can_interface.ros2can'
:Ensure that
ros2can.py
is correctly placed inside theros2_can_interface
package andsetup.py
is correctly configured. -
AttributeError: property 'subscriptions' of 'ROS2ToCANPublisher' object has no setter
:Rename
self.subscriptions
toself.subscription_list
or another non-conflicting name in your scripts. -
CAN Bus Not Initializing:
Verify the CAN interface name and permissions. Add your user to the
can
group:sudo usermod -aG can $USER
ros2_can_interface/
├── config
│ └── mapping.yaml
├── package.xml
├── README.md
├── resource
│ └── ros2_can_converter
├── ros2_can_converter
│ ├── can2ros.py
│ ├── __init__.py
│ └── ros2can.py
├── setup.cfg
├── setup.py
└── test
├── test_copyright.py
├── test_flake8.py
└── test_pep257.py
- ROS 2 Packages:
rclpy
,std_msgs
- Python Packages:
python-can
,pyyaml
,ament_index_python
- System Packages:
can-utils
Contributions are welcome! Please follow these steps:
- Fork the Repository.
- Create a Branch.
- Commit Changes.
- Open a Pull Request.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
- ROS 2 Community
- Python CAN Library Authors
- Open-Source Contributors
Happy Coding! 🚀