Generating coordinates in every 30 seconds

137 Views Asked by At

I want to write a function which can produce each circle coordinates in every 30 seconds. So far my code is like this:

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)

I am not sure what I am doing wrong, please help! Thank you.

Editing to provide more codes:

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

There are 1 best solutions below

3
LazyBum Q On

sched module allows you to queue tasks and run them one time after a specified delay. You could do it simple using while:

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

Also there is no point in incrementing i, python does it automatically.

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

If you want coords() to return list of all positions, then you don't have to reassign pos every cycle. Just append one pos to pos list.

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

And don't forget to return pos. Also your "current" doesn't change during cycles of "while". Since coords() returns list of positions, you can iterate over it:

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)