26
PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Embed Size (px)

Citation preview

Page 1: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

PHY 235 Robotics Workshop

Day 4

Robotic Behaviors,Light Sensing, Voltage Dividers,

LCD-MOD

Page 2: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Robot Behavior

• Open Loop Behavior: In our first challenge, we programmed our robot to do a set of specific, deterministic behaviors (move forward, emit a sound, print a message). Each time our program runs, the robot (hopefully) does the same behavior. This is called Open Loop Behavior.

Page 3: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Robot Behavior

• Closed Loop Behavior: For most real-world activity, open-loop behavior is not agile enough to be successful. The robot must be able to adapt, or change, its behavior depending on input from sensors. This is called Closed Loop Behavior. (Or feed-back) We worked on one example of this kind of behavior in our touch sensing navigation program using whiskers. (like a cat)

Page 4: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing with Photoresistors

• Another type of closed-loop behavior can be achieved by using a light sensor (photoresistor).

• A Photoresistor is a light sensitive resistor.

Page 5: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing with Photoresistors

• Photoresistors are easy to use and inexpensive.

• As the name implies, a photoresistor is a resistor that reacts to light. The active ingredient Cadmium Sulfide (CdS) allows electrons to flow more easily when light energy hits it, thus lowering it resistance (opposition to current flow).

• The brighter the light the lower the resistance.

Page 6: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing Voltage Divider

• A voltage divider can be used to determine the state of a photoresistor.

Vout = Vin * R2/(R1 + R2)

Vin

Vout

Page 7: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing Voltage Divider

• As the sensor darkens, the resistance of the photoresistor will increase – thus, the voltage at p8 will increase.

p8

Page 8: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing Voltage Divider

• In our circuit, the 220 Ohm resistor (current limiter) can be ignored compared to the other resistors. We get:

• Vo = Vdd * 2K/(2K+R) = 5V * 2K/(2K+R)• 1K ≤ R ≤ 25K (Indoor light)• 3.3 ≥ Vo ≥ 0.4

(p8)

Page 9: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Analog VS Digital Signals

• Some of the I/O Pins on the ZX-24a microcontroller can read the analog value of an input signal.

• A digital signal can only be high (5volts or 1) or low (0 volts or 0).

• Analog signals can be any value between high and low.

• Pins p8-p15 (ZX-24 programming pin numbers 13-20) can read analog input signals.

Page 10: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Analog Input Function

• GetADC(pin) - this is a ZBasic function. A function is like a subroutine, but a function returns a value. In this case, GetADC returns an integer that represents the analog voltage.

• If integer adcVal is the return value of GetADC, then the formula for conversion to the actual voltage seen by the pin is

Vo = Vdd * adcVal/1024 Or, adcVal = int((Vo/Vdd) * 1024)

Page 11: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing Voltage Divider

• Voltage at Pin: 0.4 ≤ Vo ≤ 3.3• Pin Value: 82 ≤ adcVal ≤ 675• Light Value: Dark -- Bright

Page 12: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing Voltage Divider

• Testing: – Do ACTIVITY #1: BUILDING AND TESTING

PHOTORESISTOR CIRCUITS in our Boebot textbook. In the circuit shown in the text, use pins 8 and 9 instead of pins 3 and 6. Also, substitute the program on the next slide for the test program. (This is called “lightSensor” on the code web page)

– Run the program for testing. Shine a bright light on the photoresistors and then cover them up to see what the printed values are.

Page 13: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Light Sensing Programconst p8 as byte = 13 ' only pins 8-15 can be ADC inputs const p9 as byte = 14dim p8value as integer ' variable to store pin8 analog valuedim p9value as integer ' variable to store pin9 analog value

Sub Main()do

p8value = GetADC(p8) ' Get value of photoresistorp9value = GetADC(p9) ' Get value of photoresistordebug.print "p8 = "; p8value; " p9 = "; p9valuecall delay(0.5)

loopEnd Sub

Page 14: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Breadboard Area

A Breadboard is an electrical testing area for prototyping by quickly connecting components.

• The rows are electrically connected to make connectionsbetween devices.

• Headers are providedon 2 sides for:– I/O connections (P0-P15)– Vdd: + 5 Volts– Vss: 0 Volts– Vin: Supply Voltage from battery

The Breadboard and Circuit Construction

Page 15: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Example connections of devices. Do Not Build. Note how the rows of sockets make complete paths of current between devices and from I/O headers and Vdd or Vss.

The Breadboard and Circuit Construction

Page 16: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• The LCD Terminal AppMod provides a simple and convenient method of adding a standard character LCD and 4 user-input buttons to BASIC Stamp projects.

LCD Terminal AppMod

Page 17: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• The LCD Display is added to the BOE board by means of the 2x10 AppMod

Header socket on the BOE• The LCD comes with a male 2x10 pin header. Plug one end of this into the BOE socket.

LCD Terminal AppMod

Page 18: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• Plug the other end of the male header into the underside of the LCD board, as shown.

LCD Terminal AppMod

Page 19: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• To help make a secure platform for the LCD board, we can also put a metal hexagonal post between the LCD board and

the BOE board as shown. Attach with a screw at the base and a nut at the top.

LCD Terminal AppMod

Page 20: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• Now, we will see how to write code using the LCD display. Since the buttons on the display use I/O pins 4,5,6,and 7, we must make sure we disconnect any sensors attached to those pins on the BOE.

• On the course Code page you will find a zipped ZBasic project called LCDAPP_MOD. Download this project, unzip the files, and open the project in the ZBasic IDE.

LCD Test Code

Page 21: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• Note that this project uses two source files: – LCD_DEMO.BAS

• This is the code that does the various tests of the LCD terminal’s functionality

– LCD_APPMOD.BAS• This is a “module” created by the maker of the ZX-24a. It

consists of a set of subroutines and functions that allow us to access the various capabilities of the LCD terminal

• The LCDTest project consists of these two files. When run, all code in both files is downloaded to the ZX-24a and run.

Multiple Source Files

Page 22: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

• Note that there is just one source file “LCD_DEMO.bas” that has the Main() sub-routine.

• Run the project to see how the LCD is used.

LCD Test Code

Page 23: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Team Tasks – LCD Monitor Program

• Write a program that will 1) find the IO values of the light sensors and 2) write these values to the LCD display.

• Test your program by disconnecting the USB cord and letting the Boe-Bot roam while you check the output on the LCD display.

Page 24: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Team Tasks

• Modify your program so that output of sensor values is under the control of the buttons on the LCD terminal.

• When Button 1 (farthest left) is pushed, the display should show the value of the left light sensor. When Button 2 (next left) is pushed, the display should show the value of the right sensor.

Page 25: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Team Tasks

• For the remainder of our time, work on the following task: Create a program that will have the Boe-bot follow a bright light shone on the floor in front of it.

• You can read through ACTIVITY #5: FLASHLIGHT

BEAM FOLLOWING BOE-BOT in the Boe-bot text for more info on how to orient the photoresistors. The code will be different, but the basic idea of the program will be the same in ZBasic.

Page 26: PHY 235 Robotics Workshop Day 4 Robotic Behaviors, Light Sensing, Voltage Dividers, LCD-MOD

Team Tasks

• If you want more of a challenge, you can try Projects 1 and 2 on pages 228-229 in the Boe-Bot text.