Jump to content
OMRON Forums

Improvements to Script Mode access in C


daves

Recommended Posts

Hi

 

Firstly I am really pleased about the script mode and enum access to global variables.

 

I would like to suggest an improvement and it looks like the code is almost in the pp_proj.h but not quite finished...

 

In the _PPScriptMode_ section you have 'enum globalP' and the XXXEnumGlobalXXX() functions, but they seem to be pointless (the enum is not generated). Is this right?

 

In my code I use a structure representing an axis and store references to my variables for that axis in the structure. I currently store the P or M number, then I can pass instances of the structure to a function which can then call SetGlobalVar on the reference to perform actions common to each axis. For example...

 

struct AxisObject
{
int Param1; // A parameter each of my axes has
};

struct AxisObject A1; // Two instances of an axis
struct AxisObject A2;

void InitAxes() // Set up the references
{
A1.Param1 = axis1Param1G; // Reference to 'global axis1Param1G'
A2.Param1 = axis2Param1G; // Reference to 'global axis2Param1G'
}

void AxisWork(struct AxisObject A) // Do something to an axis
{
SetGlobalVar(A.Param1, 100);
}

 

Now I cannot use the script mode access (and I really want to) as I cannot pass a reference to a P variable anymore. Also trying to use the script mode GetGlobalVar as defined in pp_proj.h does not work, so you cannot mix modes.

 

My solution (which works, I have manually coded it) is to fill the global enum as well (as it appears your intention may have been?) and give the option of calling both methods (Note the _ID suffix for the enum access):

 

New pp_proj.h generation (similar to what _EnumMode_ does):

 

enum globalP {_globalP_=-1
,axis1Param1G_ID=8192
,axis2Param1G_ID=8193};
#define	axis1Param1G	pshm->P[axis1Param1G_ID]
#define	axis2Param1G	pshm->P[axis2Param1G_ID]

 

Then:

 

struct AxisObject
{
enum globalP Param1;
};

struct AxisObject A1;
struct AxisObject A2;

void InitAxes()
{
A1.Param1 = axis1Param1G_ID;
A2.Param1 = axis2Param1G_ID;
}

void AxisWork(struct AxisObject A)
{
   SetGlobalVar(A.Param1, 100);
}

 

This works beautifully for my needs (and has some type-safety), does not affect 'normal' script mode access, and seems to fill the missing code in pp_proj.h.

 

What do people think? (I have not considered arrays)

 

Dave

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Since you are using a structure, why not just use pointers?

 

struct AxisObject

{

double * Param1; // A parameter each of my axes has

};

 

void InitAxes() // Set up the references

{

A1.Param1 = &axis1Param1G; // Reference to 'global axis1Param1G'

A2.Param1 = &axis2Param1G; // Reference to 'global axis2Param1G'

}

 

Then instead of using SetGlobalVar(unsigned, double) you just do your pointer stuff:

 

void AxisWork(struct AxisObject A)

{

*A.Param1 = 100;

}

 

In some ways its not as nice, I agree, but it works. I guess it would depend on how much of this kind of thing you are doing. With the integer index you kind of get a pass-by-reference kind of thing without dealing with pointers, so it is nice in that regard.

 

BTW, are you using structures of P vars in C to make things convenient on the C side of the fence? I've not used structures with P vars because on the motion programs it is not structured anyway and you have to resort to naming conventions.

 

I do like your idea of having both options available. It provides that flexibility seemingly without a problem.

 

KEJR

Link to comment
Share on other sites

I think this is very nice. Thanks for the idea. Now I just have to decide when is a good time to go through and delete all the GetGlobalVar/SetGlobalVar calls!

 

I just converted my code last night after getting the script mode thing working. I have to say my code reads much more nicely than it did.

 

Now if we had C++ we could get pass by reference .... (hint hint).... :o)

 

KEJR

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

×
×
  • Create New...