LeJOS OSEK C++ Library v0.4
API
Robert W. Kramer,
Disclaimer
The Lejos
OSEK C++ library and this document are © 2008 by Robert W. Kramer, and are made
available subject to the Mozilla Public License,
version 1.0. You may obtain a copy of the license at http://www.mozilla.org/MPL/.
Discussion
The LeJOS
OSEK C++ library provides a set of classes which provide abstractions for the
motors and sensors used with the Lego Mindstorms NXT,
as well as modifications to the LeJOS OSEK software
to allow C++ programs to compile and run properly.
The current version of the
C++ functionality (v0.4) was intended to accomplish the following:
This is the first version
of the library and support code. As such, it’s likely to have bugs. Caveat emptor, your mileage may vary,
and all of the standard disclaimers and clichés apply.
If you encounter a bug,
please send email describing the bug to the author at snowyowl000 -at- gmail -dot- com.
Known issues:
Credits
The author would like to
thank the following for helping to make this project possible:
The API
Available classes
Class |
Header |
AccelSensor |
AccelSensor.h |
ColorSensor |
ColorSensor.h |
CompassSensor |
CompassSensor.h |
LightSensor |
LightSensor.h |
SonarSensor |
SonarSensor.h |
SoundSensor |
SoundSensor.h |
TouchSensor |
TouchSensor.h |
PrototypeSensor |
PrototypeSensor.h |
Motor |
Motor.h |
Available enumerations
Symbol |
Value |
Description |
S1 |
0 |
Sensor port 1 |
S2 |
1 |
Sensor port 2 |
S3 |
2 |
Sensor port 3 |
S4 |
3 |
Sensor port 4 |
MOTOR_A |
0 |
Motor port A |
MOTOR_B |
1 |
Motor port B |
MOTOR_C |
2 |
Motor port C |
Notes:
AccelSensor methods
Method prototype |
Description |
AccelSensor(unsigned char _port) |
Constructor. |
~AccelSensor(
) |
Destructor. |
void activate(void) |
Turn on the sensor.
Supplies power to the sensor. |
void passivate(void) |
Turn off the sensor.
Removes power from the sensor. |
short int getXAccel(void) |
Return acceleration
along the x-axis. |
short int getYAccel(void) |
Return acceleration
along the y-axis. |
short int getZAccel(void) |
Return acceleration
along the z-axis. |
void getAccel(short
int axes[3]) |
Return acceleration
along all three axes. |
void getAccel( short int &x, short int &z) |
Return acceleration
along all three axes. |
bool isActivated(void) |
Returns true iff sensor has power. |
Notes:
ColorSensor methods
Method prototype |
Description |
ColorSensor(unsigned char _port) |
Constructor. |
~ColorSensor(
) |
Destructor. |
void activate(void) |
Turns on the sensor. |
void passivate(void) |
Turns off the sensor. |
bool isActivated(void) |
Returns true iff sensor has power. |
unsigned char getColorNumber(void) |
Returns color number. |
unsigned getColorIndex(void) |
Returns 6-bit value,
three two-bit R/G/B levels. |
void getColor(unsigned
short rgb[3]) |
Get color data. |
void getColor( short int &r, short int &g, short int &b) |
Get color data. |
short int getRed(void) |
Get red level. |
short int getGreen(void) |
Get green level. |
short int getBlue(void) |
Get blue level. |
void getRawColor(unsigned
short rgb[3]) |
Get raw color data. |
void
getRawColor( short
int &r, short int &g, short int &b) |
Get raw color data. |
short int getRawRed(void) |
Get raw red level. |
short int getRawGreen(void) |
Get raw green level. |
short int getRawBlue(void) |
Get raw blue level. |
void getNormalizedColor(unsigned
short rgb[3]) |
Get normalized color
data. |
void
getNormalizedColor(short int
&r, short
int &g, short
int &b) |
Get normalized color
data. |
short int getNormalizedRed(void) |
Get normalized red
level. |
short int getNormalizedGreen(void) |
Get normalized green
level. |
short int getNormalizedBlue(void) |
Get normalized blue
level. |
Notes:
CompassSensor methods
Method prototype |
Description |
CompassSensor(unsigned char _port) |
Constructor. |
~CompassSensor(
) |
Destructor. |
void activate(void) |
Turn on sensor. |
void passivate(void) |
Turn off sensor. |
bool isActivated(void) |
Returns true iff sensor has power. |
short int getHeading(void) |
Returns direction in
degrees. |
void beginCalibration(void) |
Puts sensor in
calibration mode. |
bool endCalibration(void) |
Puts sensor in normal
mode. Returns true iff calibration was successful. |
Notes:
LightSensor methods
Method prototype |
Description |
LightSensor( unsigned char _port,bool _lamp=true) |
Constructor. Turns on
lamp by default. |
~LightSensor(
) |
Destructor. |
void setLamp(bool _lamp) |
Turns
on sensor lamp iff _lamp is true. |
bool isLit(void) |
Returns true iff sensor has power. |
short int getValue(void) |
Returns light level. |
short int getRawValue(void) |
Returns raw light level. |
Notes:
SonarSensor methods
Method prototype |
Description |
SonarSensor(unsigned char _port) |
Constructor. |
~SonarSensor(
) |
Destructor. |
void activate(void) |
Turn on the sensor. |
void passivate(void) |
Turn off the sensor. |
bool isActivated(void) |
Returns true iff sensor has power. |
short int getDistance(void) |
Returns distance to
closest object in sensor’s field of view. |
Notes:
SoundSensor methods
Method prototype |
Description |
SoundSensor(unsigned char _port,bool
_dba=false) |
Constructor. Turns off dBA mode by default. |
~SoundSensor(
) |
Destructor. |
bool isDBA(void) |
Returns true iff sensor is in dBA mode. |
short int getValue(void) |
Returns sound level. |
short int getRawValue(void) |
Return raw sound level. |
Notes:
TouchSensor methods
Method prototype |
Description |
TouchSensor(unsigned char _port) |
Constructor. |
~TouchSensor(
) |
Destructor. |
bool isPressed(void) |
Returns true iff sensor is pressed. |
short int getRawValue(void) |
Returns raw value. |
Notes:
PrototypeSensor methods
Method prototype |
Description |
PrototypeSensor( unsigned
char _port, |
Constructor. |
~PrototypeSensor(
) |
Destructor. |
void setSampleRate(unsigned
int rate) |
Set sampling rate (4 ≤
rate ≤ 100). |
void setDigitalPorts(unsigned
char dir) |
Set direction of digital
ports. |
void writeDigitalData(unsigned
char data) |
Write bits to any
digital port configured for output. |
unsigned char readDigitalData(void) |
Read bits from any
digital port configured for input. |
void readAnalogData(unsigned
int analogPorts[5]) |
Read all ADC ports. |
unsigned int readAnalogData(int analogPort) |
Read given ADC port. |
Notes:
Motor methods
Method prototype |
Description |
Motor(unsigned char
_port) |
Constructor. |
~Motor( ) |
Destructor. |
void forward(void) |
Starts motor running
forward. |
void backward(void) |
Starts motor running
backward. |
void reverseDirection(void) |
Reverses current motor
direction. |
void stop(void) |
Stops motor with brake. |
void flt(void) |
Stops motor without
brake. |
int getTachoCount(void) |
Returns tachometer
value. |
void resetTachoCount(void) |
Resets tachometer value
to 0. |
void setPower(int _power) |
Sets motor power. |
Notes:
Other functions
Prototype |
Description |
void sleep(unsigned int duration) |
Task will sleep for duration milliseconds. Task blocks,
allowing other tasks to execute. |
Notes: