I'm new to Arduino programming, having previously only learned Basic on a micro when I was small.
I am at the early planning stage of designing a robot which I want to be able to exhibit different "behaviours".
Normally I would use If/Else to make some sort of decision tree but it seems like that would be very long winded in this case.
Is there some way to have a lookup table of functions?
So rather than something like:
"If X is true check Y else check Z.
If Y is true do A else do B
If Z is true do C else do A"
is there a shorter or cleverer way of doing things?
BernardMarx:
"If X is true check Y else check Z.
If Y is true do A else do B
If Z is true do C else do A"
is there a shorter or cleverer way of doing things?
In the specific scenario you describe, I can't think of any approach that would be shorter or simpler than your 'straw man' implementation. In C++ that would translate to:
Obviously this is not your real logic - if you told us what you actually want to do, maybe we can suggest alternative approaches that would work better for that particular problem.
David Anderson, of Dallas Robotics, has created some very successful autonomous robots and has written quite a bit about how to program robot behavior. One place to start is here: DPRG: Article - Subsumption for the SR04 and jBot Robots
Thanks guys, all I wanted at this stage was hand waving and a point in the right direction for a bit more reading.
I was worried about the imagined ballooning size of the code, so using functions to separate out the complexity then calling them as described would help a lot.
PeterH, thanks for writing out the code, I understand how to write this sort of code I was just being a bit lazy. You're right that its a strawman, the real version will decide between 5-10 behaviours based on sensory input.
As I try to visualise the problem, the idea that some sort of table would save time and make reconfiguring things later easier comes up, but I have no idea what that might even be called in this language, hence I have no idea how to word my questions.
BernardMarx:
As I try to visualise the problem, the idea that some sort of table would save time and make reconfiguring things later easier comes up, but I have no idea what that might even be called in this language, hence I have no idea how to word my questions.
There are lots of different was to handle this but short of writing a tutorial about control algorithms and data structures from first principles it's hard to give you specific advice without know what algorithm you're trying to implement. The only simple and general advice that might be relevant is that complex algorithms can often be reduced to multiple very simple algorithms if you can separate the problem into constituent parts.
Thanks for the input, I've been reading The Nature of code for pointers along similar lines. I appreciate you want to help give a specific solution but I think this has helped enough for now, got some reading to do.
All were published by the now-defunct publisher "TAB Books" - but can still be found on the used market. While the systems they depict are closer to a form of artificial intelligence rather than pre-programmed behaviors, supposedly the behaviors of the machines (and simulations) that were depicted were supposedly rather organic and somewhat purpose-driven; that is, the systems seems to exhibit true intelligence, and not pre-programmed behavior.
It should also be noted that these books describe systems involving machine and assembly language, along with BASIC programming; in other words, if you decide to investigate them, realize that you are looking at some old stuff, and take away the ideas, rather than then implementations (unless you like a challenge of converting BASIC and Assembler code to C/C++).
One of Heiserman's creations (that of "Rodney", described in the second book) actually became a commercial product - the RB5X: