Smr read

From Rsewiki
int smr_read(struct smr *robot);

Smr_read communcates with the smr daemon [smrd], updating fields in the [struct smr] pointed to by robot with values from the next sample period.


Smr_read uses some of the fields in *robot as input parameters, others for return values.

Ordinarily, zero is returned. If case of error, for example if communication is not possible, a non-zero value is returned.


Input parameters

The following fields are read by smr_read:

  robot->wait_flags:   An ORed combination of sensor flags, indicating which data smr_read should wait for.
  robot->recv_hook:   A pointer to an asynchronous message receive function. If not NULL, smr_read calls this function for each message received from smrd. Such a function can be used to process messages which would otherwise be ignored.

Return values

The following fields can be written by smr_read:

  robot->read_flags:   An ORed combination of sensor flags, indicating which data were received.
  robot->left.encoder, robot->right.encoder, robot->left.pwm, robot->right.pwm, robot->left.status, robot->right.status, robot->ls, robot->ir, robot->ad, robot->status:   Data read from server.
  robot->ticks:   Sample number read from server.
  robot->ts:   Time stamp read from server.

The valid flags are:

  SMR_FLAG_LE:   Left encoder
  SMR_FLAG_RE:   Right encoder
  SMR_FLAG_IR:   Proximity sensors
  SMR_FLAG_LS:   Line sensor
  SMR_FLAG_LV:   Left speed
  SMR_FLAG_PW:   Power module data
  SMR_FLAG_LPS:   Left motor pwm and status
  SMR_FLAG_RPS:   Right motor pwm and status


The operation of smr_read is as follows:

  1. Receive messages until 'start of sample'.
  1. Copy timing fields from 'start of sample' to *robot.
  1. Receive messages until 'end of sample', or until all data has been read as indicated in wait_flags. Copy sensor data to *robot and update robot->read_flags for each message.
  1. Done.

Not all data indicated in wait_flags is necessarily received: smr_read waits at most one sample. Test read_flags to see which data was received: if a flag is not set in read_flags the corresponding data will be old.

Data other than those indicated in wait_flags may be received: read_flags reflects whatever data was read.


Reading asychronous messages

Smr_read calls the user specified function robot->recv_hook, if specified, for each message received.

Such a function should have the type:

void recv_hook_function(struct smr *);