RoboCatz.com

Our Function Library (Updated Spring 2017)


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() {
  setMotorSync(B, C, 0, robot.powerLevel);
}
void driveForwardOnWall() {
  setMotorSync(C, B, 15, robot.powerLevel);
  resetMotorEncoder(B); resetMotorEncoder(C);
}
void driveBackward() {
  setMotorSync(B, C, 0, robot.powerLevel * -1);
}
void driveBackwardOnWall() {
  setMotorSync(C, B, 5, robot.powerLevel * -1);
  resetMotorEncoder(B); resetMotorEncoder(C);
}

Turning Functions Defined

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

void turnLeft(int degrees){
  resetMotorEncoder(B);
  long startingEncoderValue = getMotorEncoder(B);
  motor[motorB]=20;   // Rotate the wheel forwards
  while(abs(getMotorEncoder(B)-startingEncoderValue) < degrees*6) {
  }
  stopAllMotors();
}
void gradualTurnClockwise(float degrees, int turnRadiusIN) {
  if(!OkToMove) return;
  degrees -= 16 / (360 / degrees);
  float innerTurnRadius = ((turnRadiusIN * 25.4) - (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360);
  float outerTurnRadius = ((turnRadiusIN * 25.4) + (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360);
  float turnRatio = 50 - (innerTurnRadius/outerTurnRadius * 50);
  setMotorSync(C, B,-1 * turnRatio,20);
  resetMotorEncoder(B); resetMotorEncoder(C);
  repeatUntil(getMotorEncoder(C)>(outerTurnRadius/(robot.wheelDiameterMM*PI))*360) { sleep(5);	}
  setMotorSpeeds(0,0);
  stopAllMotors();
}
void gradualTurnCounterClockwise(float degrees, int turnRadiusIN) {
  if(!OkToMove) return;
  degrees -= 16 / (360 / degrees);
  float innerTurnRadius = ((turnRadiusIN * 25.4) - (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360);
  float outerTurnRadius = ((turnRadiusIN * 25.4) + (robot.distanceBetweenWheelsMM/2)) * 2 * PI * (degrees/360);
  float turnRatio = 50 - (innerTurnRadius/outerTurnRadius * 50);
  setMotorSync(B, C, turnRatio,20);
  resetMotorEncoder(B); resetMotorEncoder(C);
  repeatUntil(getMotorEncoder(B)>(outerTurnRadius/(robot.wheelDiameterMM*PI))*360) { sleep(5);	}
  setMotorSpeeds(0,0);
  stopAllMotors();
}
void gradualTurnClockwise(float degrees) { gradualTurnClockwise(degrees, 20); }
void gradualTurnCounterClockwise(float degrees) { gradualTurnCounterClockwise(degrees, 20); }

"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);
  }
}
void untilSonarLessThan(float distance_in_inches) {
  float distance_in_cm; distance_in_cm = distance_in_inches * 2.54;
  while(SensorValue((short) ultrasonicSensor) > distance_in_cm) {
  	displayCenteredBigTextLine(1, "Sensor %d", SensorValue[ultrasonicSensor]);
   sleep(5);
  }
}

void untilArmTravelled(int degrees, int defPower) {
  resetMotorEncoder(A);      // Reset the motor
  float targetDegree;
  targetDegree=(degrees * 5);// the small tan gears are 5 times smaller than the turntable gear
  if(degrees<0) { degrees *= -1; defPower *= -1; }  // If degrees are negative, then reverse the power
  motor[A] = defPower * -1;  // make the power reversed because of the gearbox being used
  repeatUntil(abs(getMotorEncoder(A)) > targetDegree) { sleep(5); }
  motor[A]=0;
}

Overloaded Functions

// Overloaded function
void driveForward(int distance) {
  driveForward();
  untilDistance(distance);
  stopAllMotors();
}
void driveForwardOnWall(int distance) {
  driveForwardOnWall();
  untilDistance(distance);
  stopAllMotors();
}
void driveBackward(int distance) {
  driveBackward();
  untilDistance(distance);
  stopAllMotors();
}
void driveBackwardOnWall(int distance) {
  driveBackwardOnWall();
  untilDistance(distance);
  stopAllMotors();
}

Follow Line Task

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

  int diff;

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

    int k=20;
    int defaultPower=25;
    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 - Key Program W

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()
{
	wait(1);
	robot.wheelDiameterMM = 43.2;
	robot.distanceBetweenWheelsMM = 130;
	robot.powerLevel=40;
	untilTouch();
	driveForward(25);
	turnRight(30);
	driveForward(10);
	turnLeft(30);
	driveForward(10);
	driveBackward(24);
	turnLeft(57);
	driveForward(22);
	driveBackward(4);
	turnRight(90);
	driveBackward(10);
	robot.powerLevel=60;
	driveForward(14);
	robot.powerLevel=40;
	driveBackward(19);
	turnRight(99);

	/*
	stopAllMotors();
	wait(2);
	driveForward(2);
	turnLeft(90);
	driveForward(3);
	driveBackward(6);
	turnRight(90);
	startTask(FollowLine);
	untilSonarLessThan(3);
	stopTask(FollowLine);

	stopAllMotors();
	wait(2);
	driveForward(2);
	turnLeft(90);
	driveForward(3);
	driveBackward(6);
	*/
}

Task Main - Deliver the Blocks

task main()
{
	wait(1);
	robot.wheelDiameterMM = 43.2;
	robot.distanceBetweenWheelsMM = 130;
	robot.powerLevel=40;

	untilTouch();
	driveForward(13);
	wait(1);
	driveForward();
	stopOnLine();
	turnRight(50);

	startTask(FollowLine);
	while(SensorValue((short) ultrasonicSensor) > 3) {
		displayCenteredBigTextLine(6, "Sensor %d", SensorValue[ultrasonicSensor]);
		//sleep(5);
	}



	untilSonarLessThan(3);
	stopTask(FollowLine);
	stopAllMotors();


	motor[A]=10;
	wait(2);
	motor[A]=0;


	robot.wheelDiameterMM = 43.2;
	robot.distanceBetweenWheelsMM = 130;
	robot.powerLevel=40;

	//COMING BACK TO BASE

	driveBackward(9);
	turnRight(270);
  driveForward();
  stopOnLine();
	turnLeft(90);


	startTask(FollowLine);
	untilDistance(36);
	stopTask(FollowLine);
	stopAllMotors();

	turnLeft(38);
	driveForward(36);



	/*while(SensorValue((short) ultrasonicSensor) > 3) {
	displayCenteredBigTextLine(6, "Sensor %d", SensorValue[ultrasonicSensor]);


	}
	*/

	/*
	stopAllMotors();
	wait(2);
	driveForward(2);
	turnLeft(90);
	driveForward(3);
	driveBackward(6);
	turnRight(90);
	startTask(FollowLine);
	untilSonarLessThan(3);
	stopTask(FollowLine);
	*/
}