|
在巡线编程中需要了解黑线的反射值,受环境传感器位置,地势等影响,反射值未必是0-100.准确知道值的范围,方便后续计算.下例是以Ev3basic编写程序,实现读出反射值情况并记录到文本文件中,利于后期分析.
本例可看到文本编辑代码的方便性.执行时小车3号口为颜色传感器,"BC"为马达口,将小车放在线边跑过线,就测试出反射值的范围.我的小车范围为8-96.以下为源码,参考Ev3basic-文本开发语言安装使用简介 http://bbs.cmnxt.com/forum.php?m ... &extra=page%3D1 上传即可执行.
'测量通过一条循线的亮度高低等情况
'设定反馈光在3口,BC为左右轮马达
'write by alexy us ev3basic
VN = 200 '统计个数
'-----清理显示标题----------------
LCD.Clear()
LCD.Write(0,16, "check the line banner ")
'-------------------------------------
selectmode = 0
Sensor.SetMode(3, selectmode) '设立读反光模式(0),3号为颜色传感器输入口
'-------------------------------------
'-------------初始化最值及数组
min = 100
max = 0
min_ro = 0
max_ro = 0
V_refkect = Vector.Init(VN ,0) '读出的光线值
V_ro = Vector.Init(VN ,0) '马达转动 计数
V_dettime = Vector.Init(VN ,0) '时间计数
setspeed = 40
Motor.ResetCount("BC")
Motor.StartSync("BC",setspeed ,setspeed )
'测试同步轮方式,前进方式与最终方式相同
old_count = Motor.GetCount("B")
For i = 0 To (VN-1)
While Motor.GetCount("B") < ( old_count +1)
EndWhile '时间太快,可能马达计数还没 转过
old_count = Motor.GetCount("B")
reflect = Sensor.ReadPercent(3)'读出的光线值
V_refkect = reflect '
V_ro = Motor.GetCount("B")
V_dettime = EV3.Time
bcount = Motor.GetCount("B")
LCD.Write(0,32, "COUNT: "+ i)
LCD.Write(0,48, "bcount: "+ bcount)
EndFor
Motor.Stop("BC","True")
'------------------完毕写入文件----
LCfile = EV3File.OpenWrite("LineCheck" + "_" + setspeed + ".txt")
'记录有关参数,如速度,模式等
Maxerror = 0
For i = 0 To (VN-2)
EV3File.WriteLine(LCfile ,V_refkect + ";" + V_ro + ";" + V_dettime)
'统计最值
If (V_refkect < min ) Then
min = V_refkect
min_ro = V_ro
EndIf
If (V_refkect > max ) then
max = V_refkect
max_ro = V_ro
EndIf
EndFor
EV3File.WriteLine(LCfile ,"re" + ";" + "ro" + ";"+"dattime") '加入栏目名
EV3File.WriteLine(LCfile,"Data file Created by program flcheckvn.sb" )
EV3File.WriteLine(LCfile,"use speed: " + setspeed )
EV3File.WriteLine(LCfile,"Max:MIn is " + max + ":" + min )
EV3File.WriteLine(LCfile,"avtime/per rota is" + (V_dettime[VN-1]-V_dettime[0] )/(V_ro[VN-1]-V_ro[0]))
EV3File.Close(LCfile)
LCD.Write(0,64, "MAX: "+ max )
LCD.Write(0,80, "MIN: "+ min )
Program.Delay (500)
|
|