ROS2 PYTHON Script Pamoja Na Mchapishaji na Majiri – Swahili ROS Tutorial

Written by Sonia

27/02/2024

This tutorial is created by Rosbotics Ambassador 021 Kevin

Rosbotics Ambassador Program https://www.theconstruct.ai/rosbotics-ambassador/)


Mambo tunaenda kujifunza

  1. Jinsi ya kutengeneza rosject
  2. Jinsi ya kutengeneza kifurushi ya ROS2.
  3. Jinsi ya kuunda hati ya python..
  4. Jinsi ya kutengeneza mchapishaji.
  5. Jinsi ya kutengeneza mteja.
  6. Jinsi ya kujenga nafasi ya kazi ya ROS2..

Orodha ya rasilimali zilizotumiwa katika chapisho hili

  1. The Construct: https://app.theconstructsim.com/
  2. ROS2 Courses –▸
    1. ROS2 Basics in 5 Days Humble (Python): https://app.theconstructsim.com/Course/132

Muhtasari.

ROS (Mfumo wa Uendeshaji wa Roboti) unakuwa “mfumo” wa kawaida wa roboti za kutengeneza programu. Katika chapisho hili, hebu tujifunze jinsi ya kuunda kifurushi cha ROS2, muhimu kwa kutoa maagizo kwa roboti, kwa kutumia amri ya ros2.

Kutengeneza rosject

Ili kufuata mafunzo haya, tunahitaji kuwa na ROS2 iliyosakinishwa katika mfumo wetu, na kwa hakika ros2_ws (ROS2 Workspace). Ili kurahisisha maisha yako, tutaunda mpango wa kubofya kiungo hiki: https://app.theconstructsim.com/ 

Hii inakuleta kwenye ukurasa wa kutua wa kuunda, hapo tutaenda kwa myrosjects na kisha kuunda rosject mpya, hatua zimeangaziwa katika miraba ya bluu.

 

Baada ya kuunda rosject mpya, weka usambazaji ambao ungependa kutumia kwa kesi yetu Humble na jina la rosject na maelezo mafupi.

 

Baada ya kubonyeza kitufe cha Unda, unapaswa kuwa na rosject kwenye rosjects zangu Bonyeza na ubonyeze RUN.

 

Kuunda kifurushi cha ros2.

Ili kuunda kifurushi cha ROS2, tunahitaji kuwa na Nafasi ya Kazi ya ROS2, na kwa hiyo, tunahitaji terminal.

Wacha tufungue terminal kwa kubofya kitufe cha Fungua terminal mpya.

Mara tu ndani ya terminal tumia amri kuona vitu vilivyopo kwenye saraka ya sasa:

ls

Kisha nenda kwenye saraka ifuatayo ambapo tutaunda kifurushi:

cd ros2_ws/src

Tumia amri ifuatayo kuunda kifurushi cha python:
ros2 pkg create --build-type ament_python --license Apache-2.0 py_pubsub

Pamoja na hayo umeunda kifurushi cha python ROS2.

 

 

Kuunda hati ya python.

Fungua kihariri cha msimbo kulia na uunde hati kwenye py_pubsub fille inayoitwa py_pubsub.py. Hii inafanywa kwa kubofya kulia kwenye py_pubsub na kubofya kuunda faili mpya

 

Nakili na ubandike yafuatayo na hati ya python ambayo ina mchapishaji na aliyejiandikisha kwenye hati yetu

import rclpy

import math
from rclpy.node import Node
from std_msgs.msg import Float32

class MyNode(Node):
    def __init__(self):
        super().__init__(‘my_node’)
        self.subscription = self.create_subscription(Float32, ‘input_topic’, self.listener_callback, 10)
        self.publisher = self.create_publisher(Float32, ‘output_topic’, 10)

    def listener_callback(self, msg):
        radians = msg.data
        degrees = radians * 180 / math.pi
        self.publisher.publish(Float32(data=degrees))

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

if __name__ == ‘__main__’:
    main()

 

Kisha ongeza laini ifuatayo kwenye setup.py kwenye sehemu ya kuingilia.

‘talker = py_pubsub.py_pubsub:main’
Faili ya stepup.py inapaswa kuonekana kama ifuatavyo:

 

from setuptools import setup

package_name = ‘py_pubsub’

