Inter-Cog Communication
Hi everyone, I have been working on a SPIN program to control a formerly-remote-conrol car, and I am having some problems. I've done some searching but havent come up with anything directly related to my main problem. I am using one cog to send pulses to the motor controllers and another to poll sensors/make decisions. I am currently storing commands in the form of numbers in a word/long (cant remember which size is readable/writeable in one clock cycle, I'll have the source in front of me tomorrow) and passing the address of these variables to the new cog as it starts. I havent gotten this to work, and I havent figured out a debug method yet (lcd,terminal,etc). However, I think the problem involves the motor control cog reading these variables, as the code works on its own. Am I going about this in the right manner? How would you guys suggest I transmit commands between cogs? Any suggestions greatly appreciated.
Thanks very much
Thanks very much

Comments
http://obex.parallax.com/objects/91/
I did not write it
{{Note that the variables given for ideal and ramp are the addresses: I use it to access the variable in real time}} PUB Start(newPinDrive, newPinSteer, newIdealDrive, newIdealSteer, newRampDrive, newRampSteer) pinDrive := newPinDrive pinSteer := newPinSteer idealDrive := newIdealDrive idealSteer := newIdealSteer rampDrive := newRampDrive rampSteer := newRampSteer realSteer := center realDrive := 1500 uS := clkfreq / 1_000_000 cognew(run, @runStack) PRI run 'this is launced into a new cog, and does three things: 'Checks the values of variables and updates as needed 'Pulses the steering servo and motor 'waits 20 ms repeat 'getValues servoProcess(pinDrive, realDrive, idealDrive, rampDrive) centerSteering servoProcess(pinSteer, realSteer, @modifiedSteer, rampSteer) waitcnt((uS*1000*20) + cnt) PRI servoProcess(pin, real, ideal, ramp) | w, length, idealHolder, rampHolder {{ real is the speed/position that the servo is at now ideal is where the servo should be ramp is the speed at which to go there 0-set real instantly to ideal 1-increment in 1 uS 2-increment in 2 uS 3-increment in 3 uS ... n-increment in n*1 uS }} outa[noparse][[/noparse]pin]~ dira[noparse][[/noparse]pin]~~ idealHolder := LONG[noparse][[/noparse]ideal] rampHolder := LONG[noparse][[/noparse]ramp] if(rampHolder == 0) real := idealHolder else if(real < idealHolder) real := real + rampHolder elseif( real > idealHolder ) real := real - rampHolder if ( real > (idealHolder - rampHolder) AND real < (idealHolder + rampHolder)) real := idealHolder length := real w := length * uS outa[noparse][[/noparse]pin]~~ ' If pulse width is non-zero waitcnt(w + cnt) ' produce the requested pulse outa[noparse][[/noparse]pin]~Also, note that the ramp ability isn't really implemented yet. It has a bug, and I haven't gotten around to fixing it. However, the inter cog communication is still viable and works well.
Here is a link where I disscussed propeller general questions, including inter object and cog communication.
http://forums.parallax.com/showthread.php?p=765086
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 Dat var long 0 PUB start ptrToVar := @var cognew(@TimeKeeper,0) DAT org 0 TimeKeeper rdlong localVal,ptrToVal ...do something... wrlong localVal,ptrToVal localVal long 0 ptrToVar long 0variable-access across cogs
here is a code-example showing access to a variable from different cogs within the same *.SPIN-File
you simple use the NAME of the variable itself
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long heart_stack[noparse][[/noparse] 20] long CogStack1[noparse][[/noparse] 20] long CogStack2[noparse][[/noparse] 20] long MyTestVar OBJ 'heart : "heartbeat" 'little object from the obex making an LED blink debug : "FullDuplexSerial" PUB Main 'the FIRST PUB-Method inside a spinfile is ALWAYS the startpoint where the program starts to run 'heart.Start(27, 200) debug.start(31, 30, 0, 9600) MyTestVar := 100 debug.str(string("Start MyTestVar=")) debug.dec(MyTestVar) debug.Tx(13) cognew(M1,@CogStack1) cognew(M2,@CogStack2) repeat waitcnt(clkfreq + cnt) debug.str(string("MyTestVar=")) debug.dec(MyTestVar) debug.Tx(13) PUB M1 repeat waitcnt(ClkFreq * 3 + cnt) MyTestVar := 1 PUB M2 repeat waitcnt(ClkFreq * 5 + cnt) MyTestVar := 2with this democode you should get an output to your terminalsoftware similar to this
As you can see from this demo
as long as you want to use a variable from the SAME *.SPIN-file
you just use the NAME of the variable
It is the same as long as you want to access the variable in the SAME object
(which is defined in another *.SPIN-File)
therefore the variable has to be defined INSIDE that object (that *.SPIN-file)
to get access to variables ACROSS OBJECTS the variable is defined in one object
and the OTHER objects can access this variable through the RAM-Adress of this variable
you get the RAM-Adress of a variable with the "@"-operator
best regards
Stefan
remember also that you have to serialize the access to the variable.· Read or Write operations required to use prop lock mechanism.
jm.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker