【问题标题】:PI control signalPI控制信号
【发布时间】:2021-05-20 01:38:55
【问题描述】:

我正在尝试将我的 PI 控制信号“u[k]”转换为占空比输出,错误 e[k] 控制方向输出....但我认为它可能会更好。

所以我使用 if 语句将 u 的值集中在 0 到 100 之间,因为那是 dc 范围。 (我似乎无法在任何地方向 u 或 u[k] 添加“abs”语句以将其保持在积极领域,所以如果有其他方法请告诉我。)

那么 u[k] 的值将成为整个脚本的直流循环。如果误差 e[k] 为正,则顺时针旋转,如果误差为负,则逆时针旋转。同时,u[k] 将控制电机的速度,使其在达到所需角度时减速或加速。告诉我

我仍然有错误

"ValueError: 多元素数组的真值不明确。使用a.any()或a.all()"

下面是我正在努力处理的代码的 sn-p。任何帮助表示赞赏

def MotorClockwise():
    GPIO.output(Motor1, GPIO.LOW) # Motor will move in clockwise direction.
    GPIO.output(Motor2, GPIO.HIGH)
    
    
def MotorAntiClockwise():
    GPIO.output(Motor1, GPIO.HIGH) # Motor will move in anti-clockwise direction.
    GPIO.output(Motor2, GPIO.LOW)
    
def MotorStop():
    GPIO.output(Motor1, GPIO.LOW) # Motor will stop.
    GPIO.output(Motor2, GPIO.LOW)
    GPIO.output(PWMPin, GPIO.LOW)


#PI controller settings
Kp = 12
Ki = 8 #integral Time

r = 4.5 #setpoint angle to be achieved
#intialization
e = np.zeros(N+2)
u = np.zeros(N+2)
  
for k in range(N+1):
 
    e[k] = r - encoder_angle #calucalted error
    #print (f"{e[k]} Error Detected")
    #time.sleep(.5)
    
    # PI Controller - produces new contrl signal
    u[k] = u[k-1] + Kp*(e[k] - e[k-1]) + (Kp/Ki)*e[k] # discrete version of PI controller
   # y[k+1] = (1+Ts*a)*y[k] + Ts*b*u[k] #discrete deff equ of process

########################下面是我挣扎的地方

while True:
    if (u > 100):
        u = 100;
        if (e[k] > 0) :
            PwmValue.ChangeDutyCycle(u)
            MotorClockwise()
    if (u < -100):
        u = -100;
        if (e[k] < 0):
            PwmValue.ChangeDutyCycle(u)
            MotorAntiClockwise()
        
    if (e[k] == 0) and (r == encoder_angle):
        MotorStop()
        print(f"Motor Stopped & Holding at {encoder_angle} Degrees")
        break
    
    

【问题讨论】:

    标签: python controller pid motordriver dc


    【解决方案1】:

    在您的if 语句中,您试图将u 与数字进行比较作为条件。 uarray。为了使比较有效,类型必须是可比较的。您无法比较数组和数字 - [1, 3] &gt; 2 的结果未定义。尝试使用 numpy 的 np.anynp.all 就像你的错误提示,python 的 any(u)all(u),或索引 u 数组 (u[k]) 来获取一个数字进行比较。

    【讨论】:

      猜你喜欢
      • 2011-11-07
      • 1970-01-01
      • 2023-02-08
      • 2011-08-31
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多