【问题标题】:How can we get the coordinates with the help of distance traveled and direction of movement?我们如何借助行进距离和运动方向获得坐标?
【发布时间】:2020-02-01 14:45:09
【问题描述】:

假设我有一个数组

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)。可以编写什么代码,以便可以从上面给定的数组中生成这些坐标。

【问题讨论】:

  • 你必须先尝试一下,然后再让别人为你解决你的作业。
  • 已经在努力了,伙计!没有一个人会把他的责任推给别人!
  • 这被证明比我有点棘手,但我想我会在大约 10 分钟内完成它......有点像在笛卡尔坐标中做线性代数以获得乐趣。
  • 好的!哈哈 ikr!
  • 前10cm是x方向还是y方向??

标签: python arrays list coordinates coordinate-systems


【解决方案1】:

仔细研究,直到你弄明白为止,哈哈。

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()

【讨论】:

  • 我为初学者做了一些可能很有趣的事情,比如在 Rotation_Matrix 条目中使用 round() 函数(这只是因为浮点数不精确。应该是 0 的东西变成了 1.6e-17或者其他的东西)。如果您有一些问题,请直接问他们。似乎模组正在让这个滑动,所以问吧。
猜你喜欢
  • 2018-11-17
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 2020-01-17
  • 2011-02-27
  • 1970-01-01
相关资源
最近更新 更多