【问题标题】:Generating coordinates in every 30 seconds每 30 秒生成一次坐标
【发布时间】:2021-10-17 00:44:54
【问题描述】:

我对 python 很陌生。我想编写一个函数,它可以在每 30 秒内生成每个圆坐标。 到目前为止我的代码是这样的:

import sched, time
import numpy as np
scheduler = sched.scheduler(time.time, time.sleep)
def coords():
   theta = np.linspace(0, 2*np.pi, 1000)
   radius = .5
   x = radius * np.sin( theta ) 
   y = radius * np.cos( theta ) 
   z = 4.2
   pos = []
   for i in range (0, 1000):
      pos = (x[i], y[i], z)
      i+=1
      #print(pos)

e1 = scheduler.enter(1, 1, coords)
e2 = scheduler.enter(30, 1, coords)

我不知道我做错了什么,请帮忙! 谢谢。

编辑以提供更多代码:

import time
import numpy as np

def distance(a, b):
    distance = np.sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])* 
    (a[1]- b[1])+(a[2]-b[2])*(a[2]-b[2]))
    return distance

def coords():
    theta = np.linspace(0, 2*np.pi, 1000)
    radius = .5
    x = radius * np.sin( theta ) 
    y = radius * np.cos( theta ) 
    z = 4.2
    pos = []
    for i in range (0, 1000):
       pos = (x[i], y[i], z)
 return pos
    
 def main():
    goal = np.array([3.6528202764153976, -7.055446756786874524, 
       8.52021764])
    current = coords()
    print(current)
    while True:
       dis = distance(goal,current)
       print(dis)
       time.sleep(3)

  if __name__ == "__main__":
       main()

【问题讨论】:

  • 它工作正常, (0.0, 0.5, 4.2) 9.438863921224838 每 3 秒打印一次。你想要 coords() 返回所有生成位置的列表(不仅仅是最后一个)吗?
  • 而在主()要电流以获得更接近目标像这样?:[0.0,0.5,4.2] 9.438863921224838 [0.0031447166580338753,0.4999901106593416,4.2] 9.437639450965634 [0.006289308919370529,0.49996044302856274,4.2] 9.436400082070367 [ 0.009433652392233545, 0.4999109982812366, 4.2] 9.435145857703839
  • 嗨@LazyBumQ,是的,我希望 coords() 每 3 秒返回一次所有生成位置(不仅仅是最后一个)的列表,以便 main() 可以计算与目标的距离。但是 coords() 只返回一个位置!有什么建议吗?
  • 更改了我的答案,这应该可以。询问您是否需要更多解释。

标签: python-3.x for-loop time coordinates sched


【解决方案1】:

sched 模块允许您将任务排队并在指定延迟后运行一次。 您可以使用 while:

while True:
    coords()
    time.sleep(30)

另外,增加 i 也没有意义,python 会自动完成。

for i in range (0, 1000):
    pos = (x[i], y[i], z)
    #i+=1

如果您希望 coords() 返回所有位置的列表,那么您不必在每个周期都重新分配 pos。只需将一个 pos 添加到 pos 列表中。

pos = []
for i in range(1000):
    pos.append((x[i], y[i], z))

别忘了返回 pos. 此外,您的“当前”在“while”周期内也不会改变。由于 coords() 返回位置列表,您可以对其进行迭代:

def main():
    goal = np.array([3.6528202764153976, -7.055446756786874524, 8.52021764])
    for current in coords(): 
        # current = coords() 
        print(current) 
        dis = distance(goal,current)
        print(dis)
        time.sleep(3)

【讨论】:

  • 感谢@LazyBum Q,我会试试这个。
  • 您好,当我从 main 调用 coords() 时,它只打印 pos 的最后一个值: (0.5, -1.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) (0.5, -1.2246467991e-2) (0.5, -1.2246467991e-2) 1.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) - 像这样。不知道我做错了什么。有什么建议吗?
  • 请看代码,我想每 30 秒用新坐标计算一次距离。导入时间 import numpy as np def distance(a, b): distance = np.sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]- b[1])*(a[1]-b[1])+(a[2]-b[2])*(a[2]-b[2])) 返回距离 def coords(): theta = np.linspace(0, 2*np.pi, 1000) 半径 = .5 x = 半径 * np.sin( theta ) y = 半径 * np.cos( theta ) z = 4.2 pos = [] for i in range (0, 1000): pos = (x[i], y[i], z) 返回 pos
  • 下一部分:def main(): 目标 = np.array([3.6528202764153976, -7.055446756786874524, 8.52021764]) current = coords() print(current) while True: dis = distance(goal,current ) print(dis) time.sleep(3) if name == "main": main()
  • 太棒了! @LazyBumQ 非常感谢!我为此苦苦挣扎了很久。我试图投票,但没有足够的声誉。
猜你喜欢
  • 2012-06-13
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
相关资源
最近更新 更多