回到做机器人上面来。
把线插入Sharp的芯片,如果没附带线,你要确保有三根线从它接出,颜色可以不同,但我希望是红、黑和白,因为这对V、G和信号时非常有意义的。
你可能要在线上接上杜邦线,像下图我那样操作。这些可以是任何颜色。
确保你准确插线,因为Sharp的芯片很容易被烧掉。
如下图所示,你会知道怎么连接,黏贴和奇怪的设置时确保你能看到导线和它的颜色。
你也要三个跳线帽,连接相连的两根针。
如果你没有,你可以用杜邦线来跳线代替,跳线帽不占地方,因此这是一个很好的选择。而线对于从一断到另一端来说,也是个不错的选择。
如接下来的照片所示,用跳线帽或者杜邦线连接模拟输入1,2,3到V.
为什么这样做?一个简单的解析就是这四个输入(0,1,2,3)是模拟的,也就是意味着他测量它们在线上受到多大的压强。不管同意与否,它们已经被连接起来。这样任一个输入的小小压强都会传递给了其他,可以悬空处理。“3”对于V来说是不需要使用,它们只是满载,而不是悬空。
确保伺服电机处在中间,就是150位置。
用一些双面胶,把Sharp的芯片装在伺服电机的角的位置,面向前。
你已经完成了基本组装了,整个产品可能会很大,你也可能会用其他配件,但如果你按指导做,以下有些机器人编程建议给你。
编程
输入这些代码,在连接机器人的情况下按F5。
main:
debug
goto main
然后把你的手从机器人的头部拿开,然后注意变量b0的改变数值,你可以凭借你掌握的知识去决定接下来会发生什么。
如果“眼睛”太接近物体,你会注意到机器人怎么走。Sharp的这颗芯片是设计监测10-18厘米范围内的物体的,小于10CM,大于18CM范围的物体,对编程来说是一个很大的挑战。
你可以选择没有这个问题的的传感器,然而Sharp这颗是最便宜的,同时也是非常容易编程的,这就是我为什么选择这颗芯片的缘故。
输入以下程序,按下F5:
high 4
low 5
有一个轮子会按一个方向转,你的轮子向前么?这是一个向前的指令。如果轮子向后,你可以尝试以下指令:
low 4
high 5
输入以下指令去转动另一个轮子:
high 6
low 7
(或者以另一种方式去改变方向)
通过微控制器的特有方式去控制方向,打开或关闭针脚上的开关,命令电机控制器指挥电机A或B正反向模式。
low 4
low 5
low 6
low 7
停止所有马达。
电机到一边是:
servo 0, 75 wait 2
另一边是:
servo 0, 225 wait 2
中间是:
servo 0, 150 wait 2
以下是一个使机器人旋转的小程序,在障碍物前面停住,左右观察寻找最佳路径,驱动去完成新的挑战。
Symbol dangerlevel = 70 ‘ how far away should thing be, before we react?
symbol turn = 300 ’ this sets how much should be turned
symbol servo_turn = 700 ‘ This sets for how long time we should wait for the servo to turn (depending on it´s speed) before we measure distance
main: ’ the main loop
readadc 0, b1 ‘ read how much distance ahead
if b1 《 dangerlevel then
gosub nodanger ’ if nothing ahead, drive forward
else
gosub whichway ‘ if obstacle ahead then decide which way is better
end if
goto main ’ this ends the loop, the rest are only sub-routines
nodanger:‘ this should be your combination to make the robot drive forward, these you most likely need to adjust to fit the way you have wired your robots motors
high 5 : high 6 : low 4 : low 7
return
whichway:
gosub totalhalt ’ first stop!
‘Look one way:
gosub lturn ’ look to one side
pause servo_turn ‘ wait for the servo to be finished turning
readadc 0, b1
gosub totalhalt
’Look the other way:
gosub rturn ‘ look to another side
pause servo_turn ’ wait for the servo to be finished turning
readadc 0, b2
gosub totalhalt
‘ Decide which is the better way:
if b1《b2 then
gosub body_lturn
else
gosub body_rturn
end if
return
body_lturn:
high 6 : low 5 : low 7 : high 4 ’ this should be your combination that turns the robot one way
pause turn : gosub totalhalt
return
body_rturn:
high 5 : low 6 : low 4 : high 7 ‘ this should be your combination that turns the robot the other way
pause turn : gosub totalhalt
return
rturn:
servo 0, 100 ’ look to one side
return
lturn:
servo 0, 200 ‘ look to the other side
return
totalhalt:
low 4 : low 5 : low 6 : low 7 ’ low on all 4 halts the robot!
Servo 0,150 ‘ face forward
wait 1 ’ freeze all for one second
return
通过聪明的编程和调整,你可以使机器人动起来,转动头部,做决定,作调整,穿过类似正门这样的洞,这些动作可以同时完成。如果你使机器人在滑动的时候转动头部,这看起来会非常酷。
评论