The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer...

16
The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4

Transcript of The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer...

Page 1: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

The George Washington University Department of ECE

ECE 1010 - Intro: Electrical & Computer Engineering

Dr. S. Ahmadi

Class 4

Page 2: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Class Outline

• Programming Left/Right Turn Capabilities for the Robot

• Light Sensor Applications

Page 3: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Turning

(Before going on, let’s make robot go forward and backwards first!)

20 - Minutes

Page 4: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Implementing Turning

• Number of different ways to turn:– 1. Turn only one motor on, and leave it

running for ‘X’ amount of seconds. X is determined by experimenting with your particular robot.

motor(1, 80); // Turning motor 1 forward @ 80%.

sleep(X);

Page 5: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Turning continued…

– 2. One motor forward, opposite motor backwards, and then leave for X seconds.

motor(1, 80); // Motor 1 goes forward @ 80%.motor(3, -80); // Motor 3 goes backward @ 80%.sleep(X); // X determined experimentally by

// programmers/designers.

Although not as efficient, you can also turn by having both motors going in the same direction, but at different speeds.

Page 6: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Calculating the time, X

• Write a short program to make a robot turn, using one of the mentioned techniques.

• Put X = 2.0, and run the program.• See how much the robot turns. Change the

value of X accordingly. If turn is much greater then 90 degrees, lessen the time, X. If it is smaller, put a larger time.

• Keep repeating until your robot executes a near perfect 90 degree turn.

Page 7: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Loops

Page 8: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

2 Different Types of Loops

• To repeat a group of commands until a condition is no longer true, we use a WHILE loop

• To repeat a group of commands a predefined # of times, we use a FOR loop

Examples:

while (side_button()==0){

beep();}

for (int count=0; count<=4; count++){

beep() ;}

WHILE LOOP FOR LOOP

Page 9: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Block Diagram For a Right Turn

Go Forward

Turn Right

Yes

NoTurn Right?

Page 10: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Flow Chart to Code for Turning Right

Go Forward

Turn Right

Yes

NoTurn Right?

while (true){

// go forward motor ( 1, 100 ) ; motor ( 3, 100 ) ;

// should we turn right? if ( analog(4) > 200 ) { // turn RT, until off black tape ao() ; while (analog(4) > 200) motor (3, 100) ; }}Flow Chart

C Code Implementation of Flow Chart

Page 11: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Block Diagram For a Right Turn or Left Turn

Go Forward

Turn Right

Right turn?

Yes

No Left turn?

Turn Left

Yes

No

Page 12: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Block Diagram for a Complete System

Go Forward

Turn Right

Right turn?

Yes

No Left turn?

Turn Left

Yes

No

Left turn?

STOP

Yes

No

Page 13: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

Sample Programint main(){ int sensor1, sensor3, Flag=1; printf("Press Start button to begin:"); while(side_button()==0); // Waits for start button to be pressed.

while(Flag==1) { sensor1=analog(4); // Reads the signal coming from analog port 4.

sensor3=analog(6); // Reads the signal coming from analog port 6.

motor(1, 10); // GO FORWARD.

motor(3, 10);

Page 14: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

if(sensor1>200) // RIGHT TURN??

{ // If Yes then….

if(sensor3>200) // …LEFT TURN??

{ // ao(); // Flag=0; // If YES then STOP!!!

} else // Right turn = yes BUT Left turn = no, then...

{ ao(); sleep(2.0); while(analog(4)>200) // Turn RIGHT!!

{ motor(3,100);

} } }

Page 15: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

else if(sensor3>200) // LEFT TURN????

{

ao();

sleep(2.0);

while(analog(6)>200) // TURN LEFT

{

motor(1,100);

}

}

}

}

Page 16: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.

U-Turn

• Making the robot U-turn, and go back along the path to the starting point.

• This can be carried out in the following way: – After sensing a black surface on BOTH light sensors,

the robot stops, and then starts to rotate in either direction.

– The robot rotates until the first sensor senses the black line, it CONTINUES rotating, but stops once the second sensor detects the black line.

– Once the rotation has been completed, the robot moves along the line in the same way as in the main part of the project until it reaches the start line.