4. BASIC Stamp II Software

(In this section, we go over the major features of the program. For more details, view the entire program (3k bs2). Note that the pin numbers used in the program correspond to the schematic diagram in section 3, above.)

The BASIC Stamp makes serial I/O very straightforward with its serout instruction. A typical use of the instruction is:


  serout MC_SOUT, 32, [$80, 0, LFWD, SPEED]	'Left motor forward at SPEED

The first value, MC_SOUT, specifies the serial output pin to use, which is P10 in our example. The second argument, 32, specifies the settings for the serial output; we use 32 because it specifies the baud rate to be 19,200, which is the maximum rate at which the motor controller can receive. The four values in square brackets are the values sent out over the serial line.

The first two values sent to the motor controller are always hex 80 (128 in decimal) and 0, which let the motor controller know that it is being issued a command. The third value specifies the motor number and direction. To help prevent mistakes with this third parameter, we defined constants for forward and backward for our two motors at the beginning of our program:

  LFWD		con	0	 
  LBAK		con	1		
  RFWD		con	2	
  RBAK		con	3

(Of course, which constant gets assigned which number depends on how your robot is wired up and what you call forward and reverse or left and right.) The final parameter in the 4-byte sequence is the speed at which the motor should run, where 0 stops the motor and 127 (7F hex) is full speed.

Note: At the beginning of the program, it is important to reset the motor controller. Make sure the serial line is high before you reset the motor controller:
  high 	MC_SOUT		’serial line idle state
  low	MC_RESET	’reset motor controller
  high	MC_RESET

The main loop of the program is rather simple since the robot does not do much. For simplicity, we use the pause instruction to determine the time that the robot backs up or turns, but the BASIC Stamp could potentially be doing something more useful during that time.

loop:	'Go forward till bump something
	serout MC_SOUT, 32, [$80, 0, LFWD, SPEED]	'Left and right motors forward at SPEED
	serout MC_SOUT, 32, [$80, 0, RFWD, SPEED]	'32 indicates 8 bits, no parity, non-inverted, 
								' buad rate 19200
	if (RBUMP = 0) then rbumped			'If bumped, turn backward in appropo direction
	if (LBUMP = 0) then lbumped
	goto	loop	


rbumped: 'Turn backward right, then spin left in place for a random time
	serout MC_SOUT, 32, [$80, 0, LBAK, SPEED]		'Turn backward for 1 sec
	serout MC_SOUT, 32, [$80, 0, RBAK, SLOWSPEED]
	pause 1000

	serout MC_SOUT, 32, [$80, 0, LBAK, SPEED]	'Spin in place for random time, TURNTIME
	serout MC_SOUT, 32, [$80, 0, RFWD, SPEED]
	random TURNTIME
	pause (TURNTIME*5) + 250			'pause between 0.25 and 1.5 seconds

	goto loop


lbumped: 'Turn backward left, then spin right in place for a random time
	serout MC_SOUT, 32, [$80, 0, LBAK, SLOWSPEED]	
	serout MC_SOUT, 32, [$80, 0, RBAK, SPEED]
	pause 1000

	serout MC_SOUT, 32, [$80, 0, LFWD, SPEED]
	serout MC_SOUT, 32, [$80, 0, RBAK, SPEED]
	random TURNTIME
	pause (TURNTIME*5) + 250

	goto loop

For more details, view the entire program (3k bs2). Note that the pin numbers used in the program correspond to the schematic diagram above.

Related Products

Pololu Micro Dual Serial Motor Controller (No Manual)
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors