Shop OBEX P1 Docs P2 Docs Learn Events
Question about try to control my servos with parallel control — Parallax Forums

Question about try to control my servos with parallel control

kevinspacekevinspace Posts: 56
edited 2010-12-29 18:39 in Propeller 1
Hello everyone!

I have an idea about the Propeller controller, but it seems to encounter some problems.

Everybody know the propeller controller which has 8 cogs, so its advantages is enable to implement parallel

control. Therefore, I want to control several servos at the same time.

I connected the Propeller Demo Board with the Propeller Servo Controller by serial port that try to control my

servos with parallel control.

But I discover some problems, I know the Propeller Demo Board chip and the Propeller Servo Controller

chip are contain multi-cog, but the communication of them is only one path (serial port ). Therefore,

the parallel control idea which will be limited to achieve it.

Is there have anyone can supply me any suggestions.

Here is my test code.

OBJ
PSC : "ServoControllerSerial"

CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000

COMPIN = 0 'Pin used for communication with the PSC
PSC_BAUD = 0 'Baud rate (0 - 2400, 1 - 38400)
VAR long Stack[100]

PUB LaunchBlinkCogs
coginit(1,Program1, @stack[0])
coginit(2,Program2, @stack[50])

PUB Program1
PSC.START(COMPIN, PSC_BAUD)
PSC.SETPOS (0, 1, 350)
dira[16] := 1
outa[16] := 1

PUB Program2
PSC.START(COMPIN, PSC_BAUD)
PSC.SETPOS (7, 1, 350)
dira[23] := 1
outa[23] := 1

The result of the test code only one servo was turning.

test code 1.JPG

I modified the test code, I added the delay time into the code, the two servo were turning.

But that is not parallel control.

test code 2.JPG
571 x 499 - 35K
574 x 525 - 37K

