PDA

View Full Version : How to use same program on 4 cogs?



T Chap
02-02-2007, 04:54 PM
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

Paul Baker
02-02-2007, 05:28 PM
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 (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

T Chap
02-02-2007, 06:38 PM
Thanks Paul, any chance there is an example of the use of a record you could point me to?

Paul Baker
02-03-2007, 02:52 AM
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[radr]
decel := LONG[radr + 4]
ramp := LONG[radr + 8]
...









▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

SailerMan
02-03-2007, 02:55 AM
Now that is helpful!! :)

T Chap
02-03-2007, 04:43 AM
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.
02-03-2007, 04:55 AM
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[radr]

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

decel := LONG[radr + 4]

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

ramp := LONG[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 (http://share.crustcrawler.com/JohnR/)

Paul Baker
02-03-2007, 05:04 AM
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 (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Post Edited (Paul Baker (Parallax)) : 2/2/2007 9:09:27 PM GMT

T Chap
02-03-2007, 06:11 AM
What a nice system, it always seems like there is a simple answer to whatever you need to do.

T Chap
02-03-2007, 03:03 PM
Do you write from the 4 cogs back to the respective variables with a similar method¿






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





**Aha! of course it does***

Post Edited (originator) : 2/3/2007 7:21:19 AM GMT