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
  OnFwd (MLEFT);                  // dojezd k care
27
  OnFwd (MRIGHT);                  // dojezd k care
28
  Wait(50);
29
  GoRed();
30
 
31
  start red_bumper;
32
  start blue_bumper;
33
  start cara_cervena;
34
  start cara_modra;
35
  semafor = false;
36
 
37
  ClearTimer (0);
38
  start stochastika;
39
}
40
 
41
task stochastika()
42
{
43
   while (true)
44
   {
45
     Wait(500);
46
     if (semafor) start blue_bumper;
47
 
48
     PlaySound (SOUND_CLICK);
49
 
50
     OnFwd (MLEFT);                  // dojezd k care
51
     OnFwd (MRIGHT);                 // dojezd k care
52
     while (BLUE > THRESHOLD);
53
     GoRed();
54
     OnRev (MLEFT);
55
     OnRev (MRIGHT);
56
     Wait(100);              // popojed kus
57
     Zatoc();
58
 
59
     ClearTimer (0);
60
   }
61
}
62
 
63
void GoBlue()
64
{
65
  OnFwd (MLEFT);
66
  OnFwd (MRIGHT);
67
  Wait(100);              // popojed kus
68
  Off(MLEFT);
69
  Off(MRIGHT);
70
}
71
 
72
void GoRed()
73
{
74
  OnRev (MLEFT);
75
  OnRev (MRIGHT);
76
  Wait(100);              // popojed kus
77
  Off(MLEFT);
78
  Off(MRIGHT);
79
}
80
 
81
void Zatoc()
82
{
83
  switch (Random(2))
84
  {
85
    case 0:
86
     OnRev (MLEFT);
87
     OnFwd (MRIGHT);
88
     break;
89
    case 1:
90
     OnFwd (MLEFT);
91
     OnRev (MRIGHT);
92
     break;
93
    case 2:
94
     OnRev (MLEFT);
95
     OnRev (MRIGHT);
96
  }
97
  Wait(100);              // popojed kus
98
  Off(MLEFT);
99
  Off(MRIGHT);
100
}
101
 
102
void k_care()  // najed do vyckavaci pozice
103
{
104
  OnFwd (MLEFT);                  // dojezd k care
105
  OnFwd (MRIGHT);                  // dojezd k care
106
  while (BLUE > THRESHOLD);
107
  GoRed();
108
  OnRev (MLEFT);
109
  OnRev (MRIGHT);
110
  Wait(100);              // popojed kus
111
  Off(MLEFT);
112
  Off(MRIGHT);
113
}
114
 
115
task red_bumper()
116
{
117
  while(true)
118
  {
119
    while (100 != RED); // utoc, kdyz neco narazi do narazniku
120
    PlaySound (SOUND_FAST_UP);
121
    stop stochastika;
122
    GoRed();
123
//!!!    Zatoc();
124
    start stochastika;
125
    if (semafor) start blue_bumper;
126
  }
127
}
128
 
129
task blue_bumper()
130
{
131
    semafor = false;
132
    while (0 == BBUMPER); // utoc, kdyz neco narazi do narazniku
133
    PlaySound (SOUND_FAST_UP);
134
    OnFwd (MLEFT);
135
    OnFwd (MRIGHT);
136
    semafor = true;
137
}
138
 
139
task cara_cervena() // hlidani natlaceni na caru
140
{
141
  while(true)
142
  {
143
    while (RED < THRESHOLD || RED > RED_WHITE); // utoc, kdyz nas neco dotlaci k care
144
    Wait(2);        // kontrola za 20ms, aby se to nespletlo s naraznikem
145
    if (RED > THRESHOLD && RED < RED_WHITE)
146
    {
147
      PlaySound (SOUND_DOUBLE_BEEP);
148
      GoBlue();
149
    }
150
  }
151
}
152
 
153
task cara_modra()    // hlidani cary
154
{
155
  while(true)
156
  {
157
     while (BLUE > THRESHOLD);    // cekej na caru
158
     stop cara_cervena;
159
     PlaySound (SOUND_DOUBLE_BEEP);
160
     GoRed();
161
     if (semafor) start blue_bumper;
162
     start cara_cervena;
163
  }
164
}
165