| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
|
This article is meant to provide you with a simple introduction to the BugBrain, how the servos work and give you programming ideas for the future. The BugBrain used for this tutorial was the BasicX version, although some information contained will be identical for the STAMP version too.
BasicXThe language we use to program the BugBrain is BasicX. BasicX is naturally very similar to the Basic line of languages include QBasic and Visual Basic. The syntax is fairly simple, with all the familiar programming constructs: if-then-else, for-loops, while-loops, subroutines, functions etc.As an example, let us look at a very simple program: Option Explicit Public Sub Main() CentreServos ' Requires BBCommon.bas Debug.Print "Hello world!" ' Output to BasicX debug screen Debug.Print End SubOption Explicit tells the compiler to only allow variables that have been declared (similar to C++/Java syntax). Many forms of Basic allow variables to be used without being declared, but it makes the code much harder to debug. Next, "Public Sub Main()" is the entry point for the program - all programs must have this subroutine. The "public" keyword denotes that Main() is accessible from different files. In short, the BasicX development environment allows you to split your program up into several files to ease development, readability and modularity. If you want a variable, subroutine or function to be accessible across different files, you must declare it as public. If you want them to be local to the file, declare them as private (again, similar to Java/C++). Next the program calls the subroutine CentreServos. Note that you can use either Subroutine parameters or Call Subroutine(parameters) to call a subroutine. Debug.Print is then called with a string. Debug.Print simply sends the string down the serial port. If the serial port is connected to the BasicX development environment, it will be displayed in the output screen. For example: *** Opening file: F:\Program Files\BasicX\BBTutorial00.bxb *** Downloading File *** Finished Downloading Hello world! The BasicX language is very easy to learn and should be simple to master by anyone who has done any programming before. For reference regarding library functions and the intricacies of the BX-24, the BasicX documentation is very extensive. So, with the programming language behind us, let us look at more BugBrain-specific details.
ServosInitially, when I started programming the BugBrain, I decided to create a common file that would include all the necessary constants and a common utility functions like moving/centring the servos, turning all the LEDs on or off etc. Therefore, I took a look at the code for moving the servos from the Yost Engineering Resource Guide (note SERVO1 and SERVO2 are constants I defined as 14 and 13, respectively):Public Sub MoveServos(ByVal Pos1 As Single,
ByVal Pos2 As Single,
ByVal Times As Integer )
Dim PulseWidth As Single
Dim i As Integer
For i = 1 to Times
PulseWidth = 0.0003 + (0.002 * Pos1)
Call PulseOut(SERVO1, PulseWidth, 1)
PulseWidth = 0.0003 + (0.002 * Pos2)
Call PulseOut(SERVO2, PulseWidth, 1)
Call Delay(0.02)
Next
End Sub
While this was all good, I was very unsure about how or why this routine worked. With a little browsing, I managed to
find this excellent article
(PDF) that describes how servos work. Thankfully, the article uses the TS-53 (the servo the BugBrain uses) as the
real-world example.
Basically, the servo works by reading an electrical pulse that varies between 1ms and 2ms in length. The length of the pulse determines the position of the shaft and therefore, for the BugBrain, the position of the legs. Yost Engineering obviously decided to let the MoveServos function take values between 0.0 and 1.0 to denote the position of the legs more naturally. This logical position is translated into seconds (the pulse width) using the formula "0.0003 + (0.002 * p)". Take a look at three typical values:
p = 0.3
PulseWidth = 0.0003 + (0.002 * 0.3)
= 0.0009 seconds (0.9 milliseconds)
Remember the direction of the BugBrain depends on the orientation of the servo. Since the BugBrain features
two servos connected to each other, it can be a little hard to visualize how values will affect the movement
of the BugBrain, but a little experience will help a great deal.
WalkingHopefully now, though, it is a little easier to see how the BugBrain walks. A typical walking routine might look like:Call MoveServos(0.7, 0.65, 15) Call MoveServos(0.3, 0.65, 15) Call MoveServos(0.3, 0.35, 15) Call MoveServos(0.7, 0.35, 15)Firstly the left leg goes forward and down, then pulls back, then the right leg goes down and pulls back. The cycle continues and you have a walking BugBrain!
ProgrammingProgramming the BugBrain is relatively easy, once you understand the various I/O pins and have an understanding how the servo system works. Since you now understand the servo mechanism, here are the constants I use for the I/O pins (they are available in the zip file):Public Const SOUT As Byte = 1 ' Serial out Public Const SIN As Byte = 2 ' Serial in Public Const LED1 As Byte = 5 ' LED 1 (left-most) Public Const LED2 As Byte = 6 ' LED 2 Public Const LED3 As Byte = 7 ' LED 3 Public Const LED4 As Byte = 8 ' LED 4 Public Const LED5 As Byte = 9 ' LED 5 Public Const LED6 As Byte = 10 ' LED 6 (right-most) Public Const SERVO2 As Byte = 13 ' Servo 2 (top) Public Const SERVO1 As Byte = 14 ' Servo 1 (bottom) Public Const SPEAKER As Byte = 15 ' Speaker Public Const PBTN1 As Byte = 16 ' Left push-button Public Const PBTN2 As Byte = 17 ' Middle push-button Public Const PBTN3 As Byte = 18 ' Right push-button Public Const ANTR As Byte = 19 ' Right antenna/feeler Public Const ANTL As Byte = 20 ' Left antenna/feeler Public Const POWER As Byte = 21 ' Power pin Public Const BXLED1 As Byte = 25 ' Red LED on BasicX chip Public Const BXLED2 As Byte = 26 ' Green LED on BasicX chipThis should make programming the BugBrain easier, or at least easier to read.
IdeasNow all that is left to do is some hands-on work with the BugBrain. Here are some ideas to get you started:
Submitted: 11/07/2003 Article content copyright © James Matthews, 2003.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -