Shop OBEX P1 Docs P2 Docs Learn Events
Fast sensor sweep help — Parallax Forums

Fast sensor sweep help

Special_KSpecial_K Posts: 162
edited 2007-03-29 02:38 in BASIC Stamp
I have a small programming issue that I am hoping that someone can help me out with. I have several sensors in a bracket looks like this /""\ . So both IR and sonar are on each surface (left, center and right)
I am using the GPD02 I know they are not the best but I have done some work with the code that comes with them and have made them cycle faster and convert the output to CM to match the pings output. My solution was to fire all the IR units at once and then wait for the first unit/pin to receive data process it and then go back to looking for the other unit/pins to got inputs. The problem is in this portion of code
ircheck:
XA=XA+1
x=0
IF IN1<>0 THEN
GOTO inleft
ELSEIF IN3<>0 THEN
GOTO inright
ELSEIF IN5<>0 THEN    'code problem...
GOTO incenter             'code problem..
ELSEIF XA<160 THEN
GOTO ircheck
ENDIF
RETURN


The unit that is read last in the if then statement always returns a 0 for distance. In this code it is the center unit but if I switch the code position with right or left IR units they will return a 0. Any help would be greatly appreciated. Full program code bellow

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-26 15:57
    Hello,

    One real issue I would solve first is the fact that you’re using GOTO to jump to a subroutine ending in RETURN. This can cause real problems including restarting the program. Correct these types of problems and that may clear up your other problems. The two may in fact be related. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ZootZoot Posts: 2,227
    edited 2007-03-26 19:41
    Are these the detectors that you enable, wait for a signal back that they're ready, then do a shiftin of the data? I would think if you turn them all on at once, you run the risk of missing one or another of them when they are ready to be clocked. I can't remember the spec on that detector but I think you are waiting for the pin state change as a signal from the detector that the data is ready?. I wonder if while you are shifting in from one detector, the other detector(s) time out and even when you clock them, the data is gone.

    I'd tried just enabling each detector one a time, clocking in the data from each, reporting the results? I know you don't want to take like 230ms to do all three, but if they test OK that way, you've at least narrowed down where the problem is. Is the 160 count a timeout or something? Are you timing out on both the detectors and in your code? 160 loops through there would be very fast -- a lot less than the 70ms that the detector might need to be ready.

    There's also the possibility of interference if all three pulse at once; checking each one in order would help see if that's the case also.

    While the jumps within your subs may be considered by some to be poor style, I don't *think* there are any errors there per se, since you're using the GOTO from within a called subroutine already. I have only done this when I've hit my limit of 4 nested subroutine calls or because I have shared code for the tail ends of two or more subroutines.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • Special_KSpecial_K Posts: 162
    edited 2007-03-27 03:15
    If I fire each GPD02 individually and wait for it to return a reading it slows down the entire sensor process (150ms). The 160 loop is just a number that works. It is enough time to get each GPD02 to return a value. I changed the goto's to gosubs and there is no change. I do not think it was that. I think there is something in the way the ircheck ends.. If I move around the elseif statements the
    ELSEIF IN3<>0 THEN
    GOTO inright
    ELSEIF IN5<>0 THEN    'code problem...
    GOTO incenter             'code problem..
    


    and make them
    ELSEIF IN5<>0 THEN    
    GOTO incenter             
    ELSEIF IN3<>0 THEN      'code problem now here..
    GOTO inright                 'code problem now here..
    
    


    With this change Now the right IR sensor returns a 0. There is something with wrong in the code.
  • DufferDuffer Posts: 374
    edited 2007-03-29 02:38
    Special-K
    You might try something like the following just to eliminate some of the questions regarding the calling structure of your code:
    Flag  VAR Byte
    Flag = 0
    DO
    IF IN1 AND NOT Flag.BIT2 THEN GOSUB InLeft
    IF IN3 AND NOT Flag.BIT0 THEN GOSUB InRight
    IF IN5 AND NOT Flag.BIT1 THEN GOSUB InCenter
    LOOP UNTIL Flag = $07
    'process the results of the "in" subs
    END
    Inleft:
    'do your stuff
    Flag = Flag | $04
    RETURN
    Inright:
    'do your stuff
    Flag = Flag | $01
    RETURN
    Incenter:
    'do your stuff
    Flag = Flag | $02
    RETURN
    
Sign In or Register to comment.