PDA

View Full Version : A/D on 16F767 for light tracking.



Ryan7777
- 7th November 2007, 18:53
Hey folks, I have some code here that doesn't seem to want to work. I think i got some of it from some example somewhere, which i've looked for again but can't seem to find, so if you wrote it, thank you and i hope you don't mind if i borrow it! anyway, Im using this for a light following robot and this is just part of the light tracking routine. the code is large so i won't post everything here, just the part that doesn't seem to run. I've isolated it so that just this bit of code and the motion control routine, which i know works. I'm using TAOS TSL14S Light to Voltage converters for my light sensors, these work pretty much like a LDR in a divider network without the extra room needed for resistors: i.e. just a VDD, VSS and output voltage. they do work according to my scope. I've also taken them out of circuit and pulled one A/D port up and the other down with resistors to know that i should have enough unbalance to get a result if everything is working as it should..

ALSO, i dont know if i like this code, it is either full left, or full right, I'd like a little center margin, like fuzzy logic, so if there is a small difference between the two sensors it'll still track straight, and only when it goes over a threshold, one way or the other will it go in that direction. but I'm having trouble thinking that through.. i've got a pretty busy life and can't spend as much time thinking about PBP code as i'd like!

so here is what I have, there might be some settings that are duplicated, or contradict each other, so feel free to point them out or point and laugh!

@ DEVICE PIC16F767, WDT_ON, INTRC_OSC_NOCLKOUT, MCLR_ON, PROTECT_OFF, PWRT_ON

OSCCON.4 = 1 ' SET INT OSC TO 8 MHZ

OSCCON.5 = 1 ' }

OSCCON.6 = 1 ' SET INT OSC TO 8 MHZ

DEFINE OSC 8 ' DEFINE 0SC AT 8 MHZ FOR PBP

CMCON = 7

' -------------------------------------------------------------

' A/D CONVERSION SETUP
ADCON1= %01001100

DEFINE ADC_BITS 10 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 5 ' ADC clock source (Fosc/16)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)

TRISA.0 = 1 ' SET TO INPUT FOR LEFT TLS SENSOR

TRISA.1 = 1 ' SET TO INPUT FOR RIGHT TLS SENSOR

' -------------------------------------------------------------

LIGHTSEEK:

ADCON0 = %01000000 ' SET FOSC/16, SET ADC CHANNEL 0, ENABLE ADC MODULE

PAUSEUS ' 50uS PAUSE TO ALLOW SAMPLING CAPACITOR TO CHARGE

ADCON0.2 = 1 ' START CONVERSION

WHILE ADCON0.1 = 1:WEND ' WAIT FOR CONVERSION TO COMPLETE

LFT_TLS.HIGHBYTE = ADRESH ' WRITE TO HIGHBYTE OF LFT_TLS

LFT_TLS.LOWBYTE = ADRESL ' WRITE TO LOWBYTE OF LFT_TLS

PAUSEUS 100 ' PAUSE 100uS BETWEEN CONVERSIONS

ADCON0 = %01001000 ' SET FOSC/16, SET ADC CHANNEL 0, ENABLE ADC MODULE

PAUSEUS 50 ' 50uS PAUSE TO ALLOW SAMPLING CAPACITOR TO CHARGE

ADCON0.2 = 1 ' START CONVERSION

WHILE ADCON0.1 = 1:WEND ' WAIT FOR CONVERSION TO COMPLETE

RHT_TLS.HIGHBYTE = ADRESH ' WRITE TO HIGHBYTE OF RHT_TLS

RHT_TLS.LOWBYTE = ADRESL ' WRITE TO LOWBYTE OF RHT_TLS

PAUSEUS 50

IF LFT_TLS > RHT_TLS THEN FWDLFT_TRN: ' IF MORE LIGHT TO LEFT, TURN LEFT

IF RHT_TLS > LFT_TLS THEN FWDRHT_TRN: ' IF MORE LIGHT TO RIGHT, TURN RIGHT

GOSUB FORWARD: ' GOTO FORWARD SUBROUTINE

RETURN

Ryan7777
- 10th November 2007, 05:56
I switched to ADCIN and everything works now as far as the A/D parts, but if anyone wants to school me in getting the direct way right, feel free... I hated to go ahead and post a reply to myself and drive my post to the top, but I didn't want anyone wasting their time on me either! BUT i still can't get the "fuzzy logic" part down.. I've done it with a single variable before, say, limiting a temperature alarm to sounding when a temperature goes above or below a certain value by a few points... I've tried the following code from John Iovine's book with no luck as well (using ADCIN and all variables are declared.. it just seems the code its self won't work)

diff = 10

IF LFT_TLS = RHT_TLS THEN Start:

IF LFT_TLS > RHT_TLS THEN Greater:

IF LFT_TLS < RHT_TLS THEN Lesser:

Greater:
b2 = LFT_TLS - RHT_TLS
IF b2 > diff THEN
GOSUB FWDLFT_TRN:
GOTO Start:
ELSE
GOTO Start:
ENDIF

Lesser:
b2 = RHT_TLS - LFT_TLS
IF b2 > diff THEN
GOSUB FWDRHT_TRN:
GOTO Start:
ELSE
GOTO Start:
ENDIF

which should dampen out the response a little, like if they are with in 10 points of each other it counts them as being equal and the bot should go straight. it just seems to make my light sensors clamp to either the high end of thier range or the low end, and adjusting
"diff" doesnt seems to make a difference.. i've tried switching < for > and everything in between... plus his code seems to use up a lot of code space for no more than what is there? anyone have anything better? ( i know i could lose one if then if i move lesser: up, im just printing as close to his code as i can for the sake of it)

again thanks ahead of time for any and all help!