Der Roboter funktioniert wunderbar - wie beschrieben. Un hübsch ist er geworden (siehe Fotos). Lediglich ein leichter Linksdrift bei Geradeausfahrt. Den haben wir über einen kleinen Trick im Code (siehe unten) korrigiert, natürlich parametrierbar ;-). Die Drift scheint sich nämlich mit abnehmender Akkuladung zu verändern...
Unser einfacher Code funktioniert ganz gut, sodass der Robo auch aus Ecken wieder rauskommt:
' ----------------------------------------------------------------
' lmr my first robot first test program
symbol dangerlevel = 80 ' how far away should thing be,
symbol dangerlevel = 80 ' how far away should thing be,
' before we react
symbol turn = 200 ' this sets how much should be turned
symbol servo_turn = 700 ' this sets for how long time we should
symbol turn = 200 ' 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 befor we
' measure distance
symbol rightdelay=10 ' after rightdelay times calling nodanger,
symbol rightdelay=10 ' after rightdelay times calling nodanger,
' turn only left wheel (drift correctiion)
' ----------------------------------------------------------------
' initialisation stuff
gosub totalhalt ' initially stop and turn head forward
b3 = 0 ' variable b3 is used to keep track of how often
' ----------------------------------------------------------------
' initialisation stuff
gosub totalhalt ' initially stop and turn head forward
b3 = 0 ' variable b3 is used to keep track of how often
' "nodanger" has been called
' ----------------------------------------------------------------
' main loop
main:
readadc 0, b1 ' read how much distance ahead
if b1 < dangerlevel then
gosub nodanger ' if nothing ahead drive forward
else
gosub whichway ' if obstacle ahaed then decide which way is better
end if
goto main
' ----------------------------------------------------------------
' including drift correction by stopping right wheel for
' ----------------------------------------------------------------
' main loop
main:
readadc 0, b1 ' read how much distance ahead
if b1 < dangerlevel then
gosub nodanger ' if nothing ahead drive forward
else
gosub whichway ' if obstacle ahaed then decide which way is better
end if
goto main
' ----------------------------------------------------------------
' including drift correction by stopping right wheel for
' one cycle after nodanger has been called rightdelay times
nodanger: ' combination to make the robot drive forward
if b3<rightdelay then
high 4 : low 5 : low 6 : high 7
else
high 4 : low 5 : low 6 : low 7
b3=0
endif
b3 =b3+1
return
' ----------------------------------------------------------------
' looking and deciding which way to turn
whichway:
gosub totalhalt ' first stop!
' Look oneway
gosub lturn ' look to one side
pause servo_turn ' wait for the servo to finish turning
readadc 0, b1
gosub totalhalt
' Look the other way
gosub rturn ' look to one side
pause servo_turn ' wait for the servo to finish turning
readadc 0, b2
gosub totalhalt
' Decide which way
if b1 <= b2 then
gosub body_lturn
else
gosub body_rturn
end if
return
' ----------------------------------------------------------------
' turn robot right
body_rturn:
high 4: low 5 : high 6 : low 7
pause turn : gosub totalhalt
return
'-----------------------------------------------------------------
' turn robot left
body_lturn:
low 4 : high 5: low 6 : high 7
pause turn : gosub totalhalt
return
' ----------------------------------------------------------------
' turn head right
rturn:
servo 0, 100
return
' ----------------------------------------------------------------
' turn head left
lturn:
servo 0, 200
return
' ----------------------------------------------------------------
nodanger: ' combination to make the robot drive forward
if b3<rightdelay then
high 4 : low 5 : low 6 : high 7
else
high 4 : low 5 : low 6 : low 7
b3=0
endif
b3 =b3+1
return
' ----------------------------------------------------------------
' looking and deciding which way to turn
whichway:
gosub totalhalt ' first stop!
' Look oneway
gosub lturn ' look to one side
pause servo_turn ' wait for the servo to finish turning
readadc 0, b1
gosub totalhalt
' Look the other way
gosub rturn ' look to one side
pause servo_turn ' wait for the servo to finish turning
readadc 0, b2
gosub totalhalt
' Decide which way
if b1 <= b2 then
gosub body_lturn
else
gosub body_rturn
end if
return
' ----------------------------------------------------------------
' turn robot right
body_rturn:
high 4: low 5 : high 6 : low 7
pause turn : gosub totalhalt
return
'-----------------------------------------------------------------
' turn robot left
body_lturn:
low 4 : high 5: low 6 : high 7
pause turn : gosub totalhalt
return
' ----------------------------------------------------------------
' turn head right
rturn:
servo 0, 100
return
' ----------------------------------------------------------------
' turn head left
lturn:
servo 0, 200
return
' ----------------------------------------------------------------
' stop motors and head forward
totalhalt:
low 4 : low 5 : low 6 : low 7
servo 0, 153
wait 1
return
totalhalt:
low 4 : low 5 : low 6 : low 7
servo 0, 153
wait 1
return
Keine Kommentare:
Kommentar veröffentlichen