Shop OBEX P1 Docs P2 Docs Learn Events
Controlling multiple servos. — Parallax Forums

Controlling multiple servos.

GrampsGramps Posts: 117
edited 2010-07-23 15:22 in BASIC Stamp
Hello!

Please give me a hand.
My Board of Ed. Stamp Board has 4 sets of servo pins, so I’m assuming we can
Control 4 servos.
Below is the code we have for one servo.
Each servo and pot gets a different set of pins, correct?
How do i rewrite the code to control 4 servos with 4 pots?





Thanks, Gramps

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'What is your life?"
"It is even a vapor that appears for a
little time and then vanishes away."
Saint James 61 A.D.

Post Edited (Gramps) : 7/17/2010 12:43:44 AM GMT
«1

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2010-07-17 02:25
    I don't think that it takes ten milliseconds to charge the cap for RCTIME, unless you are using a really big cap. I changed it to four so that four times through the loop equals 16 milliseconds of PAUSE time. Adding in the other things going on should bring the total time between servo pulses to something near 20 milliseconds. It is just an estimate and you may need to alter it.

    'Read Port WithRC Time  control servo with pot test from JP
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    PAUSE 1000
    DEBUG "program Running"
    
     pot1  PIN  7  ' 1st pot, the four pots are assumed to be on consecutive pins
    
     servo1 PIN 12 ' 1st servo, the four servos are assumed to be on consecutive pins
    
      time VAR Word
      i  VAR NIB  ' index variable used in the FOR..NEXT loop 
    
    DO
    
    FOR i = 0 to 3
      HIGH (pot1 + i)
      PAUSE 4
      RCTIME (pot1 + i), 1, time
    
      time = time */ 550 ' Scale by 0.724 (X 256 for */).
      time = time + 400 ' Offset by 500.
    
      PULSOUT (servo1 + i), time ' Send pulse to servo.
    NEXT
    
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-07-17 02:28
    Also, be aware that if you are not using a separate power supply for the servos that the current draw may cause the stamp to reset.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • ercoerco Posts: 20,256
    edited 2010-07-17 03:14
    16 I/Os anyway you want to use them:

    drive 16 servos;
    read 8 pots and drive 8 servos

    They may only give you 4 servo headers, but you can hook servos up to any I/O pin, power & ground.

    If you want to read 4 pots and drive 4 servos, your software·has to multiplex (weave them together) sequentially and keep looping through. Each servo pause won't be 20 ms, it might be only 3 or 4 since you have 4 in sequence, plus at least 1 ms (typical) to charge your pot cap.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • GrampsGramps Posts: 117
    edited 2010-07-17 13:09
    @Rich H,
    Ok, so you just list them in order? Pot 2 pin 8, pot 3 pin 9?
    I see what needs to be accomplished, but don't see the code......):
    How to separate the electronics power from the servo power? Red and black to servo power, and white and black to pins?
    @erco,
    Actually i want to drive 6 servo-pots for a total of 12 pins. So one Stamp will do this job,Correct?





    Thanks! Gramps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-17 14:08
    One Stamp is not going to do well with 6 servos because of timing. Each servo requires a pulse from 0.5ms to 2.5ms in width and the Stamp can only produce one pulse at a time. If you produce 6 pulses, one after the other for example, that's an interval of 3ms to 15ms. All well and good. Reading the pots using RCTIME takes more time and you've only got about 20ms before you have to start the cycle over again ... servos require a new pulse about every 20ms or they'll turn themselves off until the next pulse comes in ... that causes jerking or "trembling".

    Anyway, you might be able to pick the values of the pots and capacitors so the reading of a value takes less than a millisecond, but your whole system has very marginal timing with six servos. It'll work most of the time, but some servos may shake at one extreme of their range. You might gain some advantage by setting up your pots so, when the servos are at the end of their range with the 2-2.5ms control pulse, the pots are at their lowest resistance, thus read the fastest.

    Other ways of doing this "better" for six or more servos would be to use an 8-channel Analog to Digital Converter which can read the pot position faster. This is also the point where you might want to think about using a servo controller, like the ServoPAL (x 3) or a Propeller Servo Controller. Either would remove the timing constraint.

    It's important to separate the servo power from the electronics power. First of all, servos are motors and can draw quite a bit of current when stalled or under heavy load, up to 1A or so. Secondly, they generate a lot of electrical noise which can get into the power supply if not isolated. Normally, servos are run directly from a 6V to 7.5V Vin rather than from Vdd. This isolates them from the regulated 5V supply and provides a bit more voltage to them. Note: they shouldn't be used with more than 7.2 to 7.5V ... it causes more arcing at the brushes and accelerates their wear. A lot more than that (like 9V or 12V) and you may damage the servos' electronics.

    The best way to do this is to set the BOE jumper so you get Vin connected to the servo power pins. For more than 4 servos, you can get nice 3-pin male headers with long pins on both sides from Parallax and put them into your BOE breadboard, then run jumpers to ground, Vin, and an I/O pin, then plug the extra servo cables into the header.

    Post Edited (Mike Green) : 7/17/2010 2:19:48 PM GMT
  • W9GFOW9GFO Posts: 4,010
    edited 2010-07-17 14:25
    Gramps said...
    @Rich H,
    Ok, so you just list them in order? Pot 2 pin 8, pot 3 pin 9?
    I see what needs to be accomplished, but don't see the code......):
    How to separate the electronics power from the servo power? Red and black to servo power, and white and black to pins?
    This is the code;
    FOR i = 0 to 3
      HIGH (pot1 + i)
      PAUSE 4
      RCTIME (pot1 + i), 1, time
    
      time = time */ 550 ' Scale by 0.724 (X 256 for */).
      time = time + 400 ' Offset by 500.
    
      PULSOUT (servo1 + i), time ' Send pulse to servo.
    NEXT
    



    Each time through the FOR..NEXT loop it will use a different pin for the pot and servo. The first time through the loop "i" will be zero so (pot1 + i) will be 7, the next time "i" will be one and (pot1 + i) will be 8, and so on.

    As for the power supply, if you are using a six volt supply (and you are using a BOE) you can switch the jumper between the servo pins 13 and 14 to VIN instead of the default VDD. That will bypass the regulator and send the input voltage right to the servo power pins. Higher than six volts is not recommended.

    Otherwise, you can use a separate power source connected to the middle pin (power) of the servo, just be sure that the ground of the power source shares the ground with the rest of the board.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2010-07-17 15:57
    Another approach is to interleave when the POT is being read. If you can get away with it, the POT may not need to be read every single cycle.

    PSEUDO (untested) CODE:
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Initialize:
      servo1  PIN 1
      servo2  PIN 2
      servo3  PIN 3
      servo4  PIN 4
      servo5  PIN 5
      servo6  PIN 6
    
      Pot1    PIN 7   'Pots are located on 6 consecutive pins ... 7 to 12
    
      time   VAR Word(6)
    
      PotIndex    VAR Nib
    
    CenterPots:
        FOR PotIndex = 0 TO 5
            time(PotIndex) = 500
        NEXT
    
    MainLoop:
        PULSOUT servo1, time(0)  '1-2ms
        PULSOUT servo2, time(1)  '1-2ms
        PULSOUT servo3, time(2)  '1-2ms
        PULSOUT servo4, time(3)  '1-2ms
        PULSOUT servo5, time(4)  '1-2ms
        PULSOUT servo6, time(5)  '1-2ms (6ms to 12ms + overhead for the PULSOUT command)
        GOSUB OnePot
        GOSUB OnePot
        GOTO MainLoop
    
    OnePot:
        HIGH Pot1 + PotIndex
        PAUSE 4
        RCTIME Pot1 + PotIndex, 1, time(PotIndex)
    
        time(PotIndex) = time(PotIndex) */ 550 ' Scale by 0.724 (X 256 for */).
        time(PotIndex) = time(PotIndex) + 400 ' Offset by 500.
    
        PotIndex = PotIndex + 1
        PotIndex = PotIndex // 5
        RETURN
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 7/17/2010 4:07:18 PM GMT
  • GrampsGramps Posts: 117
    edited 2010-07-17 16:48
    OBC says it's time to move up to prop!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-17 16:59
    OBC is right. You can use the BS2_Functions library to help you with the conversion. Hanno's 12 Blocks is also a great way to get started.
  • Martin_HMartin_H Posts: 4,051
    edited 2010-07-17 17:33
    Sort of on topic. I've found you can control four servos with a BS2, but only two or three at a time. For example the wheels on a bot, or the Ping))), or the gripper., but not all of them at the same time. I send the unused servos a stop pulse once and forget about them until I need them again. They seem pretty inert when not being sent pulses.

    I'm currently building a robot arm with six servos, but I only plan actively control one or two servos at a time. So I'll slew to azimuth, set the shoulder and elbow, finally the wrist and gripper. Now this scheme lacks the flexibility of a propeller chip or a servo controller. But I'm hoping it will work.
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2010-07-17 19:53
    Gramps,

    I agree with OBC, it's time to move to the Prop. However, in testing the BS2 code that I provided, if you can adjust your R and C values used in the RCTIME so that it completes within 3 ms ... meaning the lines that read:

        PAUSE 4
        RCTIME Pot1 + PotIndex, 1, time(PotIndex)
    
    



    ... the PAUSE 4 could be reduced to PAUSE 1, and the actual duration of RCTIME could be adjusted to 2 ms by the Resistor and Capacitor selection.


    ...THEN, doing all that the LOOP would update up to 6 servos at 42Hz to 53Hz (<-- roughly) depending on the assigned PULSOUT values.

    The POTs would be interleaved so that they update 14 to 18 times a second.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • GrampsGramps Posts: 117
    edited 2010-07-17 20:52
    Thank you everybody for your great suggestions!
    Right now we're testing wrist and gripper functions so 2 servos is all we need.

    ServoPAL looks like the ticket!



    Gramps,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.

    Post Edited (Gramps) : 7/17/2010 9:02:36 PM GMT
  • fltcmdrfltcmdr Posts: 14
    edited 2010-07-17 22:29
    As a total novice(I know you hate that) This is all just above my head..but not out of reach.
    I gather if I want to control...say 100+ servos, that the basic stamp can only do 4 reliably.
    is there other controllers that would be better?
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-07-17 23:29
    The problem is, that the servo control signal is a 1 msec to 2 msec high 'pulse', repeated every 20 milliseconds. The 1 mSec to 2 mSec specifies to the servo the position you want -- 1 mSec is full left, 2 mSec is full right, and 1.5 msec is 'centered'.

    So, for a BS2 to output this signal, it's got to do a "PULSOUT" for the 1 to 2 mSec per servo -- using up 1 to 2 mSec per servo. So, really, one BS2 can only control 10 servo's, and then only if that's ALL it's doing. Usually you want to be able to talk to the BS2, or have it figure out a new servo position, or detect a collision, so controlling 10 servo's isn't really practical, but that's the upper limit.

    Now, the other issue is current -- a servo when 'stalled' can pull 500 mA. Most BS2 boards have a single linear regulator rated at 1.0 to 1.5 amps -- so controlling 3 servo's is the most power that board can put out.
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2010-07-17 23:54
    fltcmdr,

    With the basic stamp it would be like Allen describes in that you are going to be very limited with speed and overhead processing. With the Propeller, by itself it could control up to 32 servos from one cog without any external hardware other than one resistor per servo.

    There was a thread early on when the Servo32 object was first being put together and tested that explains how with a few external IC's a single propeller could drive up to 144 Servos, and in fact it could do more than 144, but with only 32 I/O's for the Propeller you are 'pin limited' to go any higher than 144 servos.

    Here is the discussion thread from 32 to 48 to 96 to 144 servos from one Propeller:
    http://forums.parallax.com/showthread.php?p=576634

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • ercoerco Posts: 20,256
    edited 2010-07-18 05:52
    XLNT discussion! I continue to spout and espouse the abilities of my old friend the BS2. Not long ago, the mere suggestion that we shouldn't expect a BS2 to keep up with a fast-turning quadrature encoder motivated me to run my own tests, and my beloved BS2 never missed a beat even though the motor blew up! Just when I thought I was out, you guys pull me back in...

    I won't dispute that eventually the Stamp's dance card will be full if it is continuously monitoring N pots and pulsing N servos. There are only so many milliseconds in a day. But I question if everyone needs to perfectly synchronize all their servos simultaneously. In an 8-legged walker, yes, but most applications are simpler and more forgiving. I maintain that there are valid opportunities when a vanilla BS2's 16 I/O pins can read 8 pots and drive 8 servos, or drive 16 servos...

    Not all pots & servos in all applications need continuous monitoring. There may be a preferred sequence which allows the Stamp to work with groups of 3 or 4 pots & servos at a time. If there is little or no load on a servo, then it's safe to assume that it will stay where last put, and once it's there you can stop sending pulses.

    I've been accumulating lots of those cheap Ebay servos from China, planning a multi-servo robot arm and walker project. One thing I notice is that these Chinese servos aren't geared very low. That is, they have less gear reduction (yet curiously more friction, due to cheap gears) than some name brand servos. Bottom line is, different servos will vary in their power-off holding torque. If the torque is too high and back-drives your servo, you might be able to find a different servo that has more holding torque with no signal applied. As long as the applied torque at a servo joint is lower than this holding torque, it doesn't require continuous pulsing.

    You just SET IT... AND FORGET IT! (Thanks to Ron Popeil)

    I'm taking the liberty of reading between the lines of Gramps' posts: his Shadow-Bot Arm, and his giant hi-torque servos made from leadscrews and automotive power window motors. When he inevitably combines them makes a world-crushing exoskeleton, those joints will HOLD POSITION in a power-off state, I have no doubts. Especially when he uses a dynamic braking H-bridge! So if I read him correctly, I still say a Stamp is up to the task if the software is written to only read a few pots and drive a few servos at a time. Yes, a Prop is a better processor, but we knew that already. Almost sounds like overkill to me. I'm not happy unless I get the most out of my processor. And I still have LOTS of Stamps in my drawer to use up.

    I have had up to 4 servos controlled by a BS1 in one project, only driving one servo at a time. Low loads, worked perfectly. A BS2 would have been overkill on that one!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • GrampsGramps Posts: 117
    edited 2010-07-19 12:09
    @erco,
    “a world-crushing exoskeleton”
    LOL
    Yes, you are reading between the lines well. Been hesitant to post any pics because this crude hand functions so poorly. We see some places where some errors can be corrected and may not have to scrap this one and return to the drawing board.
    A photo of efforts so far with limited budget, skills, and tools.
    Truly the human hand is a marvel of design!



    Gramps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
    2816 x 2112 - 1M
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-07-19 12:56
    Wow! Yes, for something that ambitious, learning the Propeller would be a good thing. The BS2 is a wonderful platform, extremely reliable, doesn't take much power, easily programmed and reprogrammed. If it has a weakness, it's that it's a bit slow (2,000 IPS) and limited in program and memory space (2K bytes, and 26 bytes RAM).

    The Propeller is a bit more complicated to program for (though in the year since I last tried it, maybe they've fixed that) but MUCH faster and MUCH more resources.
  • ercoerco Posts: 20,256
    edited 2010-07-19 15:26
    WOW, Gramps! Sweet build, you've been a busy boy! Gotta see some video!

    Lots of motorized joints with gearboxes there, check this video where the guy uses a sprinkler for a gearbox. Clever!

    http://www.youtube.com/watch?v=lDsq26PYhdY

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • GrampsGramps Posts: 117
    edited 2010-07-20 12:54
    Thanks again for all the suggestions and help!

    @erco, yes, using the gearbox from the sprinkler is a great idea!

    Actually, this is turning into a father-son project.
    I’ll be testing the arm using the BS2 here in FL.
    OBC will be writing some code for the Prop. When we get together in Ohio
    next month, we will attempt to get a demo set up by convention time, August 28.




    Regards, Gramps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • Joe FishbackJoe Fishback Posts: 99
    edited 2010-07-21 01:32
    Gramps,

    Parallax has a answer to the problem of sending a signal to a servo then sending
    signals to other servos and still get back in time to the first servo with a refreshing signal.
    It is called a ServoPal item # 28824 ($14.99). I plugs in between the servo and BOE. Once you
    send a signal to the ServoPal it keeps sending that same signal to the servo till you send another
    signal to that ServoPal. This allows you to send a signal to a servo then go do something else or to another servo and come back later to the first servo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ······ Joe Fishback

    -Robots are my friends-
  • GrampsGramps Posts: 117
    edited 2010-07-21 11:27
    @Joe Fishback,
    Yes, ServoPal was mentioned earlier in the thread and looks like a good option.
    Thanks for the suggestion!


    Gramps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • GrampsGramps Posts: 117
    edited 2010-07-21 11:34
    A new problem has arisen which looks to me like a software fix.

    The shoulder rotation motor is a suddenly starting and too fast.

    Can we slow it down by changing the pulse duration?

    And what about ramp up and down? Is it adjustable?





    Gramps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • ercoerco Posts: 20,256
    edited 2010-07-21 15:33
    Gramps: Was it working right previously? What changed since then (the total arm weight, probably). Is it moving too fast downward and slower upward? I'm betting it's a mechanical problem. You might need helper springs to even out the load at certain positions.

    Isn't this the big window motor controlled by the relay H-bridge that's driven by servo guts? If so, a software tweak to ramp the voltage will try to PWM (flicker) the relays, which is always a bad idea, especially with the current that motor is pulling.

    When in doubt, Youtube your problem, like last time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • GrampsGramps Posts: 117
    edited 2010-07-22 01:16
    @erco,
    This is a new motor that controls the shoulder rotation(side to side).
    The problem is that the arm is so big (and heavy) it's jerky.
    Was hoping for some ideas to dampen it down perhaps by starting
    and stopping the motor slower.
    I may have to go to a motor controller on this one rather then the
    relay-H-bridge configuration.


    Gramps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • ercoerco Posts: 20,256
    edited 2010-07-22 01:51
    Can you lower the voltage to that motor?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • GrampsGramps Posts: 117
    edited 2010-07-22 02:26
    Yes! Good idea. we will try that....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • ercoerco Posts: 20,256
    edited 2010-07-22 05:05
    A big resistor acts like a damper for electric motors. Quick to try, but wastes energy as heat and you lose torque.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • GrampsGramps Posts: 117
    edited 2010-07-22 12:28
    We used a resister on the motor that controlled side to side motion
    on the bot we built in Mongolia, it's just such a wasteful way to use
    energy!
    What about a choke coil-cap arrangement?




    .

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'What is your life?"
    "It is even a vapor that appears for a
    little time and then vanishes away."
    Saint James 61 A.D.
  • ercoerco Posts: 20,256
    edited 2010-07-22 15:27
    Not sure about the coil & cap. Sounds more like a filter than a damper. What happened when you just lowered the voltage?

    If I read you correctly, this joint has a vertical axis and (other than binding from a heavy load) doesn't fight gravity. Seems like this should be easy to control at low speed. At higher speeds, you have too much rotating momentum to fight. You may simply need a drive motor with lower gearing that moves more slowly with mucho el Torquo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
Sign In or Register to comment.