Lab Project 3: Adaptive Traffic Light Control Systems NOTE: You will need a flash-drive to save your work, as you will need it the next time you are in lab. Data will be cleared from the computers at the end of each lab section, so save your work to your own disk. Objective In this experiment, you will be using the computer program MATLAB to interact with a Data Acquisition (DAQ) Device. The DAQ device is connected to 12 light emitting diodes (LEDs), which represent traffic lights at a 4-way intersection. The DAQ device is also connected to magnetic reed switches mounted under each lane. The model cars are equipped with powerful neodymium magnets that will cause the magnetic reed switches to close; this can be detected by the DAQ device and will be used as inputs for detecting the presence of cars at the intersection. In the first part of the experiment, you will write three functions: a function to initialize the DAQ device, a function to send data to the traffic lights, and a function to receive data from the road sensors. In the second part of the experiment, you will write a program that controls the traffic lights, incorporating the functions written in Part 1. Materials Computer MATLAB Traffic Light Assembly with DAQ device USB cable Toy cars with magnets Storage Device from home to save work. DAQ and the Traffic Light Module The control unit of the traffic light module is a 24 bit DAQ device from Measurement Computing. Each data bit is referred to as a port and is connected to a physical pin on the DAQ device. The bits are grouped into 3 banks: A, B, and C. Hardware limitations of the DAQ Device require that all ports on bank A are configured to be either input or output; the same goes for bank B. Bank C, however, can be broken into 2 sets,: Ports C0-C3 must all have the same direction, and ports C4-C7 must all have the same direction.
15
Embed
Lab Project 3: Adaptive Traffic Light Control Systems
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Lab Project 3:
Adaptive Traffic Light Control Systems
NOTE: You will need a flash-drive to save your work, as you will need it the next
time you are in lab. Data will be cleared from the computers at the end of each lab section, so
save your work to your own disk.
Objective
In this experiment, you will be using the computer program MATLAB to interact with a Data
Acquisition (DAQ) Device. The DAQ device is connected to 12 light emitting diodes (LEDs),
which represent traffic lights at a 4-way intersection. The DAQ device is also connected to
magnetic reed switches mounted under each lane. The model cars are equipped with powerful
neodymium magnets that will cause the magnetic reed switches to close; this can be detected by
the DAQ device and will be used as inputs for detecting the presence of cars at the intersection.
In the first part of the experiment, you will write three functions: a function to initialize the DAQ
device, a function to send data to the traffic lights, and a function to receive data from the road
sensors.
In the second part of the experiment, you will write a program that controls the traffic lights,
incorporating the functions written in Part 1.
Materials
Computer
MATLAB
Traffic Light Assembly with DAQ device
USB cable
Toy cars with magnets
Storage Device from home to save work.
DAQ and the Traffic Light Module
The control unit of the traffic light module is a 24 bit DAQ device from Measurement
Computing. Each data bit is referred to as a port and is connected to a physical pin on the DAQ
device. The bits are grouped into 3 banks: A, B, and C. Hardware limitations of the DAQ Device
require that all ports on bank A are configured to be either input or output; the same goes for
bank B. Bank C, however, can be broken into 2 sets,: Ports C0-C3 must all have the same
direction, and ports C4-C7 must all have the same direction.
The LED lights are connected to ports A0-B3 (See Table 1), and the vehicle sensors are connected to
ports C0-C3. This means our program needs to configure ports A0-B3 as outputs, and C0-C3 as inputs.
Table 1: DAQ Device Connection
Pin Signal Name Bit Connected To Pin Signal Name Bit Connected To
1 Port C0 16 North Sensor 21 Port A0 0 North Red
2 Port C1 17 East Sensor 22 Port A1 1 North Yellow
3 Port C2 18 South Sensor 23 Port A2 2 North Green
4 Port C3 19 West Sensor 24 Port A3 3 East Red
5 Port C4 25 Port A4 4 East Yellow
6 Port C5 26 Port A5 5 East Green
7 Port C6 27 Port A6 6 South Red
8 Port C7 28 Port A7 7 South Yellow
9 GND 29 GND
10 n/c 30 PC +5V
11 n/c 31 GND
12 GND 32 Port B0 8 South Green
13 n/c 33 Port B1 9 West Red
14 n/c 34 Port B2 10 West Yellow
15 GND 35 Port B3 11 West Green
16 n/c 36 Port B4
17 GND 37 Port B5
18 n/c 38 Port B6
19 GND 39 Port B7
20 CTR 40 GND (n/c: Not connected, GND: Ground, PC +5V: 5 volt power from PC, CTR: Counter function of DAQ device)
When using MATLAB, you will refer to the various ports as their designated bit. All you need to
know is what bit is connected to what light or sensor. For more information on the other pins and
signal names, view the USB-1024LS manual on Moodle.
How to Use This Document Syntax, as found in the MATLAB Help documentation:
Code for you to use directly in your program:
Note: Anytime you see code in a box, you may use it in your program. If you choose to use
different variable names, keep in mind that this document will be referring to variable and
function names based on the sample code provided.
Terminology addline():
A function in the DAQ Toolbox that assigns a hardware line to a DAQ device.
Bit:
The numerical equivalent of a digital port.
Board/Board #:
The number assigned to a DAQ device by Instacal.
DAQ Device:
A device used for inputting and outputting data from a computer. The traffic light module has a
DAQdevice in its center that detects signals from the sensors and outputs signals to the traffic lights.
digitalio():
Short for “digital input and output;” a function in the DAQ Toolbox that creates an object for
communicating with a DAQ device.
Function:
An M-file that accepts input arguments and returns output arguments.
Globalize:
Declare a variable as a global variable.
Global Variable:
A global variable is a variable that is accessible to any function that declares it. If you want more than one
function to share a single copy of a variable, simply declare the variable as global in all the functions. Do
the same thing at the command line if you want the base workspace to access the variable. The global
declaration must occur before the variable is actually used in a function.
Hardware Line:
A port on a DAQ device. For example A2 and A6 are two different hardware lines.
Infinite loop:
A program loop that runs infinitely. Press Ctrl+C to break the program.
Interrupt:
A signal sent to the computer that ‘interrupts’ the computers current processes to temporarily process
another routine.
Instacal
The configuration utility for Measurement Computing DAQ devices such as the USB-1024LS.
Object:
An object is a type of variable that contains function information. An example of an object could be a
line on a plot, or an input or output range on a DAQ device.
Port:
An input or output on a DAQ device.
putvalue()/getvalue():
Functions for sending/receiving data to/from a DAQ device.
Script
A simple program that contains no inputs.
Note: Refer to the DAQ Toolbox MATLAB Help documentation on Moodle for more information
about the DAQ Toolbox functions used in this Lab.
Setup Note: If the following steps are performed out of order MATLAB will not recognize the Traffic
Light Module.
1. Plug in the traffic light module using the USB cable provided if it is not already plugged in.
One end of the USB cable connects to the USB port on the DAQ device in the base of the
traffic light module, the other connects to a USB port on the back of the computer. Please do
not connect the traffic light module to the USB ports on the front of the computer.
2. Open InstaCal from the icon located on the desktop.
(InstaCal is the configuration utility for the DAQ device.)
2a. If you get a PnP Board Detection message saying that a device is not connected, press
OK to continue and remove this board. If your traffic light module is properly connected a
Plug and Play Board Detection window will appear. Click OK to add the board to the
configuration file. You will not see this box if the computer is already configured to
communicate with the device.
3. If the device is seen by InstaCal, you will see a similar window. Take note of the Board #.
The Board# will probably be zero (0) because only one board is connected to the computer.
You can change the board number by right clicking on the device if necessary. The board
number does not need to be zero, however you do need to know what it is so that you can tell
MATLAB what device you are trying to communicate with.
4. Close InstaCal and start MATLAB
Part 1
Before you leave lab, you must write 3 functions
M-File 1 – Initializing the DAQ Device 1. Open a new M-file and save it as initiateLights.m. Be sure to include comments in your
code. Declare two global variables:
Recall that the lights and sensors are wired as follows, noting that a bit is the same as a
hardware line:
2. The following will add a digitalio object called trafficLights, configure it for output on
bits 0-11, and clear the data on the device at those ports. Add this to your M-file:
%Create a digital I/O object trafficLights for a %MCC board with hardware ID 0.
trafficLights = digitalio('mcc',0);
%Add twelve hardware lines (0-11) to trafficLights, %configuring them for output.
addline(trafficLights,0:11,'out');
%Turn off all lights by sending a zero to each bit putvalue(trafficLights,[0,0,0,0,0,0,0,0,0,0,0,0]);
%An alternate way to turn off all lights:
%putvalue(trafficLights,zeros(1,12));
3. Add the code necessary to add a digitalio object to sensorStatus, and configure it for input
on bits 16-19. You do not need to clear data on an input digitalio object.
4. Save the M-file. (See the Appendix for more information on these functions and how
they work).
M-File 2 – Changing the Lights This function must accept 2 values as input arguments: the color of the north and south bound
lights and the color of the east and west bound lights. The values for the input arguments for
each set of lights should operate as follows: an input of 0 should make the lights red, an input of
1 should make the lights yellow, and an input of 2 should make the lights green. This function
will change the lights illuminated on the traffic light module according to its inputs. Note: This
function is very similar to the “Your Turn” you completed in lecture 16.
1. Create a new M-file and save it as changeLights.m. Be sure to include comments in your
code.
2. You will need to globalize the object (declared in the first M-File) that interfaces with the
lights. This allows the function to access the same object declared in the first function. Do
not globalize the sensor object in this function.
3. Write the lines of code necessary to change the lights according to the function’s input
arguments:
Examples:
1. To change all of the lights to red the following line of code would be used: