简体   繁体   English

我如何只运行一次 python 模块的一部分?

[英]How do i run part of a python module only once?

I'm making a PID controller for an RC car for cruise control in python, and wanted to separate it into a module, so the code main code looks cleaner.我正在为 python 中用于巡航控制的遥控车制作 PID controller,并希望将其分离成一个模块,因此代码主代码看起来更干净。 I can do this, but the problem would be that i have to create an object once for the PID controller and set its constants, and the remaining part of the code should run every time the function is called我可以这样做,但问题是我必须为 PID controller 创建一次 object 并设置其常量,并且代码的其余部分应该在每次调用 ZC1C425268E68385D1AB5074C14ZA 时运行

This is my code for the PID controller:这是我的 PID controller 代码:

from simple_pid import PID

def PID(rpm_array1, target_speed):
    Kp = 80
    Ki = 60
    Kd = 0.01
    Kf = 180 / target_speed

    pid = PID(Kp, Ki, Kd)
    pid.sample_time = 0.05

    avg_rpm_rear = (rpm_array1[2]+rpm_array1[3])/2
    speed = (avg_rpm_rear/60)*0.355; 

    pid.setpoint = target_speed

    x = pid(speed)

    pid_output = x + (target_speed * Kf)
    if(pid_output>0):
        throttle_target = pid_output+1455

    if throttle_target >= 2500 :
        throttle_target = 2500
    elif throttle_target <= 1455 :
        throttle_target = 1455

    return throttle_target

And i would like to use it like this:我想像这样使用它:

import PID_module

while True:
    target_throttle = PID_module.PID(rpm_array1, target_speed)

What would be the proper way to do this?这样做的正确方法是什么?

I'm not really sure if this is what you're after but hopefully this gives you some idea of how to solve your problem.我不确定这是否是您所追求的,但希望这能让您对如何解决问题有所了解。

class PID:
    def __init__(self, Kp, Ki, Kd):
        self.Kp = Kp
        self.Ki = Ki
        self.Kd = Kd

    def throttle_target(self, rpm_array, target_speed):
        Kf = 180/target_speed
        avg_rpm_rear = (rpm_array[2]+rpm_array[3])/2
        speed = (avg_rpm_rear/60)*0.355
        pid_output = speed + (target_speed * Kf)
        if (pid_output > 0):
            target_throttle = pid_output + 1455
        
        if (target_throttle >= 2500):
            target_throttle = 2500
        elif (target_throttle <= 1455):
            target_throttle = 1455
        return target_throttle

Run some test case:运行一些测试用例:

import numpy as np
pid = PID(80, 60, 0.01)
rpm_array = np.array([0,10,20,30,40,50])
target_speed = 50
x = pid.throttle_target(rpm_array, target_speed)
print(x)

This gives 1635.15 .这给出了1635.15

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

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