-
Notifications
You must be signed in to change notification settings - Fork 1
/
original_moontracker.py
1888 lines (1823 loc) · 58.3 KB
/
original_moontracker.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
"""
Motor control and recording for Lunaero
Motor A is up and down
Motor B is right and left
"""
# Standard imports
import io
import multiprocessing as mp
import os
import os.path
import subprocess
import sys
import threading
import time
# Non-standard imports
import cv2
import pygame
# Third Party imports
import numpy as np
# Special Mode Imports
from _datetime import datetime
# Local Imports
#position = (620, 20)
#os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0]) + "," + str(position[1])
pygame.display.init()
pygame.font.init()
# Get information for the current screen size.
SWID, SHEI = pygame.display.Info().current_w, pygame.display.Info().current_h
# Convert to the size of each quadrant of the screen
QWID = int(SWID/2)
QHEI = int(SHEI/2)
#white = (255, 255, 255)
RED = (255, 0, 0)
FONT = pygame.font.SysFont('monospace', 25)
SCREEN = pygame.display.set_mode((SWID, SHEI))
# Enable blind mode while tracking.
# True = don't display tracking window to save cpu cycles
# False = display visuals (default)
BLIND = False
# Select the device you are using.
# Raspberry Pi with Raspberry Pi Camera: 0
# Linux Computer with USB Camera: 1
# WiringPi Compatible Computer (Odroid N2) with USB Camera: 2
# WiringPi Compatible Computer (Odroid N2) with ActionCam: 3
# Raspberry Pi with ActionCam: 4
# TODO detect hardware
DEV = 0
#Enable logs with True, disable with False:
LOGS = False
#Asychronous Recording
ASYNC = True
class TextInput:
"""
Copyright 2017, Silas Gyger, [email protected], All rights reserved.
Borrowed from https://github.com/Nearoo/pygame-text-input under the MIT license.
This class lets the user input a piece of text, e.g. a name or a message.
This class let's the user input a short, one-lines piece of text at a blinking cursor
that can be moved using the arrow-keys. Delete, home and end work as well.
"""
def __init__(self, initial_string="", font_family="", font_size=35, antialias=True, \
text_color=(255, 0, 0), cursor_color=(127, 0, 0), repeat_keys_initial_ms=400, \
repeat_keys_interval_ms=35):
"""
:param initial_string: Initial text to be displayed
:param font_family: name or list of names for font (see pygame.font.match_font
for precise format)
:param font_size: Size of font in pixels
:param antialias: Determines if antialias is applied to font (uses more processing power)
:param text_color: Color of text (duh)
:param cursor_color: Color of cursor
:param repeat_keys_initial_ms: Time in ms before keys are repeated when held
:param repeat_keys_interval_ms: Interval between key press repetition when helpd
"""
# Text related vars:
self.antialias = antialias
self.text_color = text_color
self.font_size = font_size
self.input_string = initial_string # Inputted text
# Fonts
if not os.path.isfile(font_family):
font_family = pygame.font.match_font(font_family)
self.font_object = pygame.font.Font(font_family, font_size)
# Text-surface will be created during the first update call:
self.surface = pygame.Surface((1, 1))
self.surface.set_alpha(0)
# Vars to make keydowns repeat after user pressed a key for some time:
self.keyrepeat_counters = {} # {event.key: (counter_int, event.unicode)} (look for "***")
self.keyrepeat_intial_interval_ms = repeat_keys_initial_ms
self.keyrepeat_interval_ms = repeat_keys_interval_ms
# Things cursor:
self.cursor_surface = pygame.Surface((int(self.font_size/20+1), self.font_size))
self.cursor_surface.fill(cursor_color)
self.cursor_position = len(initial_string) # Inside text
self.cursor_visible = True # Switches every self.cursor_switch_ms ms
self.cursor_switch_ms = 500 # /|\
self.cursor_ms_counter = 0
# Init clock
self.clock = pygame.time.Clock()
def update(self, events):
"""Update the values in the box
"""
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
self.cursor_visible = True # So the user sees where he writes
# If none exist, create counter for that key:
if event.key not in self.keyrepeat_counters:
self.keyrepeat_counters[event.key] = [0, event.unicode]
if event.key == pygame.K_BACKSPACE:
self.input_string = (self.input_string[:max(self.cursor_position - 1, 0)] + \
self.input_string[self.cursor_position:])
# Subtract one from cursor_pos, but do not go below zero:
self.cursor_position = max(self.cursor_position - 1, 0)
elif event.key == pygame.K_DELETE:
self.input_string = (self.input_string[:self.cursor_position] + \
self.input_string[self.cursor_position + 1:])
elif event.key == pygame.K_RETURN:
print(str(self.input_string))
return True
elif event.key == pygame.K_RIGHT:
# Add one to cursor_pos, but do not exceed len(input_string)
self.cursor_position = min(self.cursor_position + 1, len(self.input_string))
elif event.key == pygame.K_LEFT:
# Subtract one from cursor_pos, but do not go below zero:
self.cursor_position = max(self.cursor_position - 1, 0)
elif event.key == pygame.K_END:
self.cursor_position = len(self.input_string)
elif event.key == pygame.K_HOME:
self.cursor_position = 0
else:
# If no special key is pressed, add unicode of key to input_string
self.input_string = (self.input_string[:self.cursor_position] + \
event.unicode + self.input_string[self.cursor_position:])
self.cursor_position += len(event.unicode) # Some are empty, e.g. K_UP
elif event.type == pygame.KEYUP:
# *** Because KEYUP doesn't include event.unicode,
#this dict is stored in such a weird way
if event.key in self.keyrepeat_counters:
del self.keyrepeat_counters[event.key]
# Update key counters:
for key in self.keyrepeat_counters:
self.keyrepeat_counters[key][0] += self.clock.get_time() # Update clock
# Generate new key events if enough time has passed:
if self.keyrepeat_counters[key][0] >= self.keyrepeat_intial_interval_ms:
self.keyrepeat_counters[key][0] = (self.keyrepeat_intial_interval_ms - \
self.keyrepeat_interval_ms)
event_key, event_unicode = key, self.keyrepeat_counters[key][1]
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, key=event_key,\
unicode=event_unicode))
# Re-render text surface:
self.surface = self.font_object.render(self.input_string, self.antialias, self.text_color)
# Update self.cursor_visible
self.cursor_ms_counter += self.clock.get_time()
if self.cursor_ms_counter >= self.cursor_switch_ms:
self.cursor_ms_counter %= self.cursor_switch_ms
self.cursor_visible = not self.cursor_visible
if self.cursor_visible:
cursor_y_pos = self.font_object.size(self.input_string[:self.cursor_position])[0]
# Without this, the cursor is invisible when self.cursor_position > 0:
if self.cursor_position > 0:
cursor_y_pos -= self.cursor_surface.get_width()
self.surface.blit(self.cursor_surface, (cursor_y_pos, 0))
self.clock.tick()
return False
def get_surface(self):
"""
Called to get the surface
"""
return self.surface
def get_text(self):
"""
Called to get the text string
"""
return self.input_string
def get_cursor_position(self):
"""
Called to get the cursor position
"""
return self.cursor_position
def set_text_color(self, color):
"""
Called to set the color of the text
"""
self.text_color = color
def set_cursor_color(self, color):
"""
Called to set the color of the mouse cursor
"""
self.cursor_surface.fill(color)
def clear_text(self):
"""
Called to clear the text string.
"""
self.input_string = ""
self.cursor_position = 0
class TimeLoop():
"""
This class contains screens which ask the user to verify the time listed on the raspberry pi.
The intended use is to provide for instances when the pi is not able to auto sync the clock
with an internet connection, yet not use an RTC.
"""
tix = TextInput()
def __init__(self):
"""
:param startup: Toggle switch for while loop
:param timetuple: Empty tuple to hold time values
:param mrgn: Margin around text to act as padding
:param ftsz: Font size in points
"""
self.startup = True
self.timetuple = ()
self.mrgn = 10
self.ftsz = 25
def firstcheck(self):
"""
Ask the user to confirm that the time is correct
"""
timetuple = ()
while self.startup:
SCREEN.fill((0, 0, 0))
lctn = self.mrgn + self.ftsz
hpos = QWID - 22*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render('-----------------CHECK CLOCK-----------------', True, RED),\
(hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
SCREEN.blit(FONT.render(' Does this time seem correct? ', True, RED),\
(hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
hpos = QWID - round(len(self.thetime())/2)*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render(str(self.thetime()), True, RED), (hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
hpos = QWID - 22*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render(' Press: [y]es or [n]o.... ', True, RED),\
(hpos, lctn))
pygame.display.update()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
return True
if event.key == pygame.K_n:
if LOGS:
print("changing time")
pygame.time.wait(100)
i = 0
while i < 6:
timetuple += (int(self.timeinput(i)),)
i += 1
# Add milliseconds
timetuple += (0,)
if LOGS:
print(timetuple)
while not self.tzpick():
pass
self.startup = not self.startup
return timetuple
def timeinput(self, ith):
"""
Pygame. Constructs a tuple of date values to use when setting the date and time
"""
while True:
SCREEN.fill((0, 0, 0))
lctn = self.mrgn + self.ftsz
hpos = QWID - 22*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render('-----------------CHECK CLOCK-----------------', True, RED),\
(hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
hpos = QWID - 22*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render('Enter the date/time as accurately as possible', True, RED),\
(hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
hpos = QWID - 11*(self.ftsz-self.mrgn)
if ith == 0:
SCREEN.blit(FONT.render('Year (last two-digits)', True, RED), (hpos, lctn))
elif ith == 1:
SCREEN.blit(FONT.render('Month (two-digits)', True, RED), (hpos, lctn))
elif ith == 2:
SCREEN.blit(FONT.render('Day (two-digits)', True, RED), (hpos, lctn))
elif ith == 3:
SCREEN.blit(FONT.render('Hour (two-digits)', True, RED), (hpos, lctn))
elif ith == 4:
SCREEN.blit(FONT.render('Minute (two-digits)', True, RED), (hpos, lctn))
elif ith == 5:
SCREEN.blit(FONT.render('Second (two-digits)', True, RED), (hpos, lctn))
hpos = QWID + 11*(self.ftsz-self.mrgn)
events = pygame.event.get()
SCREEN.blit(self.tix.get_surface(), (hpos, lctn))
if self.tix.update(events):
outputstring = self.tix.get_text()
if len(outputstring) == 2 and outputstring.isdigit():
self.tix.clear_text()
if ith == 0:
outputstring = 2000 + int(outputstring)
return outputstring
else:
self.badinput(lctn)
pygame.display.update()
def badinput(self, blah):
"""
Pygame. Complains to the user if they input an invalid string for the date values and
prompts them to try again.
"""
trigger = True
while trigger:
lctn = blah + self.ftsz + self.mrgn
hpos = QWID - 22*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render('xxxxxxxxxxxxxx TWO DIGITS ONLY xxxxxxxxxxxxxx', True, RED),\
(hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
SCREEN.blit(FONT.render(' Press [Return] to re-enter that value. . . ', True, RED),\
(hpos, lctn))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
self.tix.clear_text()
pygame.time.wait(300)
trigger = False
pygame.display.update()
pygame.display.update()
return
def tzpick(self):
"""
Pygame. Multiple choice selection for timezone
Sets the timezone when finished.
"""
trigger = True
SCREEN.fill((0, 0, 0))
lctn = self.mrgn + self.ftsz
hpos = QWID - 22*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render('Which timezone are you in right now? ', \
True, RED), (hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
hpos = QWID - 11*(self.ftsz-self.mrgn)
SCREEN.blit(FONT.render('[a] - UTC', True, RED), (hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
SCREEN.blit(FONT.render('[b] - Pacific', True, RED), (hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
SCREEN.blit(FONT.render('[c] - Mountain', True, RED), (hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
SCREEN.blit(FONT.render('[d] - Central', True, RED), (hpos, lctn))
lctn = lctn + self.ftsz + self.mrgn
SCREEN.blit(FONT.render('[e] - Eastern', True, RED), (hpos, lctn))
while trigger:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
os.environ['TZ'] = 'America/Los_Angeles'
trigger = False
elif event.key == pygame.K_b:
os.environ['TZ'] = 'America/Denver'
trigger = False
elif event.key == pygame.K_c:
os.environ['TZ'] = 'Europe/London'
trigger = False
elif event.key == pygame.K_d:
os.environ['TZ'] = 'America/Chicago'
trigger = False
elif event.key == pygame.K_e:
os.environ['TZ'] = 'America/New_York'
trigger = False
pygame.display.update()
time.tzset()
return True
def thetime(self):
"""
This function simply grabs the current time with the time zone and returns it.
:returns: - outval - time formatted as a string with the timezone included
"""
outval = time.time()
outval = datetime.fromtimestamp(outval)
outval = outval.strftime('%Y-%m-%d %H:%M:%S')
outval = str(outval) + " " + str(time.localtime().tm_zone)
return outval
def timeloop_running(self):
"""
Executes a time loop function. Use as a while loop threshold until it returns false.
:returns: - False - when finished
"""
timeval = self.firstcheck()
while not timeval:
pass
if isinstance(timeval, tuple):
self._linux_setdate(timeval)
pygame.time.wait(500)
return False
def _linux_setdate(self, time_tuple):
"""
This function sets the time of the system based on a tuple of values.
:param time_tuple: This is the tuple which represents the target time setting.
Subvalues are:
Year (needs to be 4 digits)
Month (the rest are 2 digits)
Day
Hour
Minute
Second
Millisecond
"""
import shlex
time_string = datetime(*time_tuple).isoformat()
#subprocess.call(shlex.split("timedatectl set-ntp false")) # May be necessary
subprocess.call(shlex.split("sudo date -s '%s'" % time_string))
#subprocess.call(shlex.split("sudo hwclock -w"))
return
class FFmpegVideoCapture():
"""
Adapted from:
Reading video from FFMPEG using subprocess - aka when OpenCV VideoCapture fails
By:
Emanuele Ruffaldi 2016
"""
def __init__(self, source="rtsp://127.0.0.1:8554", width=640, height=360):
"""
Initialize the FFMPEG hack stream
:param source: The source of the udp stream, defaults to ActionCam default
:param width: The width of the image stream, defaults to 640
:param height: The height of the image stream, defaults to 360
"""
# Find local parameters of the go environment
self.gopath = str(self.goget())
self.gopath = self.gopath + "/bin/actioncam"
# Run FFMPEG to capture the stream
self.ffmpeg = subprocess.Popen(["ffmpeg", "-i", source, "-f", "rawvideo", "-pix_fmt",\
"yuv420p", "-"], stdout=subprocess.PIPE)
# Local Size Variables
self.width = width
self.height = height
self.halfh = int(self.height/2)
self.halfw = int(self.width/2)
self.fsize = int(width*height*6/4)
# Read the output of the stream to confirm we are getting whatwe expect
self.output = self.ffmpeg.stdout
def read(self):
"""
This function reads the image from the buffer stream.
:returns: - ret - Confirmation that this captured an image
- result - The image that was pulled (w=640, h=360, bgr)
"""
if self.ffmpeg.poll():
return False, None
out = self.output.read(self.fsize)
if out == "":
return False, None
# Y fullsize
# U w/2 h/2
# V w/2 h/2
area = int(self.width * self.height)
# Get the y, u, and v signals from the ffmpeg stream
yyy = np.frombuffer(out[0:area], dtype=np.uint8).reshape((self.height, self.width))
uuu = np.frombuffer(out[area:area + int(area/4)],\
dtype=np.uint8).reshape((self.halfh, self.halfw))
vvv = np.frombuffer(out[area+int(area/4):], \
dtype=np.uint8).reshape((self.halfh, self.halfw))
# Resize the shrunken u and v streams to meet the y size
uuu = cv2.resize(uuu, (self.width, self.height))
vvv = cv2.resize(vvv, (self.width, self.height))
# Merge the yuv and convert to the more useful BGR
result = cv2.merge((yyy, uuu, vvv))
result = cv2.cvtColor(result, cv2.COLOR_YUV2BGR)
return True, result
def goget(self):
"""
This function returns the golang environmental path for the system
"""
return subprocess.check_output(["go", "env", "GOPATH"]).decode().strip('\n')
def start_server(self):
"""
This function starts the RTSP server. It must be run using a non-blocking method.
"""
proc = subprocess.Popen([self.gopath, "rtsp", "192.168.100.1"],\
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#out, err = proc.communicate()
return proc
def killcamera(self):
"""
Kills the camera stream
"""
#TODO
return
class MotorFunctions():
"""
Functions which are unique to the motor control system used in LunAero
"""
if DEV in (0, 4):
import RPi.GPIO as GPIO
elif DEV == 1:
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.FT232H as FT232H
# Local Imports
from Moontracker_Classes import rpt_control
rpt = rpt_control.RPTControl()
elif DEV in (2, 3):
import wiringpi as wpi
else:
raise IOError("Invalid Hardware Selected")
def __init__(self):
"""Initialize the GPIO pins by associating the values of the pins we are using to variable.
Initalize two pins with software controlled PWM with requisite duty cycles and freq.
Start these PWM when they are ready.
Assign values to the motion and ratio thresholds
' """
# Defines the pins being used for the GPIO pins.
if LOGS:
print("Defining GPIO pins")
if DEV in (0, 4):
self.GPIO.setmode(self.GPIO.BCM)
self.apinp = 17 #Pulse width pin for motor A (up and down)
self.apin1 = 27 #Motor control - high for up
self.apin2 = 22 #Motor control - high for down
self.bpin1 = 10 #Motor control - high for left
self.bpin2 = 9 #Motor control - high for right
self.bpinp = 11 #Pulse width pin for motor B (right and left)
# Setup GPIO and start them with 'off' values
pins = (self.apin1, self.apin2, self.apinp, self.bpin1, self.bpin2, self.bpinp)
for i in pins:
self.GPIO.setup(i, self.GPIO.OUT)
if i != self.apinp or self.bpinp:
self.GPIO.output(i, self.GPIO.LOW)
else:
self.GPIO.output(i, self.GPIO.HIGH)
elif DEV == 1:
self.rpt.set_pwm_freq_precisely(70)
self.rpt.set_pwm_freq(4000)
self.apinp = 0 #Pulse width pin for motor A (up and down)
self.apin1 = 2 #Motor control - high for up
self.apin2 = 1 #Motor control - high for down
self.bpin1 = 3 #Motor control - high for left
self.bpin2 = 4 #Motor control - high for right
self.bpinp = 5 #Pulse width pin for motor B (right and left)
elif DEV in (2, 3):
self.wpi.wiringPiSetup()
self.apinp = 0 #Pulse width pin for motor A (up and down)
self.apin1 = 2 #Motor control - high for up
self.apin2 = 3 #Motor control - high for down
self.bpin1 = 12 #Motor control - high for left
self.bpin2 = 13 #Motor control - high for right
self.bpinp = 14 #Pulse width pin for motor B (right and left)
# Setup GPIO and start them with 'off' values
pins = (self.apin1, self.apin2, self.apinp, self.bpin1, self.bpin2, self.bpinp)
for i in pins:
self.wpi.pinMode(i, 1)
if i not in (self.apinp, self.bpinp):
self.wpi.digitalWrite(i, 0)
else:
self.wpi.softPwmCreate(i, 0, 100)
else:
raise IOError("Invalid Hardware Selected")
freq = 10000
self.dca = 0 # Set duty cycle variable to zero at first
self.dcb = 0
if DEV in (0, 4):
if DEV == 0:
self.pwm = self.GPIO.PWM
self.pwma = self.pwm(self.apinp, freq) # Initialize PWM on pwmPins
self.pwmb = self.pwm(self.bpinp, freq)
self.pwma.start(self.dca) # Start pulse width at 0 (pin held low)
self.pwmb.start(self.dcb) # Start pulse width at 0 (pin held low)
elif DEV in (2, 3):
self.wpi.softPwmWrite(self.apinp, self.dca)
self.wpi.softPwmWrite(self.bpinp, self.dcb)
self.acount = 0
self.bcount = 0
self.olddir = 0 # Stores old movement direction; 1 left, 2 right
self.aspect = QWID/QHEI
# the moon must be displaced by this amount for movement to occur.
self.lostratio = 0.001 # a percentage of frame height
self.vtstop = 0.055 * QHEI #offset to stop vertical movement (must be < Start)
self.htstop = 0.05 * QWID #image offset to stop horizontal movement (must be < Start)
# Set a counter to ensure we don't move in the same direction too long
self.move_count_x = 0
self.move_count_y = 0
self.prev_dir_x = 0
self.prev_dir_y = 0
self.move_count_thresh = 200
return
def pinhigh(self, channel):
"""
Pulls a pin high based on the method for the RPi or computer controls
:param channel: - Channel/pin to set high.
"""
if DEV in (0, 4):
self.GPIO.output(channel, self.GPIO.HIGH)
elif DEV == 1:
self.rpt.set_pwm(channel, 4096, 0)
elif DEV in (2, 3):
self.wpi.digitalWrite(channel, 1)
else:
raise IOError("Invalid Hardware Selected")
return
def pinlow(self, channel):
"""
Pulls a pin low based on the method for the RPi or computer controls
:param channel: - Channel/pin to set low.
"""
if DEV in (0, 4):
self.GPIO.output(channel, self.GPIO.LOW)
elif DEV == 1:
self.rpt.set_pwm(channel, 0, 4096)
elif DEV in (2, 3):
self.wpi.digitalWrite(channel, 0)
else:
raise IOError("Invalid Hardware Selected")
return
def setduty(self, motor):
"""
Sets a duty cycle of a pin based on the method for the RPi or computer controls
:param motor: - byte letter (python format string) used to denote which motor we want
to control. Either 'A' or 'B'.
"""
if DEV in (0, 4):
if motor == 'A':
self.pwma.ChangeDutyCycle(self.dca)
elif motor == 'B':
self.pwmb.ChangeDutyCycle(self.dcb)
else:
raise RuntimeError("asked to set duty cycle for non-existant motor")
elif DEV == 1:
if motor == 'A':
self.rpt.set_pwm(self.apinp, 1, self.rpt.perc_to_pulse(self.dca))
elif motor == 'B':
self.rpt.set_pwm(self.bpinp, 1, self.rpt.perc_to_pulse(self.dcb))
else:
raise RuntimeError("asked to set duty cycle for non-existant motor")
elif DEV in (2, 3):
if motor == 'A':
self.wpi.softPwmWrite(self.apinp, self.dca)
elif motor == 'B':
self.wpi.softPwmWrite(self.bpinp, self.dcb)
else:
raise RuntimeError("asked to set duty cycle for non-existant motor")
else:
raise IOError("Invalid Hardware Selected")
def loose_wheel(self):
"""
Gives some extra umph when changing direction for the looser horizontal gear
"""
if LOGS:
print("Left Right power move")
self.dcb = 100
self.setduty('B')
# Sets movement in opposite direction, remember that this will be backwards!
if self.olddir == 1:
self.motright()
elif self.olddir == 2:
self.motleft()
pygame.time.wait(3000)
self.dcb = 25
self.setduty('B')
def sustained_movement(self, direct):
"""
Check to see if we are still moving in the same direciton
If so, add to a counter
If we surpass a threshold, crash.
:param direct: - the direction we are currently moving (0 null, 1 up, 2 down, 3 left, 4 right, 12 neither up nor down, 34 neither left nor right))
"""
if direct == (1, 2):
if self.prev_dir_y == direct:
self.move_count_y += 1
else:
self.move_count_y = 0
if self.move_count_y > self.move_count_thresh:
raise RuntimeError("moving in the same direction too long (y)")
self.prev_dir_y = direct
elif direct == (3, 4):
if self.prev_dir_x == direct:
self.move_count_x += 1
else:
self.move_count_x = 0
if self.move_count_x > self.move_count_thresh:
raise RuntimeError("moving in the same direction too long (x)")
self.prev_dir_x = direct
elif direct == 12:
self.prev_dir_y = 0
self.move_count_y = 0
elif direct == 34:
self.prev_dir_x = 0
self.move_count_x = 0
return
def check_move(self, diffx, diffy, ratio):
"""
Check the values for the difference between x and y of the observed image to the
perfect center of the screen and move the camera to center the image
"""
if ratio < self.lostratio:
return 2
else:
if abs(diffx) < abs((diffy*self.aspect)):
if abs(diffy) > self.vtstop:
if diffy > 0:
self.motup()
self.sustained_movement(1)
else:
self.motdown()
self.sustained_movement(2)
self.speedup("Y")
return 0
else:
if abs(diffx) > self.htstop:
if diffx > 0:
if self.olddir == 2:
self.loose_wheel()
self.motleft()
self.sustained_movement(3)
else:
if self.olddir == 1:
self.loose_wheel()
self.motright()
self.sustained_movement(4)
self.speedup("X")
return 0
if (abs(diffx) < self.htstop and self.dcb > 0):
self.motstop("X")
self.sustained_movement(34)
return 0
if (abs(diffy) < self.vtstop and self.dca > 0):
self.motstop("Y")
self.sustained_movement(12)
return 0
if (abs(diffx) < self.htstop and abs(diffy) < self.vtstop):
self.motstop("B")
self.sustained_movement(12)
self.sustained_movement(34)
return 1
def motstop(self, direct):
"""
Stops the motors in an intelligent way
:param direct: Which directional motor are you stopping? X, Y, or Both?
"""
if LOGS:
print("stopping", direct)
if direct == "B":
while self.dca > 0 or self.dcb > 0:
if self.dca > 10:
self.dca = 10 #quickly stop motor going full speed
else:
self.dca = self.dca - 1 #slowly stop motor going slow (tracking moon)
if self.dcb > 10:
self.dcb = 10
else:
self.dcb = self.dcb - 1
self.setduty('A')
self.setduty('B')
time.sleep(.005)
self.pinlow(self.apin1)
self.pinlow(self.apin2)
self.pinlow(self.bpin1)
self.pinlow(self.bpin2)
elif direct == "Y":
while self.dca > 0:
self.dca = self.dca - 1
self.setduty('A')
time.sleep(.01)
self.pinlow(self.apin1)
self.pinlow(self.apin2)
elif direct == "X":
while self.dcb > 0:
self.dcb = self.dcb - 1
self.setduty('B')
time.sleep(.01)
self.pinlow(self.bpin1)
self.pinlow(self.bpin2)
return
def motdown(self):
"""
Move motors to point scope DOWN
"""
if LOGS:
print("moving down")
self.pinhigh(self.apin1)
self.pinlow(self.apin2)
return
def motup(self):
"""
Move motors to point scope UP
"""
if LOGS:
print("moving up")
self.pinlow(self.apin1)
self.pinhigh(self.apin2)
return
def motright(self):
"""
Move motors to point scope RIGHT
"""
if LOGS:
print("moving right")
self.olddir = 2
self.pinhigh(self.bpin1)
self.pinlow(self.bpin2)
return
def motleft(self):
"""
Move motors to point scope LEFT
"""
if LOGS:
print("moving left")
self.olddir = 1
self.pinlow(self.bpin1)
self.pinhigh(self.bpin2)
return
def speedup(self, direct):
"""
Increase the motor speed by altering the duty cycle of the motor
:param direct: Which motor? X or Y?
The acount/bcount switch increases speed at a slower rate for already high speeds,
this prevents zooming about too much.
"""
if direct == "Y":
if self.dca < 10:
self.dca = 10
elif self.dca < 25:
self.dca += 1
elif self.dca < 40:
if self.acount > 2:
self.dca += 1
self.acount = 0
else:
self.acount += 1
self.setduty('A')
if LOGS:
print("speedup ", direct, self.dca)
elif direct == "X":
if self.dcb < 10:
self.dcb = 10
elif self.dcb < 25:
self.dcb += 1
elif self.dcb < 40:
if self.bcount > 2:
self.dcb += 1
self.bcount = 0
else:
self.bcount += 1
self.setduty('B')
print("speedup", direct, self.dcb)
return
def setdc(self, ina, inb):
"""
Used to set the dutycycle from outside the class
:param ina: the input value for the duty cycle setting for motor A
:param inb: the input value for the duty cycle setting for motor B
"""
self.dca = ina
self.dcb = inb
return
def wait_timer(self):
"""This function checks the current move speed of the motor and outputs a value for
pygame.wait which has an inverse relationship to the speed.
returns - val - ms delay period for pygame.wait function
"""
from math import exp
speed = max(self.dca, self.dcb)
val = int(2560*exp(-.1386*speed))
return val
def cleanup(self):
""" Required to be called at end of program
"""
if DEV in (0, 4):
self.GPIO.cleanup()
elif DEV == 1:
self.rpt.pwm.set_all_pwm(4096, 0)
elif DEV in (2, 3):
self.wpi.softPwmWrite(self.apinp, 0)
self.wpi.softPwmWrite(self.bpinp, 0)
for i in (self.apin1, self.apin2, self.bpin1, self.bpin2):
self.pinlow(i)
else:
raise IOError("Invalid Hardware Selected")
return
#def demo_on(self):
##TEST Remove this portion if you are done with it.
#self.pinhigh(13)
#self.pinlow(14)
#self.rpt.set_pwm(15, 1, self.rpt.perc_to_pulse(25))
#return
#def demo_off(self):
##TEST Remove this portion if you are done with it.
#self.pinlow(13)
#self.pinlow(14)
#self.pinlow(15)
#return
class VideoCaptureAsync:
"""
from http://blog.blitzblit.com/2017/12/24/asynchronous-video-capture-in-python-with-opencv/
"""
def __init__(self, src=0, width=1920, height=1080):
"""
:param src: - Source of stream for cv2.VideoCapture
:param width: - width of stream
:param height: - height of stream
"""
self.src = src
self.cap = cv2.VideoCapture(self.src)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.grabbed, self.frame = self.cap.read()
self.started = False
self.read_lock = threading.Lock()
def set(self, var1, var2):
"""
Sets the variables you input
"""
self.cap.set(var1, var2)
def start(self):
"""
Start the threaded stream
"""
if self.started:
print('[!] Asynchroneous video capturing has already been started.')
return None
self.started = True
self.thread = threading.Thread(target=self.update, args=())
self.thread.start()
return self
def update(self):
"""
Update the threaded stream
"""
while self.started:
grabbed, frame = self.cap.read()
with self.read_lock:
self.grabbed = grabbed
self.frame = frame
def read(self):
"""
Read from the stream
"""
with self.read_lock:
frame = self.frame.copy()
grabbed = self.grabbed
return grabbed, frame
def stop(self):
"""
Stop the stream
"""
self.started = False
self.thread.join()
def __exit__(self, exec_type, exc_value, traceback):
"""
Graceful Exit
"""
self.cap.release()
class CameraFunctions():