RoboCatz.com

Our Function Library (Fall 2020)


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.

Driving Forward and Turning

// JavaScript Function Library
// Created: Fall 2020

function driveBackward(inches, pwr) {
	myPower = 30
        if( pwr*1 > 0 ) myPower=pwr             // Same as: if(typeof pwr == 'number') myPower=pwr*1
	syncMotors(B, C, myPower)               // Start driving backward
	resetEncoder(B)                         // Reset the encoder
	while( abs(encoderValue(B)) < inches * 58 ) { // Convert inches to degrees by multiplying by 58
	  clearScreen()                         // Clear the screen
	  drawText( 10, 10, encoderValue(B), 2 ) // Show the encoder value
	  sleep(100)                            // Move for a period of time
	}                                       // Repeat the loop
}
function driveForward(inches, pwr) {
	myPower = 30
        if( pwr*1 > 0 ) myPower=pwr             // Same as: if(typeof pwr == 'number') myPower=pwr*1
	resetEncoder(B)                         // Reset the encoder
	syncMotors(B, C, myPower * -1)                   // Start driving forward
	while( abs(encoderValue(B)) < inches * 58 ) { // Convert inches to degrees by multiplying by 58
	  clearScreen()                         // Clear the screen
	  drawText( 10, 10, encoderValue(B))    // Show the encoder value
	  sleep(100)                            // Move for a period of time
	}                                       // Repeat the loop
}
function swingTurnCounterClockwise(degrees) {
	syncMotors( B, C, 30, -100 )            // Start a swing turn
	resetGyroSensor()                       // Reset the sensor
	while( abs(gyroSensorValue()) < degrees ) {  // While the absolute value of the sensor is less than 90 degrees
	  sleep(50)                             // Move for a period of time
	}                                       // Repeat the loop
}
function swingTurnClockwise(degrees) {
	syncMotors( B, C, 30, 100 )            // Start a swing turn
	resetGyroSensor()                       // Reset the sensor
	while( abs(gyroSensorValue()) < degrees ) {  // While the absolute value of the sensor is less than 90 degrees
	  sleep(50)                             // Move for a period of time
	}                                       // Repeat the loop
}
function pointTurnCounterClockwise(degrees) {
	syncMotors( B, C, 30, -200 )            // Start a point turn
	resetGyroSensor()                       // Reset the sensor
	while( abs(gyroSensorValue()) < degrees ) {  // While the absolute value of the sensor is less than 90 degrees
	  sleep(50)                             // Move for a period of time
	}                                       // Repeat the loop
}
function pointTurnClockwise(degrees) {
	syncMotors( B, C, 30, 200 )             // Start a point turn
	resetGyroSensor()                       // Reset the sensor
	while( abs(gyroSensorValue()) < degrees ) {  // While the absolute value of the sensor is less than 90 degrees
	  sleep(50)                             // Move for a period of time
	}                                       // Repeat the loop
}

Accessory Motor

function accessoryMotor(degrees) {
	setMotor(A, 30)                         // Start accessory motor
	resetEncoder(A)                         // Reset the encoder
	while( abs(encoderValue(A)) < degrees ) {
	  clearScreen()                         // Clear the screen
	  drawText( 10, 10, encoderValue(A), 2 ) // Show the encoder value
	  sleep(100)                            // Move for a period of time
	}                                       // Repeat the loop
}

Using the Functions in your Program

driveBackward(1)
driveForward(40)
swingTurnClockwise(60)
driveBackward(10)
driveBackward(10, 40)
driveForward(40)
driveBackward(10, 50)
driveBackward(10, 60)
driveBackward(10, 70)
driveBackward(10, 80)
driveBackward(10, 90)