在单一四旋翼导航代码中的简单数值错误。

huangapple go评论76阅读模式
英文:

Simple ValueError in a single quadrotor navigation code

问题

我遇到了一个简单的四旋翼代码的新手错误。我读到的错误是"ValueError: not enough values to unpack (expected 5, got 4)",出现在以下这行代码:

States[0:6], Thrust_calc, z_accel, x_accel, y_accel = quadrotor(States, S_desired[6:11], Thrust)

我将代码分成了两个文件。相关的代码行如下所示:

for j in range(N):
            
    x_vel_desired = (final_x_coordinate - States[6]) / (50 * dt)
    x_accel_desired = 0
    y_vel_desired = (final_y_coordinate - States[7]) / (50 * dt)
    y_accel_desired = 0

    S_desired[0], S_desired[1], S_desired[2] = final_x_coordinate, x_vel_desired, x_accel_desired
    S_desired[3], S_desired[4], S_desired[5] = final_y_coordinate, y_vel_desired, y_accel_desired
    
    # 期望的角度
    # Theta
    S_desired[6] = angle_objective(S_desired[0:3], States[6], States[9], States[0], Thrust, Kxp1, Kxd1)
    
    # Phi
    S_desired[7] = angle_objective(S_desired[3:7], States[7], States[10], States[1], Thrust, Kyp1, Kyd1)
    
    # 无人机高度位置、速度和加速度
    # 获取四旋翼的状态
    States[0:6], Thrust_calc, z_accel, x_accel, y_accel = quadrotor(States, S_desired[6:11], Thrust)
    
    # 推力的增量平滑
    if Thrust_calc - Thrust > 0.05:
        Thrust = Thrust_calc + 0.05
    elif Thrust - Thrust_calc > 0.05:
        Thrust = Thrust_calc - 0.05

    # 获取X_POS和X_VEL
    States[9], States[6] = EulerIntegration(x_accel, States[9], States[6])
    States[10], States[7] = EulerIntegration(y_accel, States[10], States[7])

另一个文件是:

def quadrotor(States, desired_states, Thrust):
    inputs = [0] * 4
    # desired_states = list(map(int, desired_states))
    # PID参数
    Kp, Ki, Kd = 4.05, 0, 20.64

    Torque = [0] * 4

    # PID控制器。滚转 = 0
    err_phi = desired_states[1] - States[1]
    Torque[1] = (err_phi * Kp + err_phi * Ki * dt - States[4] * Kd * dt) * I[0, 0]

    # PID控制器。俯仰 = 0
    err_theta = desired_states[0] - States[0]
    Torque[2] = (err_theta * Kp + err_theta * Ki * dt - States[3] * Kd * dt) * I[1, 1]

    # PID控制器。偏航 = 0
    err_yaw = desired_states[2] - States[2]
    Torque[3] = (err_yaw * Kp + err_yaw * Ki * dt - States[5] * Kd * dt) * I[2, 2]

    # PID控制器。高度误差 = 0
    err_height = desired_states[3] - States[8]
    Torque[0] = err_height * Kp + err_height * Ki * dt - States[8] * Kd * dt + (m * g) / (math.cos(States[1]) * math.cos(States[0]))
    if Torque[0] > 8:
        Torque[0] = 8
    elif Torque[0] < 3:
        Torque[0] = 3

    # 输入
    inputs[0] = 1 / (4 * k) * Torque[0] - 1 / (2 * k * L) * Torque[2] - 1 / (4 * b) * Torque[3]
    inputs[1] = 1 / (4 * k) * Torque[0] - 1 / (2 * k * L) * Torque[1] + 1 / (4 * b) * Torque[3]
    inputs[2] = 1 / (4 * k) * Torque[0] + 1 / (2 * k * L) * Torque[2] - 1 / (4 * b) * Torque[3]
    inputs[3] = 1 / (4 * k) * Torque[0] + 1 / (2 * k * L) * Torque[1] + 1 / (4 * b) * Torque[3]

    phi_acel = Torque[1] / I[0, 0]
    theta_acel = Torque[2] / I[1, 1]
    psi_acel = Torque[3] / I[2, 2]

    theta_vel, theta = EulerIntegration(theta_acel, States[3], States[0])
    phi_vel, phi = EulerIntegration(phi_acel, States[4], States[1])
    psi_vel, psi = EulerIntegration(psi_acel, States[5], States[2])

    z_accel = -g + (math.cos(theta) * math.cos(phi)) * Torque[0] / m
    x_accel = (math.sin(psi) * math.sin(phi) + math.cos(psi) * math.sin(theta) * math.cos(phi)) * Torque[0] / m

    results = [theta, phi, psi, theta_vel, phi_vel, psi_vel]

    return results, Torque[0], z_accel, x_accel

