Shop OBEX P1 Docs P2 Docs Learn Events
sharing variables between cogs — Parallax Forums

sharing variables between cogs

Hi everybody,

it has been a long time that I did not code for the propeller-chip.
Now I'm back. I have quite some experience with coding in Delphi so I'm familiar with the basic concepts.

But I do not remember much how things are coded in spin.
I have a somehow advanced project that I want to do: driving mutliple stepper-motors controlled through a serial interface.

My basic idea is: one cog receives commands and sets up a second cog to do the step-creation.
For the step-creation I want to use this principle:

A loop is running very fast. As fast as possible. This loop is incrementing a variable with each iteration.
If for one of multiple stepper-motors step-pulses shall be created depends on a variable "StepsToDo"
as long as StepsToDo is bigger than zero => create a step-pulse decrement variable StepsToDo by 1

The frequency of the step-pulses shall be depend on a divisor
everytime counter // divisor == 0 create a step-pulse. I'm aware of, that this method does not allow any frequency only certain frequencies. But this is OK.

This means the "master-cog" must set variables for the code running in the step-pulses-creation-cog
How is this done? Do I have to do this by using the cog-stack? Or simply assigning a value to a "normal declared" variable?

Is there any example where I can study the principle?

best regards Stefan

Comments

  • Just an opinion, but it seems like a cleaner idea to create a stepper object that can accept step values through a method call. This will make your code easier to manage, and allow the stepper object to be used in other projects. That is, put the hard work into the stepper object so that it can be used anywhere.

    This means the "master-cog" must set variables for the code running in the step-pulses-creation-cog
    How is this done? Do I have to do this by using the cog-stack? Or simply assigning a value to a "normal declared" variable?

    Just to help you get back into the swing of sharing values between cogs, the little demo I attached starts a background Spin cog that takes a shared address and the number of ticks between. The background cog changes the variable, the foreground cog pushes the new value out to a terminal.

    Technically, a secondary Spin cog running in a project can see the global variables of that project. That said, I tend to pass the address of the shared memory to the background. If the background changes from Spin to PASM, you still have what you need.

  • I got an error running your jm shared memory, in the PUB setup, "term.tstart(BR_TERM)" yields an error, so I changed it to "term.start(31,30,1,9600)" and it worked.

  • JonnyMacJonnyMac Posts: 8,912
    edited 2022-04-24 15:25

    I got an error running your jm shared memory, in the PUB setup, "term.tstart(BR_TERM)" yields an error, so I changed it to "term.start(31,30,1,9600)" and it worked.

    Replace your version of jm_fullduplexserial.spin with the one in the ZIP. I added tstart (terminal start) a long time ago. You'll need to copy jm_nstr.spin to your Library folder, too (this does number-to-string conversions).

  • I kinda thought you may have revised it and kept the same name. I already had all the jm_’s in my library so I just saved the demo. I myself like to rename my programs Rev. A ,B etc.. just a matter of style.

  • JonnyMacJonnyMac Posts: 8,912
    edited 2022-04-25 13:26

    I do that for applications, but not libraries.

Sign In or Register to comment.