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

# SIR Class Reference

`#include <SIR.h>`

Inheritance diagram for SIR:

List of all members.

## Detailed Description

This class (Sensor and Image Recognition) provides high-level information to the behaviors by pre-processing the data internally. The provided information includes current obstacle distances as received by the infra-red sensors. But in particular information about the locations of cubes and robots that are visible on camera are provided. As all behaviors should use the same SIR thread this class is designed as a singleton class. So at any given time there is at most one instance of the class. The method SIR::getSIR() is used to access that instance.

Author:
  Jia L. Du.

## Public Member Functions
bool | obstacleLeft ()
bool | obstacleRight ()
bool | obstacleFront ()
double | getLeftMargin ()
double | getRightMargin ()
double | getFrontMargin ()
bool | robotDetected ()
bool | cubeDetected ()
void | getRobotPosition (double &x, double &y, double &phi)
void | getGlobalCubePosition (double &x, double &y)
void | getDensity (double &density, double &centerOfDensityX, double &centerOfDensityY)
void | setCubeSize (int size)
void | setCubeColor (int color)
bool | obstacle (void)
bool | spawn ()
void | getLocalCubePosition (double &x, double &y)
## Static Public Member Functions
SIR * | getSIR ()
## Protected Member Functions
| SIR (char *name, int stackSize, int priority, int id)
void | run (void)
## Static Protected Member Functions
void | staticRun (void)
## Private Member Functions
void | takePicture ()
void | takeColorPicture ()
bool | detectCubes ()
bool | detectRobots ()
bool | hasCubeColor (BYTE hue)
## Private Attributes
double | cubeDistanceX
double | cubeDistanceY
double | cubePositionX
double | cubePositionY
double | obstacleThresholdFront
bool | robotDetectedFlag
bool | cubeDetectedFlag
double | minimumCubeSize
int | cubeColor
int | allowedCubeColorDeviation
double | obstacleThresholdSides
image | currentImage
colimage | currentColorImage
Camera * | camera
ImageFilter | imageFilter
PSDs * | psds
Drive * | drive
## Static Private Attributes
SIR * | me
SIR | aSIR
const double | row2Meter [imagerows]
const double | yFactor [imagerows]
const double | yFactorMinus [imagerows]

## Constructor & Destructor Documentation

SIR::SIR | ( | char * | name,
|  | int | stackSize,
|  | int | priority,
|  | int | id
| ) | `[protected]`

  | Constructor for SIR. 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

bool SIR::cubeDetected | ( |  | ) |

| Indicates if a cube is currently visible on the camera Returns: true if a cube is on the camera

bool SIR::detectCubes | ( |  | ) | `[private]`

| Tries to detect cubes in the current color image See also: currentColorImage Returns: true if at least one cube was detected

bool SIR::detectRobots | ( |  | ) | `[private]`

| Tries to detect robots in the current image See also: currentImage Returns: true if at least one robot was detected

void SIR::getDensity | ( | double & | density,
|  | double & | centerOfDensityX,
|  | double & | centerOfDensityY
| ) |

  | Estimates the cube density in the image by counting the pixels that have the cube color Parameters:
  density | The value can range from 0.0 (no cubes) to 1.0 (all camera pixels have the cube color)
  centerOfDensityX | x coordinate of the estimated center of the cube cluster relative to the current robot position
  centerOfDensityY | y coordinate of the estimated center of the cube cluster relative to the current robot position

double SIR::getFrontMargin | ( |  | ) |

| Returns the distance from the robot to the next object in the front Returns: distance in meter

void SIR::getGlobalCubePosition | ( | double & | x,
|  | double & | y
| ) |

| Passes the last known position of the last detected cube in global coordinates (i.e. with (0,0) and not the current robot position as origin). If no cube has been detected yet (0,0) is passed. SIR::cubeDetected() should be used before calling this method to check if the data is up to date (i.e. the cube whose position is passed is indeed still visible). If more than one cube was detected on the image the position of the closest cube is passed

double SIR::getLeftMargin | ( |  | ) |

| Returns the distance from the robot to the next object to the left Returns: distance in meter

void SIR::getLocalCubePosition | ( | double & | x,
|  | double & | y
| ) |

| Passes the last known position of the last detected cube in local coordinates. If no cube has been detected yet (0,0) is passed. As the position is passed in local coordinates (that is the relative distance from cube to robot at the time when the image - on which the cube was detected - was taken) this information expires quickly. SIR::cubeDetected() should definitely be used before calling this method to check if the data is up to date (i.e. the cube whose position is passed is indeed still visible). If more than one cube was detected on the image the position of the closest cube is passed

double SIR::getRightMargin | ( |  | ) |

| Returns the distance from the robot to the next object to the right Returns: distance in meter

void SIR::getRobotPosition | ( | double & | x,
|  | double & | y,
|  | double & | phi
| ) |

| Passes the last known position of the last detected robot. If no robot has been detected yet (0,0,0) is passed. SIR::robotDetected() should be used before calling this method to check if the data is up to date (i.e. the robot whose position is passed is indeed still visible). If more than one robot was detected on the image the position of the closest robot is passed

SIR * SIR::getSIR | ( |  | ) | `[static]`

| This method is used to get the instance of this singleton class Returns: A pointer to the instance

bool SIR::hasCubeColor | ( | BYTE | hue | ) | `[private]`

| Checks if the passed hue is similar to the cube color See also: cubeColor allowedCubeColorDeviation Returns: true if similar

bool SIR::obstacle | ( | void |  | ) |

| Indicates whether an obstacle is detected by any of the infra-red sensors Returns: true if there is an obstacle

bool SIR::obstacleFront | ( |  | ) |

| Indicates whether an obstacle is detected by the front infra-red sensor Returns: true if there is an obstacle

bool SIR::obstacleLeft | ( |  | ) |

| Indicates whether an obstacle is detected by the left infra-red sensor Returns: true if there is an obstacle

bool SIR::obstacleRight | ( |  | ) |

| Indicates whether an obstacle is detected by the right infra-red sensor Returns: true if there is an obstacle

bool SIR::robotDetected | ( |  | ) |

| Indicates if a robot is currently visible on the camera Returns: true if a robot is on the camera

void SIR::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.

void SIR::setCubeColor | ( | int | color | ) |

| Not used yet

void SIR::setCubeSize | ( | int | size | ) |

| Not used yet

bool SIR::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 SIR::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

void SIR::takeColorPicture | ( |  | ) | `[private]`

| Gets a new color image from the camera and stores it in currentColorImage See also: currentColorImage

void SIR::takePicture | ( |  | ) | `[private]`

| Gets a new image from the camera and stores it in currentImage See also: currentImage

## Member Data Documentation

int SIR::allowedCubeColorDeviation`[private]`

| By how much the color of a pixel may deviate and still be accepted as cube color

SIR SIR::aSIR`[static, private]`

| The single instance that exists of this class See also: SIR::getSIR()

Camera* SIR::camera`[private]`

| The SIR has a Camera to get pictures

int SIR::cubeColor`[private]`

| Stores the color of the cubes to be pushed as hue

bool SIR::cubeDetectedFlag`[private]`

| Flag to indicate that a cube is visible on the camera. Used internally See also: SIR::cubeDetected()

double SIR::cubeDistanceX`[private]`

| Stores the x value of the last known position of the last detected cube in local coordinates (That is the x distance from cube to robot at the time when the image - on which the cube was detected - was taken)

double SIR::cubeDistanceY`[private]`

| Stores the y value of the last known position of the last detected cube in local coordinates (That is the y distance from cube to robot at the time when the image - on which the cube was detected - was taken)

double SIR::cubePositionX`[private]`

| Stores the x value of the last known position of the last detected cube (in global coordinates, i.e. with (0,0) and not the current robot position as origin)

double SIR::cubePositionY`[private]`

| Stores the y value of the last known position of the last detected cube (in global coordinates, i.e. with (0,0) and not the current robot position as origin)

colimage SIR::currentColorImage`[private]`

| The color image that is currently used for the detection of cubes (by SIR::detectCubes(), SIR::getDensity())

image SIR::currentImage`[private]`

| The grayscale image that is currently used for the detection of robots (SIR::detectRobots())

Drive* SIR::drive`[private]`

| The SIR needs the Drive to receive position information

ImageFilter SIR::imageFilter`[private]`

| The SIR has an ImageFilter to process the taken pictures

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

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

double SIR::minimumCubeSize`[private]`

| Stores the minimum size that an object must have to be accepted as a cube (in meter)

double SIR::obstacleThresholdFront`[private]`

| How far the distance to an object may drop before the object is marked as an obstacle by the front PSD sensor (in meter)

double SIR::obstacleThresholdSides`[private]`

| How far the distance to an object may drop before the object is marked as an obstacle by the left/right PSD sensor (in meter)

PSDs* SIR::psds`[private]`

| The SIR needs the PSDs to receive infra-red distance information

bool SIR::robotDetectedFlag`[private]`

| Flag to indicate that a robot is visible on the camera. Used internally See also: SIR::robotDetected()

const double SIR::row2Meter`[static, private]`

