参照了lessisawesome的文章,http://blog.csdn.net/maninbehind/article/details/9715137
小车端(即树莓派),记得装RPI GPIO python
import RPi.GPIO as GPIO #GPIO package from socket import * import sys import time #import car # for example # GPIO.setmode(GPIO.BOARD) #set mode # GPIO.setup(13, GPIO.OUT) #set gpio in or out # GPIO.setup(15, GPIO.OUT) #set gpio in or out # GPIO.output(13, GPIO.HIGH) #set out HIGH or LOW # GPIO.output(15, GPIO.LOW) #set out HIGH or LOW class Wheel: pins ={‘a‘:[13,15],‘b‘:[16,18],‘c‘:[19,21],‘d‘:[22,24]}# four (wheel-gpio prot) dict def __init__(self,name): self.name = name self.pin = Wheel.pins[self.name] GPIO.setmode(GPIO.BOARD) GPIO.setup(self.pin[0],GPIO.OUT) GPIO.setup(self.pin[1],GPIO.OUT) self.stop() def forward(self): GPIO.output(self.pin[0],GPIO.HIGH) GPIO.output(self.pin[1],GPIO.LOW) def stop(self): GPIO.output(self.pin[0],False) GPIO.output(self.pin[1],False) def back(self): GPIO.output(self.pin[0],False) GPIO.output(self.pin[1],True) class Car: wheels=[Wheel(‘a‘),Wheel(‘b‘),Wheel(‘c‘),Wheel(‘d‘)] @staticmethod def init(): GPIO.setmode(GPIO.BOARD) @staticmethod def forward(): for wheel in Car.wheels: wheel.forward() @staticmethod def back(): for wheel in Car.wheels: wheel.back() @staticmethod def left(): Car.wheels[0].forward() Car.wheels[1].forward() Car.wheels[3].back() Car.wheels[2].back() @staticmethod def right(): Car.wheels[2].forward() Car.wheels[3].forward() Car.wheels[0].back() Car.wheels[1].back() @staticmethod def stop(): Car.wheels[0].stop() Car.wheels[1].stop() Car.wheels[3].stop() Car.wheels[2].stop() ######################################################### commands ={‘forward‘:Car.forward, ‘back‘:Car.back, ‘stop‘:Car.stop, ‘left‘:Car.left, ‘right‘:Car.right } def execute(command): print command commands[command]() HOST =‘10.1.1.190‘ #the ip of rapberry pi PORT = 8888 s= socket(AF_INET, SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) print (‘listening on 8888‘) while 1: conn, addr = s.accept() print (‘Connected by:‘, addr) while 1: command= conn.recv(1024).replace(‘\n‘,‘‘) if not command:break execute(command) conn.close()
手机端,记得装qpython这个python环境
#-*- coding:utf-8 -*- import kivy from kivy.lang import Builder from kivy.uix.gridlayout import GridLayout from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.button import Button from socket import * host = ‘10.1.1.190‘ port = 8888 bufsiz =1024 addr = (host, port) def SendComand(cmd): s=socket(AF_INET, SOCK_STREAM) s.connect(addr) s.send(cmd) s.close Builder.load_string(‘‘‘ <CarControlScreen>: cols: 1 Button: text: ‘forward‘ on_release: root.forward() Button: text: ‘back‘ on_release: root.back() Button: text: ‘left‘ on_release: root.left() Button: text: ‘right‘ on_release: root.right() Button: text: ‘stop‘ on_release: root.stop() ‘‘‘) class CarControlScreen(GridLayout): def left(self): print "Left" SendComand(‘left‘) def right(self): print "Right" SendComand(‘right‘) def forward(self): print "forward" SendComand(‘forward‘) def back(self): print "back" SendComand(‘back‘) def stop(self): print "Stop" SendComand(‘stop‘) class myApp(App): def build(self): return CarControlScreen() if __name__ == ‘__main__‘: myApp().run()