序言
这部分主要是通过某些模块获取控制信息,输入单独写一个模块,以后想要调整成其他的设备只需要单独改一下这部分的代码
哦……目前模仿我这个小车前提要求要有个手柄233
正文
这部分主要是使用了pygame里面的joystick模块
joystick的学习教程
使用joystick简化只保留摇杆操作
最后贴上我添加的小车控制部分,使用了多线程(其实也不是很需要)
test_shoubing_noclass.py
import pygame
import threading
import time
#初始化
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock()
#车辆各个参数设置
car_max_speed=10 #车辆最大移动速度
car_max_turn=3 #车辆最大转向速度
camera_max_speed=10 #视角转移速度
done=False
left_speed=0 #车辆左半边轮子的速度
right_speed=0 #车辆右半边轮子的速度
vis_v_par=0 #摄像头舵机水平转向速度
vis_v_hor=0 #摄像头舵机垂直转向速度
def shoubin_thread():
global left_speed
global right_speed
global vis_v_par
global vis_v_hor
#寻找电脑上的手柄数量,一般我只连了一个手柄(因为只有一个)
joystick_count = pygame.joystick.get_count()
for i in range(joystick_count):
#读取对应设备
joystick = pygame.joystick.Joystick(i)
joystick.init()
while (not done ):
# 这句话很重要,这是保障实时读取手柄摇杆信息,反正不能去掉
pygame.event.get()
#获取摇杆数量
axes = joystick.get_numaxes()
ls=0
rs=0
vps = 0
vhs = 0
for i in range(axes):
axis = joystick.get_axis(i)
if i==0:
# print("zhuanxiang ",axis*car_max_turn)
ls+=axis*car_max_turn
rs-=axis*car_max_turn
if i==1:
# print("zhixing ",-axis*car_max_speed)
ls-=axis*car_max_speed
rs -= axis * car_max_speed
if i==4:#上是负数
vps=axis*camera_max_speed
if i == 3: # 左是负数
vhs = -axis * camera_max_speed
# print(i,axis)
left_speed = ls
right_speed = rs
vis_v_par = vps
vis_v_hor = vhs
# print('左轮速度 = ', left_speed, " 右轮速度 = ", right_speed)
# print('相机水平移动速度 = ', vis_v_par, " 相机垂直移动速度 = ", vis_v_hor)
clock.tick(20)
def print_thread():
while (not done):
print('================')
print('左轮速度 = ',left_speed," 右轮速度 = ",right_speed)
print('相机水平移动速度 = ', vis_v_par, " 相机垂直移动速度 = ", vis_v_hor)
def get_shoubing_info():
return left_speed,right_speed,vis_v_par,vis_v_hor
t1 =threading.Thread(target=shoubin_thread)
t1.start()
# t2 =threading.Thread(target=print_thread)
# t2.start()
如何运行
1.最简单的,将最后面t2的注释去掉就好了
2.亦或者通过调用的方法
import test_shoubing_noclass#这里放上面的文件的名字
while(1):
print(test_shoubing_noclass.get_shoubing_info())
对代码的简单补充
因为树莓派小车是四轮固定的,没有转向轮,所以转向是通过两边轮胎的差速来达成转向
所以我将两边轮胎的转速分开控制