How do I use complex PUB's in newcog
EXPO5
Posts: 2
I'm using the folowing code to control 4 motors using 3 sensors.
The code is already working, when only using 1 cog. the problem with this is, that, while prefroming an evasive manouvre, I can't get any sensordata. So using a cog to read the sensors simultaniously, should solve my problem, but I can't get it working.
can anybody help me with this problem?
The code is already working, when only using 1 cog. the problem with this is, that, while prefroming an evasive manouvre, I can't get any sensordata. So using a cog to read the sensors simultaniously, should solve my problem, but I can't get it working.
can anybody help me with this problem?
pub Start Init repeat cognew(ReadSensors, @Sensorstack) cognew(WritePST, @PSTstack) if data1>100 evaderight else goBack if data2>100 evadeleft else goback if data3>100 gostraight else goback PUB writePST PST.dec(data1) PST.tab PST.dec(data2) PST.tab PST.dec(data3) PST.Newline PUB ReadSensors AcquireValue1 AcquireValue2 AcquireValue3 data:=AcquireValue1 data2:=AcquireValue2 data3:=AcquireValue3 PUB goStraight QiK.SetSpeedM0(10,20) QiK.SetSpeedM1(10,-20) QiK2.SetSpeedM0(10,-20) QiK2.SetSpeedM1(10,20)
Comments
PUB Start
cognew...
cognew...
repeat
I had a similar concern when I wrote my object for the 74C922 Keypad object.
So instead of ;
data1 := AcquireValue1
you should use;
byte[@data1] := AcquireValue1
and in your Start method use;
if byte[@data1] > 100
evaderight
etc
Reading/Writing Bytes of Main Memory (Syntax 3) -- page 53 of the manual.
(Also just in case you copied your methods straight from the Prop Tool -- you have data1 in the top method, and just data in the bottom method -- TYPO?)
Your code should look something like what I show below. I'm assuming that all the routines are in the same file (or object) so you can access data1, data2 and data3 directly. BTW, I changed "data" to "data1" in ReadSensors. I think that's what you intended. The cognew's should only be called once, so I moved them out of the repeat loop. I added a repeat loop to the two routines ReadSensors and writePST.
Note that your three cogs are all running repeat loops independent of each other, so there is no synchronization among them. You may want to use waitcnt to make them run at the same loop interval, or better yet, use a variable that is set and cleared by one of the cogs to make the other two cogs wait. That way, only your main cog needs to use waitcnt and the other cogs will be synchronized with it.
I don't know what your AcquireValue routines look like, so I don't know if you need to call them twice per loop. I suspect you only need to call them once per loop when setting the data value.
It would help if you posted all your code. You can attach the files to your post by clicking on the "Go Advanced" link.
Dave
I'm also trying something with a variable infront of cognew, but it's not yet in this version of the code.
When I write cognew outside of a repeatloop, it seems to be doing even less.
Stating that cognew outside of the loop is doing even less makes a buggy code surely not working
To say it really clear
your use of cognew is like this
you want to drill a whole into a piece of wood taking a drilling machine into your hand
now shortly after start drilling the whole with the first drilling machine you take a second drilling machine
and want to drill into the EXACTLY same whole as the first drilling machine is working on
That's just something that MUST go wrong under all circumstances.
So the bugs must be somewhere else.
As you haven't provided much information about what you are trying to do. Please post a description of how your project works.
I'm willing to help but I won't analyse your code line by line until I know what your code does
best way to do this is to comment your code what all important things.
And to have an easier comparison attach the code that already works for one motor.
Please use the archive-function of the propellertool for this. It is much easier as you only create ONE zip-file containing ALL *.spin-files at once and the propellertool adds hierarchy-information
how the spin-files depend from each other.
Please give a detailed description of what do you expect the motors doing and what the do instead.
best regards
Stefan