Python Programming Module 4 Sensors and Loops Python Programming, 2/e1.

Slides:



Advertisements
Similar presentations
Robofest 2005 Introduction to Programming RIS 2.0 RCX Code.
Advertisements

Sensors.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
BEGINNER EV3 PROGRAMMING Lesson
Introduction to Sensors
EIGHTH GRADE ROBOTICS KITTATINNY REGIONAL HIGH SCHOOL MR. SHEA Introduction to Robotics Day4.
Session 2  The Touch Sensor  The Colour Sensor.
The NXT is the brain of a MINDSTORMS® robot. It’s an intelligent, computer-controlled LEGO® brick that lets a MINDSTORMS robot come alive and perform.
LEGO Mindstorms NXT Programming We will be using the Common Palette for our Robots This is how you download your program onto the brick Drag and drop a.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Testbed: Exercises.
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.
Program ultrasonic range sensor in autonomous mode
GIRLS Robotic Camp. Let’s Begin Meet and Greet – Camp leaders introduce themselves – Students introduce themselves.
Programming 101 The Common Palette Content provided by Connor Statham (6 th Grade Student) Formatting by Shannon Sieber.
Unit 5 – “Watch Out!”. Introduction New Topics Case Structures New Functions Less? Comparison Function Ultrasonic Sensor.
LabVIEW Program and Data Flow Review LabVIEW Robotics Fundamentals.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
LabVIEW Basics Review LabVIEW Robotics Fundamentals.
Today’s Agenda 1.Scribbler Program Assignment 1.Project idea due next class 2.Program demonstration due Wednesday, June 3 2.Attendance & lab pair groupings.
EV3 Workshop Oct 3, 2015 Instructor: Chris Cartwright
The George Washington University Department of ECE ECE Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.
Algorithms Writing instructions in the order they should execute.
Robotics NXT-G: variables, file Rotation sensor Lab: Use buttons to hit specific ball. Homework: Postings. Start planning mapping the room.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Oregon Robotics Tournament and Outreach Program RCX Basics.
Programming 101 The Common Palette Content provided by Connor Statham (9 th Grade Student) Formatting by Shannon Sieber.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Mindstorm NXT-G Introduction Towson University Robotics.
BEGINNER FLL PROGRAMMING WORKSHOP BY DROIDS ROBOTICS & EV3LESSONS.
Introduction to Robots and the Mind - Programming with Sensors - Bert Wachsmuth & Michael Vigorito Seton Hall University.
Lego Mindstorm Robots 9797 kit.  Students will learn how to identify how to detect a change in a condition.  Students will learn where and how to identify.
Forward Until Near Stop when near a wall.
Python Programming Module 3 Functions Python Programming, 2/e1.
USING SWITCHES TO PROGRAM A CANDY SORTER DESIGNED FOR USE WITH LMS EV3 PROGRAMMING AND BUILDING ENVIRONMENT.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
EV3 Attachments and Sensors. Ultrasonic Sensor  Purpose: detect distance from an object.  What it does: Generates sound waves and reads their echoes.
The George Washington University Department of ECE ECE Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.
LEGO Robotics Workshop
Introduction to Programming in RobotC
By Sanjay and Arvind Seshan
Exploring Computer Science Lesson 6-5
BEGINNER EV3 PROGRAMMING Lesson
Python: Control Structures
By Sanjay and Arvind Seshan
IF statements.
Touch Sensor.
Introduction To Flowcharting
Introduction To Robot Sensors
Using Switches to Program A candy Sorter
BEGINNER PROGRAMMING LESSON
INTERMEDIATE PROGRAMMING LESSON
Sensors Training.
BEGINNER EV3 PROGRAMMING Lesson
Motors and Sensors Large Motor
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
INTERMEDIATE PROGRAMMING LESSON
Exploring Computer Science Lesson 6-5
BEGINNER PROGRAMMING LESSON
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Storing Values as Variables
BEGINNER PROGRAMMING LESSON
Using Switches to Program A candy Sorter
3. Decision Structures Rocky K. C. Chang 19 September 2018
Computer Science Core Concepts
BEGINNER PROGRAMMING LESSON
Compiled from various Internet sources Presented by Mr. Hatfield
Lego MINDSTORMS EV3.
BEGINNER PROGRAMMING LESSON
Oregon Robotics Tournament and Outreach Program
Presentation transcript:

Python Programming Module 4 Sensors and Loops Python Programming, 2/e1

Sensors In addition to the temperature sensor we have the following sensors: – Touch Sensors (2) – a button that can tell when it is pressed. – Color/Light Sensor – an optical sensor that senses ambient light or reflected color. – Ultrasonic Sensor – uses echo location to determine its distance from an object. – Gyro Sensor – uses accelerometers to measure its rotation about a particular axis. Python Programming, 2/e2

Attaching Sensors You must plug the sensors into the input ports on the EV3 brick. These are the ports labeled 1, 2, 3 and 4. When you name a sensor in Python you can auto detect the sensor. touch=TouchSensor() Python Programming, 2/e3

