PART 6 树莓派小车+QT (TCP控制)

  1. 树莓派作为服务器的程序

(1)服务器tcp_server_socket程序

可以实现小车前进、后退、左转、右转、加减速(可能不行)

carMoveControl.py

python 复制代码
import  RPi.GPIO as GPIO
import time
import tty,sys,select,termios
import socket
import string
import threading
import carMove

run_car  = '1'  #鎸夐敭鍓?back_car = '2'  #鎸夐敭鍚?left_car = '3'  #鎸夐敭宸?right_car = '4' #鎸夐敭鍙?stop_car = '0'  #鎸夐敭鍋?#灏忚溅鐘舵€佸€煎畾涔?enSTOP = 0
enRUN =1
enBACK = 2
enLEFT = 3
enRIGHT = 4
enTLEFT =5
enTRIGHT = 6

 #灏忚溅鍜岃埖鏈虹姸鎬佸彉閲?g_CarState = 0
g_ServoState = 0
#灏忚溅閫熷害鍙橀噺


recvbuf = ""


        
try:
   
    #閫氳繃socket鍒涘缓鐩戝惉濂楁帴瀛楀苟璁剧疆涓洪潪闃诲妯″紡
    tcp_server_socket= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    #濉厖缁戝畾鏈嶅姟鍣ㄧ殑ip鍦板潃鍜岀鍙e彿
    #娉ㄦ剰锛氳繖閲屼竴瀹氳鏍规嵁鑷繁鐨勬爲鑾撴淳鐨刬p鍦板潃鏉ュ~port
    tcp_server_socket.bind(("", 6002))
    tcp_server_socket.listen(128)  
    #tcp_server_socket.setblocking(0)  

    #鐩戝惉瀹㈡埛绔殑杩炴帴
    while True:  
        print ("waiting for connection....")     
        tcp_client_socket,tcp_client_addr = tcp_server_socket.accept()
        print ("Start recv!")
        
        if tcp_server_socket:
            connectflag = 1
            print ("new user :%s " % str(tcp_client_addr))
                #璁剧疆杩炴帴濂楁帴瀛椾负闈為樆濉炵殑妯″紡骞跺皢杩炴帴鐨勫鎺ュ瓧鏀惧叆鍒扮洃鍚垪琛ㄤ腑

            #TCP鎺ュ彈鏁版嵁
        while True:
            recvbuf = tcp_client_socket.recv(128)
            print(recvbuf)
            
            global speed
            speed = 30

             #TCP鎺ュ彈鏁版嵁
            if recvbuf == b"$0,0,0,0,0,0,1,0,0,0#": # speed up
                speed += 5
                print ("Car Speed is : %d", speed)
                if speed > 100:
                    speed = 100 #灏忚溅鍔犻€?            elif recvbuf == b"$0,0,0,0,0,0,2,0,0,0#": # speed down
                speed -= 5
                if speed < 10:  #灏忚溅鍑忛€?                    speed= 10  
                print ("Car Speed is : %d", speed)

            elif recvbuf == b"$1,0,0,0,0,0,0,0,0,0#":
                carMove.goForward()
            elif recvbuf == b"$2,0,0,0,0,0,0,0,0,0#":
                carMove.goBackward()
            elif recvbuf == b"$3,0,0,0,0,0,0,0,0,0#":
                carMove.turnLeft()
            elif recvbuf == b"$4,0,0,0,0,0,0,0,0,0#":
                carMove.turnRight()            
            elif recvbuf == b"$0,0,0,0,0,0,0,0,0,0#":                    
                carMove.motorStop()  
                
           
            else:
                break
        tcp_client_socket.close()
        print ("Start recv over!")
       
    tcp_client_socket.close()
except KeyboardInterrupt:
    pass

GPIO.cleanup()

调用的小车程序 carMove.py

python 复制代码
import  RPi.GPIO as GPIO
import time
import tty,sys,select,termios


run_car  = '1'  #鎸夐敭鍓?back_car = '2'  #鎸夐敭鍚?left_car = '3'  #鎸夐敭宸?right_car = '4' #鎸夐敭鍙?stop_car = '0'  #鎸夐敭鍋? 

speed = 20

#璁剧疆GPIO鍙d负BCM缂栫爜鏂瑰紡
GPIO.setmode(GPIO.BCM)

#蹇界暐璀﹀憡淇℃伅
GPIO.setwarnings(False)

# AIN涓哄乏鍓嶈疆
P1_PWMA, P1_AIN1, P1_AIN2 = 26, 13,19
# AIN涓哄乏鍚庤疆
P1_PWMA1, P1_AIN11, P1_AIN22 = 17, 22, 27

# BIN涓哄彸鍓嶈疆
P2_PWMB, P2_BIN1, P2_BIN2 = 25, 24, 23
# BIN涓哄彸鍚庤疆
P2_PWMB1, P2_BIN11, P2_BIN22 = 16, 21, 20

#宸︿晶杞瓙

P1 = [P1_PWMA, P1_AIN1, P1_AIN2,
      P1_PWMA1, P1_AIN11, P1_AIN22]

#鍙充晶杞瓙
P2 = [P2_PWMB, P2_BIN1, P2_BIN2,
      P2_PWMB1, P2_BIN11, P2_BIN22]

#PWM浣跨敤10000Hz鐨勯鐜囨晥鏋滆緝濂斤紝杈撳叆鐨勮皟閫熷尯闂翠负0锝?00,浣嗚皟閫熶负5宸﹀彸鏃朵篃鏄彲浠ョЩ鍔↙_Up_Motor= GPIO.PWM(P1_PWMA,10000)
         
GPIO.setup(P1,GPIO.OUT)
GPIO.setup(P2,GPIO.OUT)
L_Up_Motor = GPIO.PWM(P1_PWMA,10000)
L_Up_Motor.start(0)
L_Down_Motor = GPIO.PWM(P1_PWMA1,10000)
L_Down_Motor.start(0)
R_Up_Motor= GPIO.PWM(P2_PWMB,10000)
 
R_Up_Motor.start(0) 

R_Down_Motor = GPIO.PWM(P2_PWMB1,10000)
R_Down_Motor.start(0)

      
# A 0 1 姝?   A1 1 0 姝?      B 0  1 姝?  B1  1  0姝? 
#宸﹁浆 A 0 0 鍙? A1 0 1 鍙?        B 0  1    B1   0    0    zuozhuan:zuocez fanzhuan, youce zhengzhuan
Diretion = [
    [0,0, 0,0, 0,0, 0,0],
    [0,1, 1,0, 0,1, 1,0],
    [1,0, 0,1, 1,0, 0,1],
    [1,0, 0,1, 0,1, 1,0],  
    [0,1, 1,0, 1,0, 0,1]  
    ]
#瀹氫箟鏂瑰悜  
      



def DIRETION(diretion):
        GPIO.output(P1_AIN1,diretion[0]) #AIN1
        GPIO.output(P1_AIN2,diretion[1]) #AIN2   P1_AIN2涓哄乏鍓嶈疆
        

        GPIO.output(P1_AIN11,diretion[2]) #AIN2   P1_AIN2涓哄乏鍚庤疆
        GPIO.output(P1_AIN22,diretion[3]) #AIN1
            
        GPIO.output(P2_BIN1,diretion[4]) #BIN2  BIN涓哄彸鍓嶈疆 
        GPIO.output(P2_BIN2,diretion[5]) #BIN1
            
        GPIO.output(P2_BIN11,diretion[6]) #BIN1     
        GPIO.output(P2_BIN22,diretion[7]) #BIN2  BIN涓哄彸鍚庤疆 
   

def reset():

        GPIO.output(13, GPIO.LOW)
        GPIO.output(19, GPIO.LOW)
        GPIO.output(22, GPIO.LOW)
        GPIO.output(27, GPIO.LOW)
        GPIO.output(24, GPIO.LOW)
        GPIO.output(23, GPIO.LOW)
        GPIO.output(21, GPIO.LOW)
        GPIO.output(20, GPIO.LOW)

#     A = {'s': 0, 'w':1, 'b':2, 'l':3,'r':4}


def goForward( ):
           
        DIRETION(Diretion[1])
        L_Up_Motor.ChangeDutyCycle(speed)
        L_Down_Motor.ChangeDutyCycle(speed)
        R_Up_Motor.ChangeDutyCycle(speed)
        R_Down_Motor.ChangeDutyCycle(speed)



def goBackward( ):
            
        DIRETION(Diretion[2])
        L_Up_Motor.ChangeDutyCycle(speed)
        L_Down_Motor.ChangeDutyCycle(speed)
        R_Up_Motor.ChangeDutyCycle(speed)
        R_Down_Motor.ChangeDutyCycle(speed)
        
def turnLeft( ):
        
    
        DIRETION(Diretion[3])
        L_Up_Motor.ChangeDutyCycle(speed)
        L_Down_Motor.ChangeDutyCycle(speed)
        R_Up_Motor.ChangeDutyCycle(speed)
        R_Down_Motor.ChangeDutyCycle(speed)

def turnRight( ):
           
        DIRETION(Diretion[4])
        L_Up_Motor.ChangeDutyCycle(speed)
        L_Down_Motor.ChangeDutyCycle(speed)
        R_Up_Motor.ChangeDutyCycle(speed)
        R_Down_Motor.ChangeDutyCycle(speed)
        
def motorStop( ):
    
        DIRETION(Diretion[0])
        L_Up_Motor.ChangeDutyCycle(0)
        L_Down_Motor.ChangeDutyCycle(0)
        R_Up_Motor.ChangeDutyCycle(0)
        R_Down_Motor.ChangeDutyCycle(0)

    
''' 
try:
    
    goForward()
    time.sleep(2)
    goBackward()
    time.sleep(2)
    turnLeft()
    time.sleep(2)
    turnRight()
    time.sleep(2)
    time.sleep(1)
       
    reset()
    GPIO.cleanup()


except KeyboardInterrupt:
    pass
'''
相关推荐
这儿有一堆花8 分钟前
从图像到精准文字:基于PyTorch与CTC的端到端手写文本识别实战
人工智能·pytorch·python
SunnyDays101128 分钟前
Python 高效实现 PDF 转 Word:告别手动复制粘贴
python·pdf转word·pdf转docx·pdf转doc·pdf到word转换
hhzz1 小时前
Pythoner 的Flask项目实践-绘制点/线/面并分类型保存为shpfile功能(Mapboxgl底图)
python·flask·gis·mapboxgl
Lxinccode1 小时前
python(42) : 监听本地文件夹上传到服务器指定目录
服务器·开发语言·python·文件上传服务器·监听文件上传服务器
我是华为OD~HR~栗栗呀2 小时前
前端面经-高级开发(华为od)
java·前端·后端·python·华为od·华为·面试
木头左2 小时前
跨周期共振效应在ETF网格参数适配中的应用技巧
开发语言·python·算法
爱蹦跶的精灵2 小时前
降级版本Pillow解决freetypefont has no attribute getsize问题
python·pillow
一人の梅雨3 小时前
亚马逊 MWS 关键字 API 实战:关键字搜索商品列表接口深度解析与优化方案
python·spring
唐叔在学习5 小时前
pip安装太慢?一键切换国内镜像源,速度飞起!
后端·python
Gz、5 小时前
Spring Boot 常用注解详解
spring boot·后端·python