<<Prev 1 2 3 4 5 6 7 8 9 10 Next>>
NXT Vision VBScript code
To translate the vision properties to motor command variables we need to add the VBScript module.
This module will allow us to use VBScript programming to calculate the appropriate motor
commands to send to the NXT and also know what mode we are in. There are 7 modes that
we need to perform in this tutorial.
- Open the grippers for 3 seconds. Then switch them off.
- Spin around in place until you see a blue and big enough ball.
- Move towards the ball and stop when it goes under the camera's field or view
- Spin around looking for a reddish cone
- Move towards the cone and stop when close enough
- Release the ball by opening the grippers for 3 seconds
- Cleanup ready for next round.
The steps are quite basic but you need to keep track of what step or state you are in as each
entry into the VBScript module needs to reestablish what mode it is in. The VBScript module
is not like calling a function which returns when it is finished as the VBScript module
is only called once each new image has been processed and reactions to that new information is
desired. You can think of this type of programming as a pipeline where you cannot stop the pipeline
otherwise everything else will stop functioning. The best you can do is perform a check depending
on what state you are in and what the motor and gripper variables should be in that state. Below we
see the full annotated VBScript code used to maintain state during our task.
' the speed of the robot motors (a constant)
SPEED = 30
' grab what state we are in using the
' getvariable routine
state = GetVariable("state")
' jump to the appropriate state
select case state
case 0:
SetVariable "Status: ", "Opening grippers"
' start the open gripper timer if it is not already
' initialized
if GetVariable("GRABBER_OPEN") = "0" then
SetVariable "GRABBER_OPEN", Timer()
end if
' only execute this step for < 2 seconds
if Timer() - GetVariable("GRABBER_OPEN") < 3 then
' set the grabber variable to open ... this is
' passed to the NXT as a motor command
SetVariable "GRABBER", 180
else
' set the grabber variable to neutral
SetVariable "GRABBER", 128
' re-zero the time so we can use it again below
SetVariable "GRABBER_OPEN", 0
' transition to the next state
SetVariable "state", 1
end if
case 1:
SetVariable "Status: ", "Searching for ball"
' grab the COG size that is created by the COG
' module
size = GetVariable("COG_BOX_SIZE")
xw = (GetVariable("IMAGE_WIDTH")/2)
' if a big enough blob is seen then proceed to
' the next state
if size > 20 and GetVariable("COG_X") > xw - 60 _
and GetVariable("COG_X") < xw + 60 then
SetVariable "MAX_SIZE", size
SetVariable "state", 2
else
' otherwise turn left until we see something big
' enough to track the left and right motor
' variables are passed to the NXT
SetVariable "LEFT_MOTOR", 128
SetVariable "RIGHT_MOTOR", 215
end if
case 2:
SetVariable "Status: ", "Approaching ball"
size = GetVariable("COG_BOX_SIZE")
maxSize = GetVariable("MAX_SIZE")
if size > maxSize then
SetVariable "MAX_SIZE", size
end if
' if we passed over the ball we are near enough to
' the ball to grab it this is determined by the COG
' going blank OR the ball suddenly got much smaller
' since we probably picked up a bit of blue noise
' once the ball when out of view
if (size < 20 or GetVariable("COG_Y") = "") _
and maxSize > 100 then
' stop the motors
SetVariable "LEFT_MOTOR", 128
SetVariable "RIGHT_MOTOR", 128
' turn on the grabber
SetVariable "GRABBER", 40
' continue to the next state
SetVariable "state", 3
else
' otherwise keep steering towards the ball's COG
' get the screen with and height (will always
' be 320 in our case)
xw = (GetVariable("IMAGE_WIDTH")/2)
' how far off center is the blob?
xMove=GetVariable("COG_X")-xw
xoff = xw - 5
' if the blob is off to the right then increase
' the right motor a little depending on how far
' off right we are
if xMove > 0 then
rightMove = 215-SPEED+(((xoff-xMove-k)/xoff)*SPEED)
leftMove = 215
else
' otherwise up the left motor in a similar manner
leftMove = 215-SPEED+(((xoff-(-xMove)-k)/xoff)*SPEED)
rightMove = 215
end if
' set the variables so that the NXT module can use them
SetVariable "LEFT_MOTOR", leftMove
SetVariable "RIGHT_MOTOR", rightMove
end if
case 3:
' we've got a ball now head towards the red cone!!
SetVariable "Status: ", "Searching for cone"
' the processing pipeline has already switched the blob
' tracking to a red object since state >= 3. We just
' need to find it and head towards it.
size = GetVariable("COG_BOX_SIZE")
if size > 30 then
SetVariable "state", 4
else
' turn left until we see something big enough to track
SetVariable "LEFT_MOTOR", 128
SetVariable "RIGHT_MOTOR", 215
end if
case 4:
SetVariable "Status: ", "Approaching cone"
' this routine is exactly like the one above
' except for the criteria that the blob cog
' size is 200
size = GetVariable("COG_BOX_SIZE")
' if we are near enough to a cone drop the ball
if size > 200 then
' stop the robot
SetVariable "LEFT_MOTOR", 128
SetVariable "RIGHT_MOTOR", 128
SetVariable "state", 5
else
' otherwise keep steering towards the cone's COG
xw = (GetVariable("IMAGE_WIDTH")/2)
xMove=GetVariable("COG_X")-xw
xoff = xw - 5
if xMove > 0 then
rightMove = 215-SPEED+(((xoff-xMove-k)/xoff)*SPEED)
leftMove = 215
else
leftMove = 215-SPEED+(((xoff-(-xMove)-k)/xoff)*SPEED)
rightMove = 215
end if
SetVariable "LEFT_MOTOR", leftMove
SetVariable "RIGHT_MOTOR", rightMove
end if
case 5:
SetVariable "Status: ", "Opening grippers"
' initialize timer
if GetVariable("GRABBER_OPEN") = "0" then
SetVariable "GRABBER_OPEN", Timer()
end if
' open grabber .. but only execute this step
' for < 3 seconds
if Timer() - GetVariable("GRABBER_OPEN") < 3 then
SetVariable "GRABBER", 180
else
SetVariable "GRABBER", 128
SetVariable "GRABBER_OPEN", "0"
SetVariable "state", 6
end if
case 6:
SetVariable "Status: ", "Misson Complete"
' clean up for next run and stop all robot motors
SetVariable "GRABBER", 128
SetVariable "GRABBER_OPEN", "0"
SetVariable "GRABBER_CLOSE", "0"
SetVariable "LEFT_MOTOR", 128
SetVariable "RIGHT_MOTOR", 128
end select
And lets have a look at the specific image processing pipeline ...
<<Prev 1 2 3 4 5 6 7 8 9 10 Next>>
|