Shop OBEX P1 Docs P2 Docs Learn Events
How to use same program on 4 cogs? — Parallax Forums

How to use same program on 4 cogs?

T ChapT Chap Posts: 4,223
edited 2007-02-03 07:03 in Propeller 1
For 4 motors using 4 cogs and the same code on each, only changing the Step and Dir pins plus variables, I am looking for a way to use the same code without having to copy and paste it over and over again as the code gets edited. The code is long, and each time I copy and paste code for motor X to the other 3 cogs, I am having to go through it and type over the "X" places with the correct new letter, Y, Z, or R.

Here is my only thought to solve it: Let the code be only in one method, but it gets passed parameters for the 2 pins from the new cog like shown below, then each cog runs the program using the pins it got passed. Is this the best plan for what I want to do?



PUB Start
   cognew (movex, @stack1)
   cognew (movey, @stack2)
   cognew (movez, @stack3)
   cognew (mover, @stack4)


PUB moveX
   motor_control(0, 1) 

PUB moveY
   motor_control(2, 3) 

PUB moveZ
   motor_control(4, 5) 

PUB moveR
   motor_control(6, 7) 

PUB moter_control(Step, Dir)
    'run the motor on pins rec'd




There are also 6 variables used in the code with names associated with each motor, i.e. Xaccel, Xdecel, Xarate, Xdrate, Xcurrent, Xrun, Xrun80. How to have one code block use 4 sets of variables? Passing the pins are easy, how do I pass the variable names to use for each case?

Thanks for any suggestions

Post Edited (originator) : 2/2/2007 9:08:05 AM GMT

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-02-02 09:28
    Create a record (block of variables) for each copy to be run, then pass a pointer to the beginning of each record when calling the function. Each running copy will use the base pointer plus an offset to access each individual parameter.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • T ChapT Chap Posts: 4,223
    edited 2007-02-02 10:38
    Thanks Paul, any chance there is an example of the use of a record you could point me to?
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-02-02 18:52
    LONG Xaccel, Xdecel, Xramp, X...LONG Yaccel, Ydecel, Yramp, Y...
    ...
     
     
     
    motor_control(1,@Xaccel)
    motor_control(3,@Yaccel)
    ...
     
     
    PUB motor_control(pinstart,radr) | firstpin, secondpin, accel, decel, ramp, ...
      firstpin := pinstart
      secondpin := pinstart + 1
      accel := LONG[noparse][[/noparse]radr]
      decel := LONG[noparse][[/noparse]radr + 4]
      ramp := LONG[noparse][[/noparse]radr + 8]
      ...
     
     
     
     
     
     
     
     
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • SailerManSailerMan Posts: 337
    edited 2007-02-02 18:55
    Now that is helpful!! [noparse]:)[/noparse]
  • T ChapT Chap Posts: 4,223
    edited 2007-02-02 20:43
    Yes that is great! I don't get the +4 +8 convention, but I assume it will continue with +4 increments for every new variable.

    Thanks Paul

    Post Edited (originator) : 2/2/2007 8:49:45 PM GMT
  • John R.John R. Posts: 1,376
    edited 2007-02-02 20:55
    The +4 and +8 is the "offset" from the radr "starting point" and is the number of bytes to move. So, in the example code:

    accel := LONG[noparse][[/noparse]radr]

    will get the LONG variable (4 bytes worth is a long) starting at the memory location passed in radr

    decel := LONG[noparse][[/noparse]radr + 4]

    will get the variable starting 4 bytes higher that the address radr points to and

    ramp := LONG[noparse][[/noparse]radr + 8]

    will get the next variable "slot", and yes, to get the fourth, add 12 and so on.

    The factor of "4" is how many bytes a LONG takes in memory.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.
    Link to My Nomad Build Log
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-02-02 21:04
    Correct, and if your using BYTE variables you increment by one and·WORD variables you increment by 2.

    And one important note about this technique is you cannot mix data types in the same record, because the compiler groups all similar types together (LONGs with LONGs, WORDs with WORDs, BYTEs with BYTEs). So if you try to create a record consisting of LONGs and WORDs the compiler will physically seperate them so you cannot know what the proper offset is to get to the next data type.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 2/2/2007 9:09:27 PM GMT
  • T ChapT Chap Posts: 4,223
    edited 2007-02-02 22:11
    What a nice system, it always seems like there is a simple answer to whatever you need to do.
  • T ChapT Chap Posts: 4,223
    edited 2007-02-03 07:03
    Do you write from the 4 cogs back to the respective variables with a similar method¿


    
    LONG[noparse][[/noparse]radr] := somevalue
    LONG[noparse][[/noparse]radr + 4] := somethingelse
    





    **Aha! of course it does***

    Post Edited (originator) : 2/3/2007 7:21:19 AM GMT
Sign In or Register to comment.