You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WheelchairAdapter.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # imports
  2. from socket import *
  3. import threading
  4. import queue
  5. import time
  6. # connect to BCI2000 in a new thread
  7. class BCI2000ReceiverThread(threading.Thread):
  8. def __init__(self, id, phaseInSequenceQueue, StimulusCodeQueue):
  9. threading.Thread.__init__(self, name="BCI2000ReceiverThread%d" % (id,))
  10. self.phaseInSequenceQueue=phaseInSequenceQueue
  11. self.StimulusCodeQueue=StimulusCodeQueue
  12. self.running=True
  13. self.firstData = True
  14. self.repeat = 0
  15. #udp settings
  16. self.host = "localhost"
  17. self.port = 5003 # BCI2000
  18. self.UNITY_PORT = 5002 # unity
  19. self.FEATHER_PORT = 5001 # feather gui
  20. self.go = True
  21. self.buf = 1024
  22. self.addr = (self.host,self.port)
  23. #connect to udp socket
  24. self.UDPSock = socket(AF_INET,SOCK_DGRAM)
  25. self.UDPSock.bind(self.addr) # BIND to LISTEN
  26. print('%s initialized to %s at port %s' % (self.getName(), self.host, self.port))
  27. print('Relaying SelectedTargets to port -- %s ---' % self.UNITY_PORT)
  28. self.relay_unity = socket(AF_INET,SOCK_DGRAM) # UDP RELAY TO UNITY
  29. print('Relaying all to port -- %s ---' % self.FEATHER_PORT)
  30. self.relay_feather = socket(AF_INET,SOCK_DGRAM) # UDP RELAY TO FEATHER
  31. def kill(self):
  32. self.running=False
  33. log('%s terminated' % (self.getName()))
  34. def run(self):
  35. # define a new contains method comparing strings
  36. def contains(theString, theQueryValue):
  37. return theString.find(theQueryValue) > -1
  38. while self.running:
  39. # try to receive from BCI2000 AppConnector
  40. try:
  41. self.data,self.addr = self.UDPSock.recvfrom(self.buf)
  42. #print(self.data) #####
  43. if self.firstData:
  44. print ("UDP is receiving...")
  45. self.firstData=0
  46. if "SelectedTarget 0" in self.data:
  47. self.go = True
  48. if "SelectedTarget" in self.data and "SelectedTarget 0" not in self.data and self.go == True:
  49. print("Relaying Command to Unity: ", self.data)
  50. self.relay_unity.sendto(self.data, (self.host, self.UNITY_PORT))
  51. self.go = False
  52. if contains(self.data, "PhaseInSequence"):
  53. phaseInSequence=int(self.data.replace('PhaseInSequence ',''))
  54. self.phaseInSequenceQueue.put(phaseInSequence)
  55. #print (phaseInSequence)
  56. if contains(self.data, "StimulusCode "):
  57. print("Relaying Command to TactileBCIfilter: ", self.data)
  58. self.relay_feather.sendto(self.data, (self.host, self.FEATHER_PORT))
  59. StimulusCode=int(self.data.replace('StimulusCode ',''))
  60. if not StimulusCode == 0 and self.repeat == 0:
  61. self.repeat = 1
  62. self.StimulusCodeQueue.put(StimulusCode)
  63. self.Code = StimulusCode
  64. elif StimulusCode == 0:
  65. self.repeat = 0
  66. # print (StimulusCode)
  67. # wait a short time and try again
  68. except error:
  69. time.sleep(0.0001)
  70. print('sleeping')
  71. continue
  72. # def run(self):
  73. # a = 1
  74. def returnProgramStatus(self):
  75. return self.running
  76. ### M A I N P R O G R A M M ###
  77. # # initialize queue
  78. # phaseInSequenceQueue = queue.Queue()
  79. # StimulusCodeQueue = queue.Queue()
  80. # RunApplication = True
  81. # #start BCI2000ReceiverThread
  82. # BCI2000ReceiverThread1=BCI2000ReceiverThread(1, phaseInSequenceQueue, StimulusCodeQueue)
  83. # BCI2000ReceiverThread1.start()