# Presentation: 322980

## Robotics With the XBC ControllerSession 4

- Instructor: David Culp
- Email: _[culpd](mailto:culpd@cfbisd.edu)__[@](mailto:culpd@cfbisd.edu)__[cfbisd](mailto:culpd@cfbisd.edu)__[.](mailto:culpd@cfbisd.edu)__[edu](mailto:culpd@cfbisd.edu)_

## Learning Goals

- The student will learn about functions and modular programming, using the IC preprocessor, reading XBC buttons and Botball functions.

## Review

- if-then, statements
- Loops....
- while
- for-next
- for (count = 100; count >=10; count-=10)

## Review Continued...

- Variables
- Four main types
- int
- long
- float
- char
- Must have a name
- Letters, numbers and underscore allowed
- Must begin with a letter or the underscore
- Must be declared before use
- Local variables have precedence over global variables

## Analog sensors

- Return a range of values
- Use the analog and analog12 functions to access.
- Cannot use analog port #7
- Battery voltage monitor
- power_level() function returns the current battery voltage as a float.
- Ports 0 and 1 are “floating” ports for use with the IR range finder.

## A short review program...

- void main()
- {
-     float batt;
-     int power;
-     while (!b_button())
-       {
-         batt= power_level();
-         display_clear();
-         printf("Battery Voltage: %f", batt);
-         sleep(.1);
-     }
- }

## A short review assignment

- Modify the program so that once the ‘B’ button is pressed the program enters a for-next loop.
- The loop counts from –100 to 100 in increments of 5 and sets the power of the left and right motors to this value.
- There is a .1 second pause at the bottom of the loop.
- Turn your motors off after the loop is done.

## void main()
{
    float batt;
    int power;

    while (!b_button())
      {
        batt= power_level();
        display_clear();
        printf("Battery Voltage: %f", batt);
        sleep(.1);
    }

    for (power = -100; power <=100; power +=5)
    {
        motor(0,power);
        motor(2,power);
        sleep(.1);
    }
    ao();
}

- {
-     float batt;
-     int power;
-     while (!b_button())
-       {
-         batt= power_level();
-         display_clear();
-         printf("Battery Voltage: %f", batt);
-         sleep(.1);
-     }
-     for (power = -100; power <=100; power +=5)
-     {
-         motor(0,power);
-         motor(2,power);
-         sleep(.1);
-     }
-     ao();
- }

## Functions in C

- A function is a separate block of code with a unique name that does a particular job.
- Functions let you chop up a long program into named sections.
- Functions can accept **parameters** and can **return** a result.
-  All code belonging to a function is contained in curly braces {}.
- We have already been using them!!

## A very simple example...

- void main()
- {
-     print_hello();
- }
- void print_hello()
- {
-     display_clear();
-     printf("Hello!!\n");
-     return;
- }

## Things to notice....

- The function had a name - “print_hello”
- Function names follow the same rules as variable names.
- The void means the function will not return a value.
- All the code belonging to the function was contained in curly braces.
- The return keyword tells the function to go back to where it came from.  It was optional in this case but enhances readability.

## Returning a value and accepting parameters.

- void main()
- {
-     int a=10;
-     int b=20;
-     printf("Result = %d", add_two_numbers(a,b) );
- }
- int add_two_numbers(int num1, int num2)
- {
-     return num1+num2;
- }

## Things to notice.....

- The function definition:
- int add_two_numbers(int num1, int num2)
- The preceding int means the function will return an int value.
- The parameter definitions:
- (int num1, int num2)
- Tells the compiler that this function accepts two int paramters called num1 and num2.
- We can access those parameters by name.
- Parameters are local to the function.
- The return keyword returns the desired value back to the caller.  It **MUST** appear in any function that returns a value.
- A function may return **one and only one** value.

## Why use functions?

- Functions increase the readability and portability of your programs.
- Allows much easier debugging.
- Allows you to reuse your code.
- Can shorten your overall program.

## A short assignment

- Write a short program that has a function called square that accepts an integer number and returns an integer number which is the parameter squared.
- Print the result on the Gameboy screen.

## Possible solution:

- void main()
- {
-     int a=10;
-     printf("%d\n", square(a));
- }
- int square(int num)
- {
-     return (num * num);
- }

## Another short assignment

- Start a new program and write four functions called forward, back, left and right.
- These functions accept an int number which is the power level to set your motors to.
- They return no value.
- Use the functions to cause your robot to move around in any way you choose.

## Getting started on the assignment....

- void main()
- {
-     Your control code;
- }
- void forward(int power)
- {
-     motor(0,power);
-     motor(2,power);
- }
- Other functions will go here....

