Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

linetrack.c

Go to the documentation of this file.
00001 
00014 /*
00015  *  The contents of this file are subject to the Mozilla Public License
00016  *  Version 1.0 (the "License"); you may not use this file except in
00017  *  compliance with the License. You may obtain a copy of the License at
00018  *  http://www.mozilla.org/MPL/
00019  *
00020  *  Software distributed under the License is distributed on an "AS IS"
00021  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
00022  *  License for the specific language governing rights and limitations
00023  *  under the License.
00024  *
00025  *  The Original Code is legOS code, released October 17, 1999.
00026  *
00027  *  The Initial Developer of the Original Code is Markus L. Noga.
00028  *  Portions created by Markus L. Noga are Copyright (C) 1999
00029  *  Markus L. Noga. All Rights Reserved.
00030  *
00031  *  Contributor(s): Markus L. Noga <markus@noga.de>
00032  */
00033 
00034 #include <config.h>
00035 #if defined(CONF_DSENSOR) && defined(CONF_DMOTOR)
00036 
00037 #include <conio.h>
00038 #include <unistd.h>
00039 #include <tm.h>
00040 
00041 #include <dsensor.h>
00042 #include <dmotor.h>
00043 
00044 #define LIGHTSENS       SENSOR_2
00045 #define DARK_THRESH     0x40
00046 #define BRIGHT_THRESH   0x48
00047 
00048 #define NORMAL_SPEED (2*MAX_SPEED/3)
00049 #define TURN_SPEED   (MAX_SPEED)
00050 
00051 // #define STRAIGHT_LINE
00052 
00053 
00054 static wakeup_t dark_found(wakeup_t data) {
00055   return LIGHT(LIGHTSENS)<(unsigned short)data;
00056 }
00057 
00058 static wakeup_t bright_found(wakeup_t data) {
00059   return LIGHT(LIGHTSENS)>(unsigned short)data;
00060 }
00061 
00062 static void locate_line() {
00063   motor_a_speed(NORMAL_SPEED);
00064   motor_c_speed(NORMAL_SPEED);
00065   motor_a_dir(fwd);
00066   motor_c_dir(fwd);
00067   
00068   wait_event(dark_found,DARK_THRESH);
00069 }
00070 
00071 static void follow_line() {
00072   int dir=0;
00073   
00074   
00075   while (!shutdown_requested()) {
00076     motor_a_speed(NORMAL_SPEED);
00077     motor_c_speed(NORMAL_SPEED);
00078     motor_a_dir(fwd);
00079     motor_c_dir(fwd);
00080     
00081     if (wait_event(bright_found,BRIGHT_THRESH) != 0)
00082     {    
00083             if(dir==0)
00084             motor_a_dir(rev);
00085             else
00086             motor_c_dir(rev);
00087 #ifdef STRAIGHT_LINE
00088                 dir=!dir;
00089 #endif
00090         
00091             motor_a_speed(TURN_SPEED);
00092           motor_c_speed(TURN_SPEED);
00093     
00094         wait_event(dark_found,DARK_THRESH);
00095     }
00096   }
00097 }
00098 
00099 int main(int argc, char *argv[]) {
00100   ds_active(&LIGHTSENS);
00101   
00102   locate_line();
00103   follow_line();
00104   
00105   return 0;
00106 }
00107 #else
00108 #warning linetrack.c requires CONF_DSENSOR and CONF_DMOTOR
00109 #warning linetrack demo will do nothing
00110 int main(int argc, char *argv[]) {
00111   return 0;
00112 }
00113 #endif // defined(CONF_DSENSOR) && defined(CONF_DMOTOR)

Generated on Fri Feb 25 08:02:35 2005 for brickos by  doxygen 1.3.9.1