0% found this document useful (0 votes)
130 views

ECE 206 Lab Manual

Pressure Sensor on Port 1 Light Sensor 1 on Port 2 (detects open position) Light Sensor 2 on Port 3 (detects closed position) Motor on Port A Task: Program the NXT brick to control the automatic sliding door using the flowchart provided. The program should: 1. Check the pressure sensor and light sensors continuously in a loop. 2. If pressure sensor reads 1 (someone is on the mat), open the door by setting the motor to forward. 3. Keep the door open until both light sensors read >=40, indicating door is not fully open/closed. 4. When both light sensors read >=40, start closing the door by setting the motor

Uploaded by

Arian Bidgoli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

ECE 206 Lab Manual

Pressure Sensor on Port 1 Light Sensor 1 on Port 2 (detects open position) Light Sensor 2 on Port 3 (detects closed position) Motor on Port A Task: Program the NXT brick to control the automatic sliding door using the flowchart provided. The program should: 1. Check the pressure sensor and light sensors continuously in a loop. 2. If pressure sensor reads 1 (someone is on the mat), open the door by setting the motor to forward. 3. Keep the door open until both light sensors read >=40, indicating door is not fully open/closed. 4. When both light sensors read >=40, start closing the door by setting the motor

Uploaded by

Arian Bidgoli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CALIFORNIA STATE UNIVERSITYNORTHRIDGE

ELECTRICAL ENGINEERING

ECE 206L Computing For Electrical Engineers


LABORATORY MANUAL

Maintained By: Dr. Xiaojun Geng


Department of Electrical and Computer Engineering

(Revised: 08/29/2016)

1
Table of Contents

ECE206L Laboratory Report Format ............................................................................................... 3

LAB EXERCISE #1: Drawing Flowcharts ........................................................................................... 4

LAB EXERCISE #2: NXC Programming -- Spiral Path Using Loops.................................................... 6

LAB EXERCISE #3: NXC Programming -- Automatic Sliding Door Control ....................................... 8

LAB EXERCISE #4: Communication with Bluetooth Wireless ....................................................... 10

LAB EXERCISE #5: Track Follower .................................................................................................. 11

LAB EXERCISE #6: C++ Programming --Series and Parallel Resistance (Value Accumulation
without using Array) ..................................................................................................................... 12

LAB EXERCISE #7: C++ Programming --User Created Functions .................................................. 13

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:

1. Symbols that sequentially follow another symbol must be aligned vertically.


2. No lines cross one another.
3. All lines must either be horizontal or vertical; there must be no diagonal (slanting) lines.
4. The True or False ( T or F ) label of the condition symbol must be placed next to the condition
above the line as show in Flowcharts 1A or 1B instead of directly on the line.
5. The entire flowchart should be adjusted to fit on one page.

Figure 1. Spiral Path.

4
Figure 2. Two flowcharts.

5
LAB EXERCISE #2:
NXC Programming -- Spiral Path Using Loops

Prelab: First draft of the NXC program.

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.

Figure 3. Spiral Path.

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.

NXC Code Samples:


Set Variable n to 6 n = 6; //NQC is case sensitive
//need to declare n before use: int n;
Subtraction: FwdTime = FwdTime - 250;

Go Forward 2.5 seconds OnFwd(OUT_AC, 40); //power 40%


Wait (2500);
Off(OUT_AC);

Turn Right block: OnFwd(OUT_C, 40); //Could be OUT_A depending on


//your wiring
Off(OUT_A);
Wait (1500); // adjust the time to get 90°
Off(OUT_C);

Repeat 6 times: for ( i = 0; i < 6; i ++)


{
//insert instructions to repeat here
};

Repeat n times block: for ( j = 0; j < n; j ++)


{
//insert instructions to repeat here
};

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

SENSOR_2 : Light Sensor for detecting door is completely open.


Light Sensor reading < 40 (black) means door completely open
>= 40 (white) means door not completely open

SENSOR_3 : Light Sensor for detecting door is completely close.


Light Sensor reading < 40 (black) means door completely close
>= 40 (white) means door not completely close

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

SetSensorType(S3, SENSOR_TYPE_LIGHT); //sensor 3 is a light sensor


