Otto - riadiaci program v.2

From DT^2
Revision as of 11:01, 3 August 2018 by 158.195.189.186 (talk)
Jump to: navigation, search

Program umožňuje priame riadenie robota (aj cez BlueTooth). Teraz už funguje aj na pinoch 2,4 a môže sa naraz programovať aj komunikovať.

Na komunikáciu s robotom môžete použiť napr. program Putty: putty.exe.

klaves servo pin Kalibracia
a/q ľavá ruka 10 4
;/p pravá ruka 11 5
z/x ľavá noha 9 3
,/. pravá noha 6 2
d/c ľavá päta 5 1
k/m pravá päta 3 0
1/9 zníž/zvýš rýchlosť pohybu -
TXD BT 2
RXD BT 4
TRIG UZ 7
ECHO UZ 8
Sirena 12

Ďalej umožňuje

  • kalibrovať strednú polohu servomotorov (pomocou klávesu H)
    • pomocou + a - najdeme nulovu polohu serva
    • eneter ideme na dalsie servo
    • ukoncime E ulozenie do eprom
    • potvrdime Y
  • kalibrovať limity pre všetky stupne voľnosti (pomocou klávesu J), zobrazenie celej kalibrácie: kláves G
  • celú kalibráciu je možné zapísať do trvalej pamäte EEPROM pomocou klávesu E (používať opatrne!), po zapnutí sa automaticky načíta
  • pozdraví po klávese -, ruky dá hore na kláves R
  • načíta choreografiu po klávese @,
  • zobrazí načítanú choreografiu na kláves ? a
  • zatancuje načítanú choreografiu - kláves t.
  • Na klávesách 3-6 je možné nastaviť rôznu kombináciu (funkcie kombinacia1() - kombinacia4()).
  • Kláves U testuje ultrazvuk
  • Medzera resetne všetky servo motory do strednej polohy.
  1 #include <Servo.h>
  2 #include <EEPROM.h>
  3 
  4 #define ECHO_BT_TO_USB 1
  5 
  6 #define US_TRIG  7
  7 #define US_ECHO  8
  8 
  9 #define BT_RX   2 
 10 #define BT_TX   4
 11 
 12 #define SIRENA 12
 13 
 14 //maximalna dlzka choreografie
 15 #define CHOREO_LEN 200
 16 
 17 // cisla pinov, kde su zapojene servo motory
 18 #define PIN_SERVO_LAVA_RUKA   10 
 19 #define PIN_SERVO_PRAVA_RUKA  11
 20 #define PIN_SERVO_LAVA_NOHA   9
 21 #define PIN_SERVO_PRAVA_NOHA  6
 22 #define PIN_SERVO_LAVA_PATA   5
 23 #define PIN_SERVO_PRAVA_PATA  3
 24 
 25 #define S1 3
 26 #define S2 5
 27 #define S3 6
 28 #define S4 9
 29 #define S5 10
 30 #define S6 11
 31 #define SIRENA 12
 32 
 33 //maximalna dlzka choreografie
 34 #define CHOREO_LEN 200
 35 
 36 // tu su serva cislovane 1-6
 37 #define SERVO_LAVA_RUKA   5 
 38 #define SERVO_PRAVA_RUKA  6
 39 #define SERVO_LAVA_NOHA   4
 40 #define SERVO_PRAVA_NOHA  3
 41 #define SERVO_LAVA_PATA   2
 42 #define SERVO_PRAVA_PATA  1
 43 
 44 // ak su niektore serva naopak, je tu jednotka
 45 uint8_t servo_invertovane[6] = {0, 0, 1, 1, 0, 1};
 46 
 47 // znaky, ktorymi sa ovladaju jednotlive stupne volnosti
 48 char znaky_zmien[] = {'a', 'q', ';', 'p', 'z', 'x', ',', '.', 'd', 'c', 'k', 'm' };
 49 // co robia jednotlive znaky (znamienko urcuje smer)
 50 int8_t zmeny[] = {SERVO_LAVA_RUKA, -SERVO_LAVA_RUKA, 
 51                   SERVO_PRAVA_RUKA, -SERVO_PRAVA_RUKA, 
 52                   -SERVO_LAVA_NOHA, SERVO_LAVA_NOHA, 
 53                   -SERVO_PRAVA_NOHA, SERVO_PRAVA_NOHA, 
 54                    SERVO_LAVA_PATA, -SERVO_LAVA_PATA, 
 55                    SERVO_PRAVA_PATA, -SERVO_PRAVA_PATA }; 
 56 
 57 // sem si mozno ulozit svoju kalibraciu
 58 //uint8_t prednastavena_kalibracia[] = { 78, 69, 83, 80, 50, 67 };
 59 uint8_t prednastavena_kalibracia[] = { 90, 90, 90, 90, 90, 90 };
 60 
 61 uint8_t dolny_limit[] = {0, 0, 0, 0, 0, 0, 0, 0};
 62 uint8_t horny_limit[] = {180, 180, 180, 180, 180, 180};
 63 
 64 Servo s[6];
 65 
 66 uint16_t ch_time[CHOREO_LEN];
 67 uint8_t ch_servo[CHOREO_LEN];
 68 uint8_t ch_val[CHOREO_LEN];
 69 int ch_len;
 70 uint8_t kalib[6];
 71 int stav[6];
 72 int krok;
 73 
 74 void setup() {
 75   Serial.begin(9600);
 76   init_serial(9600);
 77   init_ultrasonic();
 78 
 79   randomSeed(analogRead(1));
 80   s[0].attach(PIN_SERVO_PRAVA_PATA);
 81   s[1].attach(PIN_SERVO_LAVA_PATA);
 82   s[2].attach(PIN_SERVO_PRAVA_NOHA);
 83   s[3].attach(PIN_SERVO_LAVA_NOHA);
 84   s[4].attach(PIN_SERVO_PRAVA_RUKA);
 85   s[5].attach(PIN_SERVO_LAVA_RUKA);
 86   precitaj_kalibraciu_z_EEPROM();
 87   for (int i = 0; i < 6; i++)
 88   {
 89     kalib[i] = prednastavena_kalibracia[i];
 90     stav[i] = kalib[i];
 91     s[i].write(stav[i]);
 92   }
 93   ch_len = 0;
 94   krok = 7;
 95   ahoj();
 96   ruky2();
 97   delay(100);  
 98   serial_println("\r\n  Otto DTDT");
 99 }
100 
101 void loop() {
102   char z = -1;
103   if (serial_available()) z = serial_read();
104 #ifdef ECHO_BT_TO_USB
105     if (Serial.available()) z = Serial.read();
106 #endif  
107   
108   if (z != -1)
109   { 
110     if (pohyb_znakom(z)) return;   
111     else if (pohyb_kombinacia(z)) return; 
112     else if (z == '@') nacitaj_choreografiu();
113     else if (z == '?') vypis_choreografiu();
114     else if (z == 't') zatancuj_choreografiu();
115     else if (z == '-') ahoj();
116     else if (z == ' ') reset();
117     else if (z == 'H') kalibruj();
118     else if (z == 'J') nastav_limity();
119     else if (z == 'G') vypis_kalibraciu();
120     else if (z == 'L') nacitaj_kalibraciu();
121     else if (z == 'E') zapis_kalibraciu_do_EEPROM();
122     else if (z == 'R') ruky();
123     else if (z == '9') zvys_krok();
124     else if (z == '1') zniz_krok();
125     else if (z == 'U') test_ultrazvuk();
126   }
127   int16_t d = measure_distance();
128   if (d < 10) menu_ultrasonic_request();
129 }
130 
131 void test_ultrazvuk()
132 {
133   int i = 0;
134   while ((serial_available() == 0) && (Serial.available() == 0))
135   {
136     serial_println_num(measure_distance());
137     delay(100);
138   }
139   serial_read();
140 }
141 
142 void menu_ultrasonic_request()
143 {
144   uint32_t tm = millis();
145   int d = measure_distance();
146   int count;
147   while ((millis() - tm < 3000) && (d < 15) & (count < 10)) 
148   {
149     delay(10);
150     d = measure_distance();
151     if (d == 10000) count++;
152     else count = 0;
153   }
154   if (millis() - tm >= 3000)
155     ultrasonic_menu();
156 }
157 
158 void ultrasonic_menu()
159 {
160   int selection = 0;
161   tone(SIRENA, 880, 200);
162   
163   do {  
164     int count = 0;
165     do { 
166       int32_t d = measure_distance();
167       if (d == 10000) continue;
168       if (d >= 20) count++;
169       else count = 0;
170       delay(10);
171     } while (!serial_available() && !Serial.available() && (count < 20));
172     
173     tone(SIRENA, 440, 200);
174     uint32_t tm = millis();
175     while ((measure_distance() > 15) && (millis() - tm < 1500) && !serial_available() && !Serial.available()) delay(10);
176     if (millis() - tm >= 1500) 
177     {
178       tone(SIRENA, 2000, 50);
179       menu_command(selection);
180       return;
181     }       
182     selection++;
183     for (int i = 0; i < selection; i++)
184     {
185       tone(SIRENA, 1261, 50);
186       delay(250);
187     }
188   } while (!serial_available() && !Serial.available());
189   while (serial_available()) serial_read();
190   while (Serial.available()) Serial.read();
191 }
192 
193 void menu_command(int cmd)
194 {
195   serial_println_num(cmd);
196 }
197 
198 void precitaj_kalibraciu_z_EEPROM()
199 {
200   uint8_t value = EEPROM.read(1);
201   if (value != '~') return;
202   for (int i = 2; i < 8; i++)
203     prednastavena_kalibracia[i - 2] = EEPROM.read(i);
204   for (int i = 0; i < 6; i++)
205     dolny_limit[i] = EEPROM.read(i + 9);
206   for (int i = 0; i < 6; i++)
207     horny_limit[i] = EEPROM.read(i + 15);    
208 }
209 
210 char read_char()
211 {
212   while (!serial_available() && !Serial.available());
213   if (serial_available()) return serial_read();
214   else return Serial.read();
215 }
216 
217 void zapis_kalibraciu_do_EEPROM()
218 {
219   serial_print("Naozaj chces zapisat kalibraciu do EEPROM? [Y/n]: ");
220   char odpoved = read_char();
221   serial_println_char(odpoved);
222   if (odpoved == 'Y') 
223   {
224     EEPROM.write(1, '~');
225     for (int i = 2; i < 8; i++)
226       EEPROM.write(i, kalib[i - 2]);
227     for (int i = 0; i < 6; i++)
228       EEPROM.write(9 + i, dolny_limit[i]);
229     for (int i = 0; i < 6; i++)
230       EEPROM.write(15 + i, horny_limit[i]);
231     serial_println("ok");
232   }
233 }
234 
235 void pipni()
236 {
237   tone(SIRENA, 1568, 50);
238   delay(100);
239   tone(SIRENA, 1357, 50);
240 }
241 
242 void ruky()
243 {
244   int odloz_krok = krok;
245   delay(500);
246   krok = 90;
247   pohyb(SERVO_LAVA_RUKA);    
248   pohyb(SERVO_PRAVA_RUKA);
249   delay(1000);
250   krok = 180;
251   pohyb(-SERVO_LAVA_RUKA);    
252   pohyb(-SERVO_PRAVA_RUKA);
253   delay(1000);
254   krok = odloz_krok;
255   pipni();
256 }
257 
258 void ruky2()
259 {
260   int odloz_krok = krok;
261   delay(500);
262   krok = 180;
263   pohyb(SERVO_LAVA_RUKA);    
264   pohyb(SERVO_PRAVA_RUKA);
265   delay(1000);
266   krok = 90;
267   pohyb(-SERVO_LAVA_RUKA);    
268   pohyb(-SERVO_PRAVA_RUKA);
269   delay(1000);
270   krok = odloz_krok;
271   pipni();
272 }
273 
274 void ahoj()
275 {
276   tone(SIRENA, 1568, 50);
277   delay(70);
278   tone(SIRENA, 1175, 30);
279   delay(50);
280   tone(SIRENA, 880, 30);
281   delay(50);
282   tone(SIRENA, 1047, 50);
283   delay(70);
284   tone(SIRENA, 1245, 30);
285   delay(150);
286   tone(SIRENA, 1568, 50);
287   delay(100);
288   if (random(10) > 4) tone(SIRENA, 1357, 50);
289   else tone(SIRENA, 1047, 50);
290 }
291 
292 void nastav_koncatinu(int8_t servo, uint8_t poloha)
293 {
294   int8_t srv = (servo > 0)?servo:-servo;
295   srv--;
296   poloha += kalib[srv] - 90;
297   if (poloha > 180) poloha = 180;
298   if (poloha < 0) poloha = 0;
299   stav[srv] = poloha;
300   s[srv].write(stav[srv]);
301 }
302 
303 void pohyb(int8_t servo)
304 {
305   int8_t srv = (servo > 0)?servo:-servo;
306   srv--;
307   if (servo_invertovane[srv]) servo = -servo;
308   if (servo > 0)
309   {
310     if (stav[srv] <= horny_limit[srv] - krok) stav[srv] += krok;
311     else stav[srv] = horny_limit[srv];
312     s[srv].write(stav[srv]);
313   }
314   else if (servo < 0)
315   {
316     if (stav[srv] >= dolny_limit[srv] + krok) stav[srv] -= krok; 
317     else stav[srv] = dolny_limit[srv];
318     s[srv].write(stav[srv]);      
319   }
320 }
321 
322 uint8_t pohyb_znakom(char z)
323 {
324   for (int i = 0; i < 12; i++)
325   {
326     if (z == znaky_zmien[i])
327     {
328       int8_t servo = zmeny[i];
329       pohyb(servo);
330     }
331   }
332 }
333 
334 void kombinacia1()
335 {
336   pohyb(SERVO_LAVA_NOHA);
337   pohyb(-SERVO_PRAVA_PATA);
338 }
339 
340 void kombinacia2()
341 {
342   pohyb(SERVO_PRAVA_NOHA);
343   pohyb(-SERVO_LAVA_PATA);
344 }
345 
346 void kombinacia3()
347 {
348    pohyb(SERVO_LAVA_RUKA); 
349    pohyb(SERVO_PRAVA_RUKA); 
350 }
351 
352 void kombinacia4()
353 {
354    pohyb(-SERVO_LAVA_RUKA); 
355    pohyb(-SERVO_PRAVA_RUKA); 
356 }
357 
358 int pohyb_kombinacia(char z)
359 {
360   if (z == '3') kombinacia1();
361   else if (z == '4') kombinacia2();
362   else if (z == '5') kombinacia3();
363   else if (z == '6') kombinacia4();
364   else return 0;
365   return 1;
366 }
367 
368 int nacitajCislo()
369 {
370   int num = 0;
371   int z;
372   do {
373     z = serial_read();
374     if (z == '#') while (z != 13) z = serial_read();
375   } while ((z < '0') || (z > '9'));
376   while ((z >= '0') && (z <= '9'))
377   {
378     num *= 10;
379     num += (z - '0');
380     do { z = serial_read(); if (z == -1) delayMicroseconds(10); } while (z < 0);
381   }
382   return num;
383 }
384 
385 void nacitaj_choreografiu()
386 {
387   ch_len = 0;
388   int tm;
389   do { 
390     tm = nacitajCislo();
391     ch_time[ch_len] = tm;
392     ch_servo[ch_len] = nacitajCislo();
393     ch_val[ch_len] = nacitajCislo();
394     ch_len++;
395   if (ch_len == CHOREO_LEN) break;
396   } while (tm > 0);
397   pipni();  
398 }
399 
400 void vypis_choreografiu()
401 {
402   for (int i = 0; i < ch_len; i++)
403   {
404     serial_print_num(ch_time[i]);
405     serial_print(" ");
406     serial_print_num(ch_servo[i]);
407     serial_print(" ");
408     serial_println_num(ch_val[i]);      
409   }
410   pipni();
411 }
412 
413 void zatancuj_choreografiu()
414 {
415   for (int i = 0; i < ch_len - 1; i++)
416   {
417     delay(ch_time[i]);
418     nastav_koncatinu(ch_servo[i], ch_val[i]);
419   }
420   if (ch_len > 0) stav[ch_servo[ch_len - 1]] = ch_val[ch_len - 1];
421   pipni();
422 }
423 
424 void reset()
425 {
426   for (int i = 0; i < 6; i++) 
427   {
428     stav[i] = kalib[i];
429     s[i].write(kalib[i]);
430   }
431   pipni();
432 }
433 
434 uint8_t nalad_hodnotu_serva(uint8_t servo, uint8_t hodnota)
435 {
436     serial_print(" (+/-/ENTER): ");
437     serial_println_num(hodnota);
438     s[servo].write(hodnota);
439     char z;
440     do {
441       z = read_char();
442       if ((z == '+') && (hodnota < 180)) hodnota++;
443       else if ((z == '-') && (hodnota > 0)) hodnota--; 
444       if ((z == '+') || (z == '-'))
445       {
446         serial_print_num(hodnota); serial_print_char('\r'); 
447         s[servo].write(hodnota);
448       }
449     } while (z != 13);
450     return hodnota;
451 }
452 
453 void kalibruj()
454 {
455   for (int i = 0; i < 6; i++)
456   {
457     serial_print_num(i);
458     kalib[i] = nalad_hodnotu_serva(i, kalib[i]);
459     serial_print_num(i);
460     serial_print(": ");
461     serial_println_num(kalib[i]);
462   }
463   for (int i = 0; i < 6; i++) { serial_print_num(kalib[i]); serial_print(" "); }
464   serial_println("ok");
465   pipni();
466 }
467 
468 void nastav_limity()
469 {
470   for (int i = 0; i < 6; i++)
471   {
472     serial_print_num(i);
473     serial_print("dolny");
474     dolny_limit[i] = nalad_hodnotu_serva(i, dolny_limit[i]);
475     serial_print_num(i);
476     serial_print(" dolny: ");
477     serial_println_num(dolny_limit[i]);
478     s[i].write(kalib[i]);
479 
480     serial_print_num(i);
481     serial_print("horny");
482     horny_limit[i] = nalad_hodnotu_serva(i, horny_limit[i]);
483     serial_print_num(i);
484     serial_print(" horny: ");
485     serial_println_num(horny_limit[i]);
486     s[i].write(kalib[i]);
487   }
488   for (int i = 0; i < 6; i++) { serial_print_num(dolny_limit[i]); serial_print("-"); serial_print_num(horny_limit[i]); serial_print(" "); }
489   serial_println("ok");
490   pipni();
491 }
492 
493 void vypis_kalibraciu()
494 {
495   serial_print("stredy: ");
496   for (int i = 0; i < 6; i++) { serial_print_num(kalib[i]); serial_print(" "); }
497   serial_println();
498   serial_print("dolny limit: ");
499   for (int i = 0; i < 6; i++) { serial_print_num(dolny_limit[i]); serial_print(" "); }
500   serial_println();
501   serial_print("horny limit: ");
502   for (int i = 0; i < 6; i++) { serial_print_num(horny_limit[i]); serial_print(" "); }
503   serial_println();
504 }
505 
506 void nacitaj_kalibraciu()
507 {
508   int tm;
509   for (int i = 0; i < 6; i++) 
510     kalib[i] = nacitajCislo();
511   vypis_kalibraciu();
512   serial_println("ok");
513   pipni();
514 }
515 
516 void zvys_krok()
517 {
518   if (krok < 180) krok++;
519   serial_print("krok: ");
520   serial_println_num(krok);
521 }
522 
523 void zniz_krok()
524 {
525   if (krok > 0) krok--;
526   serial_print("krok: ");
527   serial_println_num(krok);
528 }
529 
530 // nasleduje softverova implementacia serioveho portu
531 #define SERIAL_STATE_IDLE      0
532 #define SERIAL_STATE_RECEIVING 1
533 #define SERIAL_BUFFER_LENGTH   20
534 
535 static volatile uint8_t serial_state;
536 static uint8_t serial_buffer[SERIAL_BUFFER_LENGTH];
537 static volatile uint8_t serial_buf_wp, serial_buf_rp;
538 
539 static volatile uint8_t receiving_byte;
540 
541 static volatile uint32_t time_startbit_noticed;
542 static volatile uint8_t next_bit_order;
543 static volatile uint8_t waiting_stop_bit;
544 static uint16_t one_byte_duration;
545 static uint16_t one_bit_duration;
546 static uint16_t one_bit_write_duration;
547 static uint16_t half_of_one_bit_duration;
548 
549 void init_serial(uint32_t baud_rate)
550 {
551   pinMode(2, INPUT);
552   pinMode(4, OUTPUT);
553   
554   serial_state = SERIAL_STATE_IDLE;
555   
556   one_byte_duration = 9500000 / baud_rate;
557   one_bit_duration = 1000000 / baud_rate;
558   one_bit_write_duration = one_bit_duration - 1;
559   half_of_one_bit_duration = 500000 / baud_rate;
560   
561   PCMSK2 |= 4; //PCINT18;
562   PCIFR &= ~4; //PCIF2;
563   PCICR |= 4; // PCIE2;
564 }
565 
566 ISR(PCINT2_vect)
567 {
568   uint32_t tm = micros();
569   if (serial_state == SERIAL_STATE_IDLE)
570   {    
571     time_startbit_noticed = tm;
572     serial_state = SERIAL_STATE_RECEIVING;
573     receiving_byte = 0xFF;
574     next_bit_order = 0;
575   }
576   else if (tm - time_startbit_noticed > one_byte_duration)
577   {
578       serial_buffer[serial_buf_wp] = receiving_byte;
579       serial_buf_wp++;
580       if (serial_buf_wp == SERIAL_BUFFER_LENGTH) serial_buf_wp = 0;
581       time_startbit_noticed = tm;
582       receiving_byte = 0xFF;
583       next_bit_order = 0;
584   }
585   else if (PIND & 4) 
586   {
587      int8_t new_next_bit_order = (tm - time_startbit_noticed - half_of_one_bit_duration) / one_bit_duration;
588      while (next_bit_order < new_next_bit_order)
589      {  
590         receiving_byte &= ~(1 << next_bit_order);
591         next_bit_order++;
592      }
593      if (next_bit_order == 8)
594      { 
595         serial_buffer[serial_buf_wp] = receiving_byte;
596         serial_buf_wp++;
597         if (serial_buf_wp == SERIAL_BUFFER_LENGTH) serial_buf_wp = 0;
598         serial_state = SERIAL_STATE_IDLE;
599      }        
600   } else 
601       next_bit_order = (tm - time_startbit_noticed - half_of_one_bit_duration) / one_bit_duration;
602 }
603 
604 uint8_t serial_available()
605 {
606   cli();
607   if (serial_buf_rp != serial_buf_wp) 
608   {
609     sei();
610     return 1;
611   }
612   if (serial_state == SERIAL_STATE_RECEIVING)
613   {
614     uint32_t tm = micros();
615     if (tm - time_startbit_noticed > one_byte_duration)
616     {      
617       serial_state = SERIAL_STATE_IDLE;
618       serial_buffer[serial_buf_wp] = receiving_byte;
619       serial_buf_wp++;
620       if (serial_buf_wp == SERIAL_BUFFER_LENGTH) serial_buf_wp = 0;
621       sei();
622       return 1;
623     }
624   }
625   sei();
626   return 0;
627 }
628 
629 int16_t serial_read()
630 {
631   cli();
632   if (serial_buf_rp != serial_buf_wp)
633   {
634     uint8_t ch = serial_buffer[serial_buf_rp];
635     serial_buf_rp++;
636     if (serial_buf_rp == SERIAL_BUFFER_LENGTH) serial_buf_rp = 0;
637     sei();
638     return ch;
639   }
640 
641   if (serial_state == SERIAL_STATE_RECEIVING)
642   {
643     uint32_t tm = micros();
644     if (tm - time_startbit_noticed > one_byte_duration)
645     {
646       uint8_t ch = receiving_byte;
647       serial_state = SERIAL_STATE_IDLE;  
648       sei();
649       return ch;
650     }
651   }
652   sei();
653   return -1;
654 }
655 
656 void serial_write(uint8_t ch)
657 {
658 #ifdef ECHO_BT_TO_USB
659   Serial.print((char)ch);
660 #endif
661   PORTD &= ~16;
662   delayMicroseconds(one_bit_write_duration);
663   for (uint8_t i = 0; i < 8; i++)
664   {
665     if (ch & 1) PORTD |= 16;
666     else PORTD &= ~16;
667     ch >>= 1;
668     delayMicroseconds(one_bit_write_duration);
669   }
670   PORTD |= 16;
671   delayMicroseconds(one_bit_write_duration);
672   delayMicroseconds(one_bit_write_duration);
673   delayMicroseconds(one_bit_write_duration);
674   delayMicroseconds(one_bit_write_duration);
675   delayMicroseconds(one_bit_write_duration);
676 }
677 
678 uint16_t serial_readln(uint8_t *ln, uint16_t max_length)
679 {
680   uint16_t len;
681   uint16_t ch;
682   do {
683     ch = serial_read();
684     if (ch == 13) continue;
685   } while (ch == -1);
686 
687   do {
688     if ((ch != 13) && (ch != 10) && (ch != -1)) 
689     {
690       *(ln++) = ch;
691       max_length--;
692       len++;
693     }    
694     ch = serial_read();
695   } while ((ch != 13) && max_length);
696   *ln = 0;
697   return len;
698 }
699 
700 void serial_print_num(int32_t number)
701 {
702   if (number < 0) 
703   {
704     serial_write('-');
705     number = -number;
706   }
707   int32_t rad = 1;
708   while (number / rad) rad *= 10;
709   if (number > 0) rad /= 10;
710   while (rad)
711   {
712     serial_write((char)('0' + (number / rad)));
713     number -= (number / rad) * rad;
714     rad /= 10;
715   }  
716 }
717 
718 void serial_print_char(char ch)
719 {
720   serial_write(ch);  
721 }
722 
723 void serial_print(const uint8_t *str)
724 {
725   while (*str) serial_write(*(str++));
726 }
727 
728 void serial_println(const uint8_t *str)
729 {
730   serial_print(str);
731   serial_write(13);
732   serial_write(10);
733 }
734 
735 void serial_println_num(int32_t number)
736 {
737   serial_print_num(number);
738   serial_println();
739 }
740 
741 void serial_println_char(char ch)
742 {
743   serial_write(ch);
744   serial_println();
745 }
746 
747 void serial_println()
748 {
749   serial_write(13);
750   serial_write(10);
751 }
752 
753 // nasleduje citanie z utltazvukoveho senzora
754 
755 static volatile uint32_t pulse_start;
756 static volatile int16_t distance;
757 static volatile uint8_t new_distance;
758 
759 void init_ultrasonic()
760 {
761   pinMode(US_ECHO, INPUT);
762   pinMode(US_TRIG, OUTPUT);
763   
764   PCMSK0 |= 1; //PCINT0;
765   PCIFR &= ~1; //PCIF0;
766   PCICR |= 1; // PCIE0;  
767 }
768 
769 ISR(PCINT0_vect)
770 {
771   if (PINB & 1) pulse_start = micros();
772   else 
773   {
774     distance = (int16_t)((micros() - pulse_start) / 58);
775     new_distance = 1;
776   }
777 }
778 
779 void start_distance_measurement()
780 {
781   distance = 10000;
782   new_distance = 0;
783   digitalWrite(US_TRIG, HIGH);
784   delayMicroseconds(10);
785   digitalWrite(US_TRIG, LOW);
786 }
787 
788 void wait_for_distance_measurement_to_complete()
789 {
790   uint8_t counter = 0;
791   while ((counter < 20) && !new_distance) 
792   {
793     delay(1);
794     counter++;
795   }
796   if (counter == 20)
797   {
798     pinMode(US_ECHO, OUTPUT);
799     digitalWrite(US_ECHO, HIGH);
800     delayMicroseconds(10);
801     digitalWrite(US_ECHO, LOW);
802     pinMode(US_ECHO, INPUT); 
803     delayMicroseconds(5);
804     distance = 10000;
805   }
806 }
807 
808 int16_t measure_distance()
809 {
810   start_distance_measurement();
811   wait_for_distance_measurement_to_complete();
812   return distance;
813 }