SRF08, BotBoard2 and Basic Atom

Hi All,
Could i trouble anyone for some help/advice with the following.

Initially, I’m looking to connect a SRF08 to my botboard2/Basic Atom (not pro) to test it.

Then I’ll be plugging it (SRF08) directly into the MicroMagic Systems SMB and will be using the BotBoard2/Basic Atom to talk to the SMB/p.Brain for object avoidance.

Matt at MicroMagic has a system working with his own built dev board and a Basic Stamp, but since i’ve already got a botboard2 and both basic atom and basic atom pro controllers, i thought it was worth using them for the task.

I’ve not been able to find a similar project, so to complete the first task i intend to connect the SRF08 to the BB2 as follows:

SRF08 5v -> BB2 pin 8, 5v
SRF08 SDA -> BB2 pin 8, signal
SRF08 SCL -> BB2 pin 9, signal
SRF08 0v ground -> BB2 pin 8, ground

I’ve attached a jumper to the 5v side of the (8, 9, 10, 11) bank of pins.

the BB2 has a 9v battery attached to VL.

Here’s a pic of the test set-up
http://www.us2design.co.uk/hex/SRF08.jpg

I plan to use this code to test the SRF08 - which i must admit means not a huge amount to me at the moment and was sourced on my travels around the net:

SDA      Con P8     ' Define Data pin

SCL      Con P9     ' Define Clk pin

SRF08   Con 0xE0   ' 1st Sonar I2C Address

CmdReg   Con 0      ' Sonar Command register

LightReg Con 1      ' Sonar Light sensor register

RangeReg Con 2      ' Sonar 1st Range register
Light    Var Byte   ' Light sensor

Range Var Word   ' 16 bit variable for Range
Main
    
i2cout SDA, SCL, SRF08, CmdReg, [81]
    
pause 66
    
i2cin SDA, SCL, SRF08, LightReg, [Light, Range.HighByte, Range.LowByte]
    
Serout S_OUT, i9600, "Light1 = 0x", HEX Light, ", Range1 = ", DEC Range, " cm, "] ' Use terminal window
goto main

Assuming the above works i have some code Matt from MMS gave me - which works with the basicStamp.

If anyone would be kind enough to let me know whether it’ll work with the Basic Atom/BotBoard2 - or if not what i need to do to get it working, i’d much appreciated it.

that code is attached HERE

quick update on the above request for assistance.

the code i posted works and ive been able to test the SRF08 with the BB2 in isolation.

i now need to convert the attached code (for a basicstamp) to work with a Basic Atom. I tried running it through the Basic Atom IDE app. hoping it would offer some hints to fix errors, but no such luck.

if anyone can ofer some help i’d be very grateful :wink:

thsnks, ds

 ' The main loop uses the I2C out command to start the PING on the SFR08,
' waits 70mS and then reads back from the HexEngine I2C port to gather the
' SFR08 echo. once it has the data, the light level is used to control the
' height of the ehxapod and the distance from the ranger is used to contol
' the y translate of the hexapod, if the SFR08 module is mounted on the
' front of the hexapdo this gives the effect of the hexapod moving away from
' an object in front of it, and cowering if a hand passes of the head.
'
' ----- Revision History ]------------------------------------------------
'
' ----- Compilation Switches ]--------------------------------------------

'#DEFINE FULL_DEBUG   ' COMPILE WITH PS2 DEBUG INFORMATION
'#DEFINE PIP_MODE_1   ' PIP MODE 1 FOR MORE ROBUST COMMS, 0 FOR SIMPLER COMMS
'
'
' ----- I/O Definitions ]-------------------------------------------------
LED     PIN 0     ' CHANGE TO SUIT YOUR CONFIGURATION
SER_OUT PIN 5     ' CHANGE TO SUIT YOUR CONFIGURATION
SER_IN  PIN 6     ' CHANGE TO SUIT YOUR CONFIGURATION

' ----- Constants ]-------------------------------------------------------

CMD_PIP_ESCAPE CON         $7d
CMD_PIP_HEADER CON         $7e
CMD_PIP_XOR    CON         $20

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T1200       CON     813
    T2400       CON     396
    T4800       CON     188
    T9600       CON     84
    T19K2       CON     32
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T1200       CON     2063
    T2400       CON     1021
    T4800       CON     500
    T9600       CON     240
    T19K2       CON     110
    T38K4       CON     45
    T57K6       CON     23
    T115K2      CON     2
  #CASE BS2PX
    T1200       CON     3313
    T2400       CON     1646
    T4800       CON     813
    T9600       CON     396
    T19K2       CON     188
    T38K4       CON     84
