0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

๐Ÿถ ROS 2 Servo Control for Robot Dog

0
Posted at

๐Ÿง  Previously

Raspberry Pi setup โœ…
https://qiita.com/tri-edge/items/df545c0aaac6975101ed
ROS 2 setup โœ…
https://qiita.com/tri-edge/items/fa72f52c0a7a9ef7af1f

๐Ÿ“ Introduction

Now that you have installed ROS 2 on your Raspberry Pi, the next step is to control hardware.
In this guide, you will learn how to:

  • Control a servo motor using Python
  • Integrate servo control with ROS 2
  • Send commands to move a single servo

๐Ÿ‘‰ By the end of this guide, you will be able to control a servo motor using ROS 2 commands.

๐Ÿ’ก Important Concept

ROS 2 does NOT directly control hardware. Instead:

ROS 2 โ†’ sends command โ†’ Python node โ†’ controls servo

๐Ÿ”ง Requirements

๐Ÿ”Œ Hardware

  • Raspberry Pi 4
  • FreeNove Robot Dog (or any servo motor)

๐Ÿ’ป Software

  • Raspberry Pi OS (64-bit)
  • ROS 2 (Humble installed)
  • Python 3

๐Ÿ’ก Note

In the previous tutorial, we installed python3-smbus, which is a specific Python library used for I2C communication. In this guide, we install python3-pip, which is a tool used to install Python libraries. This is important because we need pip to install additional packages like gpiozero.

In short, python3-smbus installs one library, while python3-pip allows you to install many Python libraries as needed.

๐Ÿš€ Setup

โ‘  Install required Python libraries

sudo apt update
sudo apt install python3-pip -y
pip3 install gpiozero

โœ… This installs the Python package manager (pip) and the servo control library

โ‘ก Test servo (without ROS 2)

๐Ÿ‘‰ First, confirm the servo works properly

nano servo_test.py

Add code:

from gpiozero import AngularServo
from time import sleep

servo = AngularServo(18, min_angle=-90, max_angle=90)

while True:
    servo.angle = -45
    sleep(2)
    servo.angle = 45
    sleep(2)

Save and run:

python3 servo_test.py

โœ… The servo should move left and right
๐Ÿ‘‰ If this does NOT work, fix it before continuing

โ‘ข Create ROS 2 workspace

Run:

mkdir -p ~/ros2_ws/src

๐Ÿ“ ros2_ws

  • This is your workspace name
  • You can name it anything, but ros2_ws is standard

Example location: /home/pi/ros2_ws

๐Ÿ“ src (VERY important)

Inside the workspace:

ros2_ws/
 โ””โ”€โ”€ src/

This is where your ROS 2 packages go.

๐Ÿง  Why src exists

ROS expects your packages to be inside: workspace/src/

If you donโ€™t follow this structure: โŒ ROS wonโ€™t find your packages

Run:

cd ~/ros2_ws
colcon build

๐Ÿงฑ What colcon build does

โœ… Builds your ROS 2 workspace. It:

  • compiles packages
  • prepares ROS environment
  • generates setup files

๐Ÿ“ After build, your workspace looks like:

ros2_ws/
 โ”œโ”€โ”€ src/
 โ”œโ”€โ”€ build/
 โ”œโ”€โ”€ install/
 โ””โ”€โ”€ log/

What these folders mean:

Folder Purpose
src your code (packages)
build temporary build files
install final usable ROS packages
log build logs

โœ… Final step

source install/setup.bash

๐Ÿ’ก Tip: Run this each time you open a new terminal

source ~/ros2_ws/install/setup.bash

๐Ÿง  What this does

โœ… Tells your terminal:
โ€œUse packages from this workspaceโ€
Without this:
โŒ ROS wonโ€™t see your package
โŒ ros2 run wonโ€™t work

๐Ÿง  Full flow (simple mental model)

Create folder โ†’ add code โ†’ build โ†’ enable

โ‘ฃ Create ROS 2 package

cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python robot_dog_control
cd robot_dog_control/robot_dog_control

๐Ÿถ Relating to your robot dog

You will create the following structure:

