WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

Actuators

Adds support for servos and DC motors.
This library tries to standardise on how we deal with these devices so that we can swap between different devices with minimal code changes.
Drive Speed For Motors or Continuous Rotation Servos
The drive speed is an abstract value that represents how fast, and in what direction, we want a motor to turn.
A drive speed of 0, or DRIVE_SPEED_BRAKE, means 'stop and apply the brakes'.
DRIVE_SPEED_MIN means go full speed in reverse and DRIVE_SPEED_MAX means go full speed forward.
Intermediate values will also work. ie 'DRIVE_SPEED_MAX / 2' means go at half of full speed forward.
Obviously the actual speed at which the robot moves will depend on the abilities of the actual motor/servo as well as the wheel diameter.
Drive Speed For Servos
For a servo a drive speed of 0, or DRIVE_SPEED_CENTER, means move to the centre position.
DRIVE_SPEED_MIN means move fully in one direction and DRIVE_SPEED_MAX means move fully in the other direction.
Connection Status
Actuators can also be 'disconnected' ie they are no longer sent any commands until they are 'reconnected'. Disconnecting will mean that the motors will free wheel to a stop. If the robot is on a slope then it may well start accelerating down the slope. NB This is different from setting the drive speed to 0 (or using the constant DRIVE_SPEED_BRAKE) which will cause the motor to brake - which will then stop the robot from rolling down the slope. Obviously if the motors are unable to hold the robot then it may skid down the slope.
Disconnecting a servo means that it will no longer be sent a signal and so all torque will be lost. Disconnecting the leg servos on your biped will mean that it will just crumple to the floor under its own weight.
Inverted
We also try to address the common problem of differential drive robots. Since the motors are on the sides of the robot then setting them both to turn clockwise at full speed will just cause the robot to spin on the spot. You need to make one turn clockwise and the other turn counter clockwise in order to go in a straight line. This often makes the code rather less obvious. We address this issue by having an 'inverted' state for each motor. That way we can set one motor as inverted and we can then just tell the motors to go full speed ahead and the library takes care of the rest.
Motor and Servo controller boards
Motors, due to their current requirements, are normally controlled via a separate board whereas servos can be connected directly to your controller pins or they may also be connected via a separate board.
These external boards may be connected via I2C, UARTs etc - each with their own different protocol. By using WebbotLib you don't need to know the details of how these boards work. You just call one function to set the required speed of the actuator and WebbotLib takes care of the rest by sending whatever messages it needs to send to get the job done.
Example
Since it doesn't make any difference to your own project code quite how your actuators are being controlled then its easy to come up with a generic example that will always work!
Assuming that you have created two motors/servos in Project Designer called 'left' and 'right' respectively then the following code should make them repeatedly go forward and backward. Project Designer will generate this as part of its examples.
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
 
    // Get current time in ms
    TICK_COUNT ms = loopStart / 1000;
    int16_t now = ms % (TICK_COUNT)10000; // 10 sec for a full swing
    if(now >= (int16_t)5000){ // Goes from 0ms up to 5000ms
        now = (int16_t)10000 - now; // then 5000ms down to 0ms
    }
 
    // Map the 0 - 5000 value into DRIVE_SPEED range
    DRIVE_SPEED speed = interpolate(now, 0, 5000, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
 
    // Set speed for all motors/servos
    left.setSpeed(speed);
    right.setSpeed(speed);
    return 0;
}

 

Project Designer supports the following devices

 

Valid XHTML 1.0 Transitional