Attaching Sensors You may need to specify the port if you are using the Temperature Sensor or if you are using more than one of the same type of sensor. touchL = TouchSensor(‘in1’) touchR = TouchSensor(‘in4’) The input ports are named ‘in1’, ‘in2’, ‘in3 ’ and ‘in4’. Python Programming, 2/e4

Reading Sensors Once you have the sensor attached and named you can read what it is currently sensing using its value function. u = UltrasonicSensor() u.value() This will report a distance, but what are the units? The sensors all have different modes that determine the units used. Python Programming, 2/e5

Setting Sensors Modes To determine the units you can do the following: u = UltrasonicSensor() u.mode = ‘US-DIST-CM’ u.value() Now the results will be in 10 th of a centimeter (millimeter). If the value is 324 this corresponds to 32.4 cm. Python Programming, 2/e6

Sensor Modes Touch Sensor – no modes. The value is 0 when the button is not pushed and 1 when pushed. Ultrasonic Sensor – the value is a distance. – ‘US-DIST-CM’ – distance measured in 10 th of a centimeter – ‘US-DIST-IN’ – distance measured in 10 th of an inch. Python Programming, 2/e7

Sensor Modes Gyro Sensor – the value is a rotation. – ‘GYRO-ANG’ – measures the relative number of degrees rotated since the sensor was initialized. – ‘GYRO-RATE’ – measures the rate at which the sensor is rotating in degrees/second. Python Programming, 2/e8

Sensor Modes Color Sensor – measures light intensity and color. – ‘COL-REFLECT’ – measures reflected light intensity – ‘COL-AMBIENT’ – measures ambient light intensity – ‘COL-COLOR’ – measures color 0 none 3 green6 white 1 black 4 yellow7 brown 2 blue5 red Python Programming, 2/e9

Sensors and Motors Interacting We now know how to run motor and how to read a value from a sensor. We need to figure out how to write programs where the value read by the sensor will determine what action the motors take. We need to be able to control what instructions are done and when they are done. Python Programming, 2/e10

Flow Control Python Provides three basic structures to control the order in which program statements are executed. – if – else structure – while loop – for loop We will look at each of these but let’s start with the while loop. Python Programming, 2/e11

While Loops A while loop repeats some block of code (called its body) over and over again until a particular condition becomes false. For example: Drive the robot forward while the distance from the wall is greater than 5 cm. – i.e. drive until you are 5 cm from the wall. Notice we do not know how long this will take, so this is called an indefinite loop. Python Programming, 2/e12

Indefinite Loops while : is a Boolean Statement that is either true or false. The body is a sequence of one or more statements. The of the loop executes repeatedly as long as the condition remains true. When the condition is false, the loop terminates. Python Programming, 2/e13

Indefinite Loops The condition is tested at the top of the loop. This is known as a pre-test loop. If the condition is initially false, the loop body will not execute at all. Python Programming, 2/e14

Boolean Statements A Boolean statement is one that is either true or false. It is usually constructed by comparing two mathematical expressions. A simple comparison might look like the following: Here is short for relational operator. Python Programming, 2/e15

PythonMathematicsMeaning <<Less than <=≤Less than or equal to ===Equal to >=≥Greater than or equal to >>Greater than !=≠Not equal to Python Programming, 2/e16 Relational Operators

Check for Equality Notice the use of == for equality. Since Python uses = to indicate assignment, a different symbol is required for the concept of equality. A common mistake is using = in conditions when you really want == ! Python Programming, 2/e17

Forming Simple Conditions Boolean conditions are of type bool and the Boolean values of true and false are represented by the literals True and False. >>> 3 < 4 True >>> 3 * 4 < False The expressions on either side of the operator can contain variables and/or the results of functions. u.value() > 0.5 Python Programming, 2/e18

Driving and Stopping Using a sensor and two motors and a while loop we can drive our robot until we approach an object. What we will do is set both motor to run until they are stopped ( run_forever ). We will then enter a loop that will end when the robot is 5 cm from some obstruction. We don’t need to actually do any work in this loop so the body will be the placeholder command pass. After we get out of the loop we stop the motors. Python Programming, 2/e19

Driving and Stopping from ev3 import * motorL = LargeMotor('outB') motorR = LargeMotor('outC') ultra = UltrasonicSensor() ultra.mode = 'US-DIST-CM' motorL.run_forever(duty_cycle_sp = 50) motorR.run_forever(duty_cycle_sp = 50) while(ultra.value() > 50): pass motorL.stop() motorR.stop() Python Programming, 2/e20

Activities Write a program that will pause the robot while an obstruction is within 5 cm. When the obstruction is removed, the robot will begin driving and stop when it gets within 5 cm of another obstruction. Write a program that makes the robot run while the button on the touch sensor is pushed. Write a program that will cause the robot to begin moving when the color sensor “sees” green and stops the robot when it “sees” the red. Python Programming, 2/e21