## The IC preprocessor

- The preprocessor processes an IC file before it is sent to the compiler.
- Allows us to include functions from other files and allows us to define preprocessor macros.
- Can make our code more efficient and much easier to read.

## Including another IC file

- To bring in the functions from another IC file use ‘#use’ at the TOP of the file.
- Example: #use “botball.ic”
- Brings in all the functions and variables defined in the file “botball.ic”

## A practical example of #use

- Start a new IC file called “motorcontrol.ic”
- Take all of your motor control functions and cut them from your last program.
- Paste these motor control functions into the new source file and save it.
- At the top of your ORIGINAL program place the statement #use “motorcontrol.ic” at the VERY top.
- Download and run the program.
- The program should bring in motorcontrol.ic and run normally.

## What it should look like....

- #use "motorcontrol.ic"
- void main()
- {
-     forward(100);
-     sleep(3.0);
-     ao();
-     printf("Done");
- }

## #define statements..

- #define statements allow us define preprocessor macros.
- A macro is local to the file in which it is defined
- Essentially it sets one item equal to something.
- Unlike variables they CANNOT be changed later.
**Before** the program is compiled the preprocessor replaces every instance of a macro name with its value.
- Examples:
- #define L 2
- Everywhere the word “L” appears in your IC program file will get replaced with the number 2 just before downloading.
- #define R 0
- Everywhere the word “R” appears in your IC program file will get replaced with the number 0 just before downloading.

## The power of #define

- Allows you to set commonly used values to easily read statements.
- Allows MUCH easier debugging as the program is easier to read.
- Allows easy software changes.
- #define macro names are normally ALL CAPITALS. This is not required but easily distinguishes them from variables and makes your code easy to read.

## An example....

- #define L 2
- #define R 0
- #define DELAY 3.5
- void main()
- {
-     motor(L, 100);
-     motor(R, 100);
-     sleep(DELAY);
-     ao();
- }

## A More Complex Example

- #define ST 100
- #define MAX 250
- // This tests the function "adjust" which can be used to adjust parameters
- void main()
- {
-     int speed=ST,speed_f;
-     display_clear();
-     printf("Speed = parameter to adjust");
-     speed_f=adjust(1,speed,MAX);
-     display_set_xy(0,4);
-     printf("Final Speed= %d",speed_f);
- }
- int adjust(int y, int i, int m){ //uses direction pad to adjust 0 < i < m
-     //print at line y, i = variable parameter to adjust, m = max value;
- 	 while(!a_button()){// press A to finish
-         display_set_xy(0, y);
-         printf("parameter=%d, \n  adjust w. Right-Left PAD\n  press A to end",i);
-         if(left_button()&& i>=5) i-=5;
-         if(right_button()&& i< m)i+=5;
-         sleep(.2);
-     }
-     return i;
- }

## Botball Functions.....

- To use functions specifically designed for Botball #use “Botball.ic” at the top of your program.
- Two main functions:
- wait_for_light(int port)
- Allows us to calibrate a light sensor on port and the robot will wait until the starting light turns on to begin execution
- shut_down_in (float seconds)
- The IC program downloaded will run for the specified number of seconds and then shut down all motor control and end the program.

## A Typical Botball Program

- #use "botball.ic"
- #define L 2
- #define R 0
- #define LIGHT 5
- void main()
- {
-     wait_for_light(LIGHT);
-     shut_down_in(89.9);
-     play_botball();
- }
- void play_botball()
- {
- }

## Reading XBC buttons

- Reading XBC buttons is very simple.
- Each function returns 0 if NOT pressed and 1 if pressed.
- int a_button()
- int b_button()
- int up_button()
- int down_button()
- int right_button()
- int left_button()
- int r_button() – The top right button
- int l_button() – The top left button
- int any_button()

## Your assignment

- In the last session we modified our demo-bot by adding IR range sensors.
- Modify your bumper-bot program so that you use functions and the pre-processor.
- Make four motor control functions and place them in a separate file called “motor_control.ic”.
- Include (#use) this file at the top of your program.
- Use the preprocessor to #define macros for the left and right motor and the left and right IR sensor.  You may also make a #define for the minimum “hit” distance.

## To Help You Get Started...

- #use "motor_control.ic"
- #define LEFT_SENSOR 1
- #define MIN_RANGE 200
- void main()
- {
-     while(1)
-       {
-         do stuff in here;
-     }
- }

## motor_control.ic

- #define LEFT_MOTOR 2
- #define RIGHT_MOTOR 0
- void forward()
- {
-     fd(LEFT_MOTOR);
-     fd(RIGHT_MOTOR);
- }
- .
- .
- .