Comments

  • Mike GMike G Posts: 2,702
    edited 2010-12-28 06:43
    You do have two PSCs, right?
  • Mike GMike G Posts: 2,702
    edited 2010-12-28 07:52
    It's not clear how you have everything wired up.

    If you are no longer using the PSC (I don't think that the case due to the OBJ declaration) and want to control servos directly with the Prop, then take a look in the OBEX for Servo32v7.
    http://obex.parallax.com/objects/51/

    Send two position commands if you want two servos to move at the same time using a single PSC.
  • kevinspacekevinspace Posts: 56
    edited 2010-12-28 08:29
    Thank you! Mike G
    I only used one Propeller Demo Board and one Propeller Servo Controller,
    and I set one of the Demo Board I/O Pin as serial port which connected with the PSC serial port.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-12-28 08:37
    Perhaps you need to make a "dispatcher" method that queues messages from your parallel processes and serializes them to the PSC.
  • Mike GMike G Posts: 2,702
    edited 2010-12-28 09:33
    Ah, I was thinking parallel messages all the way down the pipe not parallel processes to a serialized message. That's an interesting control mechanism.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-12-28 13:15
    I think you're forced into it as you have just one PSC and if you allow two cogs to attempt to talk to it at the same time, they will, and ultimately collide. PJMonty (www.socalhalloween.com) has a custom animatronics control system that uses this approach. There is an application called "Wall Street" that is responsible for handling messages from the various processes to and from the actual comms hardware.
  • kevinspacekevinspace Posts: 56
    edited 2010-12-28 22:13
    Hello everyone!

    I tried another way to control several servos by only used the Propeller Demo Board, that meaning the way

    which doesn't connect with the PSC.

    I downloaded the OBEX for Servo32v7 (http://obex.parallax.com/objects/51/),

    Then I used the "Servo32v7_RampDemo.spin" to control my servos. I hoped that I can let the servos turn

    at the same time by used different cogs.

    Here is my test code.

    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000 'Note Clock Speed for your setup!!
    ServoCh1 = 0 'Select DEMO servo
    ServoCh2 = 1

    VAR long Stack[100]

    PUB LaunchCogs

    coginit(1,Servo32_DEMO, @stack[0])
    coginit(2,Servo32_DEMO2, @stack[50])

    OBJ
    SERVO : "Servo32v7.spin"

    PUB Servo32_DEMO | temp
    SERVO.Start
    SERVO.Ramp
    SERVO.Set(ServoCh1,1500)
    SERVO.SetRamp(ServoCh1,700,200)
    repeat 800000
    SERVO.SetRamp(ServoCh1,2300,200)
    repeat 800000
    SERVO.SetRamp(ServoCh1,700,200)
    repeat 800000
    SERVO.SetRamp(ServoCh1,2300,200)
    repeat 800000
    SERVO.Set(ServoCh1,1500)

    PUB Servo32_DEMO2 | temp2
    SERVO.Start
    SERVO.Ramp
    SERVO.Set(ServoCh2,1500)
    SERVO.SetRamp(ServoCh2,700,200)
    repeat 800000
    SERVO.SetRamp(ServoCh2,2300,200)
    repeat 800000
    SERVO.SetRamp(ServoCh2,700,200)
    repeat 800000
    SERVO.SetRamp(ServoCh2,2300,200)
    repeat 800000
    SERVO.Set(ServoCh2,1500)

    Above the code, its result is only let "PUB Servo32_DEMO2" to implement.
    Here is the video.
    .

    I discovered that I must have to add the delay time bttween the two cogs, in this way that would be able to run the two cogs.

    coginit(1,Servo32_DEMO, @stack[0])
    waitcnt(clkfreq + cnt)
    add these
    coginit(2,Servo32_DEMO2, @stack[50])
    Here is the video
    But it doesn't achieve my ideal. (that didn't turn at the same time)
  • bsnutbsnut Posts: 521
    edited 2010-12-29 00:52
    One problem I see is the way you wrote the code and the problem could've be, that you didn't indent the code under the "repeat" instruction. Indenting code like this is important when you write programs for the Propeller chip.
    repeat 80000
        SERVO.SetRamp(ServoCh2, 2300,200)
    
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-12-29 08:14
    You need to provide some mechanism (perhaps a free pin) to allow sync between your two servo control cogs.
  • Ted,WITed,WI Posts: 4
    edited 2010-12-29 08:39
    Hi Kevin
    I looked at your code, why are you starting new cogs?
    The Servo 32v7_ RampDempo will control 32 servo with 1 cog, two with ramping. Just as written.
    Just add more servos like below …I added code to move one more servo, if you want 3 add one more up to 32 servos. The pulse for servo 1 will be on port pin 0, the output for servo2 will be on port pin1.
    I tried it on my demo board, but I only have one servo, so I had to move it and then do a reset. Both pins will drive the servo. Let me know if this works for you.


    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000 'Note Clock Speed for your setup!!
    ServoCh1 = 0
    ServoCh2 = 1 'Select DEMO servo
    VAR

    OBJ
    SERVO : "Servo32v7.spin"
    PUB Servo32_DEMO | temp
    SERVO.Start 'Start Servo handler
    SERVO.Ramp '<-OPTIONAL 'Start Background Ramping
    'Note: Ramping requires another COG
    ' If ramping is not started, then
    ' 'SetRamp' commands within the
    ' program are ignored
    '
    'Note: At ANY time, the 'Set' command overides
    ' the servo position. To 'Ramp' from the
    ' current position to the next position,
    ' you must use the 'SetRamp' command
    ' 'Set(Pin, Width)
    SERVO.Set(ServoCh1,1500) 'Move Servo 1 to Center
    SERVO.Set(ServoCh2,1500) 'Move Servo 2 to Center
    'SetRamp(Pin, Width,Delay)<-- 100 = 1 sec 6000 = 1 min
    SERVO.SetRamp(ServoCh1,2000,200) 'Pan Servo 1
    SERVO.SetRamp(ServoCh2,2000,200) 'Pan Servo 2
    repeat 1500000 'Do nothing here just to wait for
    'background ramping to complete

    SERVO.SetRamp(ServoCh1,1000,50) 'Pan Servo 1
    SERVO.SetRamp(ServoCh2,2000,200) 'Pan Servo 2
    repeat 800000 'Do nothing here just to wait for
    'background ramping to complete

    SERVO.Set(ServoCh1,1500) 'Force Servo to Center
    SERVO.Set(ServoCh2,1500) 'Force Servo to Center

    Sorry, I lost the format.But you get the Idea.
  • Mike GMike G Posts: 2,702
    edited 2010-12-29 12:23
    At first, I though Kevin simply wanted two or more servos to move at the same time which is really easy with serov32 or the PSC. So, I was scratching my head with the Parallel stuff.

    Jon hit the nail on the head. If you look at the code, Kevin wants more than one process (COG) to control servos. You can't very well have multiple processes telling commanding the servos at the same. However, you could do what Jon so succinctly suggested.
    Perhaps you need to make a "dispatcher" method that queues messages from your parallel processes and serializes them to the PSC.
    or
    You need to provide some mechanism (perhaps a free pin) to allow sync between your two servo control cogs.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-12-29 18:39
    I've never used them so this thread gave me the opportunity to play with the lock commands in Spin. I used code write out of the manual in this demo which has three processes running at the same time sending a message to a terminal. In order to prevent one from stomping on another a lock is used. It's pretty easy and you should be able to incorporate this idea into your code.

    Note how a process waits until it can set the shared lock before sending its message, then releases it immediately after.
    pri process(id, delay, lock)                                    ' start with cognew()
    
      repeat
        pause(delay)
        repeat until not lockset(lock)                              ' hold until lock is set
        term.str(string("Hello from process "))                     ' send message
        term.dec(id)
        term.tx(CR)
        lockclr(lock)                                               ' release lock
    
Sign In or Register to comment.