ros2_ws/
 โ””โ”€โ”€ src/
     โ””โ”€โ”€ **robot_dog_control/**

๐Ÿ‘‰ This package contains your robot control code, such as:

  • the servo control node
  • future movement logic

โ‘ค Create servo control node

In ROS 2, a node is a program that performs a specific task.
In this step, you will create a node that receives commands and controls a servo.

๐Ÿ“„ Create the file:

nano servo_node.py

๐Ÿ‘‰ This creates a Python file where your ROS 2 code will live. This node will:

  • Listen to a ROS 2 topic (servo_angle)
  • Receive angle values (e.g., 30, -45)
  • Move the servo to that angle

๐Ÿง  ROS 2 Servo Node

import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32
from gpiozero import AngularServo

class ServoNode(Node):
    def __init__(self):
        super().__init__('servo_node')

        self.servo = AngularServo(18, min_angle=-90, max_angle=90)

        self.subscription = self.create_subscription(
            Float32,
            'servo_angle',
            self.set_angle,
            10
        )

    def set_angle(self, msg):
        angle = msg.data
        self.get_logger().info(f'Setting angle: {angle}')
        self.servo.angle = angle


def main(args=None):
    rclpy.init(args=args)
    node = ServoNode()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

๐Ÿ’ก Key explanation

  • Float32 โ†’ the type of message (a number)
  • servo_angle โ†’ topic name
  • set_angle() โ†’ function that runs when a message is received

Example flow:

[ You type command ]
        โ†“
[ ROS topic: servo_angle ]
        โ†“
[ Your node receives message ]
        โ†“
[ Servo angle is updated ]
        โ†“
๐Ÿถ Robot moves

๐Ÿ‘‰ A command is sent to a ROS topic, the node receives it, and then controls the servo accordingly.

โ‘ฅ Update setup.py

๐Ÿ‘‰ This step tells ROS 2 how to run your node.

๐Ÿ“„ Open setup.py:

nano ~/ros2_ws/src/robot_dog_control/setup.py

๐Ÿง  Add:

entry_points={
    'console_scripts': [
        'servo_node = robot_dog_control.servo_node:main',
    ],
},

๐Ÿ’ก What this means

ใƒปservo_node โ†’ command name you will use
ใƒปrobot_dog_control.servo_node โ†’ Python file location
ใƒปmain โ†’ function that starts the program

โœ… Result

After this, you can run:

ros2 run robot_dog_control servo_node

๐Ÿ‘‰ Without this step, ROS 2 will NOT find your node โŒ

โ‘ฆ Build the package

๐Ÿ‘‰ After creating or updating code, you must build the workspace.

๐Ÿ”ง Build command

cd ~/ros2_ws
colcon build

๐Ÿง  What this does

compiles your package
registers your node with ROS 2
prepares it for execution

โš ๏ธ Important step

After building:

source install/setup.bash

It tells the terminal:
โ€œUse my newly created package and nodeโ€

๐Ÿ’ก Note:

You will often run colcon build and source install/setup.bash after making changes. Repeating these steps is normal in ROS 2 development.

โ‘ง โœ… Run and test

๐Ÿ–ฅ๏ธ Open two terminals

Terminal A:

ros2 run robot_dog_control servo_node

Terminal B:

ros2 topic pub /servo_angle std_msgs/msg/Float32 "{data: 30}"

Expected result

  • Servo moves to the specified angle
  • Terminal shows:
Setting angle: 30.0

This confirms ROS 2 can successfully control hardware.
๐Ÿ‘‰ If this works, your ROS 2 + hardware setup is fully functional.

โš ๏ธ Warning

Be careful not to force large or rapid angle changes, as this may damage the servo.

โšก Key Points

  • Always test hardware BEFORE ROS
  • ROS 2 sends commands, Python controls hardware
  • Use small angle values to avoid damage
  • Servos may require an external power supply

๐Ÿง  What is happening?

Command โ†’ ROS topic โ†’ Servo node โ†’ Servo motor
  • Topic = communication
  • Node = logic
  • Servo = hardware

๐Ÿ‘‰ This is the foundation of robot control

โœ… Summary

You now have:

  • โœ… Working servo control
  • โœ… ROS 2 node controlling hardware
  • โœ… Communication between ROS and servo

Your Raspberry Pi can now control real hardware using ROS 2.
This is the fundamental mechanism behind all robot control in ROS 2.
You are now ready to start controlling real robot movement using ROS 2.

๐Ÿš€ Next Step

๐Ÿ‘‰ Move a full leg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?