setup(
    name=package_name,
    version=’0.0.0′,
    packages=[package_name],
    data_files=[
        (‘share/ament_index/resource_index/packages’,
            [‘resource/’ + package_name]),
        (‘share/’ + package_name, [‘package.xml’]),
    ],
    install_requires=[‘setuptools’],
    zip_safe=True,
    maintainer=’user’,
    maintainer_email=’user@todo.todo’,
    description=’TODO: Package description’,
    license=’Apache-2.0′,
    tests_require=[‘pytest’],
    entry_points={
        ‘console_scripts’: [
        ‘talker = py_pubsub.pypubsub:main’,
        ],
    },
)

 

Kuunda hati ya pili ya python

Hati hii mpya tunayopaswa kuunda itatumika kujaribu ya awali ili kuhakikisha kwamba mchapishaji na anayejisajili wanaendesha vyema.

Unda faili mpya kama hapo awali iliitwa test.py

Nakili na ubandike msimbo ufuatao

import rclpy
from rclpy.node import Node
import math

from std_msgs.msg import Float32


class MinimalPublisher(Node):

    def __init__(self):
        super().__init__(‘minimal_publisher’)
        self.publisher_ = self.create_publisher(Float32, ‘input_topic’, 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0.0

    def timer_callback(self):
        msg = Float32()
        msg.data =  self.i
        self.publisher_.publish(msg)
        self.get_logger().info(‘Publishing: “%s”‘ % msg.data)
        if(self.i > math.pi):
            self.i = 0
        self.i += 0.01


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional – otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == ‘__main__’:
    main()

Hati hii hutumia kipima muda baada ya kila sekunde 0.5 ili kuchapisha ujumbe kwa digrii kwenye mada inayoitwa input_topic.

Ongeza amri ifuatayo kwa setup.py yako

‘test =  py_pubsub.test:main’,

 

Faili yako ya setup.py sasa inapaswa kuonekana kama hii

from setuptools import setup

package_name = ‘py_pubsub’

setup(
    name=package_name,
    version=’0.0.0′,
    packages=[package_name],
    data_files=[
        (‘share/ament_index/resource_index/packages’,
            [‘resource/’ + package_name]),
        (‘share/’ + package_name, [‘package.xml’]),
    ],
    install_requires=[‘setuptools’],
    zip_safe=True,
    maintainer=’user’,
    maintainer_email=’user@todo.todo’,
    description=’TODO: Package description’,
    license=’Apache-2.0′,
    tests_require=[‘pytest’],
    entry_points={
        ‘console_scripts’: [
        ‘talker = py_pubsub.py_pubsub:main’
        ‘test =  py_pubsub.test:main’,
        ],
    },
)

 

Ongeza mabadiliko yafuatayo kwenye package.xml yako

 

  <exec_depend>rclpy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

 

Sasa kifurushi chako.xml kinapaswa kuwa kama ifuatavyo

<?xml version=“1.0”?>
<?xml-model href=“http://download.ros.org/schema/package_format3.xsd” schematypens=“http://www.w3.org/2001/XMLSchema”?>
<package format=“3”>
  <name>py_pubsub</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email=“user@todo.todo”>user</maintainer>
  <license>Apache-2.0</license>

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>
  <exec_depend>rclpy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

 

Kuunda vifurushi vyetu vya ros2

Ingiza amri ifuatayo kwenye terminal

cd
cd ros2_ws


Ingiza amri zifuatazo

colcon build
source install/setup.bash

Kujaribu nodi mbili

Katika terminal ya kwanza kukimbia

ros2 run py_pubsub talker

 

 

Katika terminal ya pili endesha amri zifuatazo

cd ros2_ws
source install/setup.bash
ros2 run py_pubsub test


Unaweza basi katika kukimbia kwa terminal ya tatu 

ros2 topic list
ros2 topic echo /output_topic


Pamoja na hayo, tumethibitisha kwamba nodi mbili zinafanya kazi, Hapa kuna kiunga cha rosject https://app.theconstructsim.com/Desktop 

 

 

Kozi & Mafunzo Husika

Ikiwa unataka kujifunza zaidi kuhusu ROS na ROS2, tunapendekeza kozi zifuatazo:

Video Tutorial

Topics: python | ros2
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This