cost you around $5 for the parts,
depending on what you have in your
junk box. As far as assembly goes,
I soldered the 78L05, diode, and
resistor directly to the connector.
Software
then print the received value, as well
as the Rot value.
Note that you have to access the
ComInput command to pull the
data out of the buffer or the ComBuff
command will just keep getting larger
and won’t ever return a 0.
Laps
First, Second, Third, and
Fourth.
Completed laps.
A call to initBike is also made.
Notice that I am using com port 4. You
will need to pass the com port number
your interface is connected to.
Now, for the fun part. We will use a
development program called Zeus.
There is even a special free Nuts &
Volts version up on the Nuts & Volts
website ( www.nutsvolts.com). Let’s see
how this will work.
Sensor Test
I’m going to start with a simple
program just to test if the sensor is
working properly.
Program 1 is fairly straightforward. We open up the com port. If the
ComOpen returns a 0, we have an
error so we display an error message
and exit the program.
Once we have an open port,
we raise DTR with the ComDTR
command. This provides power to
the interface.
We then run a very tight loop that
checks the status of the com buffer
with the ComBuff command. If it
returns anything greater than 0, we
know we have a sensor event.
We increment the Rot variable
Race Program 1
I’m not going to list the Race
Program 1 here because it can be
found on the Nuts & Volts website, as
well as the Kronos Robotics website. I
will, however, walk you through each
section of the code.
There are four functions used to
make up Race Program 1.
Main Loop
Here we set a variable called
ticks. This variable will increment
every 10 milliseconds. Once we get a
tick value greater than 3, we reset the
counter and using a random number
generator, increment the position of
each of the bot pips. For instance,
take the following snipit of code:
• initBike(ComPort)
This function opens the indicated
port and sets DTR to power the
interface.
if Random(1, 10) > 4 then
rots(2) = rots(2) + 1
endif
• Draw Track()
This function draws the track. You
can change the FormBrush values to
change the track colors.
• PlotPlayers(Player,
Rotations)
This function plots a player pip at
a particular position on the track
based on its Rotation value. Note that
player 1 is the sensor and players 2-4
are the bot players.
Program 1. Sensor Test
‘Exercise Bike Sensor Test
func main()
gconst chBike 1
dim Rot as integer
clearall
if ComOpen(chBike,baud=9600,port = 4) = 0 then
MsgBox(“Error Opening Com Port”)
end
endif
‘Power up the connector
ComDTR chBike,1
Loop:
if ComBuff(chBike) > 0 then
Rot = Rot + 1
Print val(ComInput(chBike)),”Count=”+Rot
endif
DoEvents
goto Loop
endfunc
• Main()
This is the entry
point of the program.
This is where most of the
work gets done and, due
to its complexity, it is
broken down into its own
sub sections.
Here, a random number between
1 and 10 is generated. If it is greater
than 4, we will increment the bots
rotation count. This equates to a 40%
chance of this particular pip advancing one position every 40 ms.
By changing the value from 4 to 8,
you effectively double the skill level
of this bot. This would give the
appearance of that bot running faster
on average.
After the rotations are calculated,
we then draw the track and plot all
four player pips.
Main Calculate Position
Here, we do a quick bubble sort
to calculate each players position in
the race.
Main Setup
Here we set up the
program by creating a
few arrays.
Main Display Lap Data
Here, we display the text data
indicating the player name, his position, and number of laps completed.
Tots Total of all
rotations (sensor
events).
Rots Current rotations
of current lap.
Pos Race position.
Main Check Com Buffer
Here, we check for activity on the
com buffer. If we have activity, we increment the (player 1) rotation counter.
Going Further
I consider this a starting point as
58
March 2006