110 likes | 127 Views
Learn to utilize touch sensors in robotics projects. Write an IC program for intelligent robot turning. Includes code examples and classwork exercises for practical application.
E N D
The George Washington University Department of ECE ECE 1010 - Intro: Electrical & Computer Engineering Dr. S. Ahmadi Class 4
Touch Sensors • Is a type of digital sensor: - Returns a 0 if NOT hit - Returns a 1 if HIT - As with any digital sensor, it is either “ON” or “OFF” - Digital sensors connect to digital ports (7-15) on the “lower deck” of the handyboard (see diagram of the handyboard on the next slide)
Touch Sensor • Digital sensor • Connect to digital ports 8-15 on the KIPR Link • Access with functiondigital(port#) • 1 indicates switch is closed • 0 indicates switch is open
Utilizing the touch sensor to help robot turn intelligently • Write an IC program that does the following: • runs forward for an unspecified amount of time • detects a “hit” using a digital sensor attached to the front of the robot and then backs up (for x seconds --- you decide what number is best for x – HINT use a variable!) • Download and run
Simple Example To Use Bumpers // Program to make a robot go forward. // If hit detected, go backwards for 4 seconds. int main() { while( a_button()==0 ){ } // Waits for user to press start button. while(1) { motor (1,70); // Turn on motor 1, @ 3% of speed. motor (3,70); // Turn on motor 3, @ 3% of speed. if ( digital(13)==1 ) // Check sensor connected to port { motor (1, -70); // Turn on motor 1, @ 3% of speed in opposite direction. motor (3, -70); // Turn on motor 1, @ 3% of speed in opposite direction. sleep(4); } return 0 }
Classwork • AA1FB int main() { fd(0); fd(2); sleep(3); bk(0); bk(2); sleep(3); ao(); }
Classwork • AA2Sen int main(){ while(1){ printf("Sensor reading : %d\n", analog10(0)); msleep(500); } return 0; }
Classwork • AA3FSenB_once int main(){ intcolor_threshold = 200; while(1){ if(analog10(0) > color_threshold&& analog10(1) > color_threshold) break; fd(0); fd(2); msleep(100); } • bk(0); • bk(2); • sleep(2); • ao(); • return 0; • }
Classwork • AA4FSenB_loop int main(){ intcolor_threshold = 200; while(1){ while(1){ if(analog10(0) > color_threshold && analog10(1) > color_threshold) break; fd(0); fd(2); msleep(100); } bk(0); bk(2); sleep(2); ao(); msleep(100); } return 0; }
Classwork • AA5SenB_three int main(){ intcolor_threshold = 200; int n = 1; while(n <= 3){ while(1){ if(analog10(0) > color_threshold && analog10(1) > color_threshold) break; fd(0); fd(2); msleep(100); } bk(0); bk(2); sleep(2); ao(); n = n+1; msleep(100); } return 0; }