Presentation is loading. Please wait.

Presentation is loading. Please wait.

Robotics Week 4 Functions and reusable code

Similar presentations


Presentation on theme: "Robotics Week 4 Functions and reusable code"— Presentation transcript:

1 Robotics Week 4 Functions and reusable code
Compiled from various Internet sources Presented by Mr. Hatfield Robotics Week 4 Functions and reusable code

2 Last Weeks Assignment:
Move the robot forward 20 cm Move the robot backward 20 cm Do both of the above Make the robot turn left then right Make the robot turn left 90 degrees Make the robot travel in a square Make the robot dance

3 Wouldn’t It Be Nice If I did not have to cut and paste so much
If my code were not so long If my code was readable If I could just say something like move a certain distance at a certain power Well, YOU CAN!!!

4 What is a Function? A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. ... A function declaration tells the compiler about afunction's name, return type, and parameters.

5 Mr. H. Can You English That
A Function Is a set of instructions (some code) That together perform a task (does one job, and hopefully does it well) (Like move the robot a certain distance at a certain speed.) It has a name, parameters and can have a return type (you call it something, give it input, and it can give you output)

6 Remember This Code? //causes the robot to move forward 10 cm
float d = 10; //Distance for robot to travel in cm float dia = 5.6; //Standard EV3 wheel diameter in cm float pi = 3.14; //PI approximation float circ = pi * dia; //calculate the circumference float deg = (360 / circ) * d; //calculate the circumference //Resets the motor encoder using the resetMotorEncoder command resetMotorEncoder(motorC); //While the encoder for the motorC is less than deg while(getMotorEncoder(motorC) < deg) {//move until requested deg motor[motorC] = 50; motor[motorB] = 50; } motor[motorC] = 0; motor[motorB] = 0;

