第1步:筹备
步调2:代码布局
我将代码分别为这些文件,我将逐个注释:
正在接下来的步调中,我将一步一步天注释这些文件
第3步:
快递分拣机器人设计软件设计这是应用程序的次要出口面,包括机械人类
importasGPIO
垃圾分拣机器人的缺点fromimportreactor,task
importl298NDrive
importremoteControl
importlogging
importutils
log=
enableMotors=True#Motorscanbedisabledfortestingwiththis
runFrequency=1#ThefrequencyonwichthemainTaskwillberan
classAum2:
def__init__:
(“InitializingAum2robot.。.”)
#Configuration
=5555#Porttoopensotheremotecanconnect
#Configureminandmaxservopulselengths
#Keepinmindthesearenotpinsbutservoouts
=(10,11,12,13,14,15,enableMotors)
=
defprocessRemoteCommand:
=(,,-9.8,9.8,-4095,4095)
,int
defrun:
defstart
瓜果分拣机器人方案l=
#runwillbecalledwiththisfrequency
#Gatherourcodeinamainfunction
defmain:
robot=Aum2
#Standardboilerplatetocallthemainfunctiontobegin
#theprogram.
if__name__==‘__main__’:
main
有些我要对此文件颁发评论的要点:
我正在利用歪曲库来调和来自UDP的近程节制定单服务器跟机器人的次要Run方式。如你所见,该方式此刻甚么也没有做,可是其设法主意是正在此中增添代码以使机器人更具自治性。
processRemoteCommand方式将是
起头方式是正在我设置须要调和的两件事时:监听
第4步:
此文件包括两个类,即RemoteControl类,RCCommand类
importlogging
importtraceback
fromimportDatagramProtocol
fromimportreactor
importutils
log=
classRemoteControl(DatagramProtocol):
def__init__:
=port
defstart(self,onCommandReceived):
(“InitializingRemoteControl.。.”)
=onCommandReceived
defdatagramReceived(self,data,addr):
try:
(“Received%rfrom%s”%
command=RCCommand
except(KeyboardInterrupt,SystemExit):
raise
except:
_exc
classRCCommand:
闪兔分拣机#Timestamp[sec],sensorid,x,y,z,sensorid,x,y,z,sensorid,x,y,z
#890.71558,3,0.076,9.809,0.565,。..
def__init__:
=0
=0
=0
=
iflen》=5and[1].strip==“3”:
=-float
=-float
=float
(“x={}y={}z={}”.format
else:
(“Invalidmessage:{}”.format
一样是RemoteControl类,它利用歪曲的框架去侦听运转WirelessIMU使用的Android手机发送的UDP程序包。此应用程序发送存在以下布局的动静:
890.71558,3、0.076、9.809、0.565,4,-0.559、0.032,-0.134、5-21.660,-36.960,-28.140
我只对下面突出显现的动静部门感兴趣,那就是传感器ID3,和加速度计供给的三个丈量值(0.076、9.809、0.565)。
你可以正在代码中看到,类RCCommand将此动静的有用参数转换为机器人所需的x跟y值,此中
x是畴前到后的轴,而
y则是从一侧到另一侧。
我也有z轴的代码,可是我临时没有利用它。
步调5:
这是担任节制电动机的文件。为此,它将下令发送到16伺服驱动器,该驱动器毗邻到L298N电动机驱动器。我念我能够会正在我构建的每一个RaspberryPi机器人上重用该类。它的确很简略,而且没有须要庞大的处置惩罚便可实现它的事情。
importAdafruit_PCA9685
importlogging
importmath
importutils
log=
classL298NDrive:
def__init__(self,enA,in1,in2,in3,in4,enB,enabled):
(“InitializingL298Drive.。.”)
=0
=4095
=enA
=in1
=in2
=in3
=in4
=enB
=enabled
ifenabled:
defenable:
=True
物料分拣机器人的设计#InitialisethePCA9685usingthedefaultaddress。
=Adafruit_
defdisable:
=False
#InitialisethePCA9685usingthedefaultaddress。
=None
defsetSpeeds(self,rSpeed,lSpeed):
defsetSpeed(self,motor,speed):
pwm=int
(“Motor:{}speed:{}pwm:{}”.format(motor,speed,pwm))
ifmotor==1:
ifspeed》=0:
if:
_pwm
_pwm
else:
if:
_pwm
_pwm
if:
_pwm
else:#motor==2
ifspeed》=0:
if:
_pwm
_pwm
else:
if:
_pwm
_pwm
if:
_pwm
可以看出,代码的次要部门正在setSpeed(self,motor,speed)方式中,该方式吸收要设置的电动机编号,并通知伺服驱动器若何处置惩罚与该电动机关系的引脚。
大概该当给出一些注释以相识我为何要履行我的事情
L298N机电驱动器
利用6个引脚驱动两个机电:
enA用来通知速率,咱们要使电动机启动1。通电后,通知驾驶员电动机1该当沿一个标的目的挪动
in2通电时,通知驾驶员电动机1应该沿相反标的目的挪动
in3通电时,见告驾驶员机电2该当沿一个标的目的挪动
in4通知驾驶员,电动机2该当沿相反标的目的挪动
enB用于通知速率,咱们愿望电动机迁移转变
以是正在咱们的例子中,由于我对一切引脚皆利用了伺服驱动器:
当我做到那一点:
_pwm
我真的是正在通知伺服驱动器,正在一切PWM时代,该引脚该当为OFF,
当我如许做时:
_pwm
我实际上是正在通知伺服驱动器,正在一切PWM周期中,该引脚该当处于ON地位(以是我将其翻开
然后,当我如许做时:
_pwm
我利用真正的PWM引脚,并利用速率参数设置我所要求的脉冲。
步调6:
最初,这是最初的安然。它没有包括类,可是我认为我会正在其他处所重用的一切有用功用。
importmath
importlogging
log=
defmap(v,in_min,in_max,out_min,out_max):
#Checkthatthevalueisatleastin_min
ifv《in_min:
v=in_min
#Checkthatthevalueisatmostin_max
ifv》in_max:
v=in_max
return*(out_max-out_min)//+out_min
defjoystickToDiff(x,y,minJoystick,maxJoystick,minSpeed,maxSpeed):
#Ifxandyare0,thenthereisnotmuchtocalculate.。.
ifx==0andy==0:
return
#FirstComputetheangleindeg
#Firsthypotenuse
z=
#angleinradians
rad=
#andindegrees
快递分拣机器人充电angle=rad*180/
#Nowangleindicatesthemeasureofturn
#Alongastraightline,withanangleo,theturnco-efficientissame
#thisappliesforanglesbetween0-90,withangle0thecoeffis-1
#withangle45,theco-efficientis0andwithangle90,itis1
tcoeff=-1+*2
turn=tcoeff*
turn=round/100
#Andmaxofyorxisthemovement
mov=max
#Firstandthirdquadrant
if(x》=0andy》=0)or:
rawLeft=mov
rawRight=turn
else:
rawRight=mov
rawLeft=turn
#Reversepolarity
ify《0:
rawLeft=0-rawLeft
rawRight=0-rawRight
#minJoystick,maxJoystick,minSpeed,maxSpeed
#Mapthevaluesontothedefinedrang
rightOut=map(rawRight,minJoystick,maxJoystick,minSpeed,maxSpeed)
leftOut=map(rawLeft,minJoystick,maxJoystick,minSpeed,maxSpeed)
(“x={}y={},rOut={},lOut={}”.format(x,y,rightOut,leftOut))
return(rightOut,leftOut)
这里只有两种方式,第一种是从地图功能的Arduino。它将指定输入限定内的值转换为输出规模内的响应值。
我为输入值增添了限定,以是我只接管av,位于in_min跟in_max以内。
我拥有的另一个函数(joystickToDiff)略微庞大一点,可是其目标是从输入的操纵杆对转换值,以到达差动驱动机器人摆布机电所需的速率。此输出已托付,已转换为指定为参数的速率规模。
你可以正在我的博客中找到有关此部门的更多详细信息
智能分拣机器人底板闪兔分拣机器人国产分拣机器人批发商