The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer...
-
Upload
mae-simpson -
Category
Documents
-
view
213 -
download
0
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/7.jpg)
Loops
![Page 8: The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f385503460f94c55430/html5/thumbnails/16.jpg)
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.