| Initial value:
      {1.000,
       1.0000, 1.0000, 1.0000, 1.0000, 1.0000,    1.0000, 0.9000, 0.9000, 0.9000, 0.8000,
       0.8000, 0.7500, 0.7000, 0.6500, 0.6000,    0.5500, 0.5250, 0.5000, 0.4830, 0.4670,
       0.3800, 0.3675, 0.3550, 0.3425, 0.3300,    0.3200, 0.3100, 0.3000, 0.2900, 0.2800,
       0.2720, 0.2640, 0.2560, 0.2480, 0.2400,    0.2360, 0.2280, 0.2220, 0.2160, 0.2100,
       0.2050, 0.2000, 0.1950, 0.1900, 0.1850,    0.1820, 0.1790, 0.1760, 0.1730, 0.1700,
       0.1660, 0.1620, 0.1580, 0.1540, 0.1500,    0.1475, 0.1450, 0.1425, 0.1400, 0.0900,
       0.0900}
Camera dependent lookup table. Factor to get x position in meters from x position in pixels. 62 values, one for each row. 0 and 61 not used by me. Example: something is in row 1. Distance from the robot = 0.460m = row2Meter[1]. Something is in row 60. Distance from the robot = 0.0023m = row2Meter[60]

const double SIR::yFactor`[static, private]`

| Initial value:
      {0.0200,
       0.0200, 0.0200, 0.0200, 0.0200, 0.0200,    0.0200, 0.0200, 0.0200, 0.0200, 0.0200,
       0.0180, 0.0140, 0.0105, 0.0095, 0.0090,    0.0085, 0.0080, 0.0073, 0.0069, 0.0067,
       0.0095, 0.0095, 0.0095, 0.0095, 0.0095,    0.0075, 0.0075, 0.0075, 0.0075, 0.0075,
       0.0055, 0.0039, 0.0039, 0.0037, 0.0036,    0.0036, 0.0035, 0.0034, 0.0034, 0.0033,
       0.0033, 0.0032, 0.0030, 0.0028, 0.0026,    0.0025, 0.0026, 0.0027, 0.0028, 0.0029,
       0.0030, 0.0029, 0.0028, 0.0026, 0.0023,    0.0022, 0.0020, 0.0018, 0.0016, 0.0015,
       0.0015}
Camera dependent lookup table. Factor to calculate y position from pixels to meters depending on x position (unit of yFactor is width(m)/width(pixels)). 62 values, one for each row. 0 and 61 not used by me. Example: cube width = 0.1m. Cube size in pixel in row 1 (top row, furthest away) = 0.1m/(0.0093m/pixel) = 11 pixel. Cube in pixel in row 60 (bottom row, closest) = 0.1m/(0.0017m/pixel) = 59 pixel. The other direction: something is 20 pixel from the image middle and in row 1. Distance from the robot (y component) = 20*(0.0093m/pixel) = 0.186m. Something is 20 pixel from the image middle and in row 60. Distance from the robot = 20*(0.0017m/pixel) = 0.034m. yFactor is the table for objects left of the robot. yFactorMinus is the table for objects left of the robot. We obtained better results using two tables.

const double SIR::yFactorMinus`[static, private]`

| Initial value:
      {0.0100,
       0.0100, 0.0100, 0.0100, 0.0100, 0.0100,    0.0100, 0.0100, 0.0100, 0.0100, 0.0100,
       0.0059, 0.0055, 0.0053, 0.0047, 0.0045,    0.0042, 0.0040, 0.0036, 0.0034, 0.0033,
       0.0015, 0.0015, 0.0014, 0.0014, 0.0013,    0.0013, 0.0013, 0.0013, 0.0013, 0.0013,
       0.0013, 0.0013, 0.0013, 0.0012, 0.0012,    0.0012, 0.0012, 0.0012, 0.0012, 0.0011,
       0.0011, 0.0011, 0.0011, 0.0011, 0.0011,    0.0011, 0.0010, 0.0011, 0.0010, 0.0010,
       0.0010, 0.0010, 0.0010, 0.0010, 0.0010,    0.0010, 0.0010, 0.0010, 0.0010, 0.0010,
       0.0010}
Camera dependent lookup table. Factor to calculate y position from pixels to meters depending on x position (unit of yFactor is width(m)/width(pixels)). 62 values, one for each row. 0 and 61 not used by me. Example: cube width = 0.1m. Cube size in pixel in row 1 (top row, furthest away) = 0.1m/(0.0093m/pixel) = 11 pixel. Cube in pixel in row 60 (bottom row, closest) = 0.1m/(0.0017m/pixel) = 59 pixel. The other direction: something is 20 pixel from the image middle and in row 1. Distance from the robot (y component) = 20*(0.0093m/pixel) = 0.186m. Something is 20 pixel from the image middle and in row 60. Distance from the robot = 20*(0.0017m/pixel) = 0.034m. yFactor is the table for objects left of the robot. yFactorMinus is the table for objects right of the robot. We obtained better results using two tables.