SetSensorMode(S3, 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.

Post Laboratory Questions:

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

Prelab: Preliminary NXC Program


Hardware:
Sensor 1: Light sensor
Sensor 2: Touch sensor
OUT_C : Left motor
OUT_A : Right motor

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

Materials shown to the instructor in classroom:


 Well documented and correct NXC program.
 Check that the program works as intended by issuing the messages via the master NXT
brick provided by the instructor.

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.

Figure 4. Track followed by robot.

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.

The flowchart must include the following:


1. Prompt the user to select either the series or the parallel connection.
2. Prompt the user to enter the number of resistors connected.
3. Read (get) the value of each of the resistors.
4. Calculate the resistance.
5. Output the resistance value according to format specified below.
6. Avoid division by 0.

The print output of the resistance must follow this format:


1. If the resistance value is less than 1000, output only the integer portion of the value and
the Ohm unit. Example: 235 Ohm
2. If the resistance value is greater than or equal to 1000, output integer portion plus three
digits after the decimal point and the KOhm unit. Example: 2.123 KOhm

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:

𝑛 5 sin(𝑛) cos(4000𝜋𝑛 + 𝜋/3) 𝑓(𝑛)


---------------------------------------------------------------------------------------------

Show Instructor or Lab Assistant:


Show well-documented and correct C++ program, and run output to verity that your program
work as intended.

13
LAB EXERCISE #8: C++ Programming
--Series and Parallel Resistance
(Value Accumulation using Array)

Prelab:
Preliminary C++ Program

Tasks:

Repeat Laboratory Exercise 6 with these modifications:

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:

Display For series configuration:


Total series resistance is 2.123KOhm for 3 resistors:
1000 Ohm, 1000 Ohm, 123 Ohm.

Display For parallel configuration:


Total parallel resistance is 500 Ohm for 2 resistors:
1000 Ohm, 1000 Ohm.

Show Instructor or Lab Assistant:


Show well-documented and correct C++ program, and run output to verity that your program
work as intended.

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,𝑛)

𝑦[𝑛] = ∑ 𝑥[𝑛 − 𝑘]ℎ[𝑘] (1)


𝑘=0
where 𝑁 is the number of coefficients, ℎ[𝑘] is the impulse response of the filter, 𝑥[𝑛 − 𝑘] is the
input of the filter delayed 𝑘 clock cycles, 𝑦[𝑛] is the output of the filter, and 𝑥[𝑚] = 0 for 𝑚 <
0.

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:
𝑛

𝑦[𝑛] = ∑ 𝑥[𝑛 − 𝑘]ℎ[𝑘] , 𝑛<𝑁 (2)


𝑘=0
and
𝑁−1

𝑦[𝑛] = ∑ 𝑥[𝑛 − 𝑘]ℎ[𝑘], 𝑛≥𝑁 (3)


𝑘=0
where 𝑁 = 35 which is the order number for the filter we use below.

From equation (2), we can calculate 𝑦[𝑛] for 𝑛 < 35 as follows:


𝑦[0] = ℎ[0]
𝑦[1] = 𝑥[1]ℎ[0] + 𝑥[0]ℎ[1]
𝑦[2] = 𝑥[2]ℎ[0] + 𝑥[1]ℎ[1] + 𝑥[0]ℎ[2]

15
From equation (3), we can calculate 𝑦[𝑛] for 𝑛 ≥ 35 as follows:
𝑦[35] = 𝑥[35]ℎ[0] + 𝑥[34]ℎ[1] + ⋯ 𝑥[3]ℎ[32] + 𝑥[2]ℎ[33] + 𝑥[1]ℎ[34]
𝑦[36] = 𝑥[36]ℎ[0] + 𝑥[35]ℎ[1] + ⋯ 𝑥[4]ℎ[32] + 𝑥[3]ℎ[33] + 𝑥[2]ℎ[34]
𝑦[37] = 𝑥[37]ℎ[0] + 𝑥[36]ℎ[1] + ⋯ 𝑥[5]ℎ[32] + 𝑥[4]ℎ[33] + 𝑥[3]ℎ[34]

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.

h(0) = h(34) = 361.922 h(9) = h(25) = 812.822


h(1) = h(33) = 589.000 h(10) = h(24) = -934.419
h(2) = h(32) = 52.556 h(11) = h(23) = -1082.725
h(3) = h(31) = -538.095 h(12) = h(22) = 1547.666
h(4) = h(30) = -58.657 h(13) = h(21) = 1083.109
h(5) = h(29) = 499.472 h(14) = h(20) = -3229.928
h(6) = h(28) = -251.531 h(15) = h(19) = -1275.738
h(7) = h(27) = -785.168 h(16) = h(18) = 10268.660
h(8) = h(26) = 381.999 h(17) = h(17) = 17571.900

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:

1. Create three arrays 𝑥[600], ℎ[35], and 𝑦[600].


2. Fill the 𝑥 array with the 600 sample values of 𝑥(𝑡) = 100 sin(4000 𝜋𝑡) for 0 ≤ 𝑡 < 0.06
with rate of Δ𝑡 = 0.0001 per sample. Note that the frequency for the sinewave is 2 KHz.
3. Compute 𝑦[𝑛] for 0 ≤ 𝑡 < 0.06.
4. Store the 600 sample values of 𝑥[𝑛] in file filex and store the 600 sample values of 𝑦[𝑛] in
file filey.
5. Plot 𝑥[𝑛] and 𝑦[𝑛] versus 𝑛 on the same graph using Excel. Compare and comment on the
amplitude of the two sinewaves 𝑥[𝑛] and 𝑦[𝑛].

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.

Show Instructor or Lab Assistant:


Show well-documented and correct C++ program, and run output to verity that your program
work as intended.

17

You might also like