#ENDSELECT

COM_SevenBit        CON     $2000
COM_Inverted        CON     $4000
COM_Open            CON     $8000

Baud            CON     T19k2       ' match DEBUG


' ----- Variables ]-------------------------------------------------------
PIP_Byte  VAR Byte
PIP_Len   VAR Byte
PIP_Temp  VAR Byte
PIP_Buff  VAR Byte(17)
PIP_Cs    VAR Byte
Range     VAR Byte
Light     VAR Byte
W_Temp    VAR Word
Temp      VAR Byte

' ----- Initialization ]--------------------------------------------------
Setup:
HIGH SER_OUT


' ----- Program Code ]----------------------------------------------------
Main:
  ' ALLOW THE HEXENGINE TO POWER UP
  PAUSE( 1500 )
  ' RESET LEGS IF ALREADY POWERD UP
  PIP_Buff(0) = "r"
  PIP_len = 1
  GOSUB Send_PIP_Packet

  DEBUG CLS

  DO
  DEBUG HOME
  ' WAKE HEXAPOD
  PIP_Buff(0) = "+"
  PIP_len = 1
  GOSUB Send_PIP_Packet

' ----- I2C TEST Code ]----------------------------------------------------

' PINGS SRF08 MODULE CONNECTED TO I2C PORT, AND RETREIVES ECHO DATA.
' THIS ASSUMES THERE IS AN SRF08 MODULE CONNECTED, AND THAT IT HAS
' THE DEFAULT ADDRESS OF E0h
START_SFR08_PING:
  HIGH  LED
  ' SEND I2C DATA OUT OF I2C PORT
  PIP_Buff(0) = "I"
  PIP_Buff(1) = $e0   ' I2C ADDRESS
  PIP_Buff(2) = 1     ' I2C SEND DATA COUNT
  PIP_Buff(3) = 0     ' I2C REGISTER ADDRESS
  PIP_Buff(4) = 82    ' I2C DATA (PING.. RETURN DATA IN uSECONDS)
  PIP_len = 5
  GOSUB Send_PIP_Packet
  LOW LED

  ' WAIT FOR SFR08 ECHO MINIUMUM 65mS
  PAUSE(70)

GET_SRF08_ECHO:
  PIP_Buff(0) = "i"
  PIP_Buff(1) = $e0     ' I2C ADDRESS
  PIP_Buff(2) = 3       ' I2C DATA READ COUNT
  PIP_Buff(3) = 1       ' I2C REGISTER START ADDRESS
  PIP_len = 4
  GOSUB Send_PIP_Packet

  ' GRAB RETURN PACKET
  PIP_len = 4
  ' WAIT FOR HEADER, IGNORE PACKET LENGTH & CHECK SUM
  SERIN SER_IN, Baud , 100, NO_I2C_DATA, [WAIT($7E), PIP_Len, STR PIP_Buff\PIP_Len]

  ' OUTPUT DATA TO DEBUG
  #IF FULL_DEBUG #THEN
  DEBUG "LEN: ", DEC2 PIP_Len, " TYPE: '" , PIP_Buff(0), "' "
  #ENDIF
  ' STORE RANGE RESULT IN WORD
  W_Temp = Pip_Buff(2) << 8
  W_Temp = W_Temp | PIP_Buff(3)
  Temp = PIP_Buff(1)
  #IF FULL_DEBUG #THEN
  DEBUG "LIGHT: ", DEC3 Temp, " RANGE: ", DEC4 W_Temp, " uS"
  #ENDIF

  ' REDUCE AND CLAMP RANGE RESULT
  W_Temp = W_Temp / 10
  IF W_Temp < 20 THEN W_Temp = 20
  IF W_Temp > 150 THEN W_Temp = 150

  ' INTERPOLATE RANGE RESULT
  Range = ( ( W_Temp - 20 ) * ( 255 ) ) / ( 150 - 20 )
  DEBUG CR, "RANGE: ", DEC3 Range

  ' CLAMP LIGHT RESULT
  IF Temp > 150 THEN Temp = 150

  ' INTERPOLATE RANGE RESULT
  Light = ( ( Temp ) * ( 255 ) ) / ( 150 )
  DEBUG CR, "LIGHT: ", DEC3 Light


SEND_BODY_CMD:
  PIP_Buff(0) = "B"
  PIP_Buff(1) = 0       '
  PIP_Buff(2) = 0       '
  PIP_Buff(3) = 0       '
  PIP_Buff(4) = 0       '
  PIP_Buff(5) = (255-Range) -128       '
  PIP_Buff(6) = Light -128       '
  PIP_len = 7
  GOSUB Send_PIP_Packet



  GOTO  CONTINUE

NO_I2C_DATA:
  DEBUG CLREOL, "NO I2C DATA"



CONTINUE:

  LOOP

  END



' ----- PIP Packet ]----------------------------------------------------
' SENDS PIP PACKET STORED IN PIP_BUFF OF PIP_LEN LENGTH
' MODE 1 or MODE 0, DEPENDS ON COMPILATION SWITCH "PIP_MODE_1"

' SENDS PIP PACKET STORED IN PIP_BUFF OF PIP_LEN LENGTH
'
Send_PIP_Packet:
  ' SEND HEADER
  SEROUT SER_OUT, Baud, [CMD_PIP_HEADER]
  ' SEND PIP LENGHT
  PIP_Byte = PIP_Len
  GOSUB Send_Byte_Check_Codes
  ' SETUP CHECK SUM
  PIP_Cs = 0
  ' SEND PACKET
  FOR PIP_Temp = 0 TO PIP_Len-1
    PIP_Byte = PIP_Buff( PIP_Temp )
    PIP_Cs = PIP_Cs + PIP_Byte
    GOSUB Send_Byte_Check_Codes
  NEXT
  ' SEND CHECK SUM
  PIP_Byte = $ff - PIP_Cs
  GOSUB Send_Byte_Check_Codes

  RETURN


' SENDS PIP BYTE TO SERIAL PORT AND CHECKS PIP CODES
'
Send_Byte_Check_Codes:
#IF PIP_MODE_1 #THEN
  ' CHECK CODE AGAINST ILLEGAL CODES
  IF( PIP_Byte = CMD_PIP_HEADER | PIP_Byte = CMD_PIP_ESCAPE ) THEN
    ' SEND ESCAPED CODE
    SEROUT SER_OUT, Baud, [CMD_PIP_ESCAPE, PIP_Byte ^ CMD_PIP_XOR]
  ELSE
#ENDIF
    ' SEND NORMAL CODE
    SEROUT SER_OUT, Baud, [PIP_Byte]

#IF PIP_MODE_1 #THEN
  ENDIF
#ENDIF

  RETURN

What problems are you having with it? I currently don’t have my Basic Atom hooked up to anything, so it would take me awhile to set it up…

Does it compile Ok?

Areas I would check would be parameters to Serin. In particular the Baud parameter.

From my quick read through the code it looks like the SRF08 is stored on the other processor and you are simply sending commands to the other board to do the actual read? Is this correct?

How are your boards and sensors configured?

Kurt

Kurte, thanks for the reply :smiley:

This is code which matt at micromagic systems let me have - he has it working using his own carrier board and a basicStamp.

I’m trying out his SMB/p.brain/HexEngine and hoping to reuse the parts from my ‘donor’ phoenix - these are a BotBoard2 and Basic Atom - i do have an Atom Pro, but as i understand it the Stamp’ code should be very close to Atom code - so i’m looking to get that working first.

Your initial observations are correct. The SRF08 sensor is attached to the SMB (motherboard) and the Atom is sending commands to it to start the read, wait, read the echo - it then uses the reading to control the hex by sending direction commands. In practice, the hex walks along and as soon as it gets too close to an object it stops, backs away, turns until it fins a clear space then continues walking.

I opened the code in the Atom IDE and when i clicked compile it generated and error with the ‘constant’ ‘stamp’ .

This is where my currently ‘thin’ knowledge bottoms out. I assume that within constants - the $STAMP section needs to be replaced with an ATOM equivalent.

If you’re able to offer any more pointers i’d much appreciate it.

You can see Matts Hex’ working with the Basic Stamp HERE

I did a couple of quick changes to make it compile. First I got rid of the Basic stamp section of constants. I Defined Baud as N19200. There also is an incompatibility of if statements. So I broke the one line version into the proper syntax. Finally they had DO… LOOP which the atom does not have, so converted to: do … while 1 which should work. I did get it to compile, but I did not download it or verify it would work… Good Luck

Kurt

[code] ’ The main loop uses the I2C out command to start the PING on the SFR08,
’ waits 70mS and then reads back from the HexEngine I2C port to gather the
’ SFR08 echo. once it has the data, the light level is used to control the
’ height of the ehxapod and the distance from the ranger is used to contol
’ the y translate of the hexapod, if the SFR08 module is mounted on the
’ front of the hexapdo this gives the effect of the hexapod moving away from
’ an object in front of it, and cowering if a hand passes of the head.

’ ----- Revision History ]------------------------------------------------

’ ----- Compilation Switches ]--------------------------------------------

'#DEFINE FULL_DEBUG ’ COMPILE WITH PS2 DEBUG INFORMATION
'#DEFINE PIP_MODE_1 ’ PIP MODE 1 FOR MORE ROBUST COMMS, 0 FOR SIMPLER COMMS


’ ----- I/O Definitions ]-------------------------------------------------
LED con p0 ’ CHANGE TO SUIT YOUR CONFIGURATION
SER_OUT con p5 ’ CHANGE TO SUIT YOUR CONFIGURATION
SER_IN con p6 ’ CHANGE TO SUIT YOUR CONFIGURATION

’ ----- Constants ]-------------------------------------------------------

CMD_PIP_ESCAPE CON $7d
CMD_PIP_HEADER CON $7e
CMD_PIP_XOR CON $20

Baud CON N19200 ’ match DEBUG

’ ----- Variables ]-------------------------------------------------------
PIP_Byte VAR Byte
PIP_Len VAR Byte
PIP_Temp VAR Byte
PIP_Buff VAR Byte(17)
PIP_Cs VAR Byte
Range VAR Byte
Light VAR Byte
W_Temp VAR Word
Temp VAR Byte

’ ----- Initialization ]--------------------------------------------------
Setup:
HIGH SER_OUT

’ ----- Program Code ]----------------------------------------------------
Main:
’ ALLOW THE HEXENGINE TO POWER UP
PAUSE( 1500 )
’ RESET LEGS IF ALREADY POWERD UP
PIP_Buff(0) = “r”
PIP_len = 1
GOSUB Send_PIP_Packet

DEBUG CLS

DO
DEBUG HOME
’ WAKE HEXAPOD
PIP_Buff(0) = “+”
PIP_len = 1
GOSUB Send_PIP_Packet

' ----- I2C TEST Code ]---------------------------------------------------- 

' PINGS SRF08 MODULE CONNECTED TO I2C PORT, AND RETREIVES ECHO DATA. 
' THIS ASSUMES THERE IS AN SRF08 MODULE CONNECTED, AND THAT IT HAS 
' THE DEFAULT ADDRESS OF E0h 
START_SFR08_PING: 
  HIGH  LED 
  ' SEND I2C DATA OUT OF I2C PORT 
  PIP_Buff(0) = "I" 
  PIP_Buff(1) = $e0   ' I2C ADDRESS 
  PIP_Buff(2) = 1     ' I2C SEND DATA COUNT 
  PIP_Buff(3) = 0     ' I2C REGISTER ADDRESS 
  PIP_Buff(4) = 82    ' I2C DATA (PING.. RETURN DATA IN uSECONDS) 
  PIP_len = 5 
  GOSUB Send_PIP_Packet 
  LOW LED 

  ' WAIT FOR SFR08 ECHO MINIUMUM 65mS 
  PAUSE(70) 

GET_SRF08_ECHO: 
  PIP_Buff(0) = "i" 
  PIP_Buff(1) = $e0     ' I2C ADDRESS 
  PIP_Buff(2) = 3       ' I2C DATA READ COUNT 
  PIP_Buff(3) = 1       ' I2C REGISTER START ADDRESS 
  PIP_len = 4 
  GOSUB Send_PIP_Packet 

  ' GRAB RETURN PACKET 
  PIP_len = 4 
  ' WAIT FOR HEADER, IGNORE PACKET LENGTH & CHECK SUM 
  SERIN SER_IN, Baud , 100, NO_I2C_DATA, [WAIT($7E), PIP_Len, STR PIP_Buff\PIP_Len] 

  ' OUTPUT DATA TO DEBUG 
  #IF FULL_DEBUG #THEN 
  DEBUG "LEN: ", DEC2 PIP_Len, " TYPE: '" , PIP_Buff(0), "' " 
  #ENDIF 
  ' STORE RANGE RESULT IN WORD 
  W_Temp = Pip_Buff(2) << 8 
  W_Temp = W_Temp | PIP_Buff(3) 
  Temp = PIP_Buff(1) 
  #IF FULL_DEBUG #THEN 
  DEBUG "LIGHT: ", DEC3 Temp, " RANGE: ", DEC4 W_Temp, " uS" 
  #ENDIF 

  ' REDUCE AND CLAMP RANGE RESULT 
  W_Temp = W_Temp / 10 
  IF W_Temp < 20 THEN 
  		W_Temp = 20 
  endif
  IF W_Temp > 150 THEN 
  		W_Temp = 150 
  endif

  ' INTERPOLATE RANGE RESULT 
  Range = ( ( W_Temp - 20 ) * ( 255 ) ) / ( 150 - 20 ) 
  DEBUG CR, "RANGE: ", DEC3 Range 

  ' CLAMP LIGHT RESULT 
  IF Temp > 150 THEN 
  		Temp = 150 
  endif

  ' INTERPOLATE RANGE RESULT 
  Light = ( ( Temp ) * ( 255 ) ) / ( 150 ) 
  DEBUG CR, "LIGHT: ", DEC3 Light 


SEND_BODY_CMD: 
  PIP_Buff(0) = "B" 
  PIP_Buff(1) = 0       ' 
  PIP_Buff(2) = 0       ' 
  PIP_Buff(3) = 0       ' 
  PIP_Buff(4) = 0       ' 
  PIP_Buff(5) = (255-Range) -128       ' 
  PIP_Buff(6) = Light -128       ' 
  PIP_len = 7 
  GOSUB Send_PIP_Packet 



  GOTO  CONTINUE 

NO_I2C_DATA: 
  DEBUG CLREOL, "NO I2C DATA" 



CONTINUE: 

while 1

END

’ ----- PIP Packet ]----------------------------------------------------
’ SENDS PIP PACKET STORED IN PIP_BUFF OF PIP_LEN LENGTH
’ MODE 1 or MODE 0, DEPENDS ON COMPILATION SWITCH “PIP_MODE_1”

’ SENDS PIP PACKET STORED IN PIP_BUFF OF PIP_LEN LENGTH

Send_PIP_Packet:
’ SEND HEADER
SEROUT SER_OUT, Baud, [CMD_PIP_HEADER]
’ SEND PIP LENGHT
PIP_Byte = PIP_Len
GOSUB Send_Byte_Check_Codes
’ SETUP CHECK SUM
PIP_Cs = 0
’ SEND PACKET
FOR PIP_Temp = 0 TO PIP_Len-1
PIP_Byte = PIP_Buff( PIP_Temp )
PIP_Cs = PIP_Cs + PIP_Byte
GOSUB Send_Byte_Check_Codes
NEXT
’ SEND CHECK SUM
PIP_Byte = $ff - PIP_Cs
GOSUB Send_Byte_Check_Codes

RETURN

’ SENDS PIP BYTE TO SERIAL PORT AND CHECKS PIP CODES

Send_Byte_Check_Codes:
#IF PIP_MODE_1 #THEN
’ CHECK CODE AGAINST ILLEGAL CODES
IF( PIP_Byte = CMD_PIP_HEADER | PIP_Byte = CMD_PIP_ESCAPE ) THEN
’ SEND ESCAPED CODE
SEROUT SER_OUT, Baud, [CMD_PIP_ESCAPE, PIP_Byte ^ CMD_PIP_XOR]
ELSE
#ENDIF
’ SEND NORMAL CODE
SEROUT SER_OUT, Baud, [PIP_Byte]

#IF PIP_MODE_1 #THEN
ENDIF
#ENDIF

RETURN
[/code]

Kurt,
Hopefully you pick-up my ‘thank you’ PM - if not, thanks for your help - very much appreciated.

The code compiled and uploaded to my Atom without any problems.

The SRF08 jumped into life, but the legs of my hex didn’t - i was rushing to try it before work this morning, but i’ll have chance to carefully check through the connections this evening and with luck i’ll just be something i’ve plugging in incorrectly.

I ‘may’ be back asking for more hints later - or better still, i’ll be posting a vid of my hex avoiding things. :open_mouth: