Shop OBEX P1 Docs P2 Docs Learn Events
Multiple cogs not cooperating — Parallax Forums

Multiple cogs not cooperating

AlexMunroAlexMunro Posts: 7
edited 2014-04-24 17:22 in Propeller 1
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:blush:). 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

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-23 13:52
    If you use the archive tool (under the "File" menu) you can zip all the object together to make it easier for us to look at the code.

    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.
  • AlexMunroAlexMunro Posts: 7
    edited 2014-04-23 16:20
    I attached the Archive. That is a great feature, glad you pointed it out.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-04-23 17:55
    I'm curious about a couple of lines of code in your parent object.
    accStatus := i2c.read(accw, STATUS_REG_A,8,8)
        if accStatus[4] == 1  '' & %00001000 == %00001000        '' check for acceleromter data ready
    

    and
        GyrStatus := i2c.read(GyrW, STATUS_REG,8,8)
        if GyrStatus[4] == 1  '' & %0000_1000 == %0000_1000
    

    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:
    long EstAng, DTime, D1,D2, AccelX, AccelZ, GyroY, Anglst, AngVel, Ang, AccStatus, GyrStatus
     Long GyAng, T, DAng, Force, GyrConv, NAccelX, NAccelZ 
    
    

    "accStatus[4]" is another name for the long "DAng".

    "GyrStatus[4]" is another name for the variable "Force"

    I'm guessing
    if accStatus[4] == 1
    

    should be
    if accStatus & %1000 == %1000
    

    or some other way to check for the value of a single bit.
  • AlexMunroAlexMunro Posts: 7
    edited 2014-04-23 19:03
    Yes i am/was testing for a high bit. Originally I was using the bit mask that is commented out which i see is also an error. in my troubleshooting i skipped the bit test altogether and just read the register whether or not it was ready.
    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
  • AlexMunroAlexMunro Posts: 7
    edited 2014-04-24 17:22
    I got it.....
    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).
Sign In or Register to comment.