00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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)