Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import time
  2. import serial
  3. import requests
  4. from socketIO_client import SocketIO, LoggingNamespace
  5. import threading
  6. import winsound
  7.  
  8. #Sound file paths
  9. winSound = 'c:\\bigclaw-mega\sounds\completed.wav'
  10. pokeSound = 'c:\\bigclaw-mega\sounds\squeak'
  11.  
  12. #Serial Setup
  13. arduino = serial.Serial('COM6', 9600, timeout=5)
  14.  
  15. try:
  16. # Python 2
  17. import thread
  18. except:
  19. # Python 3
  20. import _thread as thread
  21.  
  22. # Listener listens for feedback from Arduino on the serial port
  23. def listener():
  24. while True:
  25. if arduino.inWaiting():
  26. data = arduino.readline().decode('ascii')[:-2]
  27. #data = arduino.readline(arduino.inWaiting()).decode('ascii')
  28. print(data)
  29. #IF PRIZE WIN
  30. if data == "prizeWin":
  31. print("Winner Winner Chicken Dinner!")
  32. winsound.PlaySound(winSound,winsound.SND_FILENAME)
  33. #IF POKE
  34. elif data == "Poke":
  35. winsound.PlaySound(pokeSound,winsound.SND_FILENAME)
  36. #arduino.flushInput()
  37.  
  38. # The Trigger below fires when a new (arg) happens
  39. def handle_command(args):
  40. #Commands
  41. #If Right Button Pressed
  42. if(args['command'].lower()=="r"):
  43. print(args['user'],"is moving right")
  44. arduino.write(b'r')
  45.  
  46. #If Left Button Pressed
  47. elif(args['command'].lower()=="l"):
  48. print(args['user'],"is moving left")
  49. arduino.write(b'l')
  50.  
  51. #If Up Button Pressed
  52. elif(args['command'].lower()=="f"):
  53. print(args['user'],"is moving forward")
  54. arduino.write(b'f')
  55.  
  56. #If Down Button Pressed
  57. elif(args['command'].lower()=="b"):
  58. print(args['user'],"is moving back")
  59. arduino.write(b'b')
  60.  
  61. #If Drop Button Pressed
  62. elif(args['command'].lower()=="g" and args['key_position'].lower()=="down"):
  63. currentUser = args['user']
  64. print(args['user'],"is dropping")
  65. arduino.write(b'g')
  66.  
  67. #If Poke Button Pressed
  68. elif(args['command'].lower()=="q" and args['key_position'].lower()=="down"):
  69. print(args['user'],"is poking")
  70. arduino.write(b'q')
  71.  
  72. def onHandleCommand(*args):
  73. thread.start_new_thread(handle_command, args)
  74.  
  75. #thread.start_new_thread(listener, args)
  76. threading.Thread(target=listener).start()
  77.  
  78. def onHandleControlConnect(*args):
  79. controlSocketIO.emit('robot_id', str(robotID))
  80.  
  81. robotID = ascascascascasc
  82. url = 'httpcasascascascascasc
  83. controlHostPort = requests.get(url).json()
  84.  
  85. controlSocketIO = SocketIO(controlHostPort['host'], controlHostPort['port'], LoggingNamespace, transports='websocket')
  86. controlSocketIO.on('connect', onHandleControlConnect)
  87. controlSocketIO.on('command_to_robot', onHandleCommand)
  88. controlSocketIO.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement