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_3
6
#define RED       SENSOR_2
7
#define BBUMPER   SENSOR_1
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_CFG(SENSOR_TYPE_LIGHT, SENSOR_MODE_RAW)); // 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
  SetTxPower(TX_POWER_HI);    // aby daleko videl
25
 
26
  OnFwd (MLEFT);         // cuknuti na shozeni radlic
27
  OnFwd (MRIGHT);
28
  Wait(20);
29
  OnRev (MLEFT);
30
  OnRev (MRIGHT);
31
  Wait(70);
32
  Off(MLEFT);
33
  Off(MRIGHT);
34
 
35
  Wait(20);  // pockej na ustaleni narazniku
36
 
37
  // rozjed tasky
38
  start blue_bumper;
39
  start cervena;
40
  start cara_modra;
41
  start stochastika;
42
}
43
 
44
task stochastika()        // nahodne otaceni
45
{
46
  while(true) Zatoc();
47
}
48
 
49
void GoBlue()       // pojezd ve smeru modreho senzoru
50
{
51
  OnFwd (MLEFT);
52
  OnFwd (MRIGHT);
53
  Wait(300);              // popojed kus
54
  Off(MLEFT);
55
  Off(MRIGHT);
56
}
57
 
58
void GoRed()         // pojezd ve smeru cerveneho senzoru
59
{
60
  OnRev (MLEFT);
61
  OnRev (MRIGHT);
62
  Wait(300);              // popojed kus
63
  Off(MLEFT);
64
  Off(MRIGHT);
65
}
66
 
67
void Obrat()         // obrat o 180 stupnu
68
{
69
  OnRev (MLEFT);
70
  OnFwd (MRIGHT);
71
  Wait(125);              // popojed kus
72
  Off(MLEFT);
73
  Off(MRIGHT);
74
}
75
 
76
void Zatoc()  // nahodne zatoceni, nebo popojeti
77
{
78
  switch (Random(1))
79
  {
80
    case 0:
81
     OnRev (MLEFT);
82
     OnFwd (MRIGHT);
83
     Wait(50);
84
     break;
85
    case 1:
86
     OnFwd (MLEFT);
87
     OnRev (MRIGHT);
88
     Wait(50);
89
     break;
90
    case 2:
91
     OnRev (MLEFT);
92
     OnRev (MRIGHT);
93
     Wait(70);
94
     break;
95
    case 3:
96
     OnFwd (MLEFT);
97
     OnFwd (MRIGHT);
98
     Wait(70);
99
     break;
100
  }
101
}
102
 
103
task blue_bumper() // naraznik na modre strane
104
{
105
  while(true)
106
  {
107
    if (1 == BBUMPER) // utoc, kdyz neco narazi do narazniku
108
    {
109
      stop stochastika;
110
      PlaySound (SOUND_FAST_UP);
111
      OnFwd (MLEFT);
112
      OnFwd (MRIGHT);
113
      Wait(50);
114
    }
115
  }
116
}
117
 
118
task cervena() // dalkovy sensor
119
{
120
  int lastlevel;
121
 
122
  lastlevel = 0;
123
  while(true)
124
  {
125
    SendMessage(0);
126
    if(lastlevel > SENSOR_2)
127
    {
128
      PlaySound(SOUND_FAST_UP);
129
      stop stochastika;
130
      OnRev (MLEFT);
131
      OnRev (MRIGHT);
132
      Wait(10);              // popojed kus
133
      Off(MLEFT);
134
      Off(MRIGHT);
135
      Obrat();
136
      GoBlue();
137
      start stochastika;
138
    }
139
    lastlevel = SENSOR_2;
140
    lastlevel -= THRESHOLD;
141
  }
142
}
143
 
144
task cara_modra()    // hlidani cary
145
{
146
  while(true)
147
  {
148
     if (BLUE < THRESHOLD)    // cekej na caru
149
     {
150
        PlaySound (SOUND_DOUBLE_BEEP);
151
        stop blue_bumper;
152
        stop stochastika;
153
        stop cervena;
154
        GoRed();
155
        start cervena;
156
        start blue_bumper;
157
        start stochastika;
158
     }
159
  }
160
}
161