How to access "external" variables
Joerg
Posts: 91
I would like to have different COG's running for controlling a stepper motors. I have gotten the basics of spin, even being a asm freak for "normal" MCU's. So what i have done is writing a program called "stepper" which will run in different COG's and i would like to have a master COG running an managing some variables, so the different COG's are acting as i need it.
So far so good; but how can i declare variables in the main routine so they will be seen by the "stepper" object?
Here what i did:
{{MotionController.spin}}
VAR
byte StepTime
OBJ
Motion : "Stepper"
PUB Main
StepTime := 1000
Motion.Start(1)
{{Stepper.spin}}
VAR
long Stack[noparse][[/noparse]9] 'Stack space for new cog
byte Cog 'Hold ID of cog in use, if any
byte StepNr 'the actual step number
PUB Start(Pin0): Success
StepNr~ 'clear the StepNr counter
DIRA[noparse][[/noparse]Pin0 .. PIN0+4]~~ 'sets the needed pins as output
Stop
Success := (Cog := cognew(ManageMotor(Pin0), @Stack) + 1)
PUB Stop
{{Stop toggling process, if any.}}
if Cog
cogstop(Cog~ - 1)
PUB ManageMotor(Pin0)
repeat
waitcnt(clkfreq / 1_000_000 * StepTime + cnt) ' Wait for steptime cycles
outa[noparse][[/noparse]Pin0 .. Pin0+4] := STEPTBL[noparse][[/noparse]StepNr]
++StepNr
DAT
STEPTBL byte 1, 5, 4, 6, 2, 10, 8, 9 'table for controling the motor (halfsteps)
But the variable StepTime is not accepted by the compiler
Thanks
Joerg
So far so good; but how can i declare variables in the main routine so they will be seen by the "stepper" object?
Here what i did:
{{MotionController.spin}}
VAR
byte StepTime
OBJ
Motion : "Stepper"
PUB Main
StepTime := 1000
Motion.Start(1)
{{Stepper.spin}}
VAR
long Stack[noparse][[/noparse]9] 'Stack space for new cog
byte Cog 'Hold ID of cog in use, if any
byte StepNr 'the actual step number
PUB Start(Pin0): Success
StepNr~ 'clear the StepNr counter
DIRA[noparse][[/noparse]Pin0 .. PIN0+4]~~ 'sets the needed pins as output
Stop
Success := (Cog := cognew(ManageMotor(Pin0), @Stack) + 1)
PUB Stop
{{Stop toggling process, if any.}}
if Cog
cogstop(Cog~ - 1)
PUB ManageMotor(Pin0)
repeat
waitcnt(clkfreq / 1_000_000 * StepTime + cnt) ' Wait for steptime cycles
outa[noparse][[/noparse]Pin0 .. Pin0+4] := STEPTBL[noparse][[/noparse]StepNr]
++StepNr
DAT
STEPTBL byte 1, 5, 4, 6, 2, 10, 8, 9 'table for controling the motor (halfsteps)
But the variable StepTime is not accepted by the compiler
Thanks
Joerg
Comments
2) You can't access variables in one object from another object as you've noticed.
One way to handle this is to have a variable in Stepper.spin called StepTime and
have a PUB method in Stepper called SetStepTime like this:
This would be called from the main program whenever you wanted to change it.
I'm sure there are all sorts of other ways depending on what you want to do.
Thanks for the answer, the byte declaration was a a mistake, but i find it really cant communicate over the main RAM! This would have solved many problems and would permit real fast, simple programming!
Is thers really no way for doing so?
Joerg
Maybe you can pass the address of the step time as an additional parameter to Manage.start, save the address
in a long variable (timeAddr) in Stepper.spin, then access it directly in Stepper.spin using long[noparse][[/noparse] timeAddr ].
One other thing I noticed is that you're setting pins 0-4 as outputs in the main cog and then trying to use
them as outputs in the second cog (ManageMotor). This won't work. Each separate cog has its own DIRA
and OUTA registers. Move the "dira[noparse][[/noparse] pin0..pin0+4 ]~~" into the ManageMotor method before the "repeat".