1. 程式人生 > >樹莓派小車By 樹莓派愛好者ITJoker(通過python socket通信實現樹莓派視頻小車)

樹莓派小車By 樹莓派愛好者ITJoker(通過python socket通信實現樹莓派視頻小車)

Coding 以及 title 密碼 command python false sin pla

本文由樹莓派愛好者ITJoker 編輯,轉載請註明出處。本人也有新浪博客同樣是樹莓派愛好者ITJoker可能名字少了IT。。。

所需材料:樹莓派2B或者2B以上,L2985n驅動板,若幹排線,電池及電池盒,usb無線網卡(免驅動),usb攝像頭(免驅動)。 所需環境:raspberry-gpio-python,以及安裝mjpg-streamer及所需的環境。(這裏就不多說了,等日後博主有時間增加這些環境的配置內容) 好了接下來開始我們的行動。 首先,開始我們先用4根母對母杜邦線分別連接L2985n的IN1,IN2,IN3,IN4然後對上樹莓派的GPIO接口11,12,13,15。(IN1=12,IN2=11,IN3=13,IN4=15) IN1,IN2控制的是OUT1,OUT2的馬達 IN3,IN4控制的是OUT3,OUT4的馬達 (註意,馬達接線要左邊的與左邊的交叉相接,即左前輪和左後輪交叉相接。以此類推右邊也是如此。) 樹莓派PIN腳圖借鑒下樹莓派實驗室的。(由於博主懶,詳細的接線方法沒上圖。)

技術分享

技術分享

接線完畢後,通過網絡上的方法用ssh 進入到樹莓派 輸入命令:(將服務端代碼復制上去)
sudo nano start.py

然後Ctrl+x再輸入Y加回車即可。

輸入命令:

sudo python start.py

即可運行!

附下小車服務端控制代碼:(請勿用於其他用途,使用時請註明轉載處及作者:樹莓派愛好者ITJoker技術分享
 1 ###########################################
 2 #Writer: Joker                                                                       #
3 #Time: 2017.6.25 # 4 ########################################### 5 # -*- coding: utf-8 -*- 6 import RPi.GPIO as GPIO 7 from socket import * 8 import time 9 def t_init(): 10 GPIO.setmode(GPIO.BOARD) 11 GPIO.setwarnings(False)
12 GPIO.setup(11,GPIO.OUT) 13 GPIO.setup(12,GPIO.OUT) 14 GPIO.setup(13,GPIO.OUT) 15 GPIO.setup(15,GPIO.OUT) 16 def t_stop(): 17 GPIO.output(11, False) 18 GPIO.output(12, False) 19 GPIO.output(13, False) 20 GPIO.output(13, False) 21 GPIO.output(15, False) 22 time.sleep(1) 23 GPIO.cleanup() 24 def t_up(): 25 t_init() 26 GPIO.output(11, True) 27 GPIO.output(12, False) 28 GPIO.output(13, True) 29 GPIO.output(15, False) 30 time.sleep(1) 31 GPIO.cleanup() 32 33 def t_down(): 34 t_init() 35 GPIO.output(11, False) 36 GPIO.output(12, True) 37 GPIO.output(13, False) 38 GPIO.output(15, True) 39 time.sleep(1) 40 GPIO.cleanup() 41 42 def t_right(): 43 t_init() 44 GPIO.output(11, False) 45 GPIO.output(12, True) 46 GPIO.output(13, True) 47 GPIO.output(15, False) 48 time.sleep(1) 49 GPIO.cleanup() 50 def t_left(): 51 t_init() 52 GPIO.output(11, True) 53 GPIO.output(12, False) 54 GPIO.output(13, False) 55 GPIO.output(15, True) 56 time.sleep(1) 57 GPIO.cleanup() 58 ######################################################### 59 #Writer: Joker # 60 #Time: 2017.6.25 # 61 ######################################################### 62 def commands (cmd): 63 if cmd == d: 64 t_down() 65 elif cmd == s: 66 t_stop() 67 elif cmd == u: 68 t_up() 69 elif cmd == l: 70 t_left() 71 elif cmd == r: 72 t_right() 73 def execute(command): 74 print command 75 commands(command) 76 HOST =192.168.1.103 #the ip of raspberry pi 77 PORT = 8888 #the port of raspberry pi 78 s= socket(AF_INET, SOCK_STREAM) 79 s.bind((HOST, PORT)) 80 s.listen(1000) 81 print (listening on 8888) 82 while 1: 83 conn, addr = s.accept() 84 print (Connected by:, addr) 85 while 1: 86 command= conn.recv(20).replace(\n,‘‘) 87 if command == g: 88 conn.close 89 break 90 elif not command:break 91 a=bytearray(command) 92 execute(a) #command 93 conn.close() 94 ############################################################################

附上小車電腦控制端鏈接:鏈接:http://pan.baidu.com/s/1jHXZvD0 密碼:0lbv 好了接下來就可以玩自己制作的視頻小車了 需要安卓控制端可以聯系QQ:649258008

樹莓派小車By 樹莓派愛好者ITJoker(通過python socket通信實現樹莓派視頻小車)