1
1

備忘録 ラズパイ5 ROS2 ④ Ubuntu Desktop 24.04LTSでROS2環境 Pythonその2responder(コーディングと実行;失敗)

Last updated at Posted at 2024-07-27

 前回、Pythonの仮想環境の下で、ros2のパッケージresponderのセットアップをしました。
 プログラムを書いていきます。

環境

  • Raspberry Pi 5 8GB
  • 追加ボード;NVMe Base for Raspberry Pi 5 (NVMe Base by Pimoroni)
  • Crucial クルーシャル P2シリーズ 500GB 3D NAND NVMe PCIe M.2 SSD CT500P2SSD8
  • 初期;RaspberryPi OS Desktop 64bit (Debian version: 12 (bookworm) Release date: March 15th 2024)
  • 現在;Ubuntu Desktop 24.04LTS(64-bit)
  • ROS2 HumbleではなくJazzy

 Windows10で、検索窓でにcmdと入れ、コマンドプロンプトを起動します。
 sshでログインします(第1回参照)。必要ならupdateします。

C:\Users\yoshi>ssh yoshi.local

Python仮想環境メモ
作る。一度だけ
yoshi@yoshi:~$ python3 -m venv ros2
仮想環境に入る。
yoshi@yoshi:~$ source ros2/bin/activate
(ros2) yoshi@yoshi:~$ pip list
Package Version
------- -------
pip 24.0
(ros2) yoshi@yoshi:~$

でる
deactivate

Pythonのプログラム responder(コーディング)

 https://zenn.dev/mol0921/articles/f8b789d90abb35を参照し、プログラムを転載しています。

(ros2) yoshi@yoshi:~/ros2_ws/src/responder/responder$ ls
__init__.py  responder.py

 の内容は、

def main():
  print('Hi from responder.')

if __name__ == '__main__':
  main()

これをエディタnanoを使って変更します。

(ros2) yoshi@yoshi:~/ros2_ws/src/responder/responder$ nano responder.py
import rclpy  # ROS2のPythonモジュール
from rclpy.node import Node
from std_msgs.msg import String # トピック通信に使うStringメッセージ型をインポート

class Responder(Node):
    def __init__(self):
        super().__init__('responder') # ROS1でいうrospy.init_node('node_name')
        self.sub = self.create_subscription(String, '/base_msg', self.msg_callback)# subscriberの宣言
        self.pub = self.create_publisher(String,'/ex_msg', 10)# publisherの宣言

    def msg_callback(self, msg):
        print(msg) # Subscribeしたメッセージをprint
	msg.data = msg.data + "!" # メッセージに「!」を付け加える処理
        self.pub.publish(msg) # 処理したメッセージをPublish

def main():
    rclpy.init()
    node = Responder()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

 ビルド・ツールcolconをインストールします。

(ros2) yoshi@yoshi:~/ros2_ws$ sudo apt install colcon

 ビルドします。

(ros2) yoshi@yoshi:~/ros2_ws$ colcon build --symlink-install --packages-select responder
Starting >>> responder
--- stderr: responder
/usr/lib/python3/dist-packages/setuptools/command/develop.py:40: EasyInstallDeprecationWarning: easy_install command is deprecated.
!!

       ********************************************************************************
       Please avoid running ``setup.py`` and ``easy_install``.
       Instead, use pypa/build, pypa/installer or other
       standards-based tools.

       See https://github.com/pypa/setuptools/issues/917 for details.
       ********************************************************************************

!!
 easy_install.initialize_options(self)
---
Finished <<< responder [1.45s]

Summary: 1 package finished [1.59s]
 1 package had stderr output: responder
(ros2) yoshi@yoshi:~/ros2_ws$

 エラーになりました。
 もしかしたら、仮想環境の影響かと思い、いったん抜けます。

(ros2) yoshi@yoshi:~/ros2_ws$ deactivate
yoshi@yoshi:~/ros2_ws$

 ビルドします。
 同じエラーが出ます。
 どうも解決していないようです。

  easy_install を pip install に置き換える

また、仮想環境Pythonに関しても、
ros2はシステムのpythonを使う。
colcon もシステムのpythonを使う?
 ビルドしても仮想環境のpythonでは実行ユニットが見えないのではないかと。

 見つかった記事;Python 仮想環境で ROS2 ノードを実行する

 Ptyonの独自進展とROS2がかみ合っていない?
 C++言語を使うことにします。


備忘録 ラズパイ5 ROS2

① ハードの用意とUbuntu Desktop 24.04LTS

② Ubuntu Desktop 24.04LTSでROS2環境 rqt_graphとturtlesim

③ Ubuntu Desktop 24.04LTSでROS2環境 Python その1 responder(セットアップ)

④ Ubuntu Desktop 24.04LTSでROS2環境 Pythonその2responder(コーディングと実行;失敗)

⑤ Ubuntu Desktop 24.04LTSでROS2環境 C++ その1 セットアップ main.c

⑥ Ubuntu Desktop 24.04LTSでROS2環境 C++ その2 セットアップ pub.cpp rqt_graph


1
1
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
1
1