INTRODUCTION TO ROBOTICS Part 5: Programming Robotics and Automation Copyright © Texas Education Agency, 2013. All rights reserved. 1.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Lab7: Introduction to Arduino
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 1: Overview of Computers & Programming
CS 111: Introduction to Programming Midterm Exam NAME _________________ UIN __________________ 10/30/08 1.Who is our hero? 2.Why is this person our hero?
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Microprocessor or Microcontroller Not just a case of “you say tomarto and I say tomayto” M. Smith, ECE University of Calgary, Canada.
16/13/2015 3:30 AM6/13/2015 3:30 AM6/13/2015 3:30 AMIntroduction to Software Development What is a computer? A computer system contains: Central Processing.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Microprocessor or Microcontroller Not just a case of “you say tomarto and I say tomayto” M. Smith, ECE University of Calgary, Canada.
Railway Foundation Electronic, Electrical and Processor Engineering.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Railway Foundation Electronic, Electrical and Processor Engineering.
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.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
Engineering 1040: Mechanisms & Electric Circuits Fall 2011 Introduction to Embedded Systems.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
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.
CS102 Introduction to Computer Programming
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex.
Lesson 3 — How a Computer Processes Data
1.1 1 Introduction Foundations of Computer Science  Cengage Learning.
Topics Introduction Hardware and Software How Computers Store Data
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Lesson 2 — How Does A Computer Process Data?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
CPS120: Introduction to Computer Science Decision Making in Programs.
Lesson 3 — How a Computer Processes Data Unit 1 — Computer Basics.
Basic of Programming Language Skill Area Computer System Computer Program Programming Language Programmer Translators.
Introduction 2 Smart Design. What is smart design? 2 Everything is designed except nature The packaging of a Band-Aid The curve of a bike frame The shape.
CS Computer Science I. BCPL was developed in 1967 as a language for writing operating systems and software compilers In 1970, the creators of the.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Concepts of Engineering and Technology Copyright © Texas Education Agency, All rights reserved.
Microcontrollers, Microcomputers, and Microprocessors
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Introduction to Computer Programming using Fortran 77.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
IC 3 BASICS, Internet and Computing Core Certification Computing Fundamentals Lesson 2 How Does a Computer Process Data?
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Chapter 1: Introduction to Computers and Programming
INTRODUCTION TO ROBOTICS Part 5: Programming
BASIC PROGRAMMING C SCP1103 (02)
Val Manes Department of Math & Computer Science
Arduino & its hardware interfacing
BASIC PROGRAMMING C SCP1103 (02)
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Chapter 1: The 8051 Microcontrollers
Chapter 1: Introduction to Computers and Programming
Computers: Hardware and Software
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Introduction to Arduinos
Course Code 114 Introduction to Computer Science
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

INTRODUCTION TO ROBOTICS Part 5: Programming Robotics and Automation Copyright © Texas Education Agency, All rights reserved. 1

“We live in a society exquisitely dependent on science and technology, in which hardly anyone knows anything about science and technology” – Carl Sagan, 1990 This statement was made before the Internet and cell phones. It has only become worse since then. Copyright © Texas Education Agency, All rights reserved. 2

Technology and Computers Technology has become very easy to use. But just beneath the surface, most technology is incredibly complex. Computers are literally the most powerful problem-solving tools humanity has ever created. We all benefit when the knowledge of how to apply those tools is more widely understood in our society. Copyright © Texas Education Agency, All rights reserved. 3

Coding and Solving Problems The ability to code is not just a way to understand technology. The ability to code is a mode of thought and a language in itself—the same as literacy or mathematics. Coding involves critical thinking, problem solving, analysis, and logic. It is not about writing code; it is about solving problems. Copyright © Texas Education Agency, All rights reserved. 4

It is time to open up the black box! Copyright © Texas Education Agency, All rights reserved. 5

Program Requirements Programming requires Something that will run the program o A computer o A microprocessor o A microcontroller (what we will use!) A computer program that lets a user write a computer program o An IDE Copyright © Texas Education Agency, All rights reserved. 6

Integrated Development Environment A computer program used to write a computer program o Allows students to write a computer program that can be downloaded from any PC o Allows users to write code in a language that is easier to use than the machine level code In this case, C and C++ o Converts the higher level language to the machine code that is specific to a family of microprocessors o Allows the microcontroller to perform the instructions and to interact with the outside world Copyright © Texas Education Agency, All rights reserved. 7

IDE Components Editor Assembler/ Compiler Simulator Debugger Programmer (optional) Emulator (optional) An IDE is specific to a device or a family of devices made by a particular manufacturer. Copyright © Texas Education Agency, All rights reserved. 8

Choosing a Platform Robots require programming and instructions to operate. o When they face a situation outside of their program, they can stop, fail, or crash. This makes the microcontroller the key piece of a robot. There are generic ways of learning about robots, but generic descriptions can only go so far for teaching. With generic descriptions, you reach a limit on the amount of learning and understanding. You need specific examples, and for specific examples we need to decide on a platform. Copyright © Texas Education Agency, All rights reserved. 9

What is a Microcontroller? A microcontroller is an entire computer on a chip. o RAM, ROM, I/O, port controllers, converters, timers, and CPU are integrated as a single system. Self contained and sometimes on a single chip Single purpose Does not require much processing power The operating system is simple, contained in ROM, and has a built in instruction decoder for the language used. They are relatively simple and easy to use. Copyright © Texas Education Agency, All rights reserved. 10

Microcontrollers The world is becoming increasingly electronic, so some type of understanding about how electronics actually work is becoming more important. One of the key advantages of a microcontroller is that people can program and use them. Microcontrollers go beyond theory because one can quickly enable them to produce the same real world outputs seen everyday. At the same time, there is a depth to microcontroller knowledge and application that is virtually unlimited. They can be used to create more powerful applications than the individual circuits alone. Copyright © Texas Education Agency, All rights reserved. 11

Choosing a Microcontroller We compile a list of features and criteria we want. o The microcontroller’s cost must be low while including a development board (below $50). o It must be easy to use and well supported. It is important to have a lot of documentation readily available. o It should be programmed in a useful and common language. o It must be popular and have an active user community. o Since the robot will be used as a general purpose platform, the microcontroller should be very feature rich in order to allow for broad experimentation. In this sense, it should have several analog and digital pins, as well as an integrated voltage regulator. Copyright © Texas Education Agency, All rights reserved. 12

Choosing a Microcontroller There are many fine examples of microcontrollers to choose from. There are a number of very good and popular high-level programming languages including C, C++, C#, Basic, and more. Taking all these factors into consideration, one should choose a microcontroller that o is inexpensive, o uses the C programming language, and o has a variety of inexpensive and available add-on boards. Copyright © Texas Education Agency, All rights reserved. 13

Open Source vs. Proprietary Choose hardware and software that are open source, which means o the boards are inexpensive and o the software is simple and flexible. Choose open-source technology with a list of public domain resources including o libraries, o working examples, and o experienced programming support. Proprietary software and hardware is controlled, limited, and expensive. Copyright © Texas Education Agency, All rights reserved. 14

Why Not a PC? A PC is expensive o hundreds of dollars vs. $30 A PC uses a lot of electrical power o 600 W vs. <.5 W A PC is not portable o uses AC not DC o is heavy and bulky A PC requires many peripheral components o keyboard, mouse, RAM, monitor, hard drive Many different types of connections to make o SATA, PATA, PCI, PCIe, VGA, USB, serial, parallel Copyright © Texas Education Agency, All rights reserved. 15

Microcontrollers vs. PC One of the key features of PC microprocessors is that they have to be backward compatible o This adds to expense and complexity o Motherboards in particular are complex and expensive Microcontrollers have fewer and simpler connections Copyright © Texas Education Agency, All rights reserved. 16

Applied Programming Start with a simple example Interface a simple device to the microcontroller The computer program provides that interface Combine physical, electrical, and software Copyright © Texas Education Agency, All rights reserved. 17

Back to the Bumper Switch A bumper switch produces an output voltage whether it is open or closed. o Zero volts is recorded as a binary zero (false). o +5 volts is recorded as a binary one (true). The values are stored as a variable. A program loop reads the variable value thousands of times per second. The true or false conditions are evaluated by the conditional loop. A program function is executed either way. Copyright © Texas Education Agency, All rights reserved. 18

The Worlds Simplest Program This program has every required element needed to run in a microcontroller, with proper syntax and formatting. However, it will not actually do anything constructive. void setup() { // put your setup code here, to run once; } void loop() { // put your main code here, to run repeatedly; } Copyright © Texas Education Agency, All rights reserved. 19

Program Parts 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, All rights reserved. 20

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

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, All rights reserved. 22

Symbols Braces { }, or curly brackets, go around executable program sections called functions. o Both an opening brace (or left brace) and a closing brace (or right brace) are needed. The semicolon ( ; ) ends a program statement or command. o An easy way to get a syntax error is to forget to use a semicolon after a command. Copyright © Texas Education Agency, All rights reserved. 23

More Symbols The parenthesis after each instruction is required and tells the program that the name used is a function and not something else. o A variable The word “void” tells the program that the functions setup () and loop () do not return any values or data. o This word is required or the program will hang up waiting for data. Copyright © Texas Education Agency, All rights reserved. 24

Keywords Keywords are words and names that are already defined in the C++ language to perform specific operations. o They must be used correctly for the program to work at all. o Failure to use symbols and keywords correctly results in something called a syntax error. You can define your own keywords. o They must be defined before you use them in a program. o It is done by naming a procedure. o It creates what is called an abstraction. Copyright © Texas Education Agency, All rights reserved. 25

Abstractions The main purpose of this part of the lesson is to introduce functions, statements, loops, variables, comments, and the formatting symbols that make up the syntax. We will use abstractions in our programming examples. This is done primarily to simplify the program. Copyright © Texas Education Agency, All rights reserved. 26

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); } Copyright © Texas Education Agency, All rights reserved. 27

Variables A variable must be defined before you can use the variable (example: to store or read data), during initialization, before the main program loop. Anytime you have a condition to evaluate you have data. Data requires a name and a memory location. o Defining a variable does both of those things. Data size is both how big the number can be and the amount of memory used to store it. o Char = 8 bits (0-255), int = 16 bits (0-65,535) Copyright © Texas Education Agency, All rights reserved. 28

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); } Assign variables Int = 16 bits Copyright © Texas Education Agency, All rights reserved. 29

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); } We like to use names for our pins Copyright © Texas Education Agency, All rights reserved. 30

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); } And for our data Copyright © Texas Education Agency, All rights reserved. 31

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 o generating video while waiting for user input, o driving around until bumping into an object, or o performing a particular task only when a specific condition is present. Most programs involve one or more loops Copyright © Texas Education Agency, All rights reserved. 32

Types of Loops Infinite loops o Usually the main program loop o Can also be the result of a programming error o Example: whilewhile Counting loops o Performs an action a specified number of times o Example: forfor Conditional loops o Performs a specific action as a result of a specific input o Example: if, elseif, else Copyright © Texas Education Agency, All rights reserved. 33

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); } Copyright © Texas Education Agency, All rights reserved. 34

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) Copyright © Texas Education Agency, All rights reserved. 35

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 Copyright © Texas Education Agency, All rights reserved. 36

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); } Statements are computer commands terminated with a semicolon ( ; ) Copyright © Texas Education Agency, All rights reserved. 37

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); } These are called functions Copyright © Texas Education Agency, All rights reserved. 38

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, All rights reserved. 39

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, All rights reserved. 40

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, All rights reserved. 41

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); } We want to set pin 7 as an input Copyright © Texas Education Agency, All rights reserved. 42

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, All rights reserved. 43

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); } Get the value on pin 7 many times as we drive around Copyright © Texas Education Agency, All rights reserved. 44

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); } Copyright © Texas Education Agency, All rights reserved. 45

Second Example What if we want to read data from an analog sensor? An analog sensor has many values over a range, such as temperature. We only need to change a few commands from the previous example. Copyright © Texas Education Agency, All rights reserved. 46

int analogdata = 0; analogdata = GetAnalogInput (7); Use a new name Copyright © Texas Education Agency, All rights reserved. 47

int analogdata = 0; analogdata = GetAnalogInput (7); Use a different command to read analog data Copyright © Texas Education Agency, All rights reserved. 48

Analog Data Program int switchPin = 7; // the number of the pushbutton //pin int analogdata = 0; // variable for reading the switch //value void setup() { // initialize the pushbutton pin as an input: pinMode(switchPin, INPUT); } void loop() { // read and store the analog value: analogdata = GetAnalogInput(switchPin); } Copyright © Texas Education Agency, All rights reserved. 49

Analog vs. Digital Input Not much difference in how you get the data Huge difference in what you can do with it Binary only has two values and you can only do two things with it Analog has many values and you can do many things Copyright © Texas Education Agency, All rights reserved. 50

Analog Input This means many decisions and potentially much more complicated programming. Use less than or greater than mathematics, more than equals. Copyright © Texas Education Agency, All rights reserved. 51

Learn By Doing You have been shown how to get started in programming. From here, the only way to learn is to learn by doing! Copyright © Texas Education Agency, All rights reserved. 52