Advertisement
Guest User

Motor driver

a guest
Mar 28th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. #!/usr/bin/python
  2. 'Motor driver program'
  3.  
  4. import socket
  5.  
  6. # Define movement commands, output pins, and states. + = pin on, - = pin off.
  7. cmds = {
  8.     'go_forward': ( 4, -5, -6,  7),
  9.     'go_back':    (-4,  5,  6, -7),
  10.     'turn_left':  (-4,  5, -6,  7),
  11.     'turn_right': ( 4, -5,  6, -7),
  12.     'stop':       (-4, -5, -6, -7),
  13. }
  14.  
  15. # Import and initialise the PiFace module
  16. import piface.pfio as pf
  17. pf.init()
  18.  
  19. def setout(val):
  20.     'Motor output function'
  21.     for p in val:
  22.         if p < 0:
  23.             pf.digital_write(-p, 0)
  24.         else:
  25.             pf.digital_write(p, 1)
  26.  
  27. # Set all relays to off
  28. setout('stop')
  29.  
  30. # Setup socket parameters
  31. HOST = ''
  32. PORT = 54321
  33.  
  34. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  35. s.bind((HOST, PORT))
  36. s.listen(1)
  37.  
  38. while True:
  39.     conn, addr = s.accept()
  40.     print 'Connected from', addr
  41.  
  42.     # Continous loop that waits for new data and acts depending
  43.     # on data content on it
  44.     while True:
  45.         data = conn.recv(1024).strip().lower()
  46.         print 'Received:', data if data else '<nothing>'
  47.         if not data: break
  48.  
  49.         if data in cmds:
  50.             setout(cmds[data])
  51.         else:
  52.             conn.send('unknown command')
  53.             print 'unknown command:', data
  54.  
  55.     conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement