RoboCatz.com

Our Function Library (Updated Spring 2016)


A function library is a set of functions, variables, constants, and algorithms that are included with the basic RobotC programs. The functions and variables in the library can then be referenced and called upon as needed in your program.

The purpose of keeping these functions and variables in one file is to make your programs more modular. Many programs can refer to and use the same function library. Also, if these functions and variables are stored in a file separate from your program, then you do not have to include all of the code below in order to access it. You can simply use one line of code to tell the compiler to include the contents of the entire library into your program. The compilter will do that at the time your program is compiled.

Structure

Most function libraries have a structure to help make them easier to understand by programmers. The format of this structure is one where the constants and variables are declared (for the most part) at the beginning of the file. This is also somewhat necessary since a constant or variable cannot be used by any part of the program before it has been defined (declared).

Declaration of Constants and Variables

The statements below show the definitions of various constants and variables. Most are assigned a value in their declaration.

//////////////////////////////////////////////////////////////////////
//
// Define constants and variables to be used in the functions below
//

typedef struct genericRobotStruct {
  int powerLevel;
  int wheelDiameterMM;
  int gearRatio;
  int distanceBetweenWheelsMM;
  bool criteriaNotMetYet;
} robotObj; robotObj robot;

#define PI 3.14159
// There are 4 characteristics of the robot which are used in various move functions.
// Each of the size characteristics are entered in milimeters (MM).
                                           // Be careful with tankbots.  This distance is to the sprockets, not the center of the tread.
// wheelDiameterMM = 43.2;              // Self explanatory. Just look at the tire to see the diameter in MM.  Use a default of 43.2--that's the typical tire we use

Primitives

Certain functions may perform very basic (primitive) operations and are often referred to in other programs/functions that follow. For this reason, primitive functions usually follow the first declaration of constants/variables.

//////////////////////////////////////////////////////////////////////
//
// Define Primitive functions that may be referenced elsewhere
//

#define setPowerLevel(pPowerLevel) robot.powerLevel=pPowerLevel
void stopOnLine() {
  while(SensorValue[colorSensor1]>20) {
  }
  stopAllMotors();
}

Moving Straight Functions Defined

void driveForward() {
  setMultipleMotors(robot.powerLevel, B, C);
}
void driveBackward() {
  setMultipleMotors(robot.powerLevel * -1, B, C);
}
void driveForward(int distance) {
  driveForward();
  untilDistance(distance);
  stopAllMotors();
}
void driveBackward(int distance) {
  driveBackward();
  untilDistance(distance);
  stopAllMotors();
}

Turning Functions Defined

//////////////////////////////////////////////////////////////////////
// Just a simple swing-turn
void turnRight2(int degrees){
  resetMotorEncoder(C);
  long startingEncoderValue = getMotorEncoder(C);
  motor[motorC]=20;   // Rotate the wheel forwards
  while(abs(getMotorEncoder(C)-startingEncoderValue) < degrees*3) {
  }
  stopAllMotors();
}

void turnLeft(int degrees){
  resetMotorEncoder(B);
  long startingEncoderValue = getMotorEncoder(B);
  motor[motorB]=20;   // Rotate the wheel forwards
  while(abs(getMotorEncoder(B)-startingEncoderValue) < degrees*3) {
  }
  stopAllMotors();
}

"Until" Functions

////////////////////////////////////////////////////////////////////////
//
void untilDistance(int moveIN) {
  resetMotorEncoder(B);
  resetMotorEncoder(C);
  float targetDegree;
  targetDegree=(moveIN * 25.4)/(robot.wheelDiameterMM*PI)*360;
  repeatUntil(abs(getMotorEncoder(B)) > targetDegree) { sleep(5);	}
}
void untilTouch() {
  while(SensorValue(touchSensor) == 0) {   // Loop until the button is pressed
    sleep(5);
  }
}

Follow Line Task

//////////////////////////////////////////////////////////////////////
//
task FollowLine_Edge () {

  int diff;

  while(true) {
    displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]);

    int k=20;
    int defaultPower=20;
    diff = SensorValue[colorSensor1] - k;
    displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]);
    displayCenteredBigTextLine(4, "diff %d", diff);
    diff/=3;
    motor[B]=defaultPower-diff;
    motor[C]=defaultPower+diff;
  }
}

//////////////////
// Follow Center of the line
//     Light sensor on each edge
task FollowLine () {
  int diff;
  while(true) {
    displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]);
    int defaultPower=20;
    diff = SensorValue[colorSensor2] - SensorValue[colorSensor1];
    displayCenteredBigTextLine(1, "Sensor %d", SensorValue[colorSensor1]);
    displayCenteredBigTextLine(4, "diff %d", diff);
    diff/=6;
    motor[B]=defaultPower-diff;
    motor[C]=defaultPower+diff;
  }
}

Detecting Lines and Colors

//////////////////////////////////////////////////////////////////////
//
void stopOnLine() {
  while(SensorValue[colorSensor1]>20) {
    sleep(5);
  }
  stopAllMotors();
}

Initializing the Speech Engine

/************************************************************************************/
#include "SpeechEngine.c";

/************************************************************************************/

Task Main - the Willy Wonka Program

The purpose of this program is to leave the base, find the line, follow it, turn and face the four balls, then crash into them and return to base.

task main()
{
  robot.wheelDiameterMM = 96;
  robot.powerLevel=30;

  driveForward(1);
  turnRight2(45);
  driveForward(8);
  driveForward();
  stopOnLine();
  driveForward(2);
  turnLeft(45);
  wait(1);
  startTask(FollowLine); // Start the line following
  untilDistance(26);
  stopTask(FollowLine);// Stop following the line
  stopAllMotors();
  turnRight2(90);      // Face the wall of balls
  robot.powerLevel=100;// Ready... Aim...
  driveForward(8);     // Crash
  wait(1);
  robot.powerLevel=20; // Slow down for this move
  driveBackward(5);    // Backup in case you are stuckon one of the trees
  wait(1);
  turnRight2(125);     // Face a south by south east
  robot.powerLevel=20; // Slow down for this move
  driveForward();
  stopOnLine();        // Drive up to the center line
  driveForward(13);    // Cross over the center line
  turnLeft(30);        // Turn toward the base
  robot.powerLevel=20; // Ok to move at about 40% power
  driveForward(10);    // Drive forward
  turnRight2(20);      // When closer to base, turn more towards the corner
  robot.powerLevel=10; // drive even slower
  driveForward();
  untilTouch();        // Wait for touch sensor
  stopAllMotors();     // Stop now.

}

Task Main - the Final Program

task main()
{
  robot.wheelDiameterMM = 96;
  robot.powerLevel=15;

  untilTouch();

  motor[motorA]=10;   // Reset the position of the arms
  motor[motorD]=10;
  wait(1);
  motor[motorA]=0;
  motor[motorD]=0;    // Arm is steep at this point
  setMotorSyncEncoder(A, D2, 0, 30, -15);  // Move the arm 30 degrees at 15% downward power

  wait(2);
  untilTouch();

  driveForward(1);        // Move away from the wall
  turnRight2(45);         // Turn toward the line
  driveForward(8);        // Drive out of base
  driveForward();         // Keep driving
  stopOnLine();           // Stop when you see the line
  driveForward(2);        // Move forward a little
  turnLeft(45);           // Face West
  startTask(FollowLine);  // Startfollowing the line
  while(SensorValue[ultrasonicSensor] > 20) { sleep(5); }
  stopTask(FollowLine);   // Close enough.  Now stop.
  stopAllMotors();        // Really STOP.

  wait(2);
  driveForward(5);        // Position the robot
  wait(2);
  setMotorSyncEncoder(A, D2, 0, 40, -15);  // Move the arm 40 degrees at 15% downward power
  wait(1);
  driveBackward(6);
  turnRight2(180);        // Do a U-Turn here
  driveForward(8);        // Get away from the models
  turnRight2(45);         // Try to get back on the line
  driveForward();
  stopOnLine();           // Stop on the center line
  driveForward(2);        // Cross over the center line
  turnLeft(40);           // Face EAST

  startTask(FollowLine);  // Startfollowing the line
  untilDistance(36);
  stopTask(FollowLine);   // Close enough.  Now stop.
  stopAllMotors();

  turnRight2(45);         // Turn toward Base
  driveForward();         // Drive toward base
  untilTouch();           // Until the touch sensor
  stopAllMotors();        // Now STOP moving.

}