Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "INTRODUCTION TO ROBOTICS Part 5: Programming Robotics and Automation Copyright © Texas Education Agency, 2013. All rights reserved. 1."— Presentation transcript:

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

2 “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, 2013. All rights reserved. 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

22 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. 22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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); } Functions are enclosed in brackets Copyright © Texas Education Agency, 2013. All rights reserved. 40

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); } Do the stuff in this function once Copyright © Texas Education Agency, 2013. All rights reserved. 41

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

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); } Do the stuff in this function over and over again Copyright © Texas Education Agency, 2013. All rights reserved. 43

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

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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google