Creating driving functions

From ROBOTC API Guide
< Tutorials‎ | Arduino Projects/Mobile Robotics/BoeBot
Revision as of 20:27, 12 October 2012 by Jwatson (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

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 with different values. It may help to think of the parameters as placeholders – all parameters must be filled in with real values when the function is called. When a parameter appears in the function's code it will simply be replaced by the value passed up during the function call.

As an example, let's say you would like to stop for one second the first time the function is called, but stop for two seconds the second time it is called. Without parameters, you would need to make another, separate function for each different stop command:

void StopFor1Second()
{
 
}
 
void StopFor1.5Seconds()
{
 
}
 
void StopFor2.78Seconds()
{
 
}
 
task main()
{
 
}

Not only will this method create multiple copies of redundant code (which is exactly why we created a function in the first place; to eliminate redundant code), it is extremely inflexible if you need to stop for a time that doesn't already have a function created for it. However, parameters allow you to pass values into a function and use that value inside the function's code. To use parameters in the StopFor1Second program, for instance, we would need to modify the function declaration to include the necessary parameters and ensure we pass appropriate values from task main().

The first step, modifying the function declaration, is were you decide what type of value is going to be passed into the function. Since we are using the Wait1MSec command we will need to use the integer type. We want to ensure the name of the integer fits what the value will be used for and is easy to read; in this case, we will call it 'stopTime'. To add the parameter to the function, simply create the integer inside of the functions parentheses:

void StopFor1Second(int stopTime)
{
 
}

Now, every time the StopFor1Second function is called it will need an integer value passed to it:


void StopFor1Second(int stopTime)
{
 
}
 
task main()
{
 StopFor1Second(1000);
}

This will pass a value of 1000 to the integer 'stopTime' in the StopFor1Second function. Now, all we need to do is add it to the code that already exists by replacing the Wait1MSec values with 'stopTime' and adding an integer value to each calling of StopFor1Second:

void StopFor1Second(int stopTime)
{
  motor[leftServo] = 0;
  motor[rightServo] = 0;
  wait1Msec(stopTime);
}
 
task main()
{
 StopFor1Second(1000);
}

If you want to stop the robot for a different amount of time (2 seconds, for example), all you have to do is add a second call to the StopFor1Second that passes a value of 2000 (2000ms = 2 seconds).

void StopFor1Second(int stopTime)
{
  motor[leftServo] = 0;
  motor[rightServo] = 0;
  wait1Msec(stopTime);
}
 
task main()
{
 StopFor1Second(1000);
 StopFor1Second(2000);
}

Finally, you may want to change the name of the function to something more appropriate since it no longer stops the robot only for one second. Remember to not only change the function name when it is declared, but also the name of the function each time it is called (in this example, change it to 'StopForTime'):

void StopForTime(int stopTime)
{
  motor[leftServo] = 0;
  motor[rightServo] = 0;
  wait1Msec(stopTime);
}
 
task main()
{
 StopForTime(1000);
 StopForTime(2000);
}

Passing Multiple Parameters

You can pass more than one parameter to a function each time you call it. Since you already know how to pass a single parameter, passing a second (or third) parameter is relatively simple. For example, let's create a new 'MoveWheels' function to accept both motor power levels and a wait time. To do this, create a new function but instead of creating one parameter variable (like you did in the StopForTime program with 'stopTime'), you will make three integer variables separated by a comma. They will be named leftPower, rightPower, and moveTime:

void MoveWheels(int leftPower, int rightPower, int moveTime)
{
 
}

Now that the MoveWheels function has been initialized, it will need to be called in task main(), just like the StopForTime function, only this time you will be passing three values to it:

void MoveWheels(int leftPower, int rightPower, int moveTime)
{
  motor[leftServo] = leftPower;
  motor[rightServo] = rightPower;
  wait1Msec(moveTime);
}
 
task main()
{
 MoveWheels(50, 50, 1000);
 MoveWheels(25, 75, 2500);
 MoveWheels(0, 0, 1000);
}

By passing up both the motor power levels and wait time parameters to the MoveWheels function, you can now change your robot's direction, speed, and movement duration with one line of code.