INTRODUCTION TO ROBOTICS Part 5: Programming

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

Lab7: Introduction to Arduino
CS 111: Introduction to Programming Midterm Exam NAME _________________ UIN __________________ 10/30/08 1.Who is our hero? 2.Why is this person our hero?
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Chapter Introduction to Computers and Programming 1.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Introduction to the Arduino
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
1 CS161 Introduction to Computer Science Topic #9.
INTRODUCTION TO ROBOTICS Part 5: Programming Robotics and Automation Copyright © Texas Education Agency, All rights reserved. 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Microcontrollers, Microcomputers, and Microprocessors
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Development Environment
Chapter 10 Programming Fundamentals with JavaScript
User-Written Functions
Val Manes Department of Math & Computer Science
CSC201: Computer Programming
Topics Introduction Hardware and Software How Computers Store Data
Arduino & its hardware interfacing
Introduction to Programming
Chapter 2 - Introduction to C Programming
Topics Introduction to Repetition Structures
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Variables, Expressions, and IO
Introduction to Arduino Microcontrollers
Chapter 2 - Introduction to C Programming
TRANSLATORS AND IDEs Key Revision Points.
Chapter 10 Programming Fundamentals with JavaScript
Automation and Robotics
Chapter 2 - Introduction to C Programming
While Loops and If-Else Structures
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Number and String Operations
Chapter 2 - Introduction to C Programming
Topics Introduction Hardware and Software How Computers Store Data
While Loops and If-Else Structures
Chapter 2 - Introduction to C Programming
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
Automation with RobotC
While Loops and If-Else Structures
Chapter 2 - Introduction to C Programming
Automation with RobotC
Lab #1: Getting Started.
While Loops and If-Else Structures
Setting up a basic program with Arduino
Introduction to C Programming
UNIT 1 First programs.
While Loops And If-Else Structures
Week 1 - Friday COMP 1600.
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

INTRODUCTION TO ROBOTICS Part 5: Programming Robotics and Automation

Copyright © Texas Education Agency, 2013. All rights reserved. Program Requirements Programming requires 1. Something that will run the program A computer A microprocessor A microcontroller (what we will use!) 2. A computer program that lets a user write a computer program RobotC Another requirement is a way to deliver (download) the program from the PC to the device. This is usually some type of cable, but the connection can also be wireless (Bluetooth/WiFi). Copyright © Texas Education Agency, 2013. All rights reserved.

What is a Microcontroller? A microcontroller is an entire computer on a chip. RAM, ROM, I/O, port controllers, converters, timers, and CPU are integrated as a single system. A microprocessor is a general purpose, multi-function device, but its primary job is to execute instructions. It will run many different programs that will seem to be running all at the same time. A microcontroller has many hardware parts built in, but is designed to run only one program at a time. It will not run more than one program. If you focus on how many functions are built into a microcontroller you might think it would be more powerful and more expensive than a microprocessor, but it is actually much simpler and much less expensive. Many microcontrollers use old and obsolete microprocessors as the CPU. Consider the fact that new microprocessors run at 3 GHz, while a newer one runs at 16 MHz. Converters include analog to digital and digital to analog.

Copyright © Texas Education Agency, 2013. All rights reserved. A Program void setup() {   // put your setup code here, to run once; } void loop() {   // put your main code here, to run repeatedly;  } The program is shown as an example of the proper format and syntax. It is a starting point. We are now going to go through a process of modifying this program to make it read a switch in a robot. This program has every required element needed to run in a microcontroller, with proper syntax and formatting. Copyright © Texas Education Agency, 2013. All rights reserved.

Sample Program These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Copyright © Texas Education Agency, 2013. All rights reserved. Program Parts This is an example from an IDE. Normal C++ has only one required function, the main () function. However, the main function does not show the important concept that programs have different parts that do different things and can be written in stages. The first thing to notice is the program has two parts A part that runs once A part that runs over and over again Copyright © Texas Education Agency, 2013. All rights reserved.

Copyright © Texas Education Agency, 2013. All rights reserved. Syntax The next thing to notice is the use of formatting symbols in the program. These symbols form what is called the syntax, which is the language the program uses. Copyright © Texas Education Agency, 2013. All rights reserved.

Copyright © Texas Education Agency, 2013. All rights reserved. Syntax Each symbol has a particular meaning. Some of the symbols are required for the program to be compiled and executed correctly. Others define parts of a program such as statements and functions. Copyright © Texas Education Agency, 2013. All rights reserved.

Comments Best Coding Practices includes comments to describe what each section of the code does in easy to understand English terms.

It has only become worse as far as normal people knowing technical details about how their devices and technology actually work. It is better in terms of our quality of life and how these devices have made our lives easier and better.

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Variables In software, a holder whose value can be changed by the program itself is called a VARIABLE. A variable must be defined before you can use the variable (example: to store or read data), during initialization, before the main program loop.

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Syntax In order statement to be executed, it must end in a semicolon (;).

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Copyright © Texas Education Agency, 2013. All rights reserved. Symbols Braces { }, or curly brackets, go around executable program sections called functions. Both an opening brace (or left brace) and a closing brace (or right brace) are needed. The semicolon ( ; ) ends a program statement or command. An easy way to get a syntax error is to forget to use a semicolon after a command. There are also square braces: [ ]. Square braces perform a different function and are not used the same way as curly brackets, so we do not typically use the term “braces” without being more specific. A statement is usually only one line of a program and a function is usually many lines (or statements). Copyright © Texas Education Agency, 2013. All rights reserved.

Functions An instruction within a larger computer program is a called a FUNCTION. The parenthesis () after each instruction is required and tells the program that the name used is a function and not something else.

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Argument A value that you pass to a routine is a called a ARGUMENT.

These general concepts are from Coding Horror, by Jeff Atwood These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Example Program These general concepts are from Coding Horror, by Jeff Atwood. Reference Atwood, J. (2012, May 15). Please don't learn to code. In Coding Horror: Programming and Human Factors. Retrieved from http://www.codinghorror.com/blog/2012/05/please-dont-learn-to-code.html

Copyright © Texas Education Agency, 2013. All rights reserved. Example Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); This is a program that can read a switch value (open or closed). Later, we will want to use that value to make a decision. Int is a keyword that means store a piece of data as 16 binary bits. switchpin and switchdata are the names we made up for those pieces of data (we are defining the data pin to read as pin 7 and assigning the switchdata initial value as 0). pinmode is a keyword. GetDigitalInput is an abstraction (and purely imaginary). The equals sign reads (or assigns) the value of pin 7 (which can only be high or low, 1 or 0) to the data location called switch data. Copyright © Texas Education Agency, 2013. All rights reserved.

Copyright © Texas Education Agency, 2013. All rights reserved. Program Loops A program (or program segment) that runs the same section of code over and over Necessary when waiting for an input while performing other tasks like generating video while waiting for user input, driving around until bumping into an object, or performing a particular task only when a specific condition is present. Most programs involve one or more loops A microcontroller will execute thousands of instructions every second. The commands inside the loop (to read data and then perform an action based on that data) can range from only a few commands to hundreds, but when using a loop once and the list of commands have been executed, the program loops back to the beginning and runs the same loop again, over and over as long as the program runs. For example, if a microcontroller can execute 20,000 instructions per second (a conservative estimate), and the loop instructions, which include reading the sensor, has 100 statements, that means the loop will run 200 times per second, and the sensor will be read (and a decision made) 200 times per second. Performing a “print” function is an example of a program loop (subroutine) that is run only when asked (hit the print button). Copyright © Texas Education Agency, 2013. All rights reserved.

Copyright © Texas Education Agency, 2013. All rights reserved. Types of Loops Infinite loops Usually the main program loop Can also be the result of a programming error Example: while Counting loops Performs an action a specified number of times Example: for Conditional loops Performs a specific action as a result of a specific input Example: if, else These are just general examples. Specific examples depend on the programming language. Copyright © Texas Education Agency, 2013. All rights reserved.

These are comments (not executed) Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); These are comments (not executed) Comments can use the double slash at the beginning of some text, but the comment only lasts for the rest of that line. If you want a comment to cover more than one line, you have to use a different symbol: /* to start the comment and */ to end the comment. This works in HTML also. Do not put any statements between these because they will not be executed. Copyright © Texas Education Agency, 2013. All rights reserved.

Copyright © Texas Education Agency, 2013. All rights reserved. Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); These are statements GetDigitalInput is an abstraction. Other terms are either defined here or are keywords. Copyright © Texas Education Agency, 2013. All rights reserved.

Statements are computer commands terminated with a semicolon ( ; ) Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Statements are computer commands terminated with a semicolon ( ; ) Copyright © Texas Education Agency, 2013. All rights reserved.

These are called functions Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); These are called functions Copyright © Texas Education Agency, 2013. All rights reserved.

Functions are enclosed in brackets Program Description int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Functions are enclosed in brackets These are called curly brackets to distinguish them from square brackets. Copyright © Texas Education Agency, 2013. All rights reserved.

Functions are enclosed in brackets Improved Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Functions are enclosed in brackets Copyright © Texas Education Agency, 2013. All rights reserved.

Do the stuff in this function once Improved Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Do the stuff in this function once Copyright © Texas Education Agency, 2013. All rights reserved.

Do the stuff in this function over and over again Improved Program int switchPin = 7; // the number of the pushbutton //pin int switchdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read the state of the pushbutton value: switchdata = GetDigitalInput(switchPin); Do the stuff in this function over and over again Copyright © Texas Education Agency, 2013. All rights reserved.