cogspin questions
RS_Jim
Posts: 1,768
in Propeller 2
Hi,
I am confused by what is happening in cogspin. I want to start a new cog that includes JonnyMac's JM_multi_adc.spin2 that starts out as " not a top object" when I start the new cog, call it looper, were all I want to do is to read 8 pins of analog and report it out in a global VAR, the compiler goes to the cogspin routine and then to never, never land. I guess I need more explanation about how to use COGSPIN.
Jim
Comments
Cogspin calls a Spin method in its own cog. This needs a cog, a method and several bytes of stack.
Example from my program:
First, declare the stack in your main program VAR section:
long sbusstack[64]
Second, write a method to work in its own cog, which in most cases has to be an infinite loop. In this case it receives and decodes SBUS bus placing results in "channel" which is also global variable array.
Notice it calls another methods, this will be also done in the method's cog
Now you can call cogspin
cogspin(6,sbus(),@sbusstack)
and the cog #6 will be executing sbus() method while your main cog will go to the next instructions.
Use 16 as a cog number if you want to get a first free available cog.
Keep in mind that Spin cogs have access to global objects and variables in your main application. For example, I'm working on a project that starts two copies of my jm_fullduplexserial and then launches a Spin cog that monitors one of those serial objects for commands coming from a serial HMI.
I allow the Propeller to manage cog allocation with this syntax (NEWCOG lets the P2 select an available cog).
Another habit is to declare the method that will be run by the background cog as PRIvate -- this just reminds me not to call that like a normal method because that method will usually have an infinite repeat loop.
ok, it is still not happening for me.
I want to run the following code in a separate cog:
I cannot figure out where the wheels have come off the track. When I start the main program (adctest) I have an on screen display of several i.o pins and that works fine but once it calls cogspin, it goes to never never land..
Jim
EDIT: I woke up at 3AM this morning remembering how to post code in the forum so I have re inserted the code, maybe it will make more sense.
EDIT2: I changed the begining of looper code to this:
I cannot see any cogspin in this code (and please edit the post so the code will be more readable)
What you have to start in the cogspin is your main (don't forget about () - main() )
cogspin(cog#,main(),stackspace)
and you have to provide stackspace - 64 longs seems to be a safe valua for the start. Too low stack space causes the program to not work as expected.
The question is: Why do you want to start this in a new cog? The AD conversion is anyway made by the smartpins in background continuously. The only gain would be that the scaling calculation of the raw value is made by another cog, this saves a few microseconds when you read the value. But is it worth to waste a second cog for that.
I edited post 3 to correct the code posting and to change the beginning of the looper cog to include a
PUB begin().
that solved the going off to never never land, but I am still not reading the ADC pins and reporting them to myBuf. I am just reading all 0. So if I can get @Jonny Mac to look at the code I will appreciate him showing me the error of my ways. I am using a stack size of 50 longs, I think that will be enough but will change to 64 to see if that helps.
Jim
No, I had started with an inline read and when it wasn't working,I thought perhaps it needed ti be run in it's own cog. I started it inline online.
I am still not getting my analog resulta so I have yo go back anda review the adc code.
Jim
Here is my simple object doing A/D conversion in a dedicated cog. I wrote this for measuring a battery voltage in a robot. Maybe in the future I will get rid of it and free this cog, but now it works as it is. The battery voltage is divided by 10 via a resistor divider and then it goes to the P2 pin, so this 33000 multiplier gives a milivolt result.
I think this is the problem:
The read() scaling expects an integer value for
hi
. For float values you would need to modify the scaling code to use the new float operators.BTW. For a code block you need 3 backticks before and after the code, not these apostrophes.
Hi,
Well, programing can certainlymake you feel dumb sometimes!. I could not figure out why I could not get Jon's adc code to work. My problem was I was trying to feed the channel number with the pin number! Once setup is run, you feed the read instruction with the channel you want read and the program selects the correct pin based upon the setup information! What I was doing wrong finally hit me like a falling wall of bricks. Thanks to Ariba's comment I went back to my origonal plan to use inline c ode to read the adc..working fine now that i try to read the channel number not the pin number.
Jim