RoboCatz.com

Main Program (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

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 in the other direction
    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 in the other direction
    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 swing turn in the other direction
    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 swing turn in the other direction
    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
}

Attachment Motor Functions

function attachmentMotor(degrees) {
   setMotor(A, -100)                     
   // Start Tilting up
   resetEncoder(A)                         
   // Reset the encoder
   while( abs(encoderValue(A)) < degrees * 1 ) { 
      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
}

Missions

function slideandstep() {
    alert('You selected ENTER')
    driveForward(26.5,20)
    pointTurnCounterClockwise(90)
    stopAllMotors()
    syncMotors(B, C,  30)
    sleep(1000)
    while(touchSensorPressed()==false) {
      
      sleep(50)
    }
    stopAllMotors() 
    sleep(1000)
    driveForward(2.1,10)
    pointTurnClockwise(83)
    stopAllMotors()
    driveForward(8.4,4)
    driveBackward(21, 35)
    stopAllMotors()
    pointTurnCounterClockwise(90)
    stopAllMotors()
    syncMotors(B, C,  30)
    sleep(1000)
    while(touchSensorPressed()==false) {
      
      sleep(50)
    }
    stopAllMotors() 
    sleep(1000)
    syncMotors(B, C, -30)
    while(lightSensorPct()>6) {
      
      sleep(50)
     
    }


    stopAllMotors()
    pointTurnClockwise(47)
    stopAllMotors()
    driveForward(6.1)
    stopAllMotors()
    setMotor(A, 6, 50)
    sleep(5000)
    stopAllMotors()
    driveBackward(20)
    pointTurnClockwise(40)
    syncMotors(B, C,  30)
    sleep(1000)
    while(touchSensorPressed()==false) {
      
      sleep(50)
    }
    stopAllMotors()
    driveForward(2)
    pointTurnCounterClockwise(90)
    stopAllMotors()
    
}
function bench() {
alert('You selected DOWN')
        driveForward(13.5,20)
        stopAllMotors()
        setMotor(A, -6, 20)
        sleep(1000)
        stopAllMotors()
        driveBackward(14,20)
        stopAllMotors()
        pointTurnClockwise(75)
        stopAllMotors()
        syncMotors(B, C,  30)
        sleep(1000)
        while(touchSensorPressed()==false) {
          
          sleep(50)
        }
        stopAllMotors()
}
function knockbench() {
        alert('You selected RIGHT')
        driveForward(10.5,20)
        stopAllMotors()
        pointTurnCounterClockwise(5)
        stopAllMotors()
        driveBackward(10.5,20)
        stopAllMotors()
        pointTurnClockwise(75)
        stopAllMotors()
        syncMotors(B, C,  30)
        sleep(1000)
        while(touchSensorPressed()==false) {
          
          sleep(50)
        }
        stopAllMotors()
}

Menu Program

while(true) {
    setLED(0)
    clearScreen()
    
    rect(10,10,160,60)
    drawText(15,30,'Press a key to')
    drawText(15,45,'  select a program')
    
    keyPressed=waitForPress()
    
    switch(keyPressed)
    {
    
    case 1:
        alert('You selected UP')
        target = 6
        gain = 1
        while(true) {
          syncMotors(B, C, 10, (lightSensorValue()-target) * gain)
          sleep(100)
        }
        break
    
      case 2:
          slideandstep()        
        break
    
      case 3:
          bench()
        break
    
      case 4:
          knockbench()
        break
       
     }            
}

MH Case 3

case 3:
    alert('You selected BENCH')
    setLED(6)
    sleep(2000)
    //BECH
    driveForward(14.2)
    stopAllMotors()
    attachmentMotor(100) // Start Tilting up
    stopAllMotors()
    // Turn wheels in opposite directions
    syncMotors( B, C, 30, 200 )
    sleep(1000)
    syncMotors( B, C, -100 )
    sleep(1000)
    stopAllMotors()
    //  turn counter clockwise to knock down bench
    //  drive forward motor a todrop the blocks from the scoop
    break