Shop OBEX P1 Docs P2 Docs Learn Events
Servo Control Code - help? — Parallax Forums

Servo Control Code - help?

jcfergusonjcferguson Posts: 86
edited 2006-09-27 18:21 in BASIC Stamp
I am working on a robotics project - I will need to control 3 servos via RF link between two basic stamps.

I wrote some code for the servo control (No RF yet, just a basic stamp hooked to joysticks and servos at this point).

Anyone with coding experience willing to take a look and see if there are things I could do better? It works, but I am concerned with making things as streamlined as possible so that I can have the stamp control other things while reading the joystick/running the servos. The code is attached here.

The next step is to make a "deadband" adjust for one of the servos (it will be a continuous rotation servo and has a tendency to creep)

Thank you for any help!

Carlos Ferguson

p.s. is this the right forum for this, or should I post in robotics?

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2006-09-26 20:15
    This looks great to me. I would tend to start partitioning the program into subroutines, at this point.
    See the attached for what I would do.

    And yes, you have 20 to 50 mSec for your main program 'loop' (or to do other things) if you're going to refresh the servo's on time.· Personally, I'd keep the "pause 20" (or whatever value) in my main loop -- as you've done -- where it's easy to 'tweak'.

    But it looks very good so far.

    Post Edited (allanlane5) : 9/26/2006 8:18:52 PM GMT
  • jcfergusonjcferguson Posts: 86
    edited 2006-09-26 21:39
    Thanks a lot for the advice!


    Is the use of subroutines like your post useful for making the code "cleaner" so that as the program grows you can still make sense of things, or is it faster?

    One other question about things as they now stand: Exercise 26 of Stampworks drives a servo using two input pins for the pot (instead of one like I am using) - in the example, the center is sent to ground, the two outer potentiometer contacts are measured with rctime. I am wondering what the advantage of such a practice is since it uses two inputs rather than one - I will look at the code again, but it is a little beyond me still to figure out why this would be advantageous (as I assume it must be since it is in the more advanced book!). Is the servo control more accurate? Smoother?

    Thanks again,

    Carlos
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-09-26 23:05
    Yes, as your code grows, it can be helpful to 'encapsulate' things in subroutines. This does make the code cleaner, and easier to maintain. It actually slows the code by 300 uSec per call or so -- but as your program gets large, it's worth the small speed hit to be able to keep track of what's going on.

    Plus, if you put in place a 'multi-tasking' approach, you may want to 'GOSUB SendServo' from more than one place to make sure the refresh limits are not exceeded.
  • jcfergusonjcferguson Posts: 86
    edited 2006-09-27 18:21
    Ok, I added some Code to my Code, it is attached below, any thoughts are VERY appreciated.

    My additions are to make all the pertinent information used in the body of the program a constant so that I can adjust the range of the servos just by changing a constant... I gather this from the code examples and suggestions in the tutorials.

    My questions now:

    1--in the constants area of the program I used some fancy multiplication to come up with my servo scaling constant. I multiplied first my 64, then later by 4 in order to actually multiply by 256 without going over the "size limit" which I found is 65536. Is this the way to do this? I would like to have all this math carried out in the constants area so I can just plug in values and go. Here is the constants area:

    Pot1LoVal CON 10 'set this to low value for joystick on pin 0
    Pot1HiVal CON 657 'set this to high value for joystick on pin0
    S1MidVal CON 750 'adjust the midpoint of the servo
    S1RangeOfMotion CON 200 'set this number to desired range of motion (0-900)
    Pot1Span CON Pot1HiVal - Pot1LoVal 'span of joystick on pin 0
    S1DeadVal CON 10 'value of dead band to solve creeping continuous-rotation drive
    ' this servo adjust constant below is for scaling the r/c time values to a range suitable for the servo.
    ' multiply by 64 and then 4 instead fo 256 in order to keep value below 16 bit max?
    S1AdjConstant CON 64 * S1RangeOfMotion / Pot1Span * 4 '

    This last line is what I am questioning - my solution is another way to do (S1RangeOfMotion/Pot1Span) * 256. This is to find a constant so I can scale the values coming from the joystick r/c circuit to a range that fits the servo parameters. Is there a better way to do this?


    2--in the beginning of the main section I switched this:

    HIGH S1CounterPin ' set r/c circuit counter pins to high (pins 0,1,2)
    HIGH S2CounterPin
    HIGH S3CounterPin

    to this:

    SPins = %0111 ' Save one command with this method?
    SPinsDirs = %0111

    is this a good idea? Does saving one command in this way "matter"?

    Again, thanks for any and all help.

    Carlos
Sign In or Register to comment.