Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1 kaklik 1
#define THRESHOLD  44     // rozhodovaci uroven mezi cernou a bilou
2
#define RED_WHITE  60     // maximum bile na cervenem senzoru
3
#define MLEFT     OUT_A
4
#define MRIGHT    OUT_C
5
#define BLUE      SENSOR_1
6
#define RED       SENSOR_2
7
#define BBUMPER   SENSOR_3
8
#define START_ALL start blue_bumper; start cara_cervena; start cara_modra;
9
 
10
int semafor;
11
 
12
task main()
13
{
14
  PlaySound (SOUND_DOUBLE_BEEP);
15
  Wait(500); // 5s podle pravidel
16
  SetSensor(BLUE,SENSOR_LIGHT);   // senzor na caru modry
17
  SetSensor(RED,SENSOR_LIGHT);   // senzor na caru cerveny
18
  SetSensor(BBUMPER, SENSOR_TOUCH);  // naraznik modry
19
  SetSensorMode (BBUMPER, SENSOR_MODE_BOOL); // koncak bude ...
20
  SetPower (MLEFT,OUT_FULL);
21
  SetPower (MRIGHT,OUT_FULL);
22
  SetDirection(MLEFT, OUT_FWD);
23
  SetDirection(MRIGHT, OUT_FWD);
24
 
25
  k_care();
26
 
27
  start red_bumper;
28
  start blue_bumper;
29
  start cara_cervena;
30
  start cara_modra;
31
  semafor = false;
32
 
33
  ClearTimer (0);
34
  start stochastika;
35
}
36
 
37
task stochastika()
38
{
39
   while (true)
40
   {
41
     Wait(500);
42
     if (semafor) start blue_bumper;
43
 
44
     PlaySound (SOUND_CLICK);
45
 
46
     OnFwd (MLEFT);                  // dojezd k care
47
     OnFwd (MRIGHT);                 // dojezd k care
48
     while (BLUE > THRESHOLD);
49
     GoRed();
50
     OnRev (MLEFT);
51
     OnRev (MRIGHT);
52
     Wait(100);              // popojed kus
53
     Zatoc();
54
 
55
     ClearTimer (0);
56
   }
57
}
58
 
59
void GoBlue()
60
{
61
  OnFwd (MLEFT);
62
  OnFwd (MRIGHT);
63
  Wait(100);              // popojed kus
64
  Off(MLEFT);
65
  Off(MRIGHT);
66
}
67
 
68
void GoRed()
69
{
70
  OnRev (MLEFT);
71
  OnRev (MRIGHT);
72
  Wait(100);              // popojed kus
73
  Off(MLEFT);
74
  Off(MRIGHT);
75
}
76
 
77
void Zatoc()
78
{
79
  switch (Random(2))
80
  {
81
    case 0:
82
     OnRev (MLEFT);
83
     OnFwd (MRIGHT);
84
     break;
85
    case 1:
86
     OnFwd (MLEFT);
87
     OnRev (MRIGHT);
88
     break;
89
    case 2:
90
     OnRev (MLEFT);
91
     OnRev (MRIGHT);
92
  }
93
  Wait(100);              // popojed kus
94
  Off(MLEFT);
95
  Off(MRIGHT);
96
}
97
 
98
void k_care()  // najed do vyckavaci pozice
99
{
100
  OnFwd (MLEFT);                  // dojezd k care
101
  OnFwd (MRIGHT);                  // dojezd k care
102
  while (BLUE > THRESHOLD);
103
  GoRed();
104
  OnRev (MLEFT);
105
  OnRev (MRIGHT);
106
  Wait(100);              // popojed kus
107
  Off(MLEFT);
108
  Off(MRIGHT);
109
}
110
 
111
task red_bumper()
112
{
113
  while(true)
114
  {
115
    while (100 != RED); // utoc, kdyz neco narazi do narazniku
116
    PlaySound (SOUND_FAST_UP);
117
    stop stochastika;
118
    Zatoc();
119
    start stochastika;
120
    if (semafor) start blue_bumper;
121
  }
122
}
123
 
124
task blue_bumper()
125
{
126
    semafor = false;
127
    while (0 == BBUMPER); // utoc, kdyz neco narazi do narazniku
128
    PlaySound (SOUND_FAST_UP);
129
    OnFwd (MLEFT);
130
    OnFwd (MRIGHT);
131
    semafor = true;
132
}
133
 
134
task cara_cervena() // hlidani natlaceni na caru
135
{
136
  while(true)
137
  {
138
    while (RED < THRESHOLD || RED > RED_WHITE); // utoc, kdyz nas neco dotlaci k care
139
    Wait(2);        // kontrola za 20ms, aby se to nespletlo s naraznikem
140
    if (RED > THRESHOLD && RED < RED_WHITE)
141
    {
142
      PlaySound (SOUND_DOUBLE_BEEP);
143
      GoBlue();
144
    }
145
  }
146
}
147
 
148
task cara_modra()    // hlidani cary
149
{
150
  while(true)
151
  {
152
     while (BLUE > THRESHOLD);    // cekej na caru
153
     stop cara_cervena;
154
     PlaySound (SOUND_DOUBLE_BEEP);
155
     GoRed();
156
     if (semafor) start blue_bumper;
157
     start cara_cervena;
158
  }
159
}
160