Take in a command while doing something else

N

Nick W

Hi all,

I will be using a C++ program to control a robot, and would like to
"accept" keyboard-entered commands (strings) while the program is
running and controlling the robot as a part of the control cycle. What
is the best way of doing this please?

Also, for the bulk of the time I will be running one particular block
of code each cycle, which will vary depending on what the robot is
doing. Is it best to use a strategy pattern for this, and pass the
robot object to the currently switched strategy object, eg
this.strategy.execute(this), or simply have different functions within
the robot object and vary which run is run.

Thanks,

Nick
 
N

Nick W

Brilliant guys, thanks for your replies.

The finite state machine was exactly what I was looking for.

'cin' looks to be the way forward, as it will work on my mac, the
department's linux boxes and on the arm microprocessor via wi-fi.

I will look into multi-threading but am not sure how well it will port
to the ARM microprocessor. Thanks for the tips.

Cheers,

Nick
 
N

Noah Roberts

Hi all,

I will be using a C++ program to control a robot, and would like to
"accept" keyboard-entered commands (strings) while the program is
running and controlling the robot as a part of the control cycle. What
is the best way of doing this please?

Best way is OS dependant polling I believe, but in some implementations
"std::cin.rdbuf()->in_avail()" may work.
Also, for the bulk of the time I will be running one particular block
of code each cycle, which will vary depending on what the robot is
doing. Is it best to use a strategy pattern for this, and pass the
robot object to the currently switched strategy object, eg
this.strategy.execute(this), or simply have different functions within
the robot object and vary which run is run.

You almost certainly want the State pattern as the central core of this.
I'm sure there's more patterns to be found in your particular problem
but your description screams State.
 
M

Michael Doubez

Best way is OS dependant polling I believe, but in some implementations
"std::cin.rdbuf()->in_avail()" may work.
[snip]

Does it ? in_avail() reports characters available before a read is
needed, not the data available in the tty.
 
J

James Kanze

@c3g2000yqd.googlegroups.com>, (e-mail address removed) says...
Best way is OS dependant polling I believe, but in some
implementations "std::cin.rdbuf()->in_avail()" may work.

Does it ? in_avail() reports characters available before a
read is needed, not the data available in the tty.

I think it depends. This is from memory, and since in_avail is
a function I don't use much, I could easily be wrong, but I
think that if there are characters available in the local
buffer, it returns the number of those characters, but if there
are not, it calls a virtual function, whose behavior is largely
implementation dependent. Returning 0 is always legal, but if
the implementation can somehow determine that more characters
can be read without blocking (e.g. by calling stat on a Unix
system), then it can (but is not required to) return that value.

Of course, this also leaves it up to the OS somewhat. I'm not
sure off hand what the st_size field of stat returns for a
keyboard (or any character special field) under Unix, for
example, but I don't think it's anything useful. On the other
hand, there are usually implementation specific functions which
directly support this sort of thing: you can put a terminal in
raw mode and use non-blocking IO under Unix, and at least under
MS-DOS and the older versions of Windows, there was a simple
function which returned a character from the keyboard, or -1 (or
0, I forget) if none was present, without waiting. That's
probably more like what the original needs, but it is very OS
specific. (Alternatively, curses has support for this, and is
available for a number of different OS's. But if all he wants
to do is check the keyboard, it is definitely overkill.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top