Sharp GP2D02 infrared sensor code

lrat

New Member
Dear all,
More then a week ago I posted a similar thread Although, I received some responses (Thank you very much to the contributors) I still couldn't work it out. Since then I have spent hours and hours to translate code from C to Basic, have read all possible information about the GP2D02 on the net but can not have it to work. As explained in my previous thread I realize there are easier sensors to interface, however I am situated on a very remote tiny island and we have no access to shops and can't receive any mail for the next six months. The only I have plenty off is time!
Could somebody please take a look at my code? The code is an extraction of what I found on the LetsMakeRobots.com website (But they are using a GP2D12 sensor which is much easier to interface).
The pulseside to Vin seems to work but on the Vout pin I constantly have an output value of 255.
A useful website explaning the GP2D02 can be found here.
Here is the code:
Code:
Symbol dangerlevel = 70 ' how far away should thing be, before we react?
symbol turn = 300 ' this sets how much should be turned
symbol servo_turn = 700 ' This sets for how long time we should wait for the servo to turn (depending on it´s speed) before we measure distance


main: ' the main loop
gosub sensorcontrol
readadc 0, b1 ' read how much distance ahead

debug
if b1 < dangerlevel then
gosub nodanger ' if nothing ahead, drive forward
else
gosub whichway ' if obstacle ahead then decide which way is better
end if
goto main ' this ends the loop, the rest are only sub-routines

sensorcontrol:
low 3
pauseus 7000 'Pause for 70 ms (7000 us)
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
high 3
pauseus 20	'Pause for 0.2 ms (20 us)
low 3
pauseus 20
return


nodanger:' this should be your combination to make the robot drive forward, these you most likely need to adjust to fit the way you have wired your robots motors
high 5 : high 6 : low 4 : low 7
return


whichway:
gosub totalhalt ' first stop!

'Look one way:
gosub lturn ' look to one side
pause servo_turn ' wait for the servo to be finished turning
readadc 0, b1
gosub totalhalt


'Look the other way:
gosub rturn ' look to another side
pause servo_turn ' wait for the servo to be finished turning
readadc 0, b2
gosub totalhalt

' Decide which is the better way:
if b1<b2 then
gosub body_lturn
else
gosub body_rturn
end if
return

body_lturn:
high 6 : low 5 : low 7 : high 4 ' this should be your combination that turns the robot one way
pause turn : gosub totalhalt
return

body_rturn:
high 5 : low 6 : low 4 : high 7 ' this should be your combination that turns the robot the other way
pause turn : gosub totalhalt
return

rturn:
servo 0, 100 ' look to one side
return

lturn:
servo 0, 200 ' look to the other side
return

totalhalt:
low 4 : low 5 : low 6 : low 7 ' low on all 4 halts the robot!
Servo 0,150 ' face forward
wait 1 ' freeze all for one second
return
I want to apologize beforehand for kind of repeating this question but I'm desperate to have it working. Once finished I will write an article about this project and will post it online with many pictures. I am sure I am not the only one struggling with this.
To the one that can help me I will mail out a bottle of wine!:rolleyes:
Many thanks in advance!
Kind regards,
Luc
 

westaust55

Moderator
Thinking here but assuming you are still using the 28X1 then each command on average will take aorund 0.25 ms to perform at 4 MHz clcok speed so the LOW and HIGH comamnds will already exceed the durations of 0.1 ms for the 8 pulses to initiate a reading.


also 70 ms is 70,000 usec not 7,000 usec

Try
Code:
sensorcontrol:
SETFREQ m8 ; change to 8 Mhz clock speed
low 3  
pause 140 'Pause for 70 ms ; pause for 2 msec with 8MHz clock speed (70,000 us)
high 3   ; will take approx 0.1 ms = pulse 1

low 3

high 3 ; pulse 2

low 3

high 3 ; pulse 3

low 3

high 3 ; pulse 4

low 3

high 3 ; pulse 5

low 3

high 3 ; pulse 6

low 3

high 3 ; pulse 7

low 3

high 3 ; pulse 8

low 3

high 3 ; now do the 2ms high min duration
pause 4 ; pause for 2 msec with 8MHz clock speed
return
 

westaust55

Moderator
Next you seem to be reading the output with this command:
readadc 0, b1 ' read how much distance ahead

but that data according to the datasheet is digital.

Read up on the SHIFTIN command in manual 2 and use the bit bash input if need be.

Now the problem is again timing. At 8Mhz we could be struggling just to perform the clocking signals.

For data input, as well as driving the clock signal you need to read in the data and store the bits.
SHIFTIN is faster by around 4 times than bit bashing.

So maybe try the SHIFTIN command at 8MHz clock speed to clock in the 8 bits of data with MSB first.
 
Last edited:

hippy

Technical Support
Staff member
I think it was a bad decision to name the GP2D02 pins Vout and Vin and not sure what the thinking was behind that. Vin is actually an active low clock from PICAXE to GP2D02, Vout is a digital data line back from the GP2D02 to PICAXE. It is effectively an SPI interface as others have noted. This is clearer from the image on the linked site ...

http://www.electronicsteacher.com/robotics/images/diagram.gif

Apart from having specific timing requirements it should be fairly straight forward to access and read the GP2D02 with a 28X1, though from the timings, it seems you will need to use SPIIN or HSPIIN to read the data.
 
Top