Project+103a

Project #103a - Creating Functions

media type="custom" key="8849608"


 * NXT doing the Figure-8 using Functions **

**Objectives: **

When you complete this lesson, you should understand the following:

How to create a function (a reusable section of code). How to call or invoke a function (pass control to the reusable section of code). How to improve the movement of your robot using PID Control. Robot behaviors can be mapped to a function allowing for smaller and more stable programs.

==**Procedures: ** ==

Please review the following videos before you begin the task:


 * Principles of PID **
 * PID control **

Also read the following following reference page: **Functions**. Stop when you reach the advanced functions section (you will cover this in a later lesson).

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Students will analyze the major task the robot is to complete (traverse panels in a Figure-8 manner). This is the same task as the one in project #102 except this time you will place the code in reusable packets called **Functions**. No **motor** or **wait** statements are allowed in the task main section of your program for this task. Instead, they will be used in the functions that you will create. The functions will be called from the task main section of your program. While you are writing the pseudo code, think about what mini-tasks will be used over and over to aid in completing the overall task. These will be your functions. Also, name the functions something meaningful (like what behavior the function will perform). The students will continue modifying and testing the program until the Robot correctly maneuvers around the Figure-8 course. Don't forget the comments! They may not seem very important on small programs, but as you write more complicated code, you will appreciate how they will help you develop and maintain the code.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">The code below will give you an example of how to write and call a function that will do a Right Swing Turn. Please notice the PID control statements are also shown for both motors to improve motor control:

<span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">void RightSwingTurn <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">{ <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg; <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">nMotorPIDSpeedCtrl[motorC] = mtrSpeedReg; <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">motor[motorB] = 0; <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">motor[motorC] = 50; <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">wait1Msec(375); <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">} <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">task main <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">{ <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">RightSwingTurn; <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">}

<span style="color: #000000; font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;">If you want to really get ahead, you could try going backwards like these students:
 * <span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 20px; letter-spacing: 0px;">Enrichment: **

media type="custom" key="8219830"

<span style="font-family: arial,helvetica,sans-serif; font-size: 13px; line-height: 19px;">**<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;">NXT doing the Figure-8 using Functions - In Reverse! **

<span style="display: block; font-family: arial,helvetica,sans-serif; font-size: 13px; height: 600px; line-height: 1.5; min-height: 600px; overflow-x: auto; padding-bottom: 2em; width: 960px;">**<span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 20px; letter-spacing: 0px;">Evaluation: **

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">You can evaluate your work using the following three-point rubric:


 * Three points**: You were able to work successfully together and demonstrated proficient understanding of how to break a large task into smaller tasks which would later be coded as Functions. You were able to complete an adequate solution to the problem by using function call only from the task main in RobotC Code. The behavior of the robot successfully completed the Figure-8 course.


 * Two points:** You were able to work together and discuss the problems that were encountered. You attempted to break the major task into smaller tasks and constructed some functions in pseudo code. You demonstrated some understanding in writing functions in RobotC statements. The behavior of your robot partially completed the Figure-8 course.


 * One point:** You worked poorly together and minimal understanding was demonstrated as to how to break a large task into smaller tasks that would later become functions, express solutions to the tasks in pseudo code, or convert the pseudo code into RobotC functions. The robot behavior demonstrated very little of the correct solution.

** Good Luck and Have FUNctions! **