繁体   English   中英

我们如何借助行进距离和运动方向获得坐标?

[英]How can we get the coordinates with the help of distance traveled and direction of movement?

假设我有一个数组

sensor_data=[10,0,5,1,10,1,20,1,20,1,15]

现在在这个:

0 表示机器人/无人机已右转并且

1 表示机器人/无人机左转。

其余的数字是行进的距离。

所以根据上面的数组,首先机器人/无人机行进了 10 厘米的距离。 然后右转。 右转后,机器人/无人机行进 5 厘米,然后向左转。 左转后行驶 10 厘米,依此类推。 所以最初机器人/无人机在 (0,0) 处。 然后它直线行进,即沿 y 方向。

因此坐标将是(0,10)。 然后在右转和行驶 5 厘米后,坐标将为 (-5,10)。 按照这样的模式,其余的坐标是:(-5,20)、(15,20) 和 (15,0)。 可以编写什么代码,以便可以从上面给定的数组中生成这些坐标。

咀嚼这个直到你弄清楚lol。

import numpy as np
from numpy import cos,sin,pi
import matplotlib.pyplot as plt

# Convert data into floats, for 
sensor_data = tuple(map(lambda x: float(x),[10,0,5,1,10,1,20,1,20,1,15]))

# Start at 0,0 in a 2D plane and start out in x-Direction
Starting_Position = np.array((0.,0.))
Starting_Direction = np.array((1.,0.))



def Rotation_Matrix(direction):

    '''Can be expanded for any angle of rotation in a 2D plane. Google rotation matrix in 2D space.'''

    a = {'left':pi/2,'right':-pi/2}[direction]

    matrix = np.array(((round(cos(a),7),round(-sin(a),7)),
                       (round(sin(a),7),round(cos(a),7))))

    return matrix



def DronePosition(Number_input,Current_Position,Current_Direction):

    if Number_input == 1.:
        New_Direction = Current_Direction.dot(Rotation_Matrix('left'))
        New_Position = Current_Position
    elif Number_input == 0.:
        New_Direction = Current_Direction.dot(Rotation_Matrix('right'))
        New_Position = Current_Position
    else:
        New_Position = Current_Position + Current_Direction*Number_input
        New_Direction = Current_Direction


    return New_Position,New_Direction

Drone_Path = np.zeros(shape=(len(sensor_data),2))

for step in range(len(sensor_data)):
    Drone_Path[step,0] = Starting_Position[0] 
    Drone_Path[step,1] = Starting_Position[1] 
    Starting_Position, Starting_Direction = DronePosition(sensor_data[step],Starting_Position,Starting_Direction)


fig, ax = plt.subplots(figsize=(6,6))

ax.plot(Drone_Path[:,0],Drone_Path[:,1])
plt.show()

暂无
暂无

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

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