Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #All the import statemets
- import socket #For UDP
- import time #For time
- import android #Android environment
- #Our Droid entity
- droid = android.Android()
- #Some constants related to IP
- UDP_IP = '192.168.1.45'
- UDP_PORT = 8888
- INET_ADDR = (UDP_IP,UDP_PORT)
- #Some constants relating to direction. These come from what I originally did on a keyboard
- MoveForward= "q"
- MoveBack = "z"
- MoveStop = "a"
- SteerLeft = "i"
- SteerRight = "p"
- SteerStop = "o"
- #Some constants relating to thresholds. Can be used to tune sensitivity
- ForwardThresh = 0.3 #> This means you're tilted forward
- BackwardThresh = -0.3 #< This means you're tilted backwards
- LeftThresh = -0.4 #< This means you're tilted left
- RightThresh = 0.4 #> This means you're tilted right
- ######################################
- #Main part of the code
- ######################################
- #Initialise the movement and steering state
- MoveState = MoveStop
- SteerState = SteerStop
- #Use these variables to get the current state
- CurrentMove = ""
- CurrentSteer= ""
- #Print port information
- print "UDP target IP:", UDP_IP
- print "UDP target port:", UDP_PORT
- #Set up the socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- #Start sensing the Android sensors. Has a timer to allow the sensors to spark up
- droid.startSensingTimed(1, 250)
- time.sleep(1)
- #Loop, detecting state and sending UDP
- while True:
- #Use a try statement and add some error handling as well
- try:
- #Read the sensor state
- s1 = droid.sensorsReadOrientation().result
- #The result is a list made up of float variables. Presented as a CSV on screen but we can mathematically manipulate
- #Set up the current states based upon the thresholds defined as constants - First for forward and back
- if s1[1] > ForwardThresh:
- CurrentMove = MoveForward
- elif s1[1] < BackwardThresh:
- CurrentMove = MoveBack
- else: CurrentMove = MoveStop
- #Now do it for left and right
- if s1[2] > RightThresh:
- CurrentSteer = SteerRight
- elif s1[2] < LeftThresh:
- CurrentSteer = SteerLeft
- else: CurrentSteer = SteerStop
- #So we've got the current state, now check with the overall logged state. If it's changed, we do some UDP fun
- #First check the forward / back state
- if CurrentMove == MoveState:
- #Do nothing
- CurrentMove = CurrentMove #Seems necessary other you get an error
- else:
- #Now we need to send UDP and update the overall state
- MoveState = CurrentMove
- sock.sendto(CurrentMove, INET_ADDR)
- print "Forward / back state changed. Sent: " + CurrentMove
- #Now the left / right state
- if CurrentSteer == SteerState:
- #Do nothing
- CurrentSteer = CurrentSteer #Seems necessary other you get an error
- else:
- #Now we need to send UDP and update the overall state
- SteerState = CurrentSteer
- sock.sendto(CurrentSteer, INET_ADDR)
- print "Left / right state changed. Sent: " + CurrentSteer
- #Chill for a sec
- time.sleep(1)
- except Exception, err: #Handle exceptions
- #Write a screen with the error
- print "Got us an exception: " + str(err)
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement