jtaseff Posted October 17, 2016 Share Posted October 17, 2016 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: // generic actuator function: open subprog extend(solenoid, limitswitch) solenoid = 1; // turn on solenoid while (limitswitch == 0) {} // wait for full extension close // 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! Link to comment Share on other sites More sharing options...
Clopedandle Posted October 17, 2016 Share Posted October 17, 2016 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 Link to comment Share on other sites More sharing options...
jtaseff Posted October 17, 2016 Author Share Posted October 17, 2016 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. Link to comment Share on other sites More sharing options...
steve.milici Posted October 24, 2016 Share Posted October 24, 2016 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. Link to comment Share on other sites More sharing options...
steve.milici Posted October 25, 2016 Share Posted October 25, 2016 Even better, just make your own #defines under M8192 so you know the number reference to pass. Link to comment Share on other sites More sharing options...
Recommended Posts