150 likes | 261 Views
Introduction to Computer Science – Chapter 5. CSc 2010 Spring 2011 Marco Valero. Overview. Exteroceptor overview Camera sensor Light sensors Proximity sensors Lists Gamepad WWW Function redux. Exteroceptor Overview. External sensors ( exteroceptor ) Camera
E N D
Introduction to Computer Science – Chapter 5 CSc 2010 Spring 2011 Marco Valero
Overview • Exteroceptor overview • Camera sensor • Light sensors • Proximity sensors • Lists • Gamepad • WWW • Function redux
Exteroceptor Overview • External sensors (exteroceptor) • Camera • Capable of taking still pictures • Light sensor • Three light sensors on front of the robot can detect brightness • Three light sensors on Fluke card as well • Proximity sensor • IR sensors on robot as well as obstacle sensors on the Fluke card
The Camera • Camera is located on the front of the Fluke dongle • takePicture(<option>) • <option> = “color”, “gray”, or empty • show(picture) • Displays the picture • savePicture(picture, filename) • Saves the picture to disk • Taking grayscale photos is easier than taking color
Light Sensing • getLight() returns a list of all three values of light sensors • getLight(<position>) returns a value of the given position • <position> = ‘left’ | ‘center’ | ‘right’ | 0 | 1 | 2 • Low values imply bright light
Light Sensing • Camera can also be used to sense light with the getBright() function • getBright(<position>) • <position> = ‘left’ | ‘center’ | ‘right’ | 0 | 1 | 2 • Higher the value, the brighter the light • Normalizing the values relative to motion is a useful idea
Proximity Sensing • Two IR sensors on the robot and three on the card • Robot has getIR() and getIR(<position>) • <position> = 0 | 1 | ‘left’ | ‘right’ • Boolean values determining whether path is clear or not • 1 implies nothing impedes path, 0 implies something does • getObstacle() and getObstacle(<position>) • <position> = 0 | 1 | 2 | ‘left’ | ‘center’ | ‘right’ • Values from 0 to 7000, with 0 implying nothing in sensor’s path and high numbers implying the presence of an object
Lists • List is a sequence of objects • Numbers, letters, strings, images • We have seen lists before with the range function • Lists can contain no items as well • []
List Operations • len(N) • Returns the length of the list, N • Index a list: list[index] • Returns the element located at index, where index is a number 0 or higher • If a list contains n elements, the last index is n – 1 • Slice operator: list[begin:end] • Returns a view of the list that starts at the beginning positions and is less than the end • Concatenate lists: + • mynewlist = mylist + myotherlist combines the two lists into a new list
List Operations • Find an element: in • 3 in [0, 1, 2, 3] , ‘city’ in [‘car’, ‘dog’, ‘house’] • Sort a list • Mylist.sort() • Reverse a list • Mylist.reverse() • Add to a list • Mylist.append(item)
Strings, a character sequence • We can iterate through sequences of characters just like a list ABC = “ABCDEFGHIJKLMNOPQRSTUVWXYZ” for letter in ABC: speak(letter) • String operation split • splits a string based on space as the separator and returns a list of strings • Sentence = “Obviously you’re not a golfer” • Words = Sentence.split()
Gamepad • We can use a gamepad to control the robot • getGamepad(<device>) • Returns the value of the device after input is received • getGamepadNow(<device>) • Immediately returns the value of the device • Axis values are from -1.0..1.0 • Button values or on/off or 1/0
WWW • We can use python modules to retrieve data from the web from urllib import * Data = urlopen(http://cs.gsu.edu/~nmancuso1/data.txt) print Data.read()
Functions Redux def <identifier>(<parameters>): <expression-list> • Python returns a value for all functions, whether explicitly or implicitly def cube(x): return x*3 • return <expression>