Advertisement
Guest User

stepper/joystick

a guest
Jul 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import math
  2.  
  3. angle = 0
  4. quit = False
  5.    
  6. def update_stepper():
  7.     global angle #reads the global angle variable
  8.     while True:
  9.         if cur_angle < angle:
  10.             step_right() ##function to step the motor once in the right direction
  11.         elif(cur_angle > angle):
  12.             step_left() ##function to step the motor once in the right direction
  13.         else:
  14.             time.sleep(0.1)
  15.         if quit: #global variable to stop the code
  16.             break
  17.  
  18. def get_angle(x, y):
  19.      angle = math.atan2(y,x)/math.pi*180
  20.      return angle
  21.  
  22. def joystick():
  23.     global angle
  24.     while True
  25.         x, y = joysticl_get_x_and_y_function_here() ##function to get the joysticks x and y position
  26.         angle = get_angle(x, y) #updates the global angle variable
  27.         if quit: #global variable to stop the code
  28.             break
  29.  
  30. def main():
  31.     global angle
  32.     reset_stepper_to_0() ##function to reset the stepper motor to its 0th place if needed
  33.     t = threading.Thread(target=update_stepper) #creates thread to run the stepper motor
  34.     t.start() #starts the thread
  35.     t2 = threading.Thread(target=joystick) #creates thread to run the joystick and get the angle variable
  36.     t2.start()
  37.     while True:
  38.         print(angle)
  39.         time.sleep(0.25)
  40.         if quit: #global variable to stop the code
  41.             break
  42.      
  43. if __name__ == "__main__":
  44.     try:
  45.         main()
  46.     except KeyboardInterrupt:
  47.         quit = True
  48.     except:
  49.         raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement