BB's File Cabinet
Misc

 

 

 

Robotic - RoboBricks2 - IRDistance2 (Rev. C)

 

                                

1.0 Introduction

The IRDistance2 module works in conjunction one or two Sharp GP2D12 infrared optical distance measuring units to return distances in the the general vicinity of 10 cm to 60 cm.  Once the distance to an object decreases to less than10 cm, the value reported by the GP2D12 will invert and begin to decrease in value as the distance to an object decreases.  These inverted values are unreliable however and therefore of no practical use.  

The module is completely opto-isolated to prevent the extremely variable current draw of the module (9mA to 220mA) from disrupting the digital circuitry.  The GP2D12 itself is powered by a battery connected to the Bus Power header on the Controller28 brick and supplied to the IRDistance2 via the dedicated power lines on the CAN Bus.  The IRDistance2 circuitry includes a voltage regulator to insure that the Bus Power delivered to the GP2D12 is a regulated +5 volts.

2.0 Revision Details

The historical detail of the revisions between subsequent versions of this brick will be detailed here.  Only revisions that create differences in the operation, programming or hookup of two modules with different revision numbers will be documented.

Rev C  - Rev C changed . 

3.0 Hookup Details

4.0 Assigned Address

The IRDistance2 has an assigned address of "0x04" but the user can reassign a different address using the Address_Set command  so long as the user assigned address is not being used by any other module connected to the CAN bus (see RoboBricks2 - General Information and/or Common Protocol ).

5.0 Default Settings

Not applicable.

6.0 Programming Hints

  • IRDistance2  Programming Command Table
  • IRDistance2 Command info:
    • The Raw Get command returns the sensor's last raw A/D value distance value within the range 0000:0000 (0x00) and 1111:1111 (0xff).  The value returned is not a metric (cm, ft, etc.) but is instead a relative value where 0x00 is "far" and 0xff is "near.
    • The Smooth Get command returns the largest value of the sensor's  last eight readings.  As with the Raw Get command, the value returned is not a metric (cm, ft, etc..) but is instead a relative value where 0x00 is "far" and 0xff is "near."
    • The Linear Get  command is not presently implemented. 

7.0  Test Code

This test code is written in MikroBasic (from MikroElektronika) and records the output of a single GP2D12 on the LCD module.  The Smooth_Get command and the Raw_Get command may be interchanged without need of changing any of the other code.

          

'******************************************************************************
' PROJECT: IRDistance2_Test.pbas

' PURPOSE: Use to test the function of a Sharp GP2D12.

' TEST PROCEDURE:
' 1. Run the program with the Low_Set = 0 and step thru values for
' the High_Set command. The first goal is to find the value for which
' the servo either stops or reaches its slowest speed. This will then
' will be the High_Set value to use to halt the servo.
' 2. Assign High_Set value that was determined in step 1 above. Next,
' find the Low_Set value which halts the servo with no creep. The
' resulting High_Set and Low_Set values to use when stopping the servo.

' DATE: 06/28/07

' MICROPROCESSOR: Microchip PIC18F2620
'
'
'******************************************************************************
'
' ====================== Register Setup =========================
'_CONFIG1L: 0xFFFF
'_CONFIG1H: 0x0032
'_CONFIG2L: 0x00F9
'_CONFIG2H: 0x00FE
'_CONFIG3L: 0xFFFF
'_CONFIG3H: 0x00F8
'_CONFIG4L: 0x00BA
'_CONFIG4H: 0xFFFF
'_CONFIG5L: 0x00FF
'_CONFIG5H: 0x00FF
'_CONFIG6L: 0x00FF
'_CONFIG6H: 0x00FF
'_CONFIG7L: 0x00FF
'_CONFIG7H: 0x00FF
'
'------------------| Timer0 Config Detail |-----------------------------------
' INTCON register = 0xa0
' bit 7: GIE = 1 enables all unmasked interrupts
' bit 6: PEIE = 0 disable periphial interrupts
' bit 5: TMR0IE = 1 enables TMR0 overflow interrupt
' bit 4: INT0IE = 0 disables external interrupts
' bit 3: RBIE = 0 disables RB port change interrupt
' bit 2: TMR0IF = 0 clear TMR0 Overflow Interrupt Flag bit
' bit 1: INT0IF = 0 clear external interrupt flag
' bit 0: RBIF = 0 clear RB port change interrupt flag
'
' T0CON register = 0xc7
' bit 7: TMR0ON = 1 enables Timer0
' bit 6: T08Bit = 1 configure as an 8-bit timer
' bit 5: T0CS = 0 Transition on internal instruction cycle clock (CLK0)
' bit 4: T0SE = 0 increment on low-to-high transition of T0CLI pin
' bit 3: PSA = 0 assign prescaler
' bit 2: TOPS2 = 1 assign 1:256 prescaler value
' bit 1: T0PS1 = 1
' bit 0: T0PS0 = 1
'
'******************************************************************************

program IRDistance2

'---------------------| PROGRAM CONSTANTS |------------------------------------

' RoboBrick2 addresses
const Laser1 as byte = 1
const MiniMotor2 as byte = 2
const MidiMotor2 as byte = 3
const IRDistance2 as byte = 4
const Shaft2 as byte = 5
const Orient5 as byte = 6
const Compass8 as byte = 7
const IO8 as byte = 8
const Sonar2 as byte = 9
const Voice1 as byte = 10
const Servo4 as byte = 11
const Camera1 as byte = 12
const Ether1 as byte = 13
const Flame1 as byte = 14
const IMU1 as byte = 15
const Sonar6 as byte = 16
const USB1 as byte = 17
const Wireless1 as byte = 18
const Controller24 as byte = 24
const Controller28 as byte = 28
const LCD32 as byte = 32

' IRDistance2 Programming Command Constants
const Raw_Get as byte = 0
const Smooth_Get as byte = 2
const Linear_Get as byte = 4
const Linearize_Dist_Set as byte = 0x10

' LCD32 Programming Command Constants
const Line_Feed as byte = 0x0a
const Form_Feed as byte = 0x0c
const Carriage_Return as byte = 0x0d
const Column_Set as byte = 0x10

'---------------------| GLOBAL VARIABLES |-------------------------------------
dim distance as byte
dim output as string[17]

'---------------------| SUB FUNCTIONS & PROCEDURES |---------------------------

sub procedure delay_secs(dim seconds as byte)
dim count as byte
for count = 0 to (seconds - 1)
delay_ms(1000)
next count
end sub

sub procecdure print_to_LCD(dim value as byte)
dim index as byte
send_addr_byte(LCD32) ' Alert LCD module
send_data_byte(Form_Feed) ' Clear display and home cursor
ByteToStr(value, output) ' Convert data byte to a string
for index = 0 to 6
send_data_byte(output[index]) ' print the string
next index
end sub

'---------------------| MAIN PROGRAM CODE |------------------------------------
main:
delay_secs(1)

'---------------------| INITIALIZATIONS |--------------------------------------

' Initialze 18F2620 Registers and ports for USART operation
' ADCON Registers
ADCON0 = 0
ADCON1 = 0x0f

' Capture and Compare Register
CMCON = 0x07

' Setup USART
TXSTA = 0x64
SPBRG = 0x01
RCSTA = 0xf0

' Configure port bit direction
trisa = 0xff
trisb = 0
trisc = 0xc0

' Clear the LCD
send_addr_byte(LCD32)
send_data_byte(Form_Feed)

'---------------------| MAIN PROGRAM LOOP |------------------------------------
while true
send_addr_byte(IRDistance2)
send_data_byte(Smooth_Get)
distance = get_data_byte()
print_to_LCD(distance)
delay_secs(1)
wend
end.

 

 

 

 

 

© 2010 All Rights Reserved