Slave Control of BrainStem Example
Last Modified: 2006-11-03
find:

basket

Acroname Robotics PDF webpage version Slave Control of BrainStem Example  PDF

Related
Products

Product image for Brainstem GP 1.0 Module
Brainstem GP 1.0 Module

Contents

Introdution

In this example, a BrainStem acts a slave controller to a BasicX BX-24 microcontroller.  The BrainStem receives command packets from the BX-24 and sends data packets back to the BX-24.  As a slave, the BrainStem requires no programming.  A library file written for the BX-24 contains functions for sending and receiving packets.  It also has constants for the command codes and some wrapper functions for reading the analog inputs, reading the GP2D02 ranger input, and setting servo positions. 

The BX-24 uses Visual Basic syntax.  This example may be used as a starting point when working with different dialects of the BASIC language on other systems. 

Heartbeat

In its default configuration, the BrainStem is constantly sending heartbeat packets to the host to check the status of the link.  A host application such as the Console must continually receive these heartbeats and reply to them, otherwise the Stem thinks the link is down and does not send any data to the host.  For a small controller like the BX-24, it is difficult to handle these incoming heartbeat packets.  It is possible to deactivate them by using the Stem's internal heartbeat mode.  This mode makes the heartbeat light blink automatically and prevents any heartbeat packets from being sent to the host controller. 

To configure the BrainStem for internal heartbeat mode, enter the following commands from the Console:

2 18 5 1 /* cmdVAL_SET, set heartbeat to quiet mode */ 2 19 /* cmdVAL_SAV, save settings to EEPROM */

The first is the cmdVAL_SET command for switching to internal heartbeat mode.  The second is the cmdVAL_SAV command for saving the current system settings to the EEPROM.  When the Stem is turned off and on again, its heartbeat light will blink automatically without being connected to the host. 

Source Code

Both devices use TTL voltage levels so no level shifter is required.  The BX-24 inverts its serial TX output at pin P1 and its serial RX input at pin P2.  The BrainStem does not have inverters on its serial lines.  When using pins P1 and P2 on the BX-24, inverters must be added on each line to get proper communication between the two devices. 

The two key routines are sendPacket and getPacket .  These routines handle transmission and reception of properly formatted BrainStem packets.  A packet consists of an address byte, a size byte, and data.  The size byte is the number of data bytes. 

Prior to calling the sendPacket routine, the bbuff character array must be initialized with packet size and packet data.  Byte 0 must have the packet size.  The data is placed in the array starting at byte 1.  The routine sends the Stem address constant, then the packet size, then loops to send out the packet data. 

The getPacket routine must be called immediately after sendPacket if a reply is expected from the Stem.  The routine waits for an address byte, then discards it.  In this example, there is only one Stem so the address does not matter.  Then the routine waits for a size byte and stores it in byte 0 of the buffer.  Then it waits for the data bytes and stores them starting at byte 1 of the buffer.  It stops after receiving the number of data bytes specified by the size byte. 

The readA2D function sends a cmdA2D_RD command and receives a cmdDEV_VAL packet.  A value of 128 is added to the analog input ID.  That is a flag for directing the data back to the host BX-24.  The 8 most significant bits of the A2D reading are in byte 3 of the data.  The function returns that value. 

The readIR02 function sends a cmdIR02_RD command and receives a cmdDEV_VAL packet.  The result is in byte 3 of the data so the function returns that value. 

The servoabs routine does not expect any data.  It sends a cmdSRV_ABS command to drive a servo to a specified position. 

These three routines are examples of typical slave controller operations.  They may be copied and modified to perform other tasks.  This code may be included in a BX-24 project that also includes the SerialPort.bas serial communication library file.  That file is included in the software that comes with the BX-24. 

' filename: stemlib.bas ' BX-24 to BrainStem Interface Software ' Wrappers for packet TX and RX ' Created 7-25-2001 ' Mark Whitney ' Acroname Inc. public const bstem as byte=2 public bbuff(0 to 12) as byte public const cmdDEV_VAL as byte=4 public const cmdVAL_GET as byte=17 public const cmdVAL_SET as byte=18 public const cmdVAL_SAV as byte=19 public const cmdSRV_SAV as byte=20 public const cmdVM_RUN as byte=21 public const cmdVM_KILL as byte=22 public const cmdDEBUG as byte=23 public const cmdRESET as byte=24 public const cmdA2D_RD as byte=25 public const cmdDIG_CFG as byte=26 public const cmdDIG_IO as byte=27 public const cmdDIG_RST as byte=28 public const cmdPTIME_RD as byte=29 public const cmdIR02_RD as byte=30 public const cmdSRV_CFG as byte=31 public const cmdSRV_LMT as byte=32 public const cmdSRV_ABS as byte=33 public const cmdSRV_REL as byte=34 public const cmdSRV_RFLX as byte=35 public const cmdSRV_STOP as byte=36 public const cmdIIC_RD as byte=37 public const cmdRAW_INPUT as byte=38 public const cmdTMR_SET as byte=39 public const cmdRFLXE_CFG as byte=40 public const cmdRFLXE_CHK as byte=41 public const cmdCTR_SET as byte=42 public const cmdCTR_CT as byte=43 public const cmdRAIL as byte=44 public const cmdFUNC as byte=45 public const cmdMPD_SET as byte=46 public const cmdMPD_CHK as byte=47 public const cmdWINDOW as byte=48 public const cmdERRAMP as byte=49 public const cmdERRATT as byte=50 public const cmdPAD_IO as byte=51 public const cmdPAD_INPUT as byte=52 public const cmdPWINDOW as byte=53 public const cmdPERRAMP as byte=54 public const cmdPERRATT as byte=55 public const cmdPTMR_SET as byte=56 public const cmdPCTR_SET as byte=57 public const cmdPCTR_WR as byte=58 sub sendPacket() dim ii as byte call PutByte(bstem) call PutByte(bbuff(0)) for ii=1 to bbuff(0) call PutByte(bbuff(CInt(ii))) next end sub sub getPacket() dim bflag As Boolean dim bchar as byte dim ii as byte do call GetByte(bchar,bflag) loop while (bflag=False) do call GetByte(bbuff(0),bflag) loop while (bflag=False) for ii=1 to bbuff(0) do call GetByte(bbuff(CInt(ii)),bflag) loop while (bflag=False) next end sub function readA2D(ByVal bch as byte) as byte bbuff(0)=2 bbuff(1)=cmdA2D_RD bbuff(2)=128+bch call sendPacket() call getPacket() readA2D=bbuff(3) end function function readIR02() as byte bbuff(0)=2 bbuff(1)=cmdIR02_RD bbuff(2)=128 call sendPacket() call getPacket() readIR02=bbuff(3) end function sub servoabs(ByVal bnum as byte, ByVal babs as byte) bbuff(0)=3 bbuff(1)=cmdSRV_ABS bbuff(2)=bnum bbuff(3)=babs call sendPacket() end sub

Revision History:

  • 2002-06-26: Example Created.
 

Related Examples:

Slave Control of BrainStem with a BasicX and Microsfot Visual Basic Example

Using Visual Basic to Control a Robot Truck and BrainStem Controllers

voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved.