Robotics Week 4 Functions and reusable code

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Team 5220 Roboknights. Outline  Getting Started  Hardware Setup/Wiring  Software Setup/Pragmas  Programming with RobotC  Grammar/Syntax  Basic Statements.
Graphical RobotC NXT (EV3) Robot Workshop 2015 Instructor: Dr. Fred Brauchler Assistant: Chris Parker 2/7/2015Lawrence Technological University1.
Right Face Introductory Presentation. Opening Activity How can you use this to make a right turn program? This is your program from Full Speed Ahead to.
IR SENSORS AND ENCODERS. LCDs Timothy Friez Class # 2.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Introduction to Python
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Overview of Project 3 Slides are available at : Updated 1/28 Due Date for project has been extended to next Friday 2/6.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
How to link the robot and the computer (Bluetooth) How to turn on and off How to connect the adaptor Fluke card connection Sec Getting Started How.
Wall Encounter By Made easy by Dwayne Abuel.
By Droids Robotics INTERMEDIATE PROGRAMMIN G LESSON MOVE DISTANCE MY BLOCK (MOVE_CM)
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
EV3 Software EV3 Robot Workshop
Available at: – Program Functions to Accept Values Program Functions to Accept Values.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
ROBOTC Software EV3 Robot Workshop
Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are.
ROBOTC Software EV3 Robot Workshop Lawrence Technological University.
Intro to Turtle Graphics (Part 2)
Python Programming Module 3 Functions Python Programming, 2/e1.
Variables. A variable is a space in your robot’s memory where you can store data, such as whole numbers, decimal numbers, and words. Variable names follow.
ROBOTC for CORTEX Teacher Training © 2011 Project Lead The Way, Inc. Automation and Robotics VEX.
Robotics Abstractions: Levels of language, world view
Introduction to Programming in RobotC
INTERMEDIATE PROGRAMMING Lesson
Mr Barton’s Maths Notes
User-Written Functions
Classes and Objects.
Chapter 6: User-Defined Functions I
Python Programming Module 3 Functions Python Programming, 2/e.
Teaching Characters to Walk: Learning Methods, Part 1
What you asked me to teach…
Web Design II Flat Rock Community Schools
Reviewing your Program
Robotics Abstractions: Levels of language, world view
Mr F’s Maths Notes Number 7. Percentages.
Variables, Expressions, and IO
PROMGRAMING YOUR ROBOT
Programming – Touch Sensors
INTERMEDIATE PROGRAMMING LESSON
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Number and String Operations
Sequencing Learning Objective: to be able to design algorithms that use sequencing.
8 + (-6) = ? = ?.
Language Basics.
Python Lessons 13 & 14 Mr. Kalmes.
Alice Variables Pepper.
Teaching Java using Turtles part 2
INTERMEDIATE PROGRAMMING LESSON
Functions In Matlab.
Maze Challenge Maze Challenge activity > TeachEngineering.org
Mr Barton’s Maths Notes
Chapter 6: User-Defined Functions I
Robotics Programming Using Shaft Encoders
Robotics Week 3 beginning wheel control
Robotics Week 6 line follower
Introduction to Primitives
Robotics Week 2 Getting Started Using the Display
Compiled from various Internet sources Presented by Mr. Hatfield
Python Lessons 13 & 14 Mr. Husch.
Variables in C Topics Naming Variables Declaring Variables
Putting all the Graphs Together
Getting started with LEGO EV3 Mindstorms software
Software Development Techniques
Presentation transcript:

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

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

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!!!

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. https://www.tutorialspoint.com/cprogramming/c_functions.htm

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)

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;

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()

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

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

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

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

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

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

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;

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

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

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 }

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

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)

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 (https://photos.app.goo.gl/qVcV5mfuTrch7PRn9)

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