Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass reference to ptr
#1
We're trying to figure out how to deal with ptr variables in script like an actual pointer.

Basically, we would like to pass a pointer by reference to a subprogram. However, the system of "arguments" in PPMAC just copies by value back and forth between R and L variables, so the function can't manipulate the address being pointed to.


Ideal case - a generic function for actuators that could get passed the pointer to IO locations, like the following:

Code:
// generic actuator function:
open subprog extend(solenoid, limitswitch)
    solenoid = 1;     // turn on solenoid
    while (limitswitch == 0) {}     // wait for full extension
close

Code:
// component that uses it:
ptr GripperOpenSolenoid -> ECAT[0].IO[15].data
ptr GripperOpenLimitSwitch -> ECAT[0].IO[16].data

open subprog OpenGripper(void)
    call extend(GripperOpenSolenoid, GripperOpenLimitSwitch);
close


Is there a way to do this by reference in the subprog argument? Or is there a way to retrieve and pass the M variable number so that the generic function could access Sys.M[12324]? Open to any other suggestions. Thanks!
Reply
#2
If I recall correctly, just put an ampersand on the desired argument. I'll modify your example below:

// generic actuator function:
open subprog extend(&solenoid, limitswitch)
solenoid = 1;
while (limitswitch == 0) {}
close

Code:
// component that uses it:
ptr GripperOpenSolenoid -> ECAT[0].IO[15].data
ptr GripperOpenLimitSwitch -> ECAT[0].IO[16].data

open subprog OpenGripper(void)
call extend(&GripperOpenSolenoid, GripperOpenLimitSwitch);
close
Reply
#3
The ampersand makes the argument into a "return value" where you can get a value back from the function. This still passes by value, not by reference. When you look at the compiled version of this code, it is dealing with a local copy stored in L and R variables, not with the pointed-to location. Thus the desired address held by the ptr (e.g. my ECAT address) never gets changed.
Reply
#4
That capability is not currently a feature of the “IDE Program Enhancements”. As you have surmised, you would need to identify the M-variable numeric assignment used and pass that manually the in the subroutine and use the Sys.M[] reference. You can find these assignments one way by query of the “ptr” name in the terminal window. It will respond with the M-variable number. It is also listed in the generated header file in the project’s root directory.
Reply
#5
Even better, just make your own #defines under M8192 so you know the number reference to pass.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)