行空板搬运车
【项目背景】在别人的仓库里找到一台带机械爪的车,说明书上说主控可用Arduino IDE编程,技术支持可访问官网,别的就没有了,可官网也访问不了,公司倒闭了?最怕遇到这种情况。那主控用不了,我来改造一下吧。
【项目设计】
使用行空板做为主控,电机驱动使用DF的L289N电机驱动。使用行空板的原因,是因为这套设备有Xbox遥控器,我会使用行空板连接Xbox遥控器{:6_202:}。
【硬件连接】
1.Xbox遥控器无线接收器插入行空板USB接口。
2.电机驱动器
因为搬运车有五个电机,两个用于控制车运动,三个用于控制机械爪,水平旋转、上下运动、爪张开与闭合。所以我使用了三个L289N电机驱动,一个驱动控制两个电机。
3.通过扩展板与电机驱动板连接
4.行空板插入扩展板
5.供电
充电宝为行空板供电,7.4V锂电池为电机驱动板供电,驱动板、扩展板、电源三者要共地。
6.整机
【编写程序】
1.使用pygame库,连接Xbox蓝牙手柄。
import pygame
import time
pygame.init()
pygame.joystick.init()
# 初始化手柄
joystick = pygame.joystick.Joystick(0)
joystick.init()2.使用Pinpong库,初始化行空板引脚
from pinpong.extension.unihiker import *
from pinpong.libs.microbit_motor import DFMotor
from pinpong.board import Board
from pinpong.board import Pin
Board().begin()
p_p8_pwm=Pin(Pin.P8, Pin.PWM)
p_p9_pwm=Pin(Pin.P9, Pin.PWM)
p_p5_out=Pin(Pin.P5, Pin.OUT)
p_p6_out=Pin(Pin.P6, Pin.OUT)
p_p16_pwm=Pin(Pin.P16, Pin.PWM)
p_p0_pwm=Pin(Pin.P0, Pin.PWM)
p_p3_pwm=Pin(Pin.P3, Pin.PWM)
p_p13_out=Pin(Pin.P13, Pin.OUT)
p_p15_out=Pin(Pin.P15, Pin.OUT)
p_p12_out=Pin(Pin.P12, Pin.OUT)3.运动控制函数
def forward(speed):#带速度参数——前进
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(1)
p_p6_out.write_digital(1)
def back(speed):#带速度参数——后退
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(0)
p_p6_out.write_digital(0)
def left(speed):#带速度参数——向左
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(0)
p_p6_out.write_digital(1)
def right(speed):#带速度参数——向右
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(1)
p_p6_out.write_digital(0)
def stop():#停止
p_p8_pwm.write_analog(0)#左电机
p_p9_pwm.write_analog(0)#右电机
p_p16_pwm.write_analog(0)#水平电机
p_p0_pwm.write_analog(0)#垂直电机
p_p3_pwm.write_analog(0)#夹电机
def Lrotate(speed):#带速度参数——水平向左原地旋转
p_p0_pwm.write_analog(speed)
p_p13_out.write_digital(1)
def Rrotate(speed):#带速度参数——水平向右原地旋转
p_p0_pwm.write_analog(speed)
p_p13_out.write_digital(0)
def up(speed):#带速度参数——垂直向上
p_p16_pwm.write_analog(speed)#左电机
p_p15_out.write_digital(0)
def down(speed):#带速度参数——垂直向下
p_p16_pwm.write_analog(speed)
p_p15_out.write_digital(1)
def Open(speed):#带速度参数——夹打开
p_p3_pwm.write_analog(speed)
p_p12_out.write_digital(0)
def Close(speed):#带速度参数——夹关闭
p_p3_pwm.write_analog(speed)
p_p12_out.write_digital(1)
4.手柄按键事件
while True:
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN:
if event.button==0:#"A"键
print("“A”键")
elif event.button==1:#"B"键
print("“B”键")
elif event.button==4:#"Y"键
print("“Y”键")
elif event.button==3:#"X"键
print("“X”键")
elif event.button==6:#"RB"键,右肩键:Right Shoulder Button
print("“RB”键")
elif event.button==7:#"LB"键,左肩键:Left Shoulder Button
print("“LB”键")
elif event.button==10:#"View"键
print("“View”键")
elif event.button==11:#"Menu"键
print("“Menu”键")
elif event.button==15:#“共享”按键
print("“共享”键")
elif event.button==12:#“xbox”按键
print("“xbox”键")
elif event.type == pygame.JOYHATMOTION:#方向键
if event.hat==0:
print(str(event.value)+":"+str(event.value))
if event.value==0:
if event.value==0:#中间位置
print("“中间位置”方向键")
stop()
elif event.value==1:#向上
print("“向上”方向键")
forward(800)
elif event.value==-1:#向下
print("“向下”方向键")
back(800)
elif event.value==-1:#向右
print("“向右”方向键")
right(800)
elif event.value==1:#向左
print("“向左”方向键")
left(800)
elif event.type == pygame.JOYAXISMOTION:
#左摇杆按键
if event.axis==0:#左右
if event.value<-0.1:#向左
left(int(abs(event.value)*1000))
print("“向左”左摇杆按键")
elif event.value>0.1:#向右
right(int(event.value*1000))
print("“向右”左摇杆按键")
else:
stop()
print("左右stop左摇杆按键")
elif event.axis==1:#上下
if event.value<-0.1:#向上
forward(int(abs(event.value)*1000))
print("“向上”左摇杆按键")
elif event.value>0.1:#向下
back(int(event.value*1000))
print("“向下”左摇杆按键")
else:
stop()
print("上下stop左摇杆按键")
#右摇杆按键
if event.axis==2:#左右
if event.value<-0.1:#向右
Rrotate(int(abs(event.value)*1000))
print("“向右”右摇杆按键")
elif event.value>0.1:#向左
Lrotate(int(event.value*1000))
print("“向左”右摇杆按键")
else:
stop()
print("“停止”右摇杆按键")
elif event.axis==3:#上下
if event.value<-0.1:#向上
up(int(abs(event.value)*1000))
print("“向上”右摇杆按键")
elif event.value>0.1:#向下
down(int(event.value*1000))
print("“向下”右摇杆按键")
else:
stop()
elif event.axis==5:#左射击键
if event.value>0:
print("左射击键")
print(int(event.value*1000))
Open(int(event.value*1000))
else:
stop()
elif event.axis==4:#右射击键
if event.value>0:
print("右射击键")
print(int(event.value*1000))
Close(int(event.value*1000))
else:
stop()
4.完整程序
import pygame
import time
pygame.init()
pygame.joystick.init()
# 初始化手柄
joystick = pygame.joystick.Joystick(0)
joystick.init()
from pinpong.extension.unihiker import *
from pinpong.libs.microbit_motor import DFMotor
from pinpong.board import Board
from pinpong.board import Pin
Board().begin()
p_p8_pwm=Pin(Pin.P8, Pin.PWM)
p_p9_pwm=Pin(Pin.P9, Pin.PWM)
p_p5_out=Pin(Pin.P5, Pin.OUT)
p_p6_out=Pin(Pin.P6, Pin.OUT)
p_p16_pwm=Pin(Pin.P16, Pin.PWM)
p_p0_pwm=Pin(Pin.P0, Pin.PWM)
p_p3_pwm=Pin(Pin.P3, Pin.PWM)
p_p13_out=Pin(Pin.P13, Pin.OUT)
p_p15_out=Pin(Pin.P15, Pin.OUT)
p_p12_out=Pin(Pin.P12, Pin.OUT)
bs=0
time1=0
time2=0
def forward(speed):#带速度参数——前进
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(1)
p_p6_out.write_digital(1)
def back(speed):#带速度参数——后退
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(0)
p_p6_out.write_digital(0)
def left(speed):#带速度参数——向左
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(0)
p_p6_out.write_digital(1)
def right(speed):#带速度参数——向右
p_p8_pwm.write_analog(speed)
p_p9_pwm.write_analog(speed)
p_p5_out.write_digital(1)
p_p6_out.write_digital(0)
def stop():#停止
p_p8_pwm.write_analog(0)#左电机
p_p9_pwm.write_analog(0)#右电机
p_p16_pwm.write_analog(0)#水平电机
p_p0_pwm.write_analog(0)#垂直电机
p_p3_pwm.write_analog(0)#夹电机
def Lrotate(speed):#带速度参数——水平向左原地旋转
p_p0_pwm.write_analog(speed)
p_p13_out.write_digital(1)
def Rrotate(speed):#带速度参数——水平向右原地旋转
p_p0_pwm.write_analog(speed)
p_p13_out.write_digital(0)
def up(speed):#带速度参数——垂直向上
p_p16_pwm.write_analog(speed)#左电机
p_p15_out.write_digital(0)
def down(speed):#带速度参数——垂直向下
p_p16_pwm.write_analog(speed)
p_p15_out.write_digital(1)
def Open(speed):#带速度参数——夹打开
p_p3_pwm.write_analog(speed)
p_p12_out.write_digital(0)
def Close(speed):#带速度参数——夹关闭
p_p3_pwm.write_analog(speed)
p_p12_out.write_digital(1)
while True:
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN:
if event.button==0:#"A"键
print("“A”键")
elif event.button==1:#"B"键
print("“B”键")
elif event.button==4:#"Y"键
print("“Y”键")
elif event.button==3:#"X"键
print("“X”键")
elif event.button==6:#"RB"键,右肩键:Right Shoulder Button
print("“RB”键")
elif event.button==7:#"LB"键,左肩键:Left Shoulder Button
print("“LB”键")
elif event.button==10:#"View"键
print("“View”键")
elif event.button==11:#"Menu"键
print("“Menu”键")
elif event.button==15:#“共享”按键
print("“共享”键")
elif event.button==12:#“xbox”按键
print("“xbox”键")
elif event.type == pygame.JOYHATMOTION:#方向键
if event.hat==0:
print(str(event.value)+":"+str(event.value))
if event.value==0:
if event.value==0:#中间位置
print("“中间位置”方向键")
stop()
elif event.value==1:#向上
print("“向上”方向键")
forward(800)
elif event.value==-1:#向下
print("“向下”方向键")
back(800)
elif event.value==-1:#向右
print("“向右”方向键")
right(800)
elif event.value==1:#向左
print("“向左”方向键")
left(800)
elif event.type == pygame.JOYAXISMOTION:
#左摇杆按键
if event.axis==0:#左右
if event.value<-0.1:#向左
left(int(abs(event.value)*1000))
print("“向左”左摇杆按键")
elif event.value>0.1:#向右
right(int(event.value*1000))
print("“向右”左摇杆按键")
else:
stop()
print("左右stop左摇杆按键")
elif event.axis==1:#上下
if event.value<-0.1:#向上
forward(int(abs(event.value)*1000))
print("“向上”左摇杆按键")
elif event.value>0.1:#向下
back(int(event.value*1000))
print("“向下”左摇杆按键")
else:
stop()
print("上下stop左摇杆按键")
#右摇杆按键
if event.axis==2:#左右
if event.value<-0.1:#向右
Rrotate(int(abs(event.value)*1000))
print("“向右”右摇杆按键")
elif event.value>0.1:#向左
Lrotate(int(event.value*1000))
print("“向左”右摇杆按键")
else:
stop()
print("“停止”右摇杆按键")
elif event.axis==3:#上下
if event.value<-0.1:#向上
up(int(abs(event.value)*1000))
print("“向上”右摇杆按键")
elif event.value>0.1:#向下
down(int(event.value*1000))
print("“向下”右摇杆按键")
else:
stop()
elif event.axis==5:#左射击键
if event.value>0:
print("左射击键")
print(int(event.value*1000))
Open(int(event.value*1000))
else:
stop()
elif event.axis==4:#右射击键
if event.value>0:
print("右射击键")
print(int(event.value*1000))
Close(int(event.value*1000))
else:
stop()
【演示视频】
https://www.bilibili.com/video/BV1rJ4m1V7qL/?share_source=copy_web
老师,我在b站看到您的AI测跌倒的视频,很有兴趣,想学习一下,不过没找到相关的帖子,请问您这边帖子还在吗,或者相关源码还在吗 可以改装一下,变成蜂群战斗机器人
页:
[1]