ここに掲載するプログラムは、実際にロボコンの全国大会で使用したものです。言語は Interactive C という M.I.T. が開発した Rug-Warrior というロボットに使われているCライクな言語です。
a:\src1.c
a:\src2.c
a:\src5.c
a:\src8.c
a:\src9.c
a:\src10.c
a:\src11.c
a:\xmas.c
a:\om.c
a:\hotaru3.c
a:\music1.c
/****************************************************************/
/* */
/* TITLE: 1.main program */
/* */
/* FILE NAME: src1.c */
/* ver.1.0 96/9/13 */
/* written by sachiya yamamoto */
/****************************************************************/
int pid2;
int new_win,old_win;
void main()
{
int condition = 0;
initialize();
pid2 = start_process(fanfare());
while(1)
{
condition = analog(pyro);
new_win=0;
if(condition <= 27)
{
printf("\nMANUAL");
manual();
i = 0;
n = 0;
}
else if(condition >= 100 && condition <= 155)
{ new_win=1;
printf("\nWIN");
win(); }
else if(condition >= 37 && condition <= 90)
{ printf("\nLOST");
lost(); }
else if(condition >= 165)
{ printf("\nETC."); }
if(old_win && !new_win)
beeper_off();
old_win=new_win;
}
}
void initialize()
{
/* main */ new_win = 0;
old_win = 0;
/* win */ n = 0;
/* lost */ i = 0;
/* shoot */ safety_1 = 0;
safety_2 = 0;
safety_3 = 0;
safety_4 = 0;
/* manual */ flag_rev = 0;
}
/*****************************************************************************/
/* */
/* FUNCTION : void RUN_AWAY() */
/* */
/* SUB FUNCTION in RUN_AWAY() : int SIGNAL() & void RUN() */
/* */
/* */
/* */
/* */
/* produced by DAISAKU TAKABAYASHI 1996,11,14 (THU) REV. */
/*****************************************************************************/
int safety_1;
int safety_2;
int safety_3;
int safety_4;
void run( float left, float right )
{
motor(0,left);
motor(1,right);
}
int signal()
{
int port_A=0x1000;
int signal=0;
int judge=0;
while(1)
{
judge = bumper() & 0b100;
signal=peek(port_A) & 0b00000110; /* masking */
if( signal==0x4 ) /* 0b 00000010 */
{
if( judge==0 )
{
return (1);
printf("shoot outer center \n");
}
if( judge==0x4 )
{
printf("shoot inner center \n");
return (4);
}
}
else if( signal==0x2 && judge==0 ) /* 0b 00000100 */
{
printf("shoot outer left \n");
return (2);
}
else if( signal==0x6 && judge==0 ) /* 0b 00000110 */
{
printf("shoot outer right \n");
return (3);
}
else
{
printf("NO Signal\n");
return (0);
}
sleep( 0.5 );
}
}
void run_away()
{
float hi_spd;
float lo_spd;
hi_spd=100.0;
lo_spd=10.0;
if( signal()==1 & safety_1==0 ) /* outer center */
{
sleep( 0.2 );
run( -hi_spd,-hi_spd );
sleep( 1.3 );
run( 0.0,0.0 );
safety_1=1;
}
else if( signal()==2 & safety_2==0 ) /* outer left */
{
sleep( 0.2 );
run( hi_spd,lo_spd );
sleep( 0.4 );
run( hi_spd,hi_spd );
sleep( 0.4 );
run( lo_spd,hi_spd );
sleep( 0.4 );
run( 0.0,0.0 );
safety_2=1;
}
else if( signal()==3 & safety_3==0 ) /* outer right */
{
sleep( 0.2 );
run( lo_spd,hi_spd );
sleep( 0.4 );
run( hi_spd,hi_spd );
sleep( 0.3 );
run( hi_spd,lo_spd );
sleep( 0.4 );
run( 0.0,0.0 );
safety_3=1;
}
else if( signal()==4 & safety_4==0 & safety_1==1 )/* inner center */
{
sleep( 0.2 );
run( -hi_spd,-hi_spd );
sleep( 1.0 );
run( 0.0,0.0 );
safety_4=1;
}
}
/************************************************************************/
/* */
/* ----- Manual Mode ----- No.1 Date 1996/08/29 */
/* No.2 Date 1996/10/06 */
/* No.3 Date 1996/10/25 */
/* */
/* Written by Satoshi Ohzora */
/* */
/************************************************************************/
int flag_rev;
void manual()
{
int fb,lr;
int flag_fb,flag_lr;
float fb_p,lr_p,turn_p;
float CENTER = 112.0;
float MAX = 186.0;
float MIN = 36.0;
float dir_p;
float dir_m;
float t;
kill_process(pid1);
kill_process(pid3);
dir_p = CENTER - MIN;
dir_m = MAX - CENTER;
fb = analog(6);
lr = analog(7);
printf("\nMANUAL");
if(fb < 100)
{ fb_p = ((CENTER-(float)fb)/dir_p)*100.0;
flag_fb = 1;} /* front */
else if(fb >= 100 && fb <= 124)
{ fb_p = 0.0;
flag_fb = 0;} /* stop */
else if(fb > 124)
{ fb_p = -(((float)fb-CENTER)/dir_m)*100.0;
flag_fb = -1;} /* back */
if(lr < 100)
{ lr_p = ((CENTER-(float)lr)/dir_p)*100.0;
flag_lr = 1;} /* right */
else if(lr >= 100 && lr <= 124)
{ lr_p = 0.0;
flag_lr = 0;} /* stop */
else if(lr > 124)
{ lr_p = -(((float)lr-CENTER)/dir_m)*100.0;
flag_lr = -1;} /* left */
turn_p = lr_p;
if(flag_fb == 1 && flag_lr == 1)
{ motor(0,fb_p);
motor(1,fb_p*(1.0-(lr_p*0.01)));}
else if(flag_fb == 1 && flag_lr == 0)
{ motor(0,fb_p);
motor(1,fb_p);}
else if(flag_fb == 1 && flag_lr == -1)
{ motor(0,fb_p*(1.0-(-lr_p*0.01)));
motor(1,fb_p);}
else if(flag_fb == 0 && flag_lr == 1)
{ if(flag_rev == -1){turn_p = -lr_p;}
else{turn_p = lr_p;}
motor(0,turn_p*0.5);
motor(1,-turn_p*0.5);}
else if(flag_fb == 0 && flag_lr == 0)
{ motor(0,0.0);
motor(1,0.0);
flag_rev = 0;} /*reverse turn off*/
else if(flag_fb == 0 && flag_lr == -1)
{ if(flag_rev == 1){turn_p = -lr_p;}
else{turn_p = lr_p;}
motor(0,turn_p*0.5);
motor(1,-turn_p*0.5);}
else if(flag_fb == -1 && flag_lr == 1)
{ motor(0,fb_p);
motor(1,fb_p*(1.0-(lr_p*0.01)));
flag_rev = -1;} /*reverse turn on(left->right)*/
else if(flag_fb == -1 && flag_lr == 0)
{ motor(0,fb_p);
motor(1,fb_p);}
else if(flag_fb == -1 && flag_lr == -1)
{ motor(0,fb_p*(1.0-(-lr_p*0.01)));
motor(1,fb_p);
flag_rev = 1;} /*reverse turn on(right->left)*/
run_away();
}
void end()
{
motor(0,0.0);
motor(1,0.0);
printf("\nE N D!");
}
int i;
int pid3;
void lost()
{
float wait1 = 0.3;
float wait2 = 0.9;
float speed1 = 100.0;
kill_process(pid1);
kill_process(pid2);
if(i == 0) { pid3 = start_process(hotaru()); }
if(i < 3)
{
motor(0,speed1);
motor(1,0.0);
sleep(wait1);
}
else if(i >= 3 && i < 6)
{
motor(0,0.0);
motor(1,speed1);
sleep(wait1);
}
else if(i >= 6 && i < 13)
{
if(i%2 == 0)
{
motor(0,speed1);
motor(1,0.0);
sleep(wait1);
}
else if(i%2 == 1)
{
motor(0,0.0);
motor(1,speed1);
sleep(wait1);
}
}
else if(i >= 13 && i < 16)
{
motor(0,-speed1);
motor(1,0.0);
sleep(wait1);
}
else if(i >= 16 && i < 19)
{
motor(0,0.0);
motor(1,-speed1);
sleep(wait1);
}
else if(i >= 19 && i < 26)
{
if(i%2 == 1)
{
motor(0,-speed1);
motor(1,0.0);
sleep(wait1);
}
else if(i%2 == 0)
{
motor(0,0.0);
motor(1,-speed1);
sleep(wait1);
}
}
else if(i >= 26 && i < 45)
{
if(i%2 == 0)
{
motor(0,speed1);
motor(1,speed1);
sleep(wait1);
}
else if(i%2 == 1)
{
motor(0,-speed1);
motor(1,-speed1);
sleep(wait1);
}
}
motor(0,0.0);
motor(1,0.0);
if(i==2 || i==5 || i==12 || i==15 || i==18 || i==25)
sleep(wait2);
else if(i >= 26 && i < 45){}
else if(i == 45) { kill_process(pid3); }
else sleep(wait1);
i++;
}
int pid1;
int n;
void win()
{
float st_v = 80.0;
float rt_v = 70.0;
float rt_v2 = 20.0;
float rt_v3 = 60.0;
float rt_v4 = 10.0;
float stop1 = 0.5;
float stop2 = 1.5;
if(n == 0)
{ kill_process(pid2);
kill_process(pid3);
pid1 = start_process(satoshi());
motor(0,-rt_v); motor(1,rt_v);
sleep(stop1);
}
else if((n >= 1 && n < 4) || (n >= 5 && n < 8)
|| (n >= 14 && n < 17) || (n >= 19 && n < 22))
{ motor(0,st_v); motor(1,st_v);
sleep(stop1-0.1);
motor(0,0.0); motor(1,0.0);
sleep(0.1);
}
else if(n == 4 || n == 17)
{ motor(0,rt_v); motor(1,-rt_v);
sleep(stop1);
}
else if(n == 8 || n == 22)
{ motor(0,-(rt_v-10.0)); motor(1,rt_v-10.0);}
else if((n >= 9 && n < 11) || (n >= 23 && n < 25))
{ sleep(stop1);
motor(0,rt_v); motor(1,rt_v2);
sleep(stop1);
motor(0,rt_v2); motor(1,rt_v);
}
else if(n >= 11 && n < 13)
{ motor(0,-rt_v2); motor(1,-rt_v);
sleep(stop1);
if(n == 11)
{ motor(0,-rt_v); motor(1,-rt_v2);
sleep(stop1);
}
}
else if(n == 13)
{ motor(0,-rt_v); motor(1,rt_v);
sleep(stop1);
}
else if(n == 25)
{ motor(0,rt_v3); motor(1,rt_v4);
sleep(stop2);
}
else if(n == 26)
{ motor(0,0.0); motor(1,0.0);
kill_process(pid1);
}
n++;
}
void fanfare()
{
char x_mas[] = "U4e4e6e2r 4e4e6e2r 4e4gD6c2d14e2r 4f4f6f2f 4f4e4e4e 4e4d4d4e8d8g 4e4e6e2r 4e4e6e2r 4e4gD6c2d14e2r 4f4f6f2f 4f4e4e4e 4g4g4f4d12c";
tempo = 10;
play(x_mas);
}
void satoshi()
{
char yohey[] = "U6e2d6c2b6c2d6c2r D6e2f6g2a6g2e6g2r 6c2d6e2r6e2r6e2d6c2d6e2r6d2r6d2r 6e2d6c2b6c2d6c2r D6e2f6g2a6g2e6g2r 6c2d6e2r6g2r6g2e6c2d6e2r6d2r6c2r";
tempo = 8;
play(yohey);
}
void hotaru()
{
char hotaru[] = "4a 6d2d4d4#f 6e2d4e4#f 4d4d4#f4a 12b4b 6a2#f4#f4d 6e2d4e4#f 6d2b4b4a 12dU4b 6a2#f4#f4d 6e2d4eU4b 6a2#f4#f4a 12b4b 6a2#f4#f4d 6e2d4e4#f 6d2b4b4a 16d";
tempo = 16;
play(hotaru);
}
int tempo = 12;
long time,newtime;
int music_current_command= 'o';
int music_next_command= 0;
void play(char song[])
{
int i, duration, accidental, delta, note, rest;
int notes[]= {0,2,3,5,7,8,10};
int old_note= 30;
play_reset();
i= 0;
while (song[i]) {
while (1) {
while (song[i] == ' ') ++i;
if (song[i] == 'X') {++i; music_next_command= song[i]; ++i;}
else break;
}
if (!song[i]) break;
while (song[i] == 'D') {
old_note -= 12;
++i;}
while (song[i] == 'U') {
old_note += 12;
++i;}
if ('0' <= song[i] && song[i] <= '9') {
duration= 0;
while ('0' <= song[i] && song[i] <= '9') {
duration = duration * 10 + song[i]-'0';
++i;
}}
if (song[i] == '#') {
accidental= 1;
++i;
} else if (song[i] == '&') {
accidental= -1;
++i;
} else {
accidental= 0;
}
if (song[i] == 'r') rest= 1;
else {
if (song[i] < 'a' || song[i] > 'g') {
printf("\nBad note:%c\n", song[i]);
beep();
beep();
beep();
sleep(5.0);
return;
}
note= notes[song[i]-'a'] + accidental;
rest= 0;
}
i++;
if (rest) {
play_note(0, duration);
} else {
delta= note - (old_note % 12);
old_note += delta;
if (delta > 5) old_note -= 12;
if (delta < -5) old_note += 12;
play_note(old_note, duration);
}}
play_note(0, 1);
music_command('o');
}
void music_command(int c)
{ music_current_command= c; }
void play_reset()
{ time= mseconds();
newtime= time+100L;
}
void play_note(int note, int duration)
{
play_note_2(note, duration*7);
play_note_2(0, duration);
}
void play_note_2(int note, int duration)
{
float freq;
int period;
if (note) { freq= 55.0 * (2. ^ (((float) note) / 12.)); }
while (mseconds() < newtime);
if (note) {
set_beeper_pitch(freq);
beeper_on();
}
else { beeper_off(); }
if (music_next_command &&
music_current_command != music_next_command){
music_command(music_next_command);
music_next_command= 0;
}
newtime += (long)(duration * tempo);
}