Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/BoeBot/Driving Functions"

From ROBOTC API Guide
Jump to: navigation, search
(Using a Function)
(Defining a Function)
Line 110: Line 110:
 
After the function is declared there is a set of curly braces that denote the start and end points of the function (similar to task main() ). Whatever code you want the function to run (in this case, the code to stop the robot for 1 second) will be inside the braces. These braces are necessary for the code to compile and run correctly.
 
After the function is declared there is a set of curly braces that denote the start and end points of the function (similar to task main() ). Whatever code you want the function to run (in this case, the code to stop the robot for 1 second) will be inside the braces. These braces are necessary for the code to compile and run correctly.
  
There are some special rules that must be followed when defining a function. The function name cannot be already defined or used by ROBOTC (a 'keyword'). The name cannot contain any whitespaces. If defining a non-void return type function, the function must return an appropriate value (again, more information on this will be available in a later tutorial. Generally, making sure the function has a relevant name helps avoid confusion (FunctionOne is much less descriptive than StopFor1Second) and using the standard C coding principle of capitalizing the first letter of each word in a function increases its readability (StopFor1Second is easier to read than stopfor1second).
+
There are some special rules that must be followed when defining a function. The function name cannot be already defined or in use by ROBOTC (a 'keyword'). The name cannot contain any whitespaces. If defining a non-void return type function, the function must return an appropriate value (again, more information on this will be available in a later tutorial. Generally, making sure the function has a relevant name helps avoid confusion (FunctionOne is much less descriptive than StopFor1Second) and using the standard C coding principle of capitalizing the first letter of each word in a function increases its readability (StopFor1Second is easier to read than stopfor1second).
  
 
Now you define the code that will be run when the function is called. Just like with "task main()", while loops, and for loops, this code is placed inside of curly brackets "{}".
 
Now you define the code that will be run when the function is called. Just like with "task main()", while loops, and for loops, this code is placed inside of curly brackets "{}".

Revision as of 18:22, 12 October 2012

Repetition

By now you have probably noticed that blocks of code are often repeated multiple times throughout your code. For example, let's look at the code to drive forward:

motor[leftServo] = 20;
motor[rightServo] = 20;

and the code to stop:

motor[leftServo] = 0;
motor[rightServo] = 0;

To reuse common segments of code without having to retype or copy and paste each line of code, you can use a function to repeatedly utilize segments of code.

What is a Function?

A Function is a special block of code that runs as a single unit when called from another location in the code. It is common for each function to represent a specific behavior in the program. Not only do they help make complex code easily accessible to the rest of the program, they allow you to run the same code multiple times with very little extra coding lines. This helps eliminate redundant code and makes the entire program length shorter.


Let's say that you are running the following code:

task main()
{
  while(true)
  {
    //drive forward at speed 20 for 1 second
    motor[leftServo] = 20;
    motor[rightServo] = 20;
    wait1Msec(1000);
 
    //stop driving for 1 second
    motor[leftServo] = 0;
    motor[rightServo] = 0;
    wait1Msec(1000);
 
    //right point turn at speed 20 for 1 second
    motor[leftServo] = 20;
    motor[rightServo] = -20;
    wait1Msec(1000);
 
    //stop driving for 1 second
    motor[leftServo] = 0;
    motor[rightServo] = 0;
    wait1Msec(1000);
 
    //drive backward at speed 10 for 2 seconds
    motor[leftServo] = -10;
    motor[rightServo] = -10;
    wait1Msec(2000);
 
    //stop driving for 1 second
    motor[leftServo] = 0;
    motor[rightServo] = 0;
    wait1Msec(1000);
  }
}

Notice that the code to make the robot stop driving for 1 second occurs three different times in the program. This is a great of example of where a function would be useful.

//stop driving for 1 second
motor[leftServo] = 0;
motor[rightServo] = 0;
wait1Msec(1000);

Defining a Function

Before we can use a function, however, we must define it. To do this we would first place the following function definition in the file before "task main()".

//function to stop the robot for 1 second
void StopFor1Second()
{
 
}
 
task main()
{
 
}

The first line contains a comment explaining what the function does. While not required to make the robot run, it is good practice to comment all functions to enable easy understanding of the code.

Next is the 'void StopFor1Second()' line. This line actually sets up the function and defines it as a 'void' type with the name of 'StopFor1Second'. The void type means that the function will not return a value to wherever it was called from. The parentheses after the function name (StopFor1Second) are used to pass parameters into the function. Neither the function type nor parameters are used in this program and will be more fully covered in a later tutorial.

After the function is declared there is a set of curly braces that denote the start and end points of the function (similar to task main() ). Whatever code you want the function to run (in this case, the code to stop the robot for 1 second) will be inside the braces. These braces are necessary for the code to compile and run correctly.

There are some special rules that must be followed when defining a function. The function name cannot be already defined or in use by ROBOTC (a 'keyword'). The name cannot contain any whitespaces. If defining a non-void return type function, the function must return an appropriate value (again, more information on this will be available in a later tutorial. Generally, making sure the function has a relevant name helps avoid confusion (FunctionOne is much less descriptive than StopFor1Second) and using the standard C coding principle of capitalizing the first letter of each word in a function increases its readability (StopFor1Second is easier to read than stopfor1second).

Now you define the code that will be run when the function is called. Just like with "task main()", while loops, and for loops, this code is placed inside of curly brackets "{}".

Using a Function

Now that the function is defined, we can move the code in "task main()" to stop the robot to the new function. Once the code is in place, we will need to 'call' the function to access it. To call a function, you simply type the name of the function (including the two parentheses) followed by a semicolon. In this case the name of the function is "StopFor1Second()", so we would use "StopFor1Second();".

//function to stop the robot for 1 second
void StopFor1Second()
{
  motor[leftServo] = 0;
  motor[rightServo] = 0;
  wait1Msec(1000);
}
 
task main()
{
  while(true)
  {
    //drive forward at speed 20 for 1 second
    motor[leftServo] = 20;
    motor[rightServo] = 20;
    wait1Msec(1000);
 
    StopFor1Second();
 
    //right point turn at speed 20 for 1 second
    motor[leftServo] = 20;
    motor[rightServo] = -20;
    wait1Msec(1000);
 
    StopFor1Second();
 
    //drive backward at speed 10 for 2 seconds
    motor[leftServo] = -10;
    motor[rightServo] = -10;
    wait1Msec(2000);
 
    StopFor1Second();
  }
}

This code will start by defining StopFor1Second(), starting task main(), and moving the robot forward for one second. When it reaches the StopFor1Second() function call, the program will 'jump' to the start of the function (opening brace), run the code inside of the StopFor1Second() function, and 'return' to where it was called from when it reaches the closing brace. Program flow will then pick up exactly where it left off; in this case, after the semicolon of the first StopFor1Second() function call. When task main reaches the second call to StopFor1Second the process repeats itself.

Advanced Functions

Of course it is possible to use functions to do more than just reduce repetition.

Passing Parameters

Parameters are a way of passing information into a function, allowing the function to run its commands differently, depending on the values it is given. It may help to think of the parameters as placeholders – all parameters must be filled in with real values when the function is called, so in the places where a parameter appears, it will simply be replaced by its given value.

Now lets say that you have some blocks of code that are very similar, and the only difference is that they have different motor speeds.

task main()
{
  //drive forward at speed 20 for 1 second
  motor[leftServo] = 20;
  motor[rightServo] = 20;
  wait1Msec(1000);
 
  //stop for 1 second
  motor[leftServo] = 0;
  motor[rightServo] = 0;
  wait1Msec(1000);
 
  //drive backward at speed 10 for 1 second
  motor[leftServo] = -10;
  motor[rightServo] = -10;
  wait1Msec(1000);
 
  //stop for 1 second
  motor[leftServo] = 0;
  motor[rightServo] = 0;
  wait1Msec(1000);
}

It is still possible to use functions to make the code less repetitive, cleaner, and more manageable by using functions with parameters. In the case of the code above, we would pass the desired motor speed to the function in a parameter.

To define a function that accepts parameters you take the basic function definition and inside the parentheses, you place the variable name and type of the parameter you wish to use. In this case we want to pass a int the represents the motor speed, so we will call it "speed".

//set both drive motors to the specified speed and wait 1 second
void SetBothMotors(int speed)
{
  motor[leftServo] = speed;
  motor[rightServo] = speed;
  wait1Msec(1000);
}

Using a function with parameters is very similar to using one without any. The differance is that when calling the function, instead of having empty parentheses, you place the value you wish to pass inside the parentheses.

//set both drive motors to the specified speed and wait 1 second
void SetBothMotors(int speed)
{
  motor[leftServo] = speed;
  motor[rightServo] = speed;
  wait1Msec(1000);
}
 
task main()
{
  //drive forward at speed 20 for 1 second
  SetBothMotors(20);
 
  //stop for 1 second
  SetBothMotors(0);
 
  //drive backward at speed 10 for 1 second
  SetBothMotors(-10);
 
  //stop for 1 second
  SetBothMotors(0);
}

You are also able pass more than one parameter to a function. This is done by separating the list of parameters with commas. So say that we needed to modify the above code to allow variable times, you would end up with

//set both drive motors to the specified speed and wait 1 second
void SetBothMotors(int speed, int time)
{
  motor[leftServo] = speed;
  motor[rightServo] = speed;
  wait1Msec(time);
}
 
task main()
{
  //drive forward at speed 20 for 1 second
  SetBothMotors(20, 1000);
 
  //stop for 1 second
  SetBothMotors(0, 1000);
 
  //drive backward at speed 10 for 2 seconds
  SetBothMotors(-10, 2000);
 
  //stop for 1 second
  SetBothMotors(0, 1000);
}