根据坐标计算并绘制旋转角度

问题描述 投票:0回答:1

我正在编写一个代码,该代码应该绘制围绕 x、y 和 z 轴的旋转。角度将根据位置数据计算得出,位置数据针对 4 个标记给出,每个标记都有 x、y 和 z 坐标。使用运动捕捉系统随时间测量位置,然后将其提取到 csv 文件中。 我正在尝试让它与大多数聊天 GPT 一起使用,但我已经有了基本的了解。 将数据与已经计算和绘制的数据集进行比较,使用不同的系统进行测量,该系统给出角速度作为输出。 目前的问题基本上是,3 个角度中的 2 个似乎相当精确,而第 3 个角度大约有 30° 偏差。

我如何提供我的数据?目前不确定在哪里/如何上传。谢谢!

这是到目前为止我的代码: 工作速度绘图:

import pandas as pd
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# load data
data = pd.read_csv('path_to_file', delimiter=';')
#%%
# extract time data
zeit = pd.to_numeric(data['time'].str.replace(',', '.'), errors='coerce')

#%%
# extract each velocity
winkelgeschwindigkeit_x = pd.to_numeric(data['wx (rad/s)'].str.replace(',', '.'), errors='coerce')
winkelgeschwindigkeit_y = pd.to_numeric(data['wy (rad/s)'].str.replace(',', '.'), errors='coerce')
winkelgeschwindigkeit_z = pd.to_numeric(data['wz (rad/s)'].str.replace(',', '.'), errors='coerce')

#%%
# compute orientation
orientierung_x = np.zeros_like(zeit)
orientierung_y = np.zeros_like(zeit)
orientierung_z = np.zeros_like(zeit)

#%%

for i in range(1, len(zeit)):
    dt = zeit.iloc[i] - zeit.iloc[i-1]
    orientierung_x[i] = orientierung_x[i-1] + winkelgeschwindigkeit_x.iloc[i] * dt
    orientierung_y[i] = orientierung_y[i-1] + winkelgeschwindigkeit_y.iloc[i] * dt
    orientierung_z[i] = orientierung_z[i-1] + winkelgeschwindigkeit_z.iloc[i] * dt

orientierung_x = orientierung_x * (180/3.1415)
orientierung_y = orientierung_y * (180/3.1415)
orientierung_z = orientierung_z * (180/3.1415)
#%%
  
# Initialise the subplot function using number of rows and columns 
figure, axis = plt.subplots(2, 2) 
  
# For Sine Function 
axis[0, 0].plot(zeit, orientierung_x) 
axis[0, 0].set_title("x over t") 
  
# For Cosine Function 
axis[0, 1].plot(zeit, orientierung_y) 
axis[0, 1].set_title("y over t") 
  
# For Tangent Function 
axis[1, 0].plot(zeit, orientierung_z) 
axis[1, 0].set_title("z over t") 
  
# For Tanh Function 
axis[1, 1].plot() 
axis[1, 1].set_title("spare square") 
  
# Combine all the operations and display 
plt.show()
# Plot der Bewegung in 3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(orientierung_x, orientierung_y, orientierung_z)

ax.set_xlabel('X-Achse')
ax.set_ylabel('Y-Achse')
ax.set_zlabel('Z-Achse')
ax.set_title('Bewegung des Handys in 3D')
plt.show()

这是我在位置计算方面的最佳尝试:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# read data
dfraw = pd.read_csv('path_to_file, skiprows = [0,1,2,3,4,6,7], delimiter=',')
df = dfraw
df.rename(columns = {'Time':'time',
                       'LASI':'x1',
                       'Unnamed: 2':'y1',
                       'Unnamed: 3':'z1',
                       'RASI':'x2',
                       'Unnamed: 5':'y2',
                       'Unnamed: 6':'z2',
                       'LPSI':'x3',
                       'Unnamed: 8':'y3',
                       'Unnamed: 9':'z3',
                       'RPSI':'x4',
                       'Unnamed: 11':'y4',
                       'Unnamed: 12':'z4'},
            inplace=True)

#%%
# naming data
zeit = df[["time"]].to_numpy()
Marker1 = df[["x1", "y1", "z1"]].to_numpy()
Marker2 = df[["x2", "y2", "z2"]].to_numpy()
Marker3 = df[["x3", "y3", "z3"]].to_numpy()
Marker4 = df[["x4", "y4", "y4"]].to_numpy()

import numpy as np
import matplotlib.pyplot as plt

# function to compute vectors
def compute_normal_vectors(marker_coordinates):
    normal_vectors = []
    for i in range(len(marker_coordinates[0])):
        # Extracting coordinates for each marker at the current time point
        marker1 = marker_coordinates[0][i]
        marker2 = marker_coordinates[1][i]
        marker3 = marker_coordinates[2][i]
        marker4 = marker_coordinates[3][i]

        # Computing vectors between the markers
        vector1 = np.array(marker4) - np.array(marker1)
        vector2 = np.array(marker3) - np.array(marker2)

        # Computing the cross product to get the normal vector
        normal_vector = np.cross(vector1, vector2)

        # Normalizing the normal vector
        normal_vector /= np.linalg.norm(normal_vector)

        normal_vectors.append(normal_vector)

    return normal_vectors

# function to compute normal angles
def compute_normal_angles(normal_vectors):
    angles = []
    for normal_vector in normal_vectors:
        # Calculate angles with x, y, and z axes
        z_angle = np.degrees(np.arccos(np.dot(normal_vector, [0, 0, 1])))
        y_angle = np.degrees(np.arccos(np.dot(normal_vector, [0, 1, 0])))
        x_angle = np.degrees(np.arccos(np.dot(normal_vector, [1, 0, 0])))
        
        angles.append((x_angle, y_angle, z_angle))
    
    return angles

# function to plot
def plot_angle_over_time(angle, time_points, title):
    time_points = np.array(time_points)
    angle = np.array(angle)
    
    # Subtracting angle at time point 0 from all other time points
    angle_at_t0 = angle[0]
    angle = angle - angle_at_t0
    
    plt.figure(figsize=(10, 6))
    
    plt.plot(time_points, angle)
    
    plt.title(title)
    plt.xlabel('Zeitpunkt')
    plt.ylabel('Winkel (in Grad)')
    plt.xticks(time_points[::2])  
    
    plt.grid(True)
    plt.show()

# adding coordinates
marker_coordinates = [Marker1, Marker2, Marker3, Marker4]
# time steps
time_points = zeit

# computing normal vectors
normal_vectors = compute_normal_vectors(marker_coordinates)
# Computing angles
angles = compute_normal_angles(normal_vectors)
# Plotting angles over time
for i, axis_name in enumerate(['X-Achse', 'Y-Achse', 'Z-Achse']):
    plot_angle_over_time([angle[i] for angle in angles], time_points, f'Winkel zur {axis_name} über der Zeit')
python pandas angle
1个回答
0
投票

不确定您到底要什么,但如果您只是想知道应该在哪里输入数据,那就从一开始就可以。 在

dfraw = pd.read_csv('path_to_file, skiprows = [0,1,2,3,4,6,7], delimiter=',')
行,您必须将“path_to_file”参数替换为...文件的路径。由于方法是
pd.read_csv()
,因此您的文件必须是.csv 格式。

另请注意,目前您的代码在

path_to_file
之后缺少单引号。与您的
delimiter
一样,path_to_file 必须位于引号之间,以便解释器可以正确解析您的字符串。

© www.soinside.com 2019 - 2024. All rights reserved.