简体   繁体   English

如何使用 ROS 导入自定义 python 模块?

[英]How to import custom python modules with ROS?

In side simulator.py, I'd imported PID.py from Control which is valid according to PyCharm IDE.在 side simulator.py 中,我从 Control 导入了 PID.py,根据 PyCharm IDE 有效。

Program Structure:程序结构:

src
├── quantum_drone
│   ├── CMakeLists.txt
│   ├── Control
│   │   ├── __init__.py
│   │   ├── Keys.py
│   │   ├── MonotonicTime.py
│   │   └── PID.py
│   ├── package.xml
│   └── scripts
│       ├── __init__.py
│       └── simulator.py

I realized I didn't have setup.py setup in my root directory (quantum_drone), so I when and set it up:我意识到我的根目录 (quantum_drone) 中没有 setup.py 设置,所以我什么时候设置它:

#!/usr/bin/env python
# noinspection PyUnresolvedReferences
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup
   
# fetch values from package.xml
setup_args = generate_distutils_setup(
     packages=['quantum_drone'],
     scripts=['Control', 'scripts'],
     package_dir={'': 'src'},
 )
 
setup(**setup_args)

In my CMakeLists.txt, I had uncommented the catkin_python_setup() and inside catkin_install_python I wrote:在我的 CMakeLists.txt 中,我取消了对 catkin_python_setup() 的注释,并在 catkin_install_python 中写道:

catkin_install_python(PROGRAMS
   scripts/simulator.py
   Control/PID.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Finally, I ran:最后,我跑了:

catkin_make 
source devel/setup.bash

I try to run: rosrun quantum_drone simulator.py我尝试运行: rosrun quantum_drone simulator.py

But I keep getting this message:但我不断收到这条消息:

Traceback (most recent call last):
  Traceback (most recent call last):
  File "/home/kannachan/drone/src/quantum_drone/scripts/simulator.py", line 3, in <module>
    from Control import PID
ImportError: No module named Control

I could've swore I did everything I needed to do, so I'm not sure where I when wrong such that I couldn't import my files/directories.我可以发誓我做了我需要做的一切,所以我不确定我哪里错了以至于我无法导入我的文件/目录。

Contents of PID.py (I can import MonotonicTime.py just fine): PID.py 的内容(我可以导入 MonotonicTime.py 就好了):

#!/usr/bin/env python

from pymavlink import mavutil
from MonotonicTime import monotonic_time
import numpy as np

current_time = monotonic_time


class PID:

    def __init__(self, vehicle, kp=0, ki=0, kd=0, velocity=(0, 0, 0)):
        self.kp = kp  # Constants (kp, ki, kd)
        self.ki = ki
        self.kd = kd
        self.drone = vehicle
        self.p_terms = np.array([0, 0, 0], dtype=float)  # 3-axis
        self.i_terms = np.array([0, 0, 0], dtype=float)
        self.d_terms = np.array([0, 0, 0], dtype=float)
        self.velocity = np.asarray(velocity)  # Desired Velocity
        self.current_velocity = np.array(self.drone.velocity)  # Current Velocity
        self.errors = np.array([0, 0, 0], dtype=float)  # Previous Error
        self.time = current_time()  # Current Time
        self.__max__ = 40  # Max Speed
        self.__min__ = 0  # Min Speed

    def reset(self):
        #  Resetting terms
        self.p_terms = np.array([0, 0, 0], dtype=float)
        self.i_terms = np.array([0, 0, 0], dtype=float)
        self.d_terms = np.array([0, 0, 0], dtype=float)
        self.errors = np.array([0, 0, 0], dtype=float)

    def calculate_change(self):
        now = current_time()
        delta_error = self.velocity - self.current_velocity  # Change in error
        delta_time = now - self.time if now - self.time else 1e-16  # Change in time
        self.time = now
        return delta_error, delta_time

    def pid_calculation(self):
        delta_error, delta_time = self.calculate_change()
        # PID controller formula
        self.p_terms = delta_error * self.kp
        self.i_terms += self.errors * delta_time
        self.d_terms = (delta_error - self.errors) / delta_time
        self.errors = delta_error
        outputs = self.kp * self.p_terms + self.ki * self.i_terms + self.kd * self.d_terms
        outputs = np.where(outputs > self.__max__, self.__max__, outputs)  # Replacing terms too big
        outputs = np.where(outputs < self.__min__, self.__min__, outputs)  # Replacing terms too small
        return outputs

    def send_command(self):
        vx, vy, vz = self.pid_calculation()
        msg = self.drone.message_factory.set_position_target_location_ned_encode(
            0,
            0, 0,
            mavutil.mavlink.MAV_FRAME_BODY_NED,
            0b0000111111000111,
            0, 0, 0,
            vx, vy, vz,
            0, 0
        )
        self.drone.send_mavlink(msg)
        self.drone.flush()

Your import is wrong.你的导入是错误的。 It should be importing from PID not Control.它应该从 PID 而不是 Control 导入。 As well, within ROS1 you don't need a setup.py file for a Python package.同样,在 ROS1 中,您不需要 Python package 的 setup.py 文件。

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

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