<<Prev 1 2 3 4 5 6 Next>>
Keeping the ball at a distance
Now that we have our COG_BOX_SIZE variable we can arbitrarily chose 85 as the size that BucketBot should
try to maintain (this equates to a couple inches from the ball). Doing so will require
forward and backwards movement based on the relative size of the
green ball or COG_BOX_SIZE. We also need to steer the robot to keep the ball in the center
of the screen. This involves rotating the robot from right to left and moving the camera from up to down.
To create these movement commands we'll add the SSC module (the
BucketBot uses electronic speed controls compatible with PWM
signals created by the SSC) to move the robot. Note that the SSC module communicates to the
SSC board via serial commands.
The SSC module will send two variables to the SSC board that range from 0 to 255 with 0 meaning
reverse, 128 neutral and 255 full forward. The BucketBot has two motors that control the left and right
wheels independently. (This is nice since it can rotate in one spot.) There is one more servo that
controls the tilt of the camera that also ranges from 0 to 255 with 220 meaning the camera is
pointed about straight. Our next task is to translate the
COG_BOX_SIZE variable into motor commands from 0 to 255 for each motor and for the tilt servo.
Doing this translation will require a bit of logic. So we add
in the VBScript Program module into the current
processing pipeline. The VBScript module allows us to insert this program
' get the current center of the image
midx = GetVariable("IMAGE_WIDTH") / 2
midy = GetVariable("IMAGE_HEIGHT") / 2
' set the movement neutral range, i.e. if the
' cog is with range of the center of the image
' then consider it ok and move straigh. This
' helps to prevent fine tune movements that
' can cause oscillations.
range = 5
' determine the factor that scales image width
' to a range of 0 to 128 .. increase beyond 128
' if your robot turns too quickly
horizFactor = 200 / midx
' vertical factor ... we don't have much
' range here so the number is larger than
' horizontal movement
vertFactor = 300 / midy
' ball size when it is too close
ballSize = 40
' initialize our start motor values to neutral
left_base = 128
right_base = 128
' get the size (width or height) of the current
' bounding box
size = GetVariable("COG_BOX_SIZE")
' if it is equal to "" then no object was detected
if size > 10 then
' get the horizontal center of gravity found by the COG
' module
cogX = GetVariable("COG_X")
' if it is less than 75 the blob is on the left side
' of the screen so move that way
if cogX < (midx - range) then
left_base = 128-(midx-cogX)/horizFactor
right_base = 128
' otherwise move to the right if above 85 pixels (and no
' movement if 75 < cogX < 85 )
elseif cogX > (midx + range) then
left_base = 128
right_base = 128-(cogX-midx)/horizFactor
end if
' if the ball is too close then we have to move backwards
if size > ballSize then
left_motor = left_base+((size-ballSize)*2)
right_motor = right_base+((size-ballSize)*2)
' otherwise move forward
else
left_motor = left_base-((ballSize-size)*2)
right_motor = right_base-((ballSize-size)*2)
end if
' set the final motor values to be picked up by
' the SSC module
SetVariable "LEFT_MOTOR", CInt(left_motor)
SetVariable "RIGHT_MOTOR", Cint(right_motor)
' now lets work on the tilt ... grab the current
' tilt value
tilt = GetVariable("TILT")
' if it was not set then default to 220 (in our case
' this is horizontal to the floor)
if tilt = "" then
tilt = 220
end if
' if the blob is below 55 then tilt down up a bit more
' otherwise tilt up a little more
cogY = GetVariable("COG_Y")
if cogY < midy - range then
tilt = tilt - (midy-cogY)/vertFactor
elseif cogY > midy + range then
tilt = tilt + (cogY-midy)/vertFactor
end if
' we don't want to look up more than horizontal to the
' floor as we do not expect the
' ball to be on the ceiling
if tilt > 220 then
tilt = 220
end if
if tilt < 0 then
tilt = 0
end if
' and set that value for the SSC module too
SetVariable "TILT", tilt
else
SetVariable "LEFT_MOTOR", 128
SetVariable "RIGHT_MOTOR", 128
end if
The program first grabs our size variable COG_BOX_SIZE and compares it with what size we'd like to maintain
in view. If the object is too big we move backwards by a relative amount or if the object is smaller than
we'd like we move forwards. However, we also need to change the relative amounts for the right and left
motors to ensure that we can rotate towards the ball as appropriately.
The LEFT_MOTOR and RIGHT_MOTOR variables that are set are then mapped to the left and right motors using
the SSC module. Likewise, the TILT variable is mapped to the
camera tilt servo. Note that they all range from 0 to 255. If you change the above
equation to be more aggressive you need to ensure that the values stay within the 0 to 255 range.
Now lets see it in action!
<<Prev 1 2 3 4 5 6 Next>>
|