Shop OBEX P1 Docs P2 Docs Learn Events
There Has to be a beter way....... Code question. — Parallax Forums

There Has to be a beter way....... Code question.

DgswanerDgswaner Posts: 795
edited 2007-05-04 21:23 in BASIC Stamp
The Bot that I'm working on is just about done. but I want to fine tune the code before I go on to the next phase. I'm using the Ping))) module connected to a servo to pan back and forth 120 deg. and read distances at 5 different spots. because I started with the roaming with ping code, the 5 directions are called left90 left45 center right45 and right90. To get the servo in each spot I use code like:

FOR x = 0 TO 10 'left 45
PULSOUT pingservo, 1950
PAUSE 10
NEXT

I have 5 bits of code like this that I use to pan the servo back and forth. and I just run them in sequence. this code works pretty good but I usually take the long way to grandmas house if you will and it seems like there must be a better way. I can't use a for next loop stepping at a specific interval because the the frequencies aren't in equal intervals. I'm not sure if that's because my servo is wearing out or what.

I tried using the read/write command in conjunction with a for next loop to read the positions of the servo and store the distances in an array indexed by the same loop but I ran out of variable space. so I kinda gave up on that option because I still have a bunch of stuff I want to add to this bot and will need code space/ variable space for them.

my current plan is to use for next loops to pan back and forth at about a 200 step and then only take distance measurements if the freq. is a specific number. but I'm worried about accuracy with this method.

I know there has to be a simple solution but I can't seem to see it. can someone point me in the right direction.

I'm using ping in conjunction with 2 IRPD sensors, the IRPD sensors have priority over the ping and I only use the ping to find the most clear direction. the ping is facing forward by default and when the IRPD or ping "hit" something. the servo turns left and then swings right the full 120 deg stopping at 4 angled points listed above and then back to center. the robot then turns the direction that has the greatest distance reading. the code I have works quite well but like I say I want to clean it up. thanks for your time

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-05-04 20:55
    How about:
    pos = 1950
    gosub pingit
    .... (more here)

    pingit:
    for x = 0 to 10
    pulsout pingservo, pos
    pause 10
    next
    return

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-05-04 21:23
    Hello,

    An often overlooked method of handling servos is simply running the servo refresh within the main loop of the program, preferably via a GOSUB to the routine which refreshes all servos. Any changes to pulse widths within the loop are immediately transferred to the servos on the next call to the subroutine. Since there are no FOR…NEXT loops there are no timing irregularities as long as you’re not changing positions faster than the servo can keep up.

    So think of it like this…Your main loop is monitoring sensors and making decisions. Instead of calling a subroutine to do something based on the conditional or sensor reading it simply changes a variable containing the pulse width. This will (almost) immediately take effect on the servos. Timing of these can be handled via passive counters within the main loop that are reset by the event and then set when then event is complete. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.