Shop OBEX P1 Docs P2 Docs Learn Events
Global Variables & Cogs — Parallax Forums

Global Variables & Cogs

treborz17treborz17 Posts: 76
edited 2014-04-21 06:40 in Propeller 1
Subject:· Global Variables & Cogs
After a couple of years of working with the Basic Stamp's basic language, I find the transition to the Propeller's spin language a little overwhelming, well, at least challenging.· My main project is the completion of a Robot utilizing the Robot Base Kit and the 12V Motor Mount Kit.· By utilizing the Parallax libraries I have put together a few OBJ's that comes close to my goal of·operating an autonomous robot.
However, this is not about robots, my problem stems with my not understanding the utilization of the Spin language usage of variables and cogs.· I have an OBJ assembly of methods that does a great job of robot motor control, and another assembly of methods that do a great job of handling the 5 Ping's that I am using for avoidance control, as viewed on a LCD display.· As would be expected when all methods are included in one main object either the Ping's are not updating while a wheel is turning or a wheel movement ends prematurely if the Pings take control.· A "Catch 22".·
Obviously, this is where the multi cog ability of the propeller takes over.· Where my lack of ability lies, is not understanding how to communicate with a cog.· As soon as I put the Ping scanning methods in a "cognew" ·I lose the ability to address from the main method, the Global VARiables I have defined to contain the five Ping Sensor's distance measurement.
I got to thinking while writing this post, that maybe this should be a task that utilizes the sending of data serially from the ping-method-cog to variables in the main method?
I have spent days reviewing all of the available Parallax lititure on the subject, and am not quite able to overcome this problem.· Any assistance would really be appreciated.
Robert

·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-08 23:39
    The trick is not to put the cog routines in a separate object. You don't have to create a new object for each cog. If you put the PING scanning methods in your main program, they will have access to the global variables. You do need to avoid having two different cogs trying to change the same variables at the same time. Usually one cog sets the variable value and the other cog just looks at it without changing it. You can't get into trouble that way unless the one cog changes two variables and the other cog expects them to be changed in some particular order.
  • treborz17treborz17 Posts: 76
    edited 2010-07-09 01:28
    Mike,

    Thank you for your response -- I thought I had tried that. However, in looking over my old design copies, and with your confirming what I already thought was the correct way to include the cog, I see that maybe I had some other issues causing some of my problem. I will setup what you have suggested, and be sure that I have done it correct, try it again and I'll let you know how it went.

    Thanks again,

    Robert
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-09 01:57
    I know it's kind of daunting at first glance, but BoeBotBasic from the Object Exchange has a PING routine that runs in its own cog and a 3-Servo driver that runs in its own cog. The routines are near the end of the program and are pretty well commented. The code that interacts with these cogs is mixed in with other code, but there are specific Basic statements to get a PING reading and to set a servo position and you can do a text search for the statement code in the comments.
  • EMHmark7EMHmark7 Posts: 93
    edited 2014-04-21 06:40
    If it can help you or future lost people:

    doc v1.2 page 212:

    During compilation of an object, all declarations in its collective VAR blocks are grouped together by type. The variables in RAM are arranged with all the longs first, followed by all words, and finally by all bytes. This is done so that RAM space is allocated efficiently without unnecessary gaps. Keep this in mind when writing code that accesses variables indirectly based on relative positions to each other.

    So we can pass the pointer of the first variable.
    As reported by Duane
    Duane Degn wrote: »
    The variables in the VAR section will get rearranged if you don't order them long, word and byte.

    We just need to declare vars in this order, all same type together.
Sign In or Register to comment.