0

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()
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • It's working, (0.0, 0.5, 4.2) 9.438863921224838 is printed every 3 seconds. Do you want coords() return list of all generated positions(not just the last one)? – LazyBum Q Aug 14 '21 at 21:58
  • And in main() you want current to get closer to goal like so?: [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 – LazyBum Q Aug 14 '21 at 22:11
  • Hi @LazyBumQ, Yes, I want coords() return list of all generated positions(not just the last one) in every 3 seconds, so that the main() can calculate the distance from goal. But the coords() only returns one position! Any advise? – sumita1998 Aug 15 '21 at 09:53
  • Changed my answer, this should work. Ask if you need more explanation. – LazyBum Q Aug 15 '21 at 10:04

1 Answers1

0

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)
LazyBum Q
  • 257
  • 4
  • 12
  • Hi again, when I am calling the coords() from main it only prints the last value of 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.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) (0.5, -1.2246467991473532e-16, 4.2) - like this. not sure what I am doing wrong. Any advice? – sumita1998 Aug 14 '21 at 16:30
  • Please see the code, I wanted to calculate distance with new coordinates in every 30 seconds. 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 – sumita1998 Aug 14 '21 at 21:32
  • next part: 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() – sumita1998 Aug 14 '21 at 21:32