7 The Previous Code Was placed in the function that we called
task main() { //code went here } If we move it above task main () and create a new function, then we can use the new function in task main()

8 void move10cm() { //a function with no return value
//causes the robot to move forward 10 cm float d = 10; //Distance for robot to travel in cm float dia = 5.6; //Standard EV3 wheel diameter in cm float pi = 3.14; //PI approximation float circ = pi * dia; //calculate the circumference float deg = (360 / circ) * d; //calculate the circumference //Resets the motor encoder using the resetMotorEncoder command resetMotorEncoder(motorC); //While the encoder for the motorC is less than deg while(getMotorEncoder(motorC) < deg) {//move until requested deg motor[motorC] = 50; motor[motorB] = 50; } motor[motorC] = 0; motor[motorB] = 0; } //Ends the function task main() { //the primary function move10cm(); } //Ends task main

9 Sort of Cool, But Not Really
What do I do, Move10cm, then Move10cm… Well, you could…But…Why?... What if I want to move 20 cm? or 25cm? void move10cm() { } //Ends the function task main() { //the primary function move10cm(); } //Ends task main

10 Variables and Parameters
Notice the (float d) on line 1 void moveDcm(float d) { //d is the distance in cm float dia = 5.6; //Standard EV3 wheel diameter in cm float pi = 3.14; //PI approximation float circ = pi * dia; //calculate the circumference float deg = (360 / circ) * d; //calculate the circumference //Resets the motor encoder using the resetMotorEncoder command resetMotorEncoder(motorC); //While the encoder for the motorC is less than deg while(getMotorEncoder(motorC) < deg) {//move until requested deg motor[motorC] = 50; motor[motorB] = 50; } motor[motorC] = 0; motor[motorB] = 0; } //Ends the function

11 Now We Have Something We can call the function and pass different parameters as often as we like Cool, if you always want to go at 50% power void moveDcm(float d) { //d is the distance in cm } //Ends the function task main() { moveDcm(10); //Moves the robot 10 cm like the original function moveDcm(55); //Moves the robot 55 cm moveDcm(12.5); //Moves the robot 12.5 cm moveDcm(200); //Moves the robot 200 cm } //Ends task main

12 Variables and Parameters (2)
Notice the (float d, int pwr) on line 1 void moveDcm(float d, int pwr) {//pwr is the power in % float dia = 5.6; //Standard EV3 wheel diameter in cm float pi = 3.14; //PI approximation float circ = pi * dia; //calculate the circumference float deg = (360 / circ) * d; //calculate the circumference //Resets the motor encoder using the resetMotorEncoder command resetMotorEncoder(motorC); //While the encoder for the motorC is less than deg while(getMotorEncoder(motorC) < deg) {//move until requested deg motor[motorC] = pwr; motor[motorB] = pwr; } motor[motorC] = 0; motor[motorB] = 0; } //Ends the function

13 Distance and Power Now We can call the function and pass different parameters as often as we like Cool, if you always want to go at 50% power void moveDcm(float d, int pwr) { //pwr is the power in % } //Ends the function task main() { moveDcm(10, 50); //Moves the robot 10 cm at 50% power moveDcm(55, 10); //Moves the robot 55 cm at 10% power moveDcm(12.5, 25); //Moves the robot 12.5 cm at 25% power moveDcm(200, 90); //Moves the robot 200 cm at 90% power } //Ends task main

14 Going in Reverse Small changes (see comments)
void backDcm(float d, int pwr) { float dia = 5.6; float pi = 3.14; float circ = pi * dia; float deg = (360 / circ) * d; resetMotorEncoder(motorC); while(getMotorEncoder(motorC) > -deg) {//notice the //greater than sign and negative sign motor[motorC] = -pwr; //notice the negative sign motor[motorB] = -pwr; //notice the negative sign } motor[motorC] = 0; motor[motorB] = 0;

15 An Easy Way to Turn Right
Notice the (float d, int pwr) on line 1 void turnRight(int deg, int pwr) {//deg is encoder degrees resetMotorEncoder(motorB); //only motorB is turning //While the encoder for the motorB is less than deg while(getMotorEncoder(motorB) < deg) {//move until requested deg motor[motorC] = 0; //Wheel does not turn motor[motorB] = pwr; //Wheel turns at power pwr } motor[motorC] = 0; motor[motorB] = 0; } //Ends the function

16 An Easy Way to Turn Left Notice the (float d, int pwr) on line 1
void turnLeft(int deg, int pwr) {//deg is encoder degrees resetMotorEncoder(motorB); //only motorC is turning //While the encoder for the motorC is less than deg while(getMotorEncoder(motorC) < deg) {//move until requested deg motor[motorC] = pwr; //Wheel turns at power pwr motor[motorB] = 0; //Wheel does not turn } motor[motorC] = 0; motor[motorB] = 0; } //Ends the function

17 Putting it all Together (or Shall We Dance?)
If we add all the functions above the task main() then we call call them whenever we like void moveDcm(float d, int pwr) {…} void backDcm(float d, int pwr) {…} void turnLeft(int deg, int pwr) {…} void turnRight(int deg, int pwr) {…} task main() { moveDcm(10, 50); //Moves the robot forward turnLeft(720, 50); //a small left turn turnRight(720, 50); //a big right turn backDcm(12.5, 25); //Moves the robot backwards }

18 The Beauty of Functions
Saves Time!!!!!!! Makes code readable Reduces amount of code needed Allows the same code to do different things Helps reduce debug time When the function works, it works, you don’t have to worry about it anymore An improvement to a function is propagated throughout the code

19 A Couple of Other Things
useCamelCaseForFunctionNames Camel Case starts with a lower case letter but each word after is capitalized Use names that make sense but are not to long Do the same for file names (you may like to capitalize the first letter)

20 You Try It Make your robot move in a square
Add new reverseLeftTurn and reverseRightTurn functions Make your robot go somewhere and return to where it started (be creative) Make your robot dance (

21 Congratulations!!! I am assuming that you have a functioning EV3 with ROBOTC I am assuming that you were able to make your robot move according to the instructions in the previous slide


Download ppt "Robotics Week 4 Functions and reusable code"

Similar presentations


Ads by Google