Multiple cogs not cooperating
AlexMunro
Posts: 7
Hello, I know you have answered many many many questions about using multiple cogs and global variables. Well here is yet another one.....
I scoured the forums before posting this to see if i could find some hint of what is causing my problem but i am having a hard time relating other peoples problems to this one in particular. I also read through the manual many many time and i am sure there is something in the manual that i am not fully understanding.
My project is a balancing robot (yes ANOTHER balancing robot). I want the accelerometer readings, gyro readings and angle calculations to be done in a single cog continuously to ensure that these critical readings are always as up to date as possible. I was able to achieve this with the calculation loop taking ~10ms which could be improved but is probably OK for my needs.
The main loop starts everything up and loops continuously to calculate the PID control values setting the motor speeds based on the angle calculations. All of the variables used in the main loop and the Angle calculation cog are declared in the main top object and there doesn't seem to be any problem with setting variable in the angle cog and reading the variables in the main loop.
Things are a bit more complex then they first appear because several other cogs are launched to support other functions, debug serial terminal, I2c spin, Float string, Float32 , etc In total 7 cogs are running:
Main Cog : 0
Debug Cog : 2 ''Parallax Serial Terminal Plus
FP cog : 3 ''Float32
FP string Cog : 4 ''FloatString
Angle cog : 5 '' My own angle calculation
Qik Serial cog : 6 ''FullDuplexSerial used to communicate with the motor controller
My Problem is this:
1) When i use Float String functions to send debug information to the serial terminal, the serial communication randomly stops within a minute. It simply aborts and i have to reload the ram and restart.
2) If i do NOT use the float string functions everything runs continuously but occasionally (a few times every second) the variables receive random data. i.e. they are written to inappropriately.
I am sure that one of the cogs is inadvertently using the same memory location as my variables. For example, i have no idea what is on Cog 1. Float32 is an object at two levels but there is only one cog, where is the other one (probably screwing up my variables)?
I am including my code (please excuse the amateur nature of it). all of the objects can be found in OBEX but I had to make a couple of small adjustments to get the cog numbers back to the main loop.
Any help on this matter would be greatly appreciated. _m_
I scoured the forums before posting this to see if i could find some hint of what is causing my problem but i am having a hard time relating other peoples problems to this one in particular. I also read through the manual many many time and i am sure there is something in the manual that i am not fully understanding.
My project is a balancing robot (yes ANOTHER balancing robot). I want the accelerometer readings, gyro readings and angle calculations to be done in a single cog continuously to ensure that these critical readings are always as up to date as possible. I was able to achieve this with the calculation loop taking ~10ms which could be improved but is probably OK for my needs.
The main loop starts everything up and loops continuously to calculate the PID control values setting the motor speeds based on the angle calculations. All of the variables used in the main loop and the Angle calculation cog are declared in the main top object and there doesn't seem to be any problem with setting variable in the angle cog and reading the variables in the main loop.
Things are a bit more complex then they first appear because several other cogs are launched to support other functions, debug serial terminal, I2c spin, Float string, Float32 , etc In total 7 cogs are running:
Main Cog : 0
Debug Cog : 2 ''Parallax Serial Terminal Plus
FP cog : 3 ''Float32
FP string Cog : 4 ''FloatString
Angle cog : 5 '' My own angle calculation
Qik Serial cog : 6 ''FullDuplexSerial used to communicate with the motor controller
My Problem is this:
1) When i use Float String functions to send debug information to the serial terminal, the serial communication randomly stops within a minute. It simply aborts and i have to reload the ram and restart.
2) If i do NOT use the float string functions everything runs continuously but occasionally (a few times every second) the variables receive random data. i.e. they are written to inappropriately.
I am sure that one of the cogs is inadvertently using the same memory location as my variables. For example, i have no idea what is on Cog 1. Float32 is an object at two levels but there is only one cog, where is the other one (probably screwing up my variables)?
I am including my code (please excuse the amateur nature of it). all of the objects can be found in OBEX but I had to make a couple of small adjustments to get the cog numbers back to the main loop.
Any help on this matter would be greatly appreciated. _m_
Comments
If you attach an archive there's a better chance people will look through your code.
I didn't see anything wrong in the parent object. It looks like you use Float32 a lot. I think you do need to be careful about calling methods in Float32 from more than one cog at a time.
There's a new and improved floating point object called F32. It's faster and smaller than earlier alternatives. I believe it's a drop in replacement for Float32.
and
It looks like you're attempting to test (incorrectly) for a bit within variables? Is this what you're trying to do?
Based on the way your variables are ordered:
"accStatus[4]" is another name for the long "DAng".
"GyrStatus[4]" is another name for the variable "Force"
I'm guessing
should be
or some other way to check for the value of a single bit.
Also i can see that i am using the [4] syntax all wrong and will stop that immediately.
In my latest attempt i made removed the bit test, Replaced Float32 with F32, and replaced the Float string i was using with the floatstring included in F32.
I am now using one less cog, floatstring doesn't seem to hang it up anymore but i am still getting random numbers popping into the variables
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:1560067656 <---- this cannot be possible
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
tErr:-170 iSpd:-340
The problem was that two separate cogs were trying to use the same F32 object. I had to give my cog its own F32 object started from the angle calculation cog. The only problem with this is that i now have f32 using up 2 cogs :frown:.
Well i still have a lot of work to do to streamline but at least that mystery is solved (sort of).