I 1/2 bring this up just to see if functionality already exists and I’m missing it…
For my robot, I have a default speed. It would be nice if I could set a constant in the main patch & access it from other patches so I don’t have to change it in multiple places.
The 2nd idea is for something similar with variables. Where I’m thinking about using it is in the range-finder routine that positions the servo. If I could recall the last position, I could calculate how long it would take to reach the new position (so I don’t have to waste time when it doesn’t move, but still wait long enough for full motion). I could store this in the struct of the node, but I end up with multiple instances & each will have its own value. It seems you would need a pulse to update the global value, but you could read it like any constant or other node output.
Are these even possible in the XOD paradigm? Am I just stuck in my procedural ways and need to be enlightened?
Create a constant-number node with the default speed value
On other patches put input-number terminal to promote that value to an input
Link the constant to inputs created
Seems like you want global variables. Functional programming tries to avoid them (for good reasons). May I suggest the same principle as before? Try to compute that value once and pass it to all consumers as a parameter.
That’s the “right way” from an academical point of view. Shared nothing, single point of truth, no side-effects, bla-bla-bla. However, it could happen it’s hardly usable in real life because of, for example, too many links, too much effort to create/change/understand, etc…
If we would have a particular example that’s correct academically but mess on practice, I’d glad to discuss which UI/presentation improvement we can make to change the experience from poor to great.
I guess I could save previous servo position in a buffer each time I call the routine that uses it. It would require pulling the routine to search for an escape into main so every time servo is called it can change buffer & get previous position from it. It would eliminate need for global var or persistent node at the cost of flattening code into fewer patches.
It would probably make most sense to have a new servo node that adds inputs for previous position and a pulse and then emits a pulse on completion/detach & also emits new position to update buffer. If I want to do a delay in servo C++ code, can I just use ::delay(time), or should I copy relevant code from delay node?
Please, don’t. Unfortunately, the Arduino’s delay is the curse of Arduino. Running it freezes everything. XOD is not an exception since it is based on Arduino runtime. In other words you hang all other nodes when call delay in yours.
Instead, use timeouts. They let to make a portion of a job, set a timeout to wait for some time to past, and then invoke again to complete the job. See the Dealing with Time in C++ for detail.