Host Engineering Forum
General Category => Do-more CPUs and Do-more Designer Software => Topic started by: RBPLC on November 20, 2021, 10:26:09 PM
-
What is the best way to make reusable code blocks within DmD? Is there any way to copy code from one Project and paste it into the current project and automatically re-assign memory elements from the pasted code in the event that the current project is already using those memory locations and they do not share the same Nicknames?
-
I know you can copy from 1 project and paste to another but the documentation goes with it. When i do it I tend to put in a new project change the documentation then paste that into the project it belongs in. Just less confusing so i don't miss something.
-
Attached is a document that shows an example of how I have accomplished this in the past. However, I have found caveats to this approach if your code block uses the DATAINFO instruction. Look it over, build a test code, and try it out. I hope this helps.
** This was originally a PowerPoint, but I had to paste the images into a Word doc in order to attach it to this thread.
-
These approaches allow you to import code blocks but they don't deal with the issue of not overwriting variables that are already in use. This is one major limitation that I frequently wish there was a good solution for.
-
These approaches allow you to import code blocks but they don't deal with the issue of not overwriting variables that are already in use. This is one major limitation that I frequently wish there was a good solution for.
True. Without the support of local variables, one must be extremely mindful when creating modular code blocks and the variable names within these blocks.
-
...and a major justification for the instantiable program blocks we've talked about.
-
Yes, locally scoped variables would be very cool.
There's also another thing that Siemens does that really works out well. I'll post more about it tomorrow when I don't have to 'type' on my phone.
-
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?
-
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?
It's doable, just a matter of available development resource. We're not Siemens or AB, so we have to choose our battles.
-
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?
If you make the "reassignable" memory locations as unassigned nicknames after you export them, the work flow to resolve them is already there. You can even do it with your existing export file (assuming you assigned nicknames to everything that is reassignable).
Here's an example section of an export file:
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
"X1","SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
"WX1","RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
"C1","PauseSimRS","",""
#END
Say we want all the ID 1's to be "reassignable" (so X1, WX1, C1), change those lines to the following
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
UB,"SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
UN,"RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
UB,"PauseSimRS","",""
#END
UB - means Unassigned Bit
UN - means Unassigned Numeric
US - means Unassigned Struct
After you import the code using Tools->Insert Instructions from File, go to the Tools->Assign Nicknames utility and resolve them for THAT specific project, e.g. RawThemostat may be WX5 in one project, D42 in another.
-
So the thing I was talking about with Siemens is this:
While FB's (function blocks) and FC's (functions -- single scan, non static) do have internal memory, and there are user defined types, there is also another thing that works out nicely for the programmer. When you create a DB (data block), it's basically just a named heap. It can contain pretty much anything. INTs, DWORDs, Timers, UDT instances, counters, whatever, as well as multidimensional arrays of any of the above, in any combination.
So the effect comes out to a potentially very large one-off UDT instance, because both the DB and the members are all named, which makes the code very readable while still keeping associated variables together in one DB.
This is a thing that kind of peripherally relates to code-modularity that I'd very much like to get in DM at some point.
-
So the thing I was talking about with Siemens is this:
While FB's (function blocks) and FC's (functions -- single scan, non static) do have internal memory, and there are user defined types, there is also another thing that works out nicely for the programmer. When you create a DB (data block), it's basically just a named heap. It can contain pretty much anything. INTs, DWORDs, Timers, UDT instances, counters, whatever, as well as multidimensional arrays of any of the above, in any combination.
So the effect comes out to a potentially very large one-off UDT instance, because both the DB and the members are all named, which makes the code very readable while still keeping associated variables together in one DB.
This is a thing that kind of peripherally relates to code-modularity that I'd very much like to get in DM at some point.
Absolutely.
In the mean time, you can get an 80% solution with existing features, with existing code blocks and existing memory model, as-is, as RBPLC was asking.
-
Right, my post wasn't in response to his application, just putting up a wish list item on a related topic.
-
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?
If you make the "reassignable" memory locations as unassigned nicknames after you export them, the work flow to resolve them is already there. You can even do it with your existing export file (assuming you assigned nicknames to everything that is reassignable).
Here's an example section of an export file:
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
"X1","SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
"WX1","RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
"C1","PauseSimRS","",""
#END
Say we want all the ID 1's to be "reassignable" (so X1, WX1, C1), change those lines to the following
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
UB,"SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
UN,"RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
UB,"PauseSimRS","",""
#END
UB - means Unassigned Bit
UN - means Unassigned Numeric
US - means Unassigned Struct
After you import the code using Tools->Insert Instructions from File, go to the Tools->Assign Nicknames utility and resolve them for THAT specific project, e.g. RawThemostat may be WX5 in one project, D42 in another.
Nice! Thank you for this information.
-
It doesn't appear as though you can program with unassigned structures. For example if I name a TMR tTestTimer and initially assign it as an unassigned structure, as I'm writing my generic code using unassigned variables, I'm not allowed to use tTestTimer.done elsewhere in my generic code.
-
It doesn't appear as though you can program with unassigned structures. For example if I name a TMR tTestTimer and initially assign it as an unassigned structure, as I'm writing my generic code using unassigned variables, I'm not allowed to use tTestTimer.done elsewhere in my generic code.
That is correct. Unassigned nicknames are typeless, and there isn't currently a way to associate type information with the documentation record.
-
It doesn't appear as though you can program with unassigned structures. For example if I name a TMR tTestTimer and initially assign it as an unassigned structure, as I'm writing my generic code using unassigned variables, I'm not allowed to use tTestTimer.done elsewhere in my generic code.
An alternative to unassigned nicknames for a structure would be to use a heap item, e.g. change NN tTestTimer for T42 to a heap item called tTestTimer. Unless you are using arrays on your T block or the original C-more driver (there's the symbolic driver), a heap item works just as well.
-
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?
If you make the "reassignable" memory locations as unassigned nicknames after you export them, the work flow to resolve them is already there. You can even do it with your existing export file (assuming you assigned nicknames to everything that is reassignable).
Here's an example section of an export file:
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
"X1","SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
"WX1","RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
"C1","PauseSimRS","",""
#END
Say we want all the ID 1's to be "reassignable" (so X1, WX1, C1), change those lines to the following
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
UB,"SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
UN,"RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
UB,"PauseSimRS","",""
#END
UB - means Unassigned Bit
UN - means Unassigned Numeric
US - means Unassigned Struct
After you import the code using Tools->Insert Instructions from File, go to the Tools->Assign Nicknames utility and resolve them for THAT specific project, e.g. RawThemostat may be WX5 in one project, D42 in another.
As a test, I tried to insert the following from file:
STR bMasterRun
TMR tRunDlyTimer 10000
// Rung $Main#2
// Offset 3
STR tRunDlyTimer.Done
OUT bRunMotor
// End of Code Block $Main
$PGMEND $Main
#BEGIN ELEMENT_DOC
UB,"bMasterRun","",""
UB,"bRunMotor","",""
US,"tRunDlyTimer","",""
#END
I get an error upon insertion that the data size must be one of bit, but this is a structure (because of the timer). If I define it as a UB, I get an error telling me that it must be a structure but it's defined as a bit.
-
STR tRunDlyTimer.Done
won't work since tRunDlyTimer is just an Unassigned Structure (not any specific structure)
You will need to do one of the following
1. define a different nickname tRunDlyTimerDone (no dot) as Unassigned Bit OR
2. Use a heap item of a Timer called tRunDlyTimer (not a nickname, but a heap item)
#2 is better because tRunDlyTimer IS a Timer (no need to "assign" it in the new project)
-
I appreciate the recommendations but the work-arounds are too cumbersome in my opinion to try to effectively implement. I would attempt the unassigned variables route but then have to create a heap item in addition to this to deal with structures is a bridge too far for me. I guess keep this on the list for future improvements.
-
I appreciate the recommendations but the work-arounds are too cumbersome in my opinion to try to effectively implement. I would attempt the unassigned variables route but then have to create a heap item in addition to this to deal with structures is a bridge too far for me. I guess keep this on the list for future improvements.
We were just talking about a manageable way (in development complexity) to do reusable code objects. Nested UDTs really opens up a lot of options and makes the problem much friendlier. DmD 2.9 is due out shortly, but hopefully we can take a run at in the next release.
-
Have a good Thanksgiving, everyone! :)
-
Have a good Thanksgiving, everyone! :)
Yes! You too!