Project+103b

Project #103b - Using Parameters with Functions

media type="custom" key="8816316"


 * NXT running the Figure-8 course with only three functions **

**Objectives: **

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

How to create a function that receives a parameter. How to call a function with a parameter. How to declare a variable. Variables can be different data types, such as integer, string, boolean, or floating-point. How many degrees of rotation are required to make the robot move n centimeters.

==**Procedures: ** ==

Please review the following videos before you begin the task:

Values and Variables Behaviors

Also read the following following reference page: **Functions**. <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;"> Stop when you reach the advanced functions (return values) section.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Watch the video above and 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 #103a except this time you will only be allowed to have **three** functions: **TurnRight, TurnLeft, GoForward**. You will change the way that a function perfoms by passing it some information. These data that are passed to the function are referred to as **parameters**. Normally a parameter will be a number, but is could be a word. The student will pass the number of degrees that they want the wheel(s) to turn to the function from the task main section. The function will then turn the wheel(s) the degrees passed to it. If you need a little help with the math tools required to solve this project, please refer to the section below called <span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 16px;">Math Facts.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">You should be very familiar with the problem solving process by now. Remember, Break the large task into smaller tasks and write the pseudo code solution. These mini solutions will normally become functions (or subroutines). Oh yes, one more thing .... comment, comment, comment ... did I mention that you should comment?

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">The code below will give you an example of how to write and pass a parameter. The code will pass the number of seconds that it wants the robot to go forward to the function called SecondsToTurn. Since the variable (t) is a floating-point data type, it can have a decimal. Thus, the value 5.6 is perfectly fine to pass as a parameter to our function.

<span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">void SecondsToTurn(float t) <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 24px;">/ / This is the Function that receives the 5.6 <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;">t = t * 1000; <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 24px;"> / / convert seconds to milliseconds. <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">motor[motorB]=50; <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">motor[motorC]=50;  <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">wait1Msec(t); <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 24px;">/ / The t now contains 5.6 seconds (5600 milliseconds) <span style="color: #002bff; display: block; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px; text-align: left;">motor[motorB]=50; <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; 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;">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;">SecondsToTurn(5.6); <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 24px;">/ / This is the Function Call passing 5.6  <span style="color: #002bff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 23px;">}

==<span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 20px; line-height: 24px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 5px;"> Math Facts: ==

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Recall that a circle has 360 degrees. <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Pi is a number that represents the ratio of the circumference of a circle divided by its diameter. <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">So if one knew the diameter of a circle, you could calculate its circumference (the distance around the circle). <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">That gives us the formula <span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 16px;">circumference = diameter x 3.14 <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;"> (remember 3.14 is an approximation of Pi). <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">The NXT has wheels that have a diameter of 56 millimeters (mm). <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">It takes 10 mm to make a centimeter, so our wheel size could also be stated as 5.6 centimeters (cm).

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Try calculating the circumference of the wheel in (cm) using the facts above? (before you read on ...)

<span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 16px;">Circumference = 5.6 cm x 3.14 <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">, Remember the <span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 16px;">blue <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;"> formula in the facts above. <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Thus, the Circumference is 17.584 cm.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Now, using a little common sense; if our wheel rotates one complete ratation then the robot will move 17.584 cm. <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Another way to express that would be: rotate your wheel 360 degrees and the robot will move 17.584 cm.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Let's take one more math tool out of our toolbox and we will have all the math needed to solve our project.

<span style="color: #ff0000; font-family: 'Comic Sans MS',cursive; font-size: 16px;">Proportion of Ratios <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">will allow us to determine how many degrees we need to have our wheel turn to have our robot move any distance we determine.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Here is how it works:

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">We know 360 degrees will make the robot go 17.584 cm. We want the robot to go 50 cm. How many degrees do <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">we need to have the robot's wheels turn to go 50 cm? So, we can set our ratios equal to one another:

<span style="font-family: 'Comic Sans MS',cursive; font-size: medium;">360 degrees <span style="color: #ffffff; font-family: 'Comic Sans MS',cursive; font-size: medium;">ff ff <span style="font-family: 'Comic Sans MS',cursive; font-size: medium;"> ? degrees <span style="font-family: 'Comic Sans MS',cursive; font-size: medium;"> = <span style="color: #ffffff; font-family: 'Comic Sans MS',cursive; font-size: medium;">﻿ <span style="font-family: 'Comic Sans MS',cursive; font-size: medium;"> <span style="font-family: 'Comic Sans MS',cursive; font-size: medium;">17.584cm <span style="color: #ffffff; font-family: 'Comic Sans MS',cursive; font-size: medium;">ffff ffff <span style="font-family: 'Comic Sans MS',cursive; font-size: medium;"> 50cm

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">The above show us a proportion of our ratios, now let's solve it:

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;"> d = (360 degrees x 50 cm) / 17.584 cm

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">so the number of degrees (d) that we need to make our wheels turn is: 1023.65787 <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Let's round to the nearest whole number, which would be 1024 degrees.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Well, what are you waiting for? Get your robot out and test our math!

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;">Use the following code in a program to test our results:

<span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">task main <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">{ <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">nMotorEncoder[motorB] = 0; <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">/ / Sets the degree counter to zero! <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">while (nMotorEncoder[motorB] < 1024) <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">/ / Loops while the degrees turned is less than 1024 degrees <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">{ <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">motor[motorB] = 50; <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">motor[motorC] = 50; <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">}

<span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">motor[motorB] = 0; <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">/ / Stop the <span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;">motor[motorC] = 0; / <span style="color: #008000; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px;"> / Motors ... You're there

<span style="color: #0000ff; font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 24px;">}

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;">Well, did the robot go 50 centimeters? GREAT! <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;">Now see if you can make the robot go 20 cm using the program. (YES, You have to calculate the degrees!). <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;">Then measure one of the straight sections of the figure-8 course and see if you can get your robot to do it. <span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; line-height: 24px;">Now, you are ready to solve the project since you have all your math equipment handy :)

<span style="font-family: arial,helvetica,sans-serif; font-size: 13px; line-height: 19px; margin: 0px; padding: 0px;">**<span style="color: #0000ff; font-family: 'Comic Sans MS',cursive; font-size: 20px; letter-spacing: 0px;">Enrichment: **

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px; margin: 0px; padding: 0px;"> When you have mastered passing the degrees to the functions, see if you can modify the functions to receive the distance that you want the robot to go by simply passing the three functions the number of Centimeters and let it calculate the degrees!


 * <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:

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;"> **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 calls only from the task main in RobotC Code. You only used Three Functions and they each received a parameter that contained the number of degrees to rotate the wheel(s). The behavior of the robot **successfully completed** the Figure-8 course.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;"> **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 that would receive a parameter for wheel rotations. The behavior of your robot **partially completed** the Figure-8 course.

<span style="font-family: 'Comic Sans MS',cursive; font-size: 16px;"> **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. You demonstrated little knowledge of how to pass parameters to functions. The robot behavior demonstrated **very little** of the correct solution.

<span style="font-family: 'Arial Black',Gadget,sans-serif; font-size: 16px; line-height: 24px;">Q: What is a good height for a basketball player? A: par a meters