Page 207 - The Unofficial Guide to Lego Mindstorms Robots
P. 207
196
Controlling Outputs (direct-motor.h)
Controlling the outputs with legOS is very easy. All you need to do is give the output a direction and a speed.
void motor_a_dir(MotorDirection dir)
void motor_b_dir(MotorDirection dir)
void motor_c_dir(MotorDirection dir)
These functions s et the direction of the RCX's outputs. The dir value can be fwd , rev , brake , and off . The off
mode is the same as Float() in NQC.∗
void motor_a_speed(unsign ed char speed)
void motor_b_speed(unsign ed char speed)
vo id motor_c_speed(unsigned char speed)
These functions set the speed for the outputs. Of course, the speed only really matters when the output direction is fwd
or rev. Values range from 0 to 255. You can use the handy constants MIN_SPE ED and MAX_SPEED if you wish.
To set outputs A and C running forward at top speed, do this:
motor_a_dir(fwd);
mot or_c_dir(fwd);
mot or_a_speed(MAX_SPEED);
motor_c_speed(MAX_SPEED);
W orking with Inputs (d irect-sensor.h)
In legOS, support for inputs is rudimentary. Remember all t hat nice input value processing in NQC and pbFORTH? In legOS,
you have to deal with the raw input values, except for light and rotation sensors. The following macros return the raw value of
u
the inp ts:
SENSOR_1
SENSOR _2
SENSOR_3
BATTERY
Use these macros to retrieve the raw value of the corresponding input. BATTERY returns an indication of the battery
level. The raw values are in the range from 0x0000 to 0xffff.
Don't expect sens ors to use the full range of raw input values. You might think a touch sensor would produce a value of
0x0000 when it's pressed and 0xffff when it's not pressed, but the actual values are not as extreme. The rule of thumb for
testing touch sensors is to test if the value falls below 0xF000, like this:
∗ If you're accustomed to working with NQC, don't get confused here: off in legOS is the same as Float() in NQC, while
brake in legOS is the same as Off() in NQC.