-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.py
executable file
·1447 lines (1047 loc) · 31.2 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import pygame
import sys
import os.path
import threading
import pygame.sndarray
from numpy import sin, array, arange, resize, pi
from robothread import RoboException, RoboThread
sensors = {}
def makeXY(x, y):
""" Generates real x,y from NXT like x,y """
if x * 2 + 2 <= 200:
rx = x * 2 + 2
else:
rx = 200
if y <= 64:
return (rx, abs(y - 64) * 2 + 1)
else:
return (rx, y * 2 + 1)
# font adapted from
# http://www.openobject.org/opensourceurbanism/Bike_POV_Beta_4
chars = [[0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x6f, 0x00, 0x00],
[0x00, 0x07, 0x00, 0x07, 0x00],
[0x14, 0x7f, 0x14, 0x7f, 0x14],
[0x00, 0x07, 0x04, 0x1e, 0x00],
[0x23, 0x13, 0x08, 0x64, 0x62],
[0x36, 0x49, 0x56, 0x20, 0x50],
[0x00, 0x00, 0x07, 0x00, 0x00],
[0x00, 0x1c, 0x22, 0x41, 0x00],
[0x00, 0x41, 0x22, 0x1c, 0x00],
[0x14, 0x08, 0x3e, 0x08, 0x14],
[0x08, 0x08, 0x3e, 0x08, 0x08],
[0x00, 0x50, 0x30, 0x00, 0x00],
[0x08, 0x08, 0x08, 0x08, 0x08],
[0x00, 0x60, 0x60, 0x00, 0x00],
[0x20, 0x10, 0x08, 0x04, 0x02],
[0x3e, 0x51, 0x49, 0x45, 0x3e],
[0x00, 0x42, 0x7f, 0x40, 0x00],
[0x42, 0x61, 0x51, 0x49, 0x46],
[0x21, 0x41, 0x45, 0x4b, 0x31],
[0x18, 0x14, 0x12, 0x7f, 0x10],
[0x27, 0x45, 0x45, 0x45, 0x39],
[0x3c, 0x4a, 0x49, 0x49, 0x30],
[0x01, 0x71, 0x09, 0x05, 0x03],
[0x36, 0x49, 0x49, 0x49, 0x36],
[0x06, 0x49, 0x49, 0x29, 0x1e],
[0x00, 0x36, 0x36, 0x00, 0x00],
[0x00, 0x56, 0x36, 0x00, 0x00],
[0x08, 0x14, 0x22, 0x41, 0x00],
[0x14, 0x14, 0x14, 0x14, 0x14],
[0x00, 0x41, 0x22, 0x14, 0x08],
[0x02, 0x01, 0x51, 0x09, 0x06],
[0x3e, 0x41, 0x5d, 0x49, 0x4e],
[0x7e, 0x09, 0x09, 0x09, 0x7e],
[0x7f, 0x49, 0x49, 0x49, 0x36],
[0x3e, 0x41, 0x41, 0x41, 0x22],
[0x7f, 0x41, 0x41, 0x41, 0x3e],
[0x7f, 0x49, 0x49, 0x49, 0x41],
[0x7f, 0x09, 0x09, 0x09, 0x01],
[0x3e, 0x41, 0x49, 0x49, 0x7a],
[0x7f, 0x08, 0x08, 0x08, 0x7f],
[0x00, 0x41, 0x7f, 0x41, 0x00],
[0x20, 0x40, 0x41, 0x3f, 0x01],
[0x7f, 0x08, 0x14, 0x22, 0x41],
[0x7f, 0x40, 0x40, 0x40, 0x40],
[0x7f, 0x02, 0x0c, 0x02, 0x7f],
[0x7f, 0x04, 0x08, 0x10, 0x7f],
[0x3e, 0x41, 0x41, 0x41, 0x3e],
[0x7f, 0x09, 0x09, 0x09, 0x06],
[0x3e, 0x41, 0x51, 0x21, 0x5e],
[0x7f, 0x09, 0x19, 0x29, 0x46],
[0x46, 0x49, 0x49, 0x49, 0x31],
[0x01, 0x01, 0x7f, 0x01, 0x01],
[0x3f, 0x40, 0x40, 0x40, 0x3f],
[0x0f, 0x30, 0x40, 0x30, 0x0f],
[0x3f, 0x40, 0x30, 0x40, 0x3f],
[0x63, 0x14, 0x08, 0x14, 0x63],
[0x07, 0x08, 0x70, 0x08, 0x07],
[0x61, 0x51, 0x49, 0x45, 0x43],
[0x3c, 0x4a, 0x49, 0x29, 0x1e],
[0x02, 0x04, 0x08, 0x10, 0x20],
[0x00, 0x41, 0x7f, 0x00, 0x00],
[0x04, 0x02, 0x01, 0x02, 0x04],
[0x40, 0x40, 0x40, 0x40, 0x40],
[0x00, 0x00, 0x03, 0x04, 0x00],
[0x20, 0x54, 0x54, 0x54, 0x78],
[0x7f, 0x48, 0x44, 0x44, 0x38],
[0x38, 0x44, 0x44, 0x44, 0x20],
[0x38, 0x44, 0x44, 0x48, 0x7f],
[0x38, 0x54, 0x54, 0x54, 0x18],
[0x08, 0x7e, 0x09, 0x01, 0x02],
[0x0c, 0x52, 0x52, 0x52, 0x3e],
[0x7f, 0x08, 0x04, 0x04, 0x78],
[0x00, 0x44, 0x7d, 0x40, 0x00],
[0x20, 0x40, 0x44, 0x3d, 0x00],
[0x00, 0x7f, 0x10, 0x28, 0x44],
[0x00, 0x41, 0x7f, 0x40, 0x00],
[0x7c, 0x04, 0x18, 0x04, 0x78],
[0x7c, 0x08, 0x04, 0x04, 0x78],
[0x38, 0x44, 0x44, 0x44, 0x38],
[0x7c, 0x14, 0x14, 0x14, 0x08],
[0x08, 0x14, 0x14, 0x18, 0x7c],
[0x7c, 0x08, 0x04, 0x04, 0x08],
[0x48, 0x54, 0x54, 0x54, 0x20],
[0x04, 0x3f, 0x44, 0x40, 0x20],
[0x3c, 0x40, 0x40, 0x20, 0x7c],
[0x1c, 0x20, 0x40, 0x20, 0x1c],
[0x3c, 0x40, 0x30, 0x40, 0x3c],
[0x44, 0x28, 0x10, 0x28, 0x44],
[0x0c, 0x50, 0x50, 0x50, 0x3c],
[0x44, 0x64, 0x54, 0x4c, 0x44],
[0x00, 0x08, 0x36, 0x41, 0x41],
[0x00, 0x00, 0x7f, 0x00, 0x00],
[0x41, 0x41, 0x36, 0x08, 0x00],
[0x04, 0x02, 0x04, 0x08, 0x04],
]
def dieTest():
while robot.paused: # pauses execution of the emulated program
__clock__.tick(20)
with robot.lock:
if robot.die:
robot.die = False
raise RoboException
def screenTest():
with robot.lock:
if threading.current_thread().name == "brick":
if robot.scr_running:
robot.scr_running = False
robot.scr_killed = True
pygame.time.delay(200)
robot.scr_running = False
def PointOut(x, y):
"""
.. [en]
PointOut(x, y)
Draw a point on the screen at (x, y)
:param int x: The x coordinate of the point
:param int y: The y coordinate of the point
.. [/en]
.. [sk]
PointOut(x, y)
Nakreslí bod na obrazovke na pozíciach (x, y)
:param int x: X-ová pozícia bodu
:param int y: Y-ová pozícia bodu
.. [/sk]
"""
screenTest()
dieTest()
x, y = makeXY(x, y)
# print y
with robot.lock:
robot.lcd.set_at((x, y), (0, 0, 0))
robot.lcd.set_at((x + 1, y), (0, 0, 0))
robot.lcd.set_at((x + 1, y + 1), (0, 0, 0))
robot.lcd.set_at((x, y + 1), (0, 0, 0))
def clearPoint(x, y):
dieTest()
x, y = makeXY(x, y)
with robot.lock:
robot.lcd.set_at((x, y), (0x43, 0x6c, 0x30))
robot.lcd.set_at((x + 1, y), (0x43, 0x6c, 0x30))
robot.lcd.set_at((x + 1, y + 1), (0x43, 0x6c, 0x30))
robot.lcd.set_at((x, y + 1), (0x43, 0x6c, 0x30))
def _printChar(x, y, char):
""" Low level function for printing chars on the Surface"""
char = ord(char)
if char < 32 or char > 126:
char = 32
char -= 32
data = chars[char]
for line in data:
for z in range(0, 8):
if line << z & 0b10000000:
PointOut(x, y - (8 - z))
else:
clearPoint(x, y - (8 - z))
x += 1
LCD_LINE1 = 64
LCD_LINE2 = 56
LCD_LINE3 = 48
LCD_LINE4 = 40
LCD_LINE5 = 32
LCD_LINE6 = 24
LCD_LINE7 = 16
LCD_LINE8 = 8
def ClearScreen():
"""
.. [en]
ClearScreen()
Clear the screen.
.. [/en]
.. [sk]
ClearScreen()
Vyčistí obrazovku.
.. [/sk]
"""
screenTest()
with robot.lock:
pygame.draw.rect(robot.lcd, pygame.Color(0x43, 0x6c, 0x30),
((0, 0), (204, 130)))
robot._cur_lcd_line = 7
def lcd_clear():
"""
.. [en]
lcd_clear()
Clear the screen.
.. [/en]
.. [sk]
lcd_clear()
Vyčistí obrazovku.
.. [/sk]
"""
ClearScreen()
def TextOut(x, y, text, clear=False):
"""
.. [en]
TextOut(x, y, text)
Print text on the screen.
:param int x: X coordinate of the text
:param int y: Y coordinate or the text
:param str text: The text to print
.. [/en]
.. [sk]
TextOut(x, y, text)
Vypíše text na obrazovku.
:param int x: X-ová pozícia textu.
:param int y: Y-ová pozícia textu.
:param str text: Text, ktorý sa má vypísať.
.. [/sk]
"""
if clear:
ClearScreen()
for char in list(text):
_printChar(x, y, char)
x += 6
def NumOut(x, y, num, clear=False):
"""
.. [en]
NumOut(x, y, num)
Print number on the screen.
:param int x: X coordinate of the text
:param int y: Y coordinate or the text
:param int num: The number to print
.. [/en]
.. [sk]
NumOut(x, y, číslo)
Vypíše číslo na obrazovku.
:param int x: X-ová pozícia textu.
:param int y: Y-ová pozícia textu.
:param int num: Číslo, ktorá chceme vypísať.
"""
num = str(num)
TextOut(x, y, num, clear)
# TODO
def lcd_print(text):
"""
.. [en]
lcd_print(text)
Print one or multiple lines of text on the LCD screen. Every line can have
at most 16 characters -- others will be omitted.
Lines are separated in text by the newline sign (\\n). Every line is printed
separately.
:param string text: text to be printed on the LCD screen
.. [/en]
.. [sk]
lcd_print(text)
Vypíše jeden alebo viac riadkov textu na LCD obrazovku. Na jeden riadok
vypíše najviac 16 znakov -- ostatné budú ignorované.
Riadky sú v texte oddelené znakom nového riadku (\\n). Každý riadok je
vypísaný samostatne.
:param string text: text, ktorý sa má vypísať na LCD obrazovku
.. [/sk]
"""
lines = text.split('\n')
for line in lines:
if robot._cur_lcd_line == 7:
lcd_clear()
robot._cur_lcd_line = 0
else:
robot._cur_lcd_line += 1
line_position = 64 - robot._cur_lcd_line* 8
TextOut(0, line_position, line)
def LineOut(x0, y0, x1, y1):
"""
.. [en]
LineOut(x0, y0, x1, y1)
Draw a line from [x0, y0] to [x1, y1].
:param int x0: X coordinate of the start point of the line
:param int y0: Y coordinate of the start point of the line
:param int x1: X coordinate of the end point of the line
:param int y1: Y coordinate of the start point of the line
.. [/en]
.. [sk]
LineOut(x0, y0, x1, y1)
Funkcia na vykreslovanie čiary od [x0, y0] do [x1, y1].
:param int x0: X-ová pozíca začiatku čiary.
:param int y0: Y-ová pozíca začiatku čiary.
:param int x1: X-ová pozíca konca čiary.
:param int y1: Y-ová pozíca konca čiary.
.. [/sk]
"""
tmp = []
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
tmp = [x0, x1]
x0 = y0
x1 = y1
y0 = tmp[0]
y1 = tmp[1]
if x0 > x1:
tmp = [x0, y0]
x0 = x1
y0 = y1
x1 = tmp[0]
y1 = tmp[1]
deltax = x1 - x0
deltay = abs(y1 - y0)
error = deltax / 2.0
y = y0
if y0 < y1:
ystep = 1
else:
ystep = -1
for x in range(x0, x1):
if steep:
PointOut(y, x)
else:
PointOut(x, y)
error = error - deltay
if error < 0:
y += ystep
error += deltax
def CircleOut(x, y, radius):
"""
.. [en]
CircleOut(x, y, radius)
Draw a circle with center at [x, y] and specified radius.
:param int x: X coordinate of the center of the circle.
:param int y: Y coordinate of the center of the circle.
:param int radius: The radius of the circle.
.. [/en]
.. [sk]
CircleOut(x, y, polomer)
Vykreslí kruh, ktorého stred je [x, y] s daným polomerom.
:param int x: X-ová pozícia stredu kruhu.
:param int y: Y-ová pozícia stredu kruhu.
:param int radius: Polomer kruhu.
.. [/sk]
"""
# x,y = makeXY(x, y)
# pygame.draw.circle(robot.lcd, (0, 0, 0), (x, y), radius*2+1, 1)
# pygame.draw.circle(robot.lcd, (0, 0, 0), (x, y), radius*2+2, 1)
# pygame.draw.circle(robot.lcd, (0, 0, 0), (x, y), radius*2+3, 1)
f = 1 - radius
ddF_x = 1
ddF_y = -2 * radius
xx = 0
yy = radius
PointOut(x, y + radius)
PointOut(x, y - radius)
PointOut(x + radius, y)
PointOut(x - radius, y)
while xx < yy:
if f >= 0:
yy -= 1
ddF_y += 2
f += ddF_y
xx += 1
ddF_x += 2
f += ddF_x
PointOut(x + xx, y + yy)
PointOut(x - xx, y + yy)
PointOut(x + xx, y - yy)
PointOut(x - xx, y - yy)
PointOut(x + yy, y + xx)
PointOut(x - yy, y + xx)
PointOut(x + yy, y - xx)
PointOut(x - yy, y - xx)
# TODO
def RectOut(x, y, width, height):
"""
.. [en]
RectOut(x, y, width, height)
Draw a rectangle from [x, y] with specified width and height.
:param int x: X coordinate of the start point of the rectangle.
:param int y: Y coordinate of the start point of the rectangle.
:param int width: The width of the rectangle.
:param int height: The height of the rectangle.
.. [/en]
.. [sk]
RectOut(x, y, šírka, výška)
Vykreslí obdĺžnik z [x, y] a podľa špecifikovanej šírky a výšky.
:param int x: X-ová pozícía ľavého horného rohu.
:param int y: Y-ová pozícia ľavého horného rohu.
:param int width: Šírka obdĺžnika.
:param int height: Výška obldĺžnika.
.. [/sk]
"""
LineOut(x, y, x + width + 1, y)
LineOut(x, y, x, y - height)
LineOut(x, y - height, x + width, y - height)
LineOut(x + width, y, x + width, y - height)
def lcd_draw_polygon(vertices):
"""
.. [en]
lcd_draw_polygon(vertices)
Draw a polygon with specified vertices.
:param 2D array vertices: [[x,y]] coordinates of the vertices.
.. [/en]
.. [sk]
lcd_draw_polygon(vrcholy)
Vykreslí polygón podľa zadaných vrcholov.
:param 2D array vertices: [[x,y]] súradnice vrcholov.
.. [/sk]
"""
for i in range(len(vertices)):
if i == len(vertices)-1:
LineOut(vertices[i][0], vertices[i][1], vertices[0][0], vertices[0][1])
else:
LineOut(vertices[i][0], vertices[i][1], vertices[i+1][0], vertices[i+1][1])
def ResetScreen():
"""
.. [en]
ResetScreen()
Return the screen to Running... state.
.. [/en]
.. [sk]
ResetScreen()
Vráti obrazovku do stavu Running... .
.. [/sk]
"""
if not robot.scr_running:
robot.scr_running = True
scr_runner = RoboThread(target=robot.running)
scr_runner.start()
def lcd_reset():
"""
.. [en]
lcd_reset()
Return the screen to Running... state.
.. [/en]
.. [sk]
lcd_reset()
Vráti obrazovku do stavu Running... .
.. [/sk]
"""
ResetScreen()
def ClearLine(line):
"""
.. [en]
ClearLine(line)
Clear one line on the screen.
:param int line: line we want to clear.
.. [/en]
.. [sk]
ClearLine(riadok)
Vyčistí jeden riadok na obrazovke.
:param int line: riadok, ktorý chceme vyčistiť.
.. [/sk]
"""
# x, y = makeXY(0, line)
# x1, y1 = makeXY(100, line-8)
# with robot.lock:
# pygame.draw.rect(robot.lcd, pygame.Color(0x43, 0x6c, 0x30),
# ((x, y), (x1, y1)))
TextOut(0, line, 16 * " ")
def Wait(milisec):
"""
.. [en]
Wait(milisec)
Waits for given number of miliseconds.
:param int milisec: number of miliseconds
.. [/en]
.. [sk]
Wait(milisekundy)
Počká na daný čas v milisekundách.
:param int milisec: číslo v milisekundách.
.. [/sk]
"""
while milisec > 1:
step = 100 if milisec > 500 else milisec
milisec -= pygame.time.delay(step)
dieTest()
def wait(milisec):
"""
.. [en]
wait(milisec)
Waits for given number of miliseconds.
:param int milisec: number of miliseconds
.. [/en]
.. [sk]
wait(milisekundy)
Počká na daný čas v milisekundách.
:param int milisec: číslo v milisekundách.
.. [/sk]
"""
return Wait(milisec)
def _sine_array_onecycle(hz, peak):
length = 44100 / float(hz)
omega = pi * 2 / length
xvalues = arange(int(length)) * omega
return (peak * sin(xvalues))
def _sine_array(hz, peak, n_samples=200):
return resize(_sine_array_onecycle(hz, peak), (n_samples,))
def PlayTone(freq, duration):
"""
.. [en]
PlayTone(freq, duration)
Play a tone.
:param int freq: Frequency of the tone in Hz.
:param int duration: For how long should the brick play this tone.
.. [/en]
.. [sk]
PlayTone(frekvencia, dĺžka)
Prehrá tón.
:param int freq: Frekvencia tónu v Hz.
:param int duration: Dĺžka prehrávania tónu v milisekundách.
.. [/sk]
"""
f = _sine_array(freq, 1, duration * 6)
f = array(zip(f, f))
sound = pygame.sndarray.make_sound(f)
channel = sound.play(-1)
channel.set_volume(0.2, 0.2)
Wait(duration)
sound.stop()
def Beep(duration):
"""
.. [en]
Beep(duration)
Beeps for a number of miliseconds.
:param int duration: For how long should the brick beeps in milliseconds.
.. [/en]
.. [sk]
Beep(dížka)
Pípne.
:param int duration: Ako dlho potrvá pípnutie v milisekundách.
.. [/sk]
"""
PlayTone(2600, duration)
OUT_A = 1
OUT_B = 2
OUT_C = 4
OUT_AB = 3
OUT_AC = 5
OUT_BC = 6
OUT_ABC = 7
def OnFwd(motor, speed):
"""
.. [en]
OnFwd(motor, speed)
Set motor to forward direction and turn it on.
:param int motor: motor we want to run.
:param int speed: speed we want to run the motor at from 0 to 100. Negative
value reverses direction.
.. [/en]
.. [sk]
OnFwd(motor, rychlost)
Nastaví motor tak, aby sa pohyboval vpred a spustí ho
:param int motor: motor, ktorý chceme spustiť.
:param int rychlost: Rýchlosť, ktorou chceme poslať robota dopredu od 0 do
100. Záporná rýchlosť zmení smer chodu motora.
.. [/sk]
"""
dieTest()
if speed <= -100:
speed = -100
if speed >= 100:
speed = 100
with robot.lock:
if motor & OUT_A:
robot.mA = speed
if motor & OUT_B:
robot.mB = speed
if motor & OUT_C:
robot.mC = speed
def OnRev(motor, speed):
"""
.. [en]
OnRev(motor, speed)
Set motor to reverse direction and turn it on.
:param int motor: motor we want to run.
:param int speed: speed we want to run the motor at from 0 to 100. Negative
value reverses direction.
.. [/en]
.. [sk]
OnRev(motor, rychlost)
Nastaví motor nastaví motor na pohyb vzad a spustí ho.
:param int motor: motor, ktorý chceme spustiť.
:param int rychlost: rýchlosť, ktorou pôjde motor od 0 do 100.
Záporná rýchlosť zmení smer chodu motora.
.. [/sk]
"""
dieTest()
speed = -speed
if speed <= -100:
speed = -100
if speed >= 100:
speed = 100
with robot.lock:
if motor & OUT_A:
robot.mA = speed
if motor & OUT_B:
robot.mB = speed
if motor & OUT_C:
robot.mC = speed
def Off(motor):
"""
.. [en]
Off(motor)
Turn the motor off (with break).
:param int motor: motor we want to stop.
.. [/en]
.. [sk]
Off(motor)
Vypne motor (a zabrzdí).
:param int motor: motor, ktorý chceme zastaviť.
.. [/sk]
"""
dieTest()
with robot.lock:
if motor & OUT_A:
robot.mA = 0
if motor & OUT_B:
robot.mB = 0
if motor & OUT_C:
robot.mC = 0
def Float(motor):
"""
.. [en]
Float(motor)
Make the motor float.
:param int motor: motor we want to stop.
.. [/en]
.. [sk]
Float(motor)
Vypne motor (so zotrvačnosťou).
:param int motor: motor, ktorý chceme zastaviť.
.. [/sk]
"""
return Off(motor)
def off(motor=None):
"""
.. [en]
off(motor)
Turn the motor off (with break).
:param int motor: motor we want to stop.
.. [/en]
.. [sk]
off(motor)
Vypne motor (a zabrzdí).
:param int motor: motor, ktorý chceme zastaviť.
.. [/sk]
"""
if motor is None:
return Off(OUT_ABC)
else:
return Off(motor)
def MotorTachoCount(motor):
"""
.. [en]
MotorTachoCount(motor)
Get motor tachometer counter value.
:param int motor: motor we want to get tachometer count from.
.. [/en]
.. [sk]
MotorTachoCount(motor)
Načíta hodnotu otáčkomeru motora.
:param int motor: motor, z ktorého chceme načítavať.
.. [/sk]
"""
dieTest()
if motor & OUT_A:
return robot.rotA
if motor & OUT_B:
return robot.rotB
if motor & OUT_C:
return robot.rotC
def MotorRotationCount(motor):
"""
.. [en]
MotorRotationCount(motor)
Get motor tachometer counter value.
:param int motor: motor we want to get tachometer count from.
.. [/en]
.. [sk]
MotorRotationCount(motor)
Načíta hodnotu otáčkomeru motora.
:param int motor: motor, z ktorého chceme načítavať.
.. [/sk]
"""
return MotorTachoCount(motor)
def RotateMotor(motor, speed, angle):
"""
.. [en]
RotateMotor(motor, speed, angle)
Rotate motor in specified direction at specified speed for the specified
number of degrees.
:param int motor: motor we want to rotate
:param int speed: speed we want to run the motor at, from 0 to 100.
Negative value reverses direction.
:param int angle: number of degrees we want to rotate the motor. Negative
value reverses direction.
.. [/en]
.. [sk]
RotateMotor(motor, rýchlosť, uhol)
Otočí motor v danom smere danou rýchlosťou a o istý počet stupňov.
:param int motor: motor, ktorý chceme točiť
:param int speed: rýchlosť, ktorou chceme točiť motor od 0 do 100. Záporná
hodnota otočí smer chodu motora.
:param int angle: počet stupňov pre otáčanie motora. Záporná hodnota otočí
smer chodu motora.
.. [/sk]
"""
OnFwd(motor, speed)
clock = pygame.time.Clock()
start = MotorTachoCount(motor)
while (MotorTachoCount(motor) - start) < angle:
dieTest()
clock.tick(40)
Off(motor)
def rotate_motor(motor, speed, angle):
"""
.. [en]
rotate_motor(motor, speed, angle)
Rotate motor in specified direction at specified speed for the specified
number of degrees.
:param int motor: motor we want to rotate
:param int speed: speed we want to run the motor at, from 0 to 100.
Negative value reverses direction.
:param int angle: number of degrees we want to rotate the motor. Negative
value reverses direction.
.. [/en]
.. [sk]
rotate_motor(motor, rýchlosť, uhol)
Otočí motor v danom smere danou rýchlosťou a o istý počet stupňov.
:param int motor: motor, ktorý chceme točiť
:param int speed: rýchlosť, ktorou chceme točiť motor od 0 do 100. Záporná
hodnota otočí smer chodu motora.
:param int angle: počet stupňov pre otáčanie motora. Záporná hodnota otočí
smer chodu motora.
.. [/sk]
"""
return RotateMotor(motor, speed, angle)