00001
00002
00003
00004
00005
00006
00007
00008 #include "robotdo.h"
00009 #include "robotu.h"
00010 #include <avr/eeprom.h>
00011 #include <avr/interrupt.h>
00012
00013
00014 void do_boredom() {
00015 #ifdef BEHAVIOUR
00016 static u08 sendval = 0;
00017 if (d_pos > 300) modus = flee;
00018 if (needpower) modus = searching;
00019 if (light_value < 5) {
00020 start_sound(go_sleep,1);
00021 modus = sleep;
00022 }
00023 if (modus != connected) {
00024 modustime = 0;
00025 l_to=positionl;
00026 r_to=positionr;
00027 }
00028 speed = 100;
00029 if (irpos_value > 50) direction = 127; else direction = 0;
00030 #else
00031 if (time == 3000) SETMODUS(ircont);
00032 #endif
00033 }
00034 void do_sleep() {
00035 #ifdef BEHAVIOUR
00036 RESETP;
00037 if ((!soundplaying) & (modustime < 5)) {
00038 modustime++;
00039 start_sound(snore,0);
00040 }
00041 if (light_value > 70) {
00042 modus = boredom;
00043 start_sound(wake_up,1);
00044 }
00045 if (needpower) modus = searching;
00046 if (modus != sleep) {
00047 modustime = 0;
00048 l_to = positionl;
00049 r_to = positionr;
00050 }
00051 #endif
00052 }
00053 void do_connected() {
00054 static u16 delayt;
00055 static u08 conmodus;
00056 static u16 write_counter;
00057 static u08 sendbuf[IRMSLENGTH];
00058 irbittime = 12;
00059 direction = 0;
00060 speed = 0;
00061 switch (modustime) {
00062 case 0:
00063 modustime = 4;
00064 sendbuf[2]= 0;
00065 if (needpower)
00066 sendbuf[0] = sendbuf[1]= 'E';
00067 else
00068 sendbuf[0] = sendbuf[1]= 'F';
00069 ir_string_send(sendbuf);
00070 break;
00071 case 1:
00072 if ((vcc_value > BATFULL) | (!needpower)) {
00073 sendbuf[0]='F';sendbuf[1]=0;
00074 ir_string_send(sendbuf);
00075 modustime = 5;
00076 needpower = 0;
00077 } else {
00078 modustime = 2;
00079 delayt = 0;
00080 }
00081 break;
00082 case 2:
00083 speed = 20;
00084 if (delayt++ == 2000) {
00085 sendbuf[0]=1;
00086 ir_string_send(sendbuf);
00087 SETMODUS(searching);
00088 }
00089 if (vcco_value > 100) {
00090 modustime = 3;
00091 delayt = 0;
00092 }
00093 break;
00094 case 3:
00095 if (delayt++ > 65000) modustime = 4;
00096 if (vcco_value < 100) speed = 20; else RESETP;
00097 break;
00098 case 4:
00099 speed = -20;
00100 if (vcco_value < 100) {
00101 modustime = 1;
00102 }
00103 break;
00104 case 5:
00105 if (vcco_value > 100) speed = -20; else RESETP;
00106 if (needpower) modustime = 0;
00107 break;
00108 }
00109 #ifdef PROGRAMMING
00110 if (stringvalid) {
00111 switch (irrstring[1]) {
00112 case 'D':
00113 ir_int_send(write_counter);
00114 break;
00115 case 'W':
00116 conmodus = 2;
00117 write_counter = 102;
00118 break;
00119 case 'Q':
00120 conmodus = 0;
00121 eeprom_write_byte((u08*)(write_counter),0xFF);
00122 ir_int_send(write_counter);
00123 break;
00124 case 'I':
00125 conmodus = 1;
00126 write_counter = 1;
00127 break;
00128 case 'R':
00129 write_counter -= 2;
00130 break;
00131 case 'V':
00132 write_counter += 2;
00133 break;
00134 case 'S':
00135 SETMODUS (script);
00136 break;
00137
00138 default:
00139 if (conmodus) {
00140 if ((irrstring[1]>='a')&&(irrstring[1]<='z')) {
00141 eeprom_write_byte((u08*)(write_counter),irrstring[1]);
00142 eeprom_write_byte((u08*)(write_counter+1),atoi((u08*)(irrstring+3)));
00143 write_counter += 2;
00144 } else if (((irrstring[1]>='0')&&(irrstring[1]<='9'))||((irrstring[1]>='A')&&(irrstring[1]<='F'))) {
00145 eeprom_write_byte((u08*)write_counter,atoi((u08*)(irrstring+1)));
00146 write_counter += 1;
00147 }
00148 }
00149 }
00150 if (conmodus != 1) ir_string_send(irrstring+1);
00151 stringvalid = 0;
00152 irrstring[0]=0;
00153 }
00154 #endif
00155 }
00156 void do_searching() {
00157 static s16 ir_value_max, ir_value_min;
00158 u16 headingdiff;
00159 static u16 headingdiffold, headingmax, mtime;
00160
00161 switch (modustime) {
00162 case 0:
00163 start_sound(go_hungry, 1);
00164 RESETP;
00165 mtime = 0;
00166 direction = 0;
00167 speed = -40;
00168 modustime = 1;
00169 break;
00170 case 1:
00171 if (mtime++ > 3000) {
00172 ir_value_max = 0;
00173 ir_value_min = 32000;
00174 headingdiffold = -500;
00175 headingmax = (heading + 1127) % POS360;
00176 modustime = 2;
00177 mtime = 0;
00178 }
00179 break;
00180 case 2:
00181 direction = 127; speed = 20;
00182 play(irstation_value);
00183 if (irstation_value > ir_value_max) {
00184 ir_value_max = irstation_value;
00185 headingmax = (heading + POS360) % POS360;
00186 headingdiffold = 0;
00187 }
00188 if (irstation_value < ir_value_min) ir_value_min = irstation_value;
00189 headingdiff = (POS360 + heading - headingmax) % POS360;
00190 if (mtime < 4000) mtime++;
00191 if ((mtime >=4000) & (headingdiffold > headingdiff + (POS360/3))) {
00192 play(0);
00193 ir_value_max = irstation_value;
00194 ir_value_min = (ir_value_max + ir_value_min)>>1;
00195 mtime = 0;
00196 modustime = 4;
00197 }
00198 headingdiffold = headingdiff;
00199 break;
00200 case 4:
00201 speed = 80;
00202 direction = 0;
00203 if (((irstation_value < ir_value_min) & (ir_value_max - ir_value_min > 4)) | (mtime++ > 2000*3) | (d_pos > 300)) {
00204 modustime = 0;
00205 }
00206 break;
00207 }
00208 if (vcco_value > 100) {
00209 RESETP;
00210 SETMODUS (connected);
00211 start_sound(get_connected,1);
00212 }
00213 }
00214 void do_empty() {
00215 START_BEEP(3);
00216 if (vcco_value > 100) {
00217 modus = connected;
00218 vcc_value = BATLOW - 1;
00219 }
00220 }
00221 void do_flee() {
00222 #ifdef BEHAVIOUR
00223 #endif
00224 }
00225 void do_script() {
00226 #ifdef SCRIPT
00227 static u16 script_counter;
00228 static u16 wait;
00229 u16 x_unclipped;
00230 static u08 x_script;
00231 u08 command, para;
00232 if (!modustime) {
00233 script_counter = INITSKRIPT;
00234 direction = 0;
00235 speed = 0;
00236 wait = 0;
00237 modustime = 1;
00238 }
00239 if (wait>0) {
00240 wait--;
00241 return;
00242 }
00243 command = eeprom_read_byte(script_counter);
00244 para = eeprom_read_byte(script_counter + 1);
00245 script_counter += 2;
00246 switch (command) {
00247 case 'r': drive_to_pos(0, (para - 128) * 10); break;
00248 case 'l': drive_to_pos((para - 128) * 10, 0); break;
00249 case 's': speed = para - 128; break;
00250 case 'd': direction = para - 128; break;
00251 case 'w': wait = para * 146; break;
00252 case 'p': playt(para); break;
00253 case 'm': SETMODUS(para); break;
00254 case 'g': script_counter = INITSKRIPT + 0*(para / 2); break;
00255 #ifdef SCRIPT2
00256 case 'h': switch (para) {
00257 case 0: x_unclipped = light_value; break;
00258 case 1: x_unclipped = radar_value>>8; break;
00259 case 2: x_unclipped = irpos_value>>8; break;
00260 case 3: x_unclipped = d_pos; break;
00261 }
00262 x_script=(x_unclipped>255)?255:x_unclipped;
00263 break;
00264 case 'i': if (x_script >= para) script_counter +=2; break;
00265 #endif
00266 default: START_BEEP(1);
00267 speed = 0;
00268 SETMODUS(connected);
00269 break;
00270 }
00271 if (needpower) SETMODUS(searching);
00272 #endif
00273 }
00274 void do_ircont() {
00275 #ifdef IRCONT
00276 static u16 stop;
00277 u08 i, imax, imaxn, i2;
00278 u08 ircmp[IRKEYS];
00279 if (modustime == 0) {
00280 modustime++;
00281 irbittime = eeprom_read_byte(IRKEYS*20+1);
00282 for (i = 0; i < IRKEYS; i++)
00283 for (i2 = 0; i2 < IRMLENGTH; i2++)
00284 cmp_target[i][i2] = eeprom_read_byte(1+i*IRMLENGTH+i2);
00285 }
00286 if (stop++ > 2000) speed = 0;
00287 if (pulsevalid) {
00288 pulsevalid = 0;
00289 imax = 0;
00290 imaxn= 0xff;
00291 for (i=0; i < IRKEYS; i++) {
00292 ircmp[i] = ir_comp(cmp_target[i], irrbuf);
00293 if (imax < ircmp[i]) {
00294 imax = ircmp[i];
00295 imaxn = i;
00296 stop = 0;
00297 }
00298 }
00299 if (imax < 130) return;
00300 speed = 40;
00301 direction = 0;
00302 switch (imaxn) {
00303 case 0:
00304 speed = 127;
00305 break;
00306 case 1:
00307 direction = 127;
00308 break;
00309 case 2:
00310 direction = -127;
00311 break;
00312 case 3:
00313 speed = -40;
00314 break;
00315 }
00316 }
00317 if (vcco_value > 100) {
00318 speed = 0;
00319 SETMODUS (connected);
00320 start_sound(get_connected,1);
00321 }
00322 #endif
00323 }
00324