### Step 1: Create a New ROS2 Package
```bash
ros2 pkg create --build-type ament_python subpub_pkg --dependencies rclpy std_msgs sensor_msgs geometry_msgs
```
### Step 2: Implement the Subpub Node
Create a new file named `subpub.py` inside the `subpub_pkg` folder with the following content:
```python
# subpub.py
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
from rclpy.qos import ReliabilityPolicy, QoSProfile
class Subpub(Node):
def __init__(self):
super().__init__('subpub')
self.publisher_ = self.create_publisher(Twist, 'cmd_vel', 10)
self.subscriber = self.create_subscription(LaserScan, '/scan', self.laser_callback, QoSProfile(depth=10, reliability=ReliabilityPolicy.RELIABLE))
self.timer_period = 0.5
self.laser_forward = 0
self.cmd = Twist()
self.timer = self.create_timer(self.timer_period, self.motion)
def laser_callback(self, msg):
self.laser_forward = msg.ranges[359]
def motion(self):
self.get_logger().info('I receive: "%s"' % str(self.laser_forward))
if self.laser_forward > 5:
self.cmd.linear.x = 0.2
self.cmd.angular.z = 0.0
elif 0.5 <= self.laser_forward < 5:
self.cmd.linear.x = 0.1
self.cmd.angular.z = 0.0
else:
self.cmd.linear.x = 0.0
self.cmd.angular.z = 0.0
self.publisher_.publish(self.cmd)
def main(args=None):
rclpy.init(args=args)
subpub = Subpub()
rclpy.spin(subpub)
subpub.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
```
### Step 3: Create a Launch File
In the `launch` folder of your package, create a file named `subpub_pkg_launch_file.launch.py`:
```bash
cd ~/ros2_ws/src/subpub_pkg
mkdir launch
cd ~/ros2_ws/src/subpub_pkg/launch
touch subpub_pkg_launch_file.launch.py
chmod +x subpub_pkg_launch_file.launch.py
```
Add the following code to the `subpub_pkg_launch_file.launch.py` file:
```python
# subpub_pkg_launch_file.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='subpub_pkg',
executable='subpub',
output='screen'),
])
```
### Step 4: Modify `setup.py`
Edit the `setup.py` file in your package to include the launch file and entry points:
```python
# setup.py
# ... (existing code)
entry_points={
'console_scripts': [
'subpub = subpub_pkg.subpub:main'
],
},
```
### Step 5: Compile Your Package
```bash
cd ~/ros2_ws
colcon build --packages-select subpub_pkg
source ~/ros2_ws/install/setup.bash
```
### Step 6: Launch the Node
Finally, launch the publisher node in your shell:
```bash
ros2 launch subpub_pkg subpub_pkg_launch_file.launch.py
```
You should now see the robot responding to laser scan data by adjusting its motion based on the logic you've implemented.
Feel free to ask if you have any questions or encounter any issues during the process!
{"title":"Create a publisher Node to move a turtlebot","description":"Absolutely, let’s structure the guide in a similar format:","contributors":"[{\"id\":\"5111e6b9-aae7-4bcf-9452-02655457ed05\",\"add\":3117,\"del\":0}]"}