Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. #All the import statemets
  2. import socket #For UDP
  3. import time #For time
  4. import android #Android environment
  5.  
  6. #Our Droid entity
  7. droid = android.Android()
  8.  
  9. #Some constants related to IP
  10. UDP_IP = '192.168.1.45'
  11. UDP_PORT = 8888
  12. INET_ADDR = (UDP_IP,UDP_PORT)
  13.  
  14. #Some constants relating to direction. These come from what I originally did on a keyboard
  15. MoveForward= "q"
  16. MoveBack = "z"
  17. MoveStop = "a"
  18. SteerLeft = "i"
  19. SteerRight = "p"
  20. SteerStop = "o"
  21.  
  22. #Some constants relating to thresholds. Can be used to tune sensitivity
  23. ForwardThresh = 0.3 #> This means you're tilted forward
  24. BackwardThresh = -0.3 #< This means you're tilted backwards
  25. LeftThresh = -0.4 #< This means you're tilted left
  26. RightThresh = 0.4 #> This means you're tilted right
  27.  
  28. ######################################
  29. #Main part of the code
  30. ######################################
  31.  
  32. #Initialise the movement and steering state
  33. MoveState = MoveStop
  34. SteerState = SteerStop
  35.  
  36. #Use these variables to get the current state
  37. CurrentMove = ""
  38. CurrentSteer= ""
  39.  
  40. #Print port information
  41. print "UDP target IP:", UDP_IP
  42. print "UDP target port:", UDP_PORT
  43.  
  44. #Set up the socket
  45. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  46.  
  47. #Start sensing the Android sensors. Has a timer to allow the sensors to spark up
  48. droid.startSensingTimed(1, 250)
  49. time.sleep(1)
  50.  
  51. #Loop, detecting state and sending UDP
  52. while True:
  53. #Use a try statement and add some error handling as well
  54. try:
  55. #Read the sensor state
  56. s1 = droid.sensorsReadOrientation().result
  57.  
  58. #The result is a list made up of float variables. Presented as a CSV on screen but we can mathematically manipulate
  59.  
  60. #Set up the current states based upon the thresholds defined as constants - First for forward and back
  61. if s1[1] > ForwardThresh:
  62. CurrentMove = MoveForward
  63. elif s1[1] < BackwardThresh:
  64. CurrentMove = MoveBack
  65. else: CurrentMove = MoveStop
  66.  
  67. #Now do it for left and right
  68. if s1[2] > RightThresh:
  69. CurrentSteer = SteerRight
  70. elif s1[2] < LeftThresh:
  71. CurrentSteer = SteerLeft
  72. else: CurrentSteer = SteerStop
  73.  
  74. #So we've got the current state, now check with the overall logged state. If it's changed, we do some UDP fun
  75. #First check the forward / back state
  76. if CurrentMove == MoveState:
  77. #Do nothing
  78. CurrentMove = CurrentMove #Seems necessary other you get an error
  79. else:
  80. #Now we need to send UDP and update the overall state
  81. MoveState = CurrentMove
  82. sock.sendto(CurrentMove, INET_ADDR)
  83. print "Forward / back state changed. Sent: " + CurrentMove
  84.  
  85. #Now the left / right state
  86. if CurrentSteer == SteerState:
  87. #Do nothing
  88. CurrentSteer = CurrentSteer #Seems necessary other you get an error
  89. else:
  90. #Now we need to send UDP and update the overall state
  91. SteerState = CurrentSteer
  92. sock.sendto(CurrentSteer, INET_ADDR)
  93. print "Left / right state changed. Sent: " + CurrentSteer
  94.  
  95. #Chill for a sec
  96. time.sleep(1)
  97. except Exception, err: #Handle exceptions
  98. #Write a screen with the error
  99. print "Got us an exception: " + str(err)
  100. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement