sudo apt install xboxdrv
sudo apt install python3-pygame
#使用手柄控制小车
使用serial控制
fe 轮子编号 f0 速度
如 fe 01 f0 128 第一个轮子 向前 速度为128
如 fe 01 fb 128 第一个轮子 后退 速度为128
#- coding: utf-8 -*
import serial
import time
import binascii
import sys
import pygame
import RPi.GPIO as GPIO
# 打开串口
#jetson-nano
ser = serial.Serial("/dev/ttyTHS1", 9600)
#raspberry 3b +
#ser = serial.Serial("/dev/ttyUSB0", 9600)
def init():
#ser = serial.Serial("/dev/ttyUSB0", 9600)
# 模块初始化
pygame.init()
pygame.joystick.init()
done = False
clock = pygame.time.Clock()
joystick_count = pygame.joystick.get_count()
print(joystick_count);
joystick = pygame.joystick.Joystick(0)
joystick.init()
name = joystick.get_name()
return done,name ,clock,joystick_count,joystick
def swClockwise(serial_object,wid,speed):
# speed = 20 + (speed/1.25)
serial_object.write((0xfe).to_bytes(1, byteorder='big'));
serial_object.write((wid).to_bytes(1, byteorder='big'));
serial_object.write((0xf0).to_bytes(1, byteorder='big'));
serial_object.write((speed).to_bytes(1, byteorder='big'));
# serial_object.write((0x00).to_bytes(1, byteorder='big'));
# serial_object.write((0x00).to_bytes(1, byteorder='big'));
# serial_object.write((0x00).to_bytes(1, byteorder='big'));
# serial_object.write((0x00).to_bytes(1, byteorder='big'));
# time.sleep(0.3);
return
def swCounterclockwise(serial_object,wid,speed):
# speed = 20 + (speed/1.25)
serial_object.write((0xfe).to_bytes(1, byteorder='big'));