完整的代码可以在这里找到。有人可以帮助我吗?

英文:

I'm having a newbie error with a simple piece of code of a flying quadrotor. The error I read is "ValueError: not enough values to unpack (expected 5, got 4) on line:

States[0:6], Thrust_calc, z_accel, x_accel, y_accel= quadrotor(States,S_desired[6:11],Thrust)

I have the code divided in two files. The involved lines are coded are shown next:

for j in range(N):
x_vel_desired = (final_x_coordinate - States[6])/(50*dt)
x_accel_desired = 0
y_vel_desired = (final_y_coordinate - States[7])/(50*dt)
y_accel_desired = 0
S_desired[0], S_desired[1], S_desired[2] = final_x_coordinate,x_vel_desired,x_accel_desired
S_desired[3], S_desired[4], S_desired[5] = final_y_coordinate,y_vel_desired,y_accel_desired
#DESIRED ANGLE
#Theta
S_desired[6] = angle_objective(S_desired[0:3],States[6],States[9],States[0],Thrust,Kxp1,Kxd1)
#Phi
S_desired[7] = angle_objective(S_desired[3:7],States[7],States[10],States[1],Thrust,Kyp1,Kyd1)
#DRONE height position, velocity and acceleration
#OBTAINING OF THE STATES OF THE QUADROTOR
States[0:6], Thrust_calc, z_accel, x_accel, y_accel= quadrotor(States,S_desired[6:11],Thrust)
#the increment of the thrust is smooth
if Thrust_calc-Thrust&gt;0.05:
Thrust=Thrust_calc+0.05
elif Thrust-Thrust_calc&gt;0.05:
Thrust=Thrust_calc-0.05
#OBTAINTION OF X_POS AND X_VEL
States[9],States[6]  = EulerIntegration(x_accel,States[9],States[6])
States[10],States[7] = EulerIntegration(y_accel,States[10],States[7])

and the other file is:

def quadrotor(States, desired_states, Thrust):
inputs=[0]*4
#desired_states=list(map(int, desired_states))
#PID Values
Kp, Ki, Kd = 4.05, 0, 20.64
Torque=[0]*4
#pid controller. Roll = 0
err_phi=desired_states[1]-States[1] 
Torque[1]=(err_phi*Kp + err_phi*Ki*dt - States[4]*Kd*dt )* I[0,0] 
#PID Controller. Pitch = 0
err_theta=desired_states[0]-States[0]
Torque[2]=(err_theta*Kp + err_theta*Ki*dt - States[3]*Kd*dt) * I[1,1]
#PID Controller. Yaw = 0
err_yaw=desired_states[2]-States[2]
Torque[3]=(err_yaw*Kp + err_yaw*Ki*dt - States[5]*Kd*dt ) * I[2,2] 
#PID Controller. Height error = 0
err_height=desired_states[3]-States[8]
Torque[0]=err_height*Kp + err_height*Ki*dt - States[8]*Kd*dt + (m*g)/(math.cos(States[1])*math.cos(States[0])) 
if Torque[0]&gt;8:
Torque[0]=8
elif Torque[0]&lt;3:
Torque[0]=3
#inputs
inputs[0]= 1/(4*k)*Torque[0] - 1/(2*k*L)*Torque[2] - 1/(4*b)*Torque[3] 
inputs[1]= 1/(4*k)*Torque[0] - 1/(2*k*L)*Torque[1] + 1/(4*b)*Torque[3] 
inputs[2]= 1/(4*k)*Torque[0] + 1/(2*k*L)*Torque[2] - 1/(4*b)*Torque[3] 
inputs[3]= 1/(4*k)*Torque[0] + 1/(2*k*L)*Torque[1] + 1/(4*b)*Torque[3] 
phi_acel=    Torque[1]/I[0,0] 
theta_acel=  Torque[2]/I[1,1] 
psi_acel=    Torque[3]/I[2,2] 
theta_vel, theta = EulerIntegration(theta_acel,States[3],States[0])
phi_vel, phi =     EulerIntegration(phi_acel,States[4],States[1])
psi_vel, psi =     EulerIntegration(psi_acel,States[5],States[2])
z_accel= -g + (math.cos(theta)*math.cos(phi))*Torque[0]/m
x_accel=(math.sin(psi)*math.sin(phi)+math.cos(psi)*math.sin(theta)*math.cos(phi))*Torque[0]/m
results= [theta, phi, psi, theta_vel, phi_vel, psi_vel]
return results, Torque[0], z_accel, x_accel

Whole code is published here. Can anyone help me, please?

答案1

得分: 0

"OK,我最后看到我必须定义'y_accel'并将其包括在变量的'return'行中"

英文:

OK, I saw finally that I have to define "y_accel" and include it in the "return" line of variables

huangapple
  • 本文由 发表于 2023年7月13日 17:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定