简体   繁体   English

在ROS catkin工作区中定义和使用Python类

[英]Defining and using a Python class within a ROS catkin workspace

I am running ROS Indigo. 我正在运行ROS Indigo。 I have what should be a simple problem: I have a utility class in my package that I want to be callable from our scripts. 我有一个简单的问题:我的软件包中有一个实用程序类,希望可以从脚本中调用它。 It only needs to be called within our own package; 它只需要在我们自己的包中调用; I don't need it to be available to other ROS packages. 我不需要将其提供给其他ROS软件包。

I defined a class named HandControl in a file HandControl.py. 我在文件HandControl.py中定义了一个名为HandControl的类。 All my attempts to import it, or use it without importing, fail. 我所有尝试导入或不使用而未导入的尝试都失败了。 Where in the catkin workspace do I put it -- the root of the package, or in scripts? 我将它放置在catkin工作区中的哪个位置(包的根目录还是脚本中)? Do I need __init.py__ anywhere (I have tried several places)? 我在任何地方都需要__init.py__ (我尝试过几个地方)?

It is a good practice to follow the standards of Python and ROS here. 在此处遵循Python和ROS的标准是一个好习惯。 Scripts are typically placed in /script directory and they should not be imported into other python scripts. 脚本通常放置在/script目录中,不应其导入其他python脚本中。 Reusable python code is an indication of a python module. 可重用的python代码是python模块的指示。 Python modules should be placed in /src/package_name and there you should create __init__.py as well. Python模块应该放置在/src/package_name并且您还应该在其中创建__init__.py This module will be available everywhere in your catkin workspace. 此模块将在您的catkin工作区中的任何地方可用。 There is a good chance this structure will help you in the future to structure things, even though you may not seem to need it at the moment. 这种结构很有可能会在将来帮助您构建事物,即使您目前似乎不需要它。 Project typically grow and following guidelines helps to maintain good code. 项目通常会增长,并且遵循准则有助于维护良好的代码。 For more specific details checkout this python doc . 有关更多具体细节,请查看此python文档

Erica, 埃里卡,

please see this school project, which was written in Python and run on ROS Indigo. 请参阅学校项目,该项目是用Python编写并在ROS Indigo上运行的。 If you'd look in the /scripts folder, you can see an example of a custom class that is being called from other scripts. 如果查看/scripts文件夹,则可以看到从其他脚本调用的自定义类的示例。 If you'd look into the launch files in /launch you can see an example of configuring the ROS nodes - maybe that is your problem. 如果您查看/launch的启动文件,您会看到一个配置ROS节点的示例-也许这就是您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM