Our Function Library (Fall 2018)
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.
Standard Robot Structure and Pragmas
#pragma config(Sensor, S1, touchSensor, sensorEV3_Touch)
#pragma config(Sensor, S2, colorSensor, sensorEV3_Color, modeEV3Color_Ambient)
#pragma config(Sensor, S3, gyroSensor, sensorEV3_Gyro)
#pragma config(Sensor, S4, ultrasonicSensor, sensorEV3_Ultrasonic)
#pragma config(Motor, motorA, A, tmotorEV3_Medium, PIDControl, encoder)
#pragma config(Motor, motorB, B, tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorC, C, tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor, motorD, D2, tmotorEV3_Medium, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
typedef struct genericRobotStruct {
int powerLevel;
int wheelDiameterMM;
int gearRatio;
int distanceBetweenWheelsMM;
bool criteriaNotMetYet;
} robotObj; robotObj robot;
Delivering Rover to the Crater
//drive fwd, sense wall, drop wheel, drive back
void LDW(){
resetMotorEncoder(B); resetMotorEncoder(C);
setMotorSync(B,C,0,20);
repeatUntil(SensorValue(ultrasonicSensor)<10){
sleep(1000);
}
stopAllMotors();
motor[D2]=-30;
sleep(1000);
}
void LUR2(){
//go backwards
setMotorSync(B,C,0,-20);
resetMotorEncoder(B); resetMotorEncoder(C);
repeatUntil(getMotorEncoder(B)< 360*-0.90) {
sleep(30);
if(SensorValue(touchSensor)==1) break;
}
stopAllMotors();
int oldGyro=0;
sleep(300);
setSensorMode(gyroSensor,modeEV3Gyro_Angle);
sleep(100);
setMotorSync(B,C,20,-20);
oldGyro=SensorValue(gyroSensor);
while(abs(oldGyro - SensorValue(gyroSensor))<45){
sleep(10);
}
setMotorSync(B,C,0,-20);
resetMotorEncoder(B); resetMotorEncoder(C);
repeatUntil(getMotorEncoder(B)<360*-0.90) {
sleep(30);
if(SensorValue(touchSensor)==1) break;
}
stopAllMotors();
setMotorSync(B,C,-20,-20);
oldGyro=SensorValue(gyroSensor);
while(abs(oldGyro - SensorValue(gyroSensor))<55){
sleep(10);
}
stopAllMotors();
}
Sound Effects
task siren(){
nVolume=6;
while(true){
playTone(440,50);
sleep(500);
playTone(660,50);
sleep(500);
}
}
task pulseSound(){
nVolume=3;
while(true){
playTone(440,10);
sleep(200);
playTone(440,50);
sleep(1000);
}
}
Main Task with 3 Cases
task main()
{
// Check to make sure the correct sensors are plugged in and working
if(SensorType(S1)== 0 || SensorType(S4)== 0){
startTask(siren);
while(SensorType(S1)== 0 || SensorType(S4)== 0){
if((SensorType(S1)==touchSensor && SensorType(S4)==ultrasonicSensor) ){
break;
}
}
stopTask(siren);
clearSounds();
}
// Start Menu
int selecteditem=0;
setLEDColor(ledRedFlash);
startTask(pulseSound);
while(true){
//Now you have the menu choices selected using the up, down, left, and right buttons
if(getButtonPress(buttonDown)){selecteditem=1;playSoundFile("Click");break;}
if(getButtonPress(buttonUp)){selecteditem=2;playSoundFile("Click");break;}
if(getButtonPress(buttonLeft)){selecteditem=3;playSoundFile("Click");break;}
if(getButtonPress(buttonRight)){selecteditem=4;playSoundFile("Click");break;}
}
stopTask(pulseSound);
clearSounds();
//Switch statement
switch(selecteditem){
//
//
case 1:
//this is rayyans program//
int currLight=0;
currLight=getColorAmbient(colorSensor);
sleep(100);
setLEDColor(ledOrangeFlash);
while(true){ // Wait for flashlight here
if(getColorAmbient(colorSensor)<4){break;}
//if(getButtonPress(buttonEnter)){playSoundFile("Click");break;}
}
playTone(440,100);
//Begin Pushing Levi's Jig
robot.wheelDiameterMM = 43.2;
robot.distanceBetweenWheelsMM = 130;
robot.powerLevel=40;
// Lower the Blue Wheels
sleep(100);
resetMotorEncoder(D2);
motor[D2]=20;
repeatUntil(abs(getMotorEncoder(D2))>180){sleep(1); }
motor[D2]=0;
resetMotorEncoder(D2);
motor[D2]=-20;
repeatUntil(abs(getMotorEncoder(D2))>180){sleep(1); }
motor[D2]=0;
//This is where the linear gear gets moved
motor[A]=50;
resetMotorEncoder(A);
repeatUntil(abs(getMotorEncoder(A))>360*3.58){sleep(1); }
motor[A]=0;
// Synchronize motors B and C
robot.powerLevel=60;
resetMotorEncoder(B);
setMotorSync(B, C, 0, robot.powerLevel);
repeatUntil(getMotorEncoder(B)>360*2) {
sleep(1);
}
robot.powerLevel=40; // Accelerate to 80
setMotorSync(B, C, 0, robot.powerLevel);
while(getMotorEncoder(B)<360*5) {
sleep(1);
if(SensorValue(touchSensor)==1) break; // hit the rocket launch pad
}
motor[B]=100; // Try to get even closer to the rocket launch pad
motor[C]=100;
sleep(1000);
playTone(770,200);
stopAllMotors(); // Stop moving
clearSounds();
sleep(1000);
//
/////////////////////////////////////////
//Sam's Program - Astronaut rescue
resetMotorEncoder(D2);
motor[D2]=10;
while(getMotorEncoder(D2)<700){sleep(10);}
stopAllMotors();
//sam's
motor[D2]=-10;
while(getMotorEncoder(D2)>400){sleep(10);}
motor[D2]=10;
while(getMotorEncoder(D2)<490){sleep(10);}
motor[D2]=-10;
while(getMotorEncoder(D2)>0){sleep(10);}
stopAllMotors();
sleep(1000);
// Drive Back to base
robot.powerLevel=-40;
resetMotorEncoder(B);
setMotorSync(B, C, 0, robot.powerLevel);
repeatUntil(abs(getMotorEncoder(B))>90) {
sleep(1);
}
motor[A]=70; // After 1/4 turn of large wheels, Start to get rid of the linear gear
while(SensorValue(ultrasonicSensor)>14) {
if(SensorValue(ultrasonicSensor)<20) {
motor[B]=-20;
motor[C]=-20;
}
if(SensorValue(ultrasonicSensor)<60) {
motor[D2]=100-SensorValue(ultrasonicSensor);
}
}
motor[B]=-20;
motor[C]=-20;
sleep(2000);
stopAllMotors();
motor[D2]=40;
// Use flashlight to stop motor D2
currLight=getColorAmbient(colorSensor);
repeatUntil(getColorAmbient(colorSensor)<4){sleep(10);}
stopAllMotors();
bFloatDuringInactiveMotorPWM=true; // permit motors to float when inactive
bool sirenStarted=false;
while(SensorType(S1)== sensorEV3_Touch || SensorType(S4)== sensorEV3_Ultrasonic){
if((SensorType(S1)==0 || SensorType(S4)==0) && sirenStarted==false){
startTask(siren);
sirenStarted=true;
}
}
while(SensorType(S1)== 0 || SensorType(S4)== 0){
if((SensorType(S1)==sensorEV3_Touch && SensorType(S4)==sensorEV3_Ultrasonic) ){
break;
}
}
stopTask(siren);
clearSounds();
bFloatDuringInactiveMotorPWM=false; // do not permit motors to float when inactive
//
case 2:
// Damir's program
setLEDColor(ledOrange);
// Wait for shade
currLight=getColorAmbient(colorSensor);
sleep(100);
setLEDColor(ledOrangeFlash);
while(true){
if(getColorAmbient(colorSensor)<4){break;}
//if(getButtonPress(buttonEnter)){playSoundFile("Click");break;}
}
//Push Damir's Jig
robot.wheelDiameterMM = 43.2;
robot.distanceBetweenWheelsMM = 130;
robot.powerLevel=30;
sleep(2000);
stopAllMotors();
setMotorSync(B, C, 0, robot.powerLevel);
while(SensorValue(touchSensor)<1) {
if(SensorValue(ultrasonicSensor)<20){
motor[B]=20;
motor[C]=90;
}
}
sleep(500);
stopAllMotors();
motor[D2]=40;
sleep(2000);
stopAllMotors();
setMotorSync(B, C, 0, -robot.powerLevel);
currLight=getColorAmbient(colorSensor);
sleep(100);
setLEDColor(ledOrangeFlash);
while(true){
if(getColorAmbient(colorSensor)<3){break;}
//if(getButtonPress(buttonEnter)){playSoundFile("Click");break;}
}
stopAllMotors();
break;
//
case 3:
setLEDColor(ledOrange);
// Wait for flashlight
currLight=getColorAmbient(colorSensor);
sleep(100);
setLEDColor(ledOrangeFlash);
while(true){
if(getColorAmbient(colorSensor)<4){break;}
//if(getButtonPress(buttonEnter)){playSoundFile("Click");break;}
}
//Push Henry's Ramp
LUR2();
LDW();
break;
case 4:
setLEDColor(ledOff);
while(true){
if(getButtonPress(buttonEnter)){playSoundFile("Click");break;}
}
//Do something else
break;
}
}