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

From ROBOTC API Guide
Jump to: navigation, search
(What is a Function?)
(What is a Function?)
Line 26: Line 26:
  
 
== What is a Function? ==
 
== 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. Each function has a unique name within the code. It is by referencing this name that you can call on and run the 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:
 
Let's say that you are running the following code:
Line 72: Line 73:
 
|}
 
|}
  
Notice that the code to make the robot stop driving for 1 second occurs 3 times in the program. This is a great of example of where a function would be useful.
+
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.
 
{|
 
{|
 
|-
 
|-
Line 85: Line 86:
 
=== Defining a Function ===
 
=== Defining a Function ===
  
So if we wanted to make the code shorter, and in most cases easier to read, we could put the stopping code into a function and call that function when we want to stop. To do this we would first place the following function definition in the file before "task main()".
+
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()".
  
{|
+
{|  
 
|-
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
|<syntaxhighlight lang="ROBOTC">
Line 93: Line 94:
 
void StopFor1Second()
 
void StopFor1Second()
 
{
 
{
  motor[leftServo] = 0;
+
 
  motor[rightServo] = 0;
+
}
  wait1Msec(1000);
+
 
 +
task main()
 +
{
 +
 
 
}</syntaxhighlight>
 
}</syntaxhighlight>
 
|-
 
|-
 
|}
 
|}
  
Lets talk about the structure of a function definition. You'll notice that there is a comment on the first line of the code block. This comment is not required but it is a good practice to have a comment that explains what the function does. This can make it easier to follow the coding and also makes it easier to come back to a program after some time and modify it.
+
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 you will see "void StopFor1Second()". This line tells the compiler that the code in the brackets is a function. The first ''word'' on the line is void. The word "void" can be replaced by a few others, however that is for a later tutorial. For now all you need to know is that "void" basically tells the compiler that when the function is called, just run the function code, then return right back to where it was called from. The next ''word'' is the name of the function. In this case we are calling the function "StopFor1Second" since it will make the robot stop for 1 second. You will also notice the parentheses immediately after the function name. The purpose of these will be discussed later.
+
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.
  
{{Note|When naming your functions you need to remember that the name can not contain any white spacing. That means no spaces, new lines, or tabs. To make function names easier to understand, it is standard practice with any C based language, to capitalize the fist letter of each "word" in the name. For example, if you are thinking "do this simple task" you would name it "DoThisSimpleTask".}}
+
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.
  
{{Note|It is a good idea to make the name of your function relevent to the task that it performs. For example, if you have a function that makes the robot drive forward, name it something like "DriveForward" and not "Function1". This will help document the code with fewer comments since you don't need to to add a comment saying that the function called is for driving forward.}}
+
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).
  
 
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:10, 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 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).

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 go ahead and replace the code in "task main()" to stop the robot, with a call to the new function. To call a function you simply enter the name of the function where you want to run it followed by a semi-colon. In this case the name of the function is "StopFor1Second()", so we would use "StopFor1Second();". So if we replace all the code we get

//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();
  }
}

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);
}