Main Page Class Hierarchy Compound List File List Compound Members File Members

# Commander Class Reference

`#include <Commander.h>`

Inheritance diagram for Commander:

List of all members.

## Detailed Description

Activates and switches between the behaviors of the robot (current implementation: Exploring, Pushing, Avoiding, Communicating. Communicating only when using local communication - see system model). The decision which behavior to activate is based on the current state of the commander and the return code it receives from the last activated behavior. Once a behavior has served its purpose it will return with a result code. The commander does not actively interrupt activated behaviors.

Author:
  Jia L. Du

## Public Member Functions
| Commander (char *name, int stackSize, int priority, int id)
void | doSleep (int hundredthSeconds)
void | doReschedule (void)
bool | spawn ()
## Protected Member Functions
void | run (void)
## Static Protected Member Functions
void | staticRun (void)
## Private Attributes
int | state
Explorer | explorer
Pusher | pusher
Avoider | avoider
Communicator | communicator
## Static Private Attributes
Commander * | me
const int | EXPLORING = 0
const int | PUSHING = 1
const int | AVOIDING = 2
const int | COMMUNICATING = 3

## Constructor & Destructor Documentation

Commander::Commander | ( | char * | name,
|  | int | stackSize,
|  | int | priority,
|  | int | id
| ) |

  | Constructor for Commander. The parameters are passed to the Thread constructor Parameters:
  name | pointer to thread name
  stackSize | size of thread stack
  priority | thread priority (valid values are: MIN_ PRI to MAX_PRI)
  id | a unique thread id

## Member Function Documentation

void Commander::doReschedule | ( | void |  | ) |

| Tells the commander that the current behavior has nothing to do right now and would like to cede control to another thread

void Commander::doSleep | ( | int | hundredthSeconds | ) |

  | Tells the commander that the calling behavior would like to sleep for at least n hundredth seconds Parameters:
  hundredthSeconds |

void Commander::run | ( | void |  | ) | `[protected]`

| The method is overwritten by the inheritor. When the thread starts the code in this run method is executed Reimplemented from Thread.

bool Commander::spawn | ( |  | ) |

| According to C++ convention, methods cannot be static and virtual at once. In the initial design run was declared as virtual which results in dynamic binding. That means the program determines at run-time which run method to use, the one of the Thread base class or the one of the inheritor. Because we have to use staticRun, we cannot use the virtual keyword anymore. Without the virtual keyword the compiler determines at compile-time which run method is used, always the run method of the Thread base class. To ensure that the inheritor indeed uses its own run method, it is necessary to overwrite the thread initialization method (spawn) as well. See also: staticRun Returns: true if successful Reimplemented from Thread.

void Commander::staticRun | ( | void |  | ) | `[static, protected]`

| Static wrapper method for run. It does nothing but to call run. That is necessary because the RoBIOS multi-tasking functions expect a pointer to a C-function (not a pointer to a C++ method) containing the code to be executed. Fortunately, pointer-to-static-C++-methods are compatible with regular pointer-to-C-functions. But the problem is that static methods can only use other static methods and variables. The reasons is that a static method, which is shared by all instances, would not know which non-static method or variable to use if there were multiple instances of the same class. Thus, the static property would have to be applied to all used (sub-)methods and (sub-) variables, and propagate through the whole class structure. By using staticRun, we can keep using our non-static run. If static methods cannot call non-static ones, how can staticRun call run? As aforementioned the reason why static method cannot use non-static members is because it is not clear which non-static member to use if there are multiple instances of the same class. That means staticRun needs a pointer to the instance whose run method is to be called. This pointer is stored in a static member variable called me. See also: me

## Member Data Documentation

Avoider Commander::avoider`[private]`

| The Commander switches to the Avoider behavior when necessary

const int Commander::AVOIDING = 2`[static, private]`

| Used by the class to keep track of its own state

const int Commander::COMMUNICATING = 3`[static, private]`

| Used by the class to keep track of its own state

Communicator Commander::communicator`[private]`

| The Commander switches to the Communicator behavior when necessary

Explorer Commander::explorer`[private]`

| The Commander switches to the Explorer behavior when necessary

const int Commander::EXPLORING = 0`[static, private]`

| Used by the class to keep track of its own state

Commander * Commander::me`[static, private]`

| Stores a pointer to an instance of Commander See also: staticRun

Pusher Commander::pusher`[private]`

| The Commander switches to the Pusher behavior when necessary

const int Commander::PUSHING = 1`[static, private]`

| Used by the class to keep track of its own state

int Commander::state`[private]`

| Used by the class to keep track of its current state. Possible states are Commander::EXPLORING, Commander::PUSHING, Commander::AVOIDING and Commander::COMMUNICATING (Note: Commander::COMMUNICATING only when local communication is used. See system model)