ECE 206 Lab Manual
ECE 206 Lab Manual
ELECTRICAL ENGINEERING
(Revised: 08/29/2016)
1
Table of Contents
LAB EXERCISE #3: NXC Programming -- Automatic Sliding Door Control ....................................... 8
LAB EXERCISE #6: C++ Programming --Series and Parallel Resistance (Value Accumulation
without using Array) ..................................................................................................................... 12
LAB EXERCISE #8: C++ Programming --Series and Parallel Resistance (Value Accumulation using
Array) ............................................................................................................................................ 14
LAB EXERCISE #9: C++ Programming --Finite Impulse Response Filter (Array Usage) ................ 15
2
ECE206L Laboratory Report Format
BASIC INFORMATION
To save paper, the cover page is not necessary. Instead, include the following information on
the upper left-hand corner of the page: your name, course number, lab exercise number,
exercise title, and date.
1. PROBLEM STATEMENT
Describe what the program does and how does it do the task. For NXC programs, include also
the hardware configuration. For NXC and C++ programs, this section should also become your
main program's comment header.
2. REVISED FLOWCHART
Include a correct flowchart which is the revision of the flowchart for the prelab.
3. COPY OF PROGRAM
Include a copy of the program, complete with the comment header(s) and comment
statements.
4. PROGRAM OUTCOME
For NXC program, describes how well the robot performs its task and states if any
improvements can be made. For C++ program, include a copy of the run-output (the actual
display output from the program) and describe the test cases that you use to prove the
correctness of the program.
5. PROBLEMS ENCOUNTERED
Describe the problems you encounter and the way you solve them.
6. COMMENTS
Give comments on the lab exercise, such as what you learn, whether it is too complicated or
too easy, any improvement you would suggest, etc.
3
LAB EXERCISE #1:
Drawing Flowcharts
Microsoft Visio software is designed to draw block diagrams, flowcharts, UML model diagrams,
electrical diagrams, etc. In this exercise, you will learn to draw the attached Flowcharts 1A and
1B. Because the software we use is an easy “click and drag” type of software, no instructions will
be given here. Instead, the instructor will demonstrate the steps in creating a flowchart in class
and you watch and follow the steps shown.
Both Flowcharts 1A and 1B will make the robot travel in the spiral paths shown in Figure 1. Two
flowcharts are given in Figure 2. Flowchart 1A uses a simpler algorithm which equates to using
less symbols in the flowchart; it uses only one loop. Flowchart 1B is more complicated; it uses
two loops. When you draw the flowcharts, you need to follow these guidelines:
4
Figure 2. Two flowcharts.
5
LAB EXERCISE #2:
NXC Programming -- Spiral Path Using Loops
Discussion:
As the spiral path consists of a sequence of forward paths and 90° turns, a brute force
method can be used to program the Robot to travel in a spiral path by a long and repetitive
sequence of forward and 90° turn instructions. This brute force method will result in a total of
thirteen forward instructions and twelve 90° turn instructions. A close look at the spiral program
reveals repetitive function calls with the same delay or progressively reduced delay.
Fig. 3 below indicates groups of forward and 90° turn function calls which have the same
delay. There are a total of six groups. The first group consists of three straight paths and three
turns and the paths are the longest. The second group consists of two straight paths and two
turns with shorter paths. The third, fourth, fifth, and sixth group each consists of two straight
paths and two turns and each with shorter paths. From this information, we can conclude that a
six iteration REPEAT loop is needed; each iteration of the loop for each group. The first iteration
involves actions for the first group which consists of three repetitive actions. These actions can
be done using a three iteration REPEAT loop. The second iteration involves actions for the second
group which consists of two repetitive actions. These actions can be done using a two iteration
REPEAT loop. The remaining iterations involve two repetitive actions which can be done using a
two iteration REPEAT loop. The length of the straight path is decreased after each iteration.
6
Task:
Program the robot to follow the spiral path depicted in Fig. 1 using the NXC programming
language. The programmer translates the flowchart into NXC codes instead of the NXT blocks. See
the lecture notes for the NXC instructions to set a variable, to spin 90°, to repeat 6 times, and to
repeat n times.
Post Lab:
o Well documented NXC program (with comments and header).
o Have the instructor verify that your NXC program works as specified.
7
LAB EXERCISE #3:
NXC Programming -- Automatic Sliding Door Control
Prelab: The modified flowchart that includes the five seconds delay in closing the door
Introduction:
An Electrical engineer uses an NXT brick to control an automatic sliding door for the entrance
to a commercial building. One pressure sensor, two light sensors, and one motor are used in the
control of the sliding door. Normally, the pressure sensor will give a reading of 0. If someone is
standing on the mat on either side of the door, the pressure sensor will give a reading of 1. One
light sensor will give a reading of less than 40 if the door is completely open and give a reading
of more than or equal to 40 if the door is NOT completely open; the other one will give a reading
of less than 40 if the door is completely closed and give a reading of more than or equal to 40 if
the door is NOT completely closed. The motor is used to open and close the door. Setting the
motor to go forward will open the door and setting the motor to go reverse will close the door.
When someone is standing on the mat on either side of the door, the door will open and remains
open until no one is standing on the mat. Also when the door opens, it will open completely; but
when the door is closing and someone stands on the mat, the door will reverse direction and will
open completely.
Hardware Configuration:
Assume that the sensors and the motor are wired to NXT brick as follows:
SENSOR_1 : Pressure Sensor (use a Touch sensor instead) placed under the door mat.
Pressure Sensor reading = 0 means no one on the mat
= 1 means someone on the mat
OUT_A : Motor.
Forward = close door; Reverse = open door
Sensor Configuration:
The programmer must explicitly specify the sensor type and sensor mode in the NXC codes.
For example, if sensor 1 is a touch sensor which gives a “0” when it is not touched and gives a
“1” when it is touched, then the NXC codes must specify the sensor type TOUCH and sensor
mode BOOL for sensor 1 as follows:
8
SetSensorType(S1, SENSOR_TYPE_TOUCH); //case sensitive
SetSensorMode(S1, SENSOR_MODE_BOOL); //must type as is
Use similar NXC instructions to set sensor 2 and sensor 3 mode and type as follows:
SetSensorType(S2, SENSOR_TYPE_LIGHT); //sensor 2 is a light sensor
SetSensorMode(S2, SENSOR_MODE_PERCENT); //in percent mode
Algorithms:
The algorithm for this exercise has been developed and discussed in the lecture and the
structured flow chart has been drawn in the lecture notes. Modify the flowchart so that it waits
at least five seconds after the door is completely open before closing the door. Translate the
modified structured flow chart to NXC codes.
Material to submit:
Show the instructor your well documented NXC program.
Have the instructor verify that your NXC program works as specified.
Investigate a sliding door on a commercial building in your area; pay attention on the drive
mechanism, the safety mechanism, the entry-request detection mechanism, etc. Compare and
contrast the commercial sliding door with the one in the lab. Include discussion on the hardware
and software.
9
LAB EXERCISE #4:
Communication with Bluetooth Wireless
Task Description:
From what we have learned so far, the NXT brick can be controlled by an NXC program. It can
also be directly controlled by the Bricx command center. Since the NXT brick has the facility to
accept messages via the Bluetooth wireless circuitry, we can write an NXC program to let the
NXT brick perform a certain task according to messages sent from another NXT brick. Being able
to control the NXT using messages has many useful applications such as remote bomb defusing,
remote car control, etc.
Write a NXC program for the NXT to continuously wait for a Bluetooth wireless message and
perform the task once according to the table below:
Message Tasks
1 Go Forward for 0.5 sec
2 Go Backward for 0.5 sec
3 Turn Left for 0.5 sec
4 Turn Right for 0.5 sec
5 Play a tone: Note A for 0.5 sec.
6 If Light SENSOR_1 is on black, increment variable cnt, and display cnt value
on the LCD. cnt is initially 0. (Use NumOut function to display the cnt value.)
7 Go forward until touch (SENSOR_2 hits something).
8 Go forward from white space, cross a black strip, and stop
10
LAB EXERCISE #5:
Track Follower
Prelab:
Flowchart and Preliminary NXC Program for a track follower program that will start from
point A on the black track below, follow a winding black path until point B, and return to point
A facing downward.
Discussion:
The clockwise track follower example in the lecture notes cannot be used here. For the
clockwise track follower, the correction needed when the robot goes off course from the
circular path is right turn whereas the correction for the track in the lab may be left turn or
right turn. The use of the light sensor in detecting the robot going off course can still be used
for the given problem; but is one light sensor sufficient? As the skill for program solving grows
with experience, no further hints or discussion will be given here. The students are expected
to thoroughly understand the problem and design the solution with minimal help from the
instructor.
11
LAB EXERCISE #6: C++ Programming
--Series and Parallel Resistance
(Value Accumulation without using Array)
Prelab:
Preliminary flowchart and C++ Program
Introduction:
The fundamental Laws in Electrical Engineering concerning resistance are:
(1) Resistors connected in series:
𝑅 = 𝑅1 + 𝑅2 … + 𝑅𝑛−1 + 𝑅𝑛
(2) Resistors connected in parallel:
1 1 1 1 1
= + + …+ +
𝑅 𝑅1 𝑅2 𝑅𝑛−1 𝑅𝑛
Tasks:
Draw and code flowchart to calculate the resistance of resistors connected either in series or
parallel.
Note: Do NOT use array in solving the problem.
In-lab Requirement:
As always, have the instructor or teaching assistant verify that your program work as
intended.
12
LAB EXERCISE #7: C++ Programming
--User Created Functions
Prelab:
Preliminary C++ Program
Introduction:
According to Taylor series expansion at 𝑥 = 0, we have
∞
𝑥 3 𝑥 5 𝑥 7 𝑥 9 𝑥11 𝑘
𝑥 2𝑘+1
sin(𝑥) = 𝑥 − + − + − + ⋯ = ∑(−1)
3! 5! 7! 9! 11! (2𝑘 + 1)!
𝑘=0
and
∞
𝑥 2 𝑥 4 𝑥 6 𝑥 8 𝑥10 𝑘
𝑥 2𝑘
cos(𝑥) = 1 − + − + − + ⋯ = ∑(−1)
2! 4! 6! 8! 10! (2𝑘)!
𝑘=0
where 𝑥 is in radian. Using these two equations, we can approximately evaluate the values of 𝑠𝑖𝑛
and 𝑐𝑜𝑠 functions for a given argument 𝑥.
Tasks:
1. Write two functions 𝑔(𝑥) = 𝑠𝑖𝑛(𝑥) and ℎ(𝑥) = 𝑐𝑜𝑠(𝑥) using the series above to obtain
accuracy to 5 decimal places.
2. Write a C++ program that uses the functions above to calculate 𝑓(𝑛) for integer 𝑛 =
0, 1, 2, … ,6, where
𝜋
𝑓(𝑛) = 5𝑔(𝑛) ∗ ℎ (4000𝜋𝑛 + )
3
𝜋
= 5 sin(𝑛) ∗ cos (4000𝜋𝑛 + ).
3
The program also display the result in the following table format:
13
LAB EXERCISE #8: C++ Programming
--Series and Parallel Resistance
(Value Accumulation using Array)
Prelab:
Preliminary C++ Program
Tasks:
1. Write a C++ program to calculate the resistance of resistors connected either in series or
parallel using array. Assume that the program can handle up to 1000 resistors.
2. The print output format of the resistance must satisfy this additional requirement: Print the
total resistance followed by values of each resistor.
Example:
14
LAB EXERCISE #9: C++ Programming
--Finite Impulse Response Filter (Array Usage)
Prelab:
Preliminary C++ Program
Introduction:
An FIR (Finite Impulse Response) filter can be represented by
min(𝑁−1,𝑛)
A discrete filter accepts a sequence of data (input), one at a time, and produces a new sequence
of data (output), one at a time. In the above equation, 𝑛 is the time index, 𝑛 = 0, 1, 2, …. The
filter behavior is explained below.
At 𝑛 = 0, the filter gets only one input which is 𝑥[0], so the filter uses 𝑥[0] to generate the
output 𝑦[0] at time moment of 𝑛 = 0. At 𝑛 = 1, the filter has two inputs available which are
𝑥[0], 𝑥[1], so it will use these two values to generate the output 𝑦[1]. When the number of input
data grows higher than 35, the filter only uses the latest 35 inputs to compute the output. In
other words, equation (1) can be split into the following two equations:
𝑛
A given optimal equiripple FIR (Finite Impulse Response) filter has the following specification:
Sample rate: 10 kHz (0.0001 sec / sample);
Passband: 0 – 2.5 kHz (lowpass), 0.5 dB maximum ripple;
Stopband: 3.0 – 5 kHz, 50dB minimum attenuation.
Using the Parks-Mcclellan program, we obtain the following impulse response values (scaled up
by 215 ; ie. to get actual value, divide by 215 ), with 𝑁 = 35.
Courtesy: Dale Clover & John Deller, Digital Signal Processing and Microcontroller, Motorola
University, Prentice-Hall, 1999.
Tasks:
Write a C++ program to do steps 1 to 4 and use Excel to do step 5:
16
Program Outcomes and Discussions:
1. Repeat steps 1 to 5 for 𝑥(𝑡) = 100 sin(4000 𝜋𝑡), frequency 2 kHz.
2. Repeat steps 1 to 5 for 𝑥(𝑡) = 100 sin(6000 𝜋𝑡), frequency 3 kHz.
3. Repeat steps 1 to 5 for 𝑥(𝑡) = 100 sin(8000 𝜋𝑡), frequency 4 kHz.
4. Compare the amplitudes of the two sinewaves 𝑥[𝑛] and 𝑦[𝑛] for frequencies 2 kHz, 3
kHz, and 4 kHz. Categorize the filter either as low pass filter, high pass filter, or
band-pass filter.
17