import numpy as np
import cv2
import pylibi2c
import time
import serial
i2c = pylibi2c.I2CDevice('/dev/i2c-1', 0x48)
##########Testprogramm für i2c Schnittstelle und ADS1015 12-Bit ADC - 4 Channel with Programmable Gain Amplifier
# Set delay
i2c.delay = 10
# Set flags
i2c.flags = pylibi2c.I2C_M_IGNORE_NAK
data = i2c.ioctl_read(0, 2)
print (data)
data = i2c.ioctl_read(1, 2)
print (data)
data = i2c.ioctl_read(2, 2)
print (data)
data = i2c.ioctl_read(3, 2)
print (data)
print('#######################')
i2c.write(1, b'\xc2\x83') #Mode Control
i2c.delay = 10
c_data = i2c.ioctl_read(0, 2)
red = int.from_bytes(c_data, byteorder = 'big')
red = red // 128
print ('red ', red)
i2c.write(1, b'\xd2\x83') #Mode Control
i2c.delay = 10
d_data = i2c.ioctl_read(0, 2)
green = int.from_bytes(d_data, byteorder = 'big')
green = green // 128
print ('green ', green)
i2c.write(1, b'\xe2\x83') #Mode Control
i2c.delay = 10
e_data = i2c.ioctl_read(0, 2)
blue = int.from_bytes(e_data, byteorder = 'big')
blue = blue // 128
print ('blue ', blue)
i2c.write(1, b'\xf2\x83') #Mode Control
i2c.delay = 10
f_data = i2c.ioctl_read(0, 2)
switch = int.from_bytes(f_data, byteorder = 'big')
switch = switch //128
print ('switch ', switch)
print('#######################')
i2c.write(1, b'\x85\x83') #Mode Control
i2c.delay = 10
data = i2c.ioctl_read(0, 2)
print (data)
data = i2c.ioctl_read(1, 2)
print (data)
data = i2c.ioctl_read(2, 2)
print (data)
data = i2c.ioctl_read(3, 2)
print (data)
########################## Ende Testprogramm #########################################
cap0 = cv2.VideoCapture("videotestsrc pattern = 2 ! video/x-raw, framerate=1/1, width=1920, height=1080 ! nvvidconv ! videoconvert ! video/x-raw, format=(string)BGR ! appsink", cv2.CAP_GSTREAMER)
winname = "Resultat"
#cv2.namedWindow(winname, cv2.WND_PROP_FULLSCREEN)
#cv2.moveWindow(winname, 0, 0)
#cv2.setWindowProperty(winname, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
#ser = serial.Serial("/dev/ttyUSB0",9600,8) # UART - USB Adapter
ser = serial.Serial("/dev/ttyACM0", 9600) # UART - USB Kabel
#ser = serial.Serial("/dev/ttyTHS1", 9600) # UART über Pins
ret, bg = cap0.read()
alt_center_coordinate = (0, 0) 9.9.2020
black = (0, 0, 0)
savered = 0
savegreen = 0
saveblue = 0
f = 8
counter = 0
altx=0
alty=0
i=0
gain=500.0
anzsat =('AN')
anzsatgl =('AL')
anzsatga =('AG')
anzsatgb =('AB')
ser.close()
ser.open()
ser.flushOutput()
ser.flushInput()
for j in range (1,10):
data = ser.readline() #Flush, Buffer leeren
print (data)
while cap0.isOpened():
data = ser.readline()
# print (data)
# Testzeile für FPS
# data = (b'$GNRMC,113312.50,A,515n.nnnnn,N,0065n.nnnnn,E,0.059,,090820,,,A,V*12\r\n') Koordinaten verändert
c = data[1:6]
cc = c.decode('UTF-8')
if (cc == 'GPGSV'): #Anzahl Satelliten GPS
anz = data[11:13]
anzsat = anz.decode('UTF-8')
# print (data)
if (cc == 'GLGSV'): ##Anzahl Satelliten GLONASS
anzgl = data[11:13]
anzsatgl = anzgl.decode('UTF-8')
# print (data)
if (cc == 'GAGSV'): ##Anzahl Satelliten Galileo
anzga = data[11:13]
anzsatga = anzga.decode('UTF-8')
# print (data)
if (cc == 'GBGSV'): ##Anzahl Satelliten BeiDou
anzgb = data[11:13]
anzsatgb = anzgb.decode('UTF-8')
# print (data)
if (cc == 'GNRMC'): # Datenzeile Uhrzeit, Koordinaten
A = data[19:29]
N = data[32:43]
s = A.decode('UTF-8')
try:
x= float(s)
except:
x =10
s = N.decode('UTF-8')
try:
y= float(s)
except:
y = 10
x = x *111.3
y = y * 71.5
# print (x,' float A, N ',y)
i = i+1
if (i == 2): #Homepunkt, Startpunkt speichern
altx = x
alty = y
if (i > 2):
nx = (altx - x) * gain + 540 # ~Mitte
ny = (y - alty) * gain + 900
# print (nx, ' nx, ny ',ny)
inx = int (nx)
iny = int (ny)
# print (inx, ' inx, iny ',iny)
if (iny > 1900): #Margins y = x-Achse!!
iny = 1900
if (iny < 1):
iny = 1
if (inx > 1060):
inx = 1060
if (inx < 1):
inx = 1
#############################
i2c.write(1, b'\xc2\x83') #Mode Control C
i2c.delay = 1
c_data = i2c.ioctl_read(0, 2)
red = int.from_bytes(c_data, byteorder = 'big')
red = red // 128
i2c.write(1, b'\xd2\x83') #Mode Control D
i2c.delay = 1
d_data = i2c.ioctl_read(0, 2)
green = int.from_bytes(d_data, byteorder = 'big')
green = green // 128
i2c.write(1, b'\xe2\x83') #Mode Control E
i2c.delay = 1
e_data = i2c.ioctl_read(0, 2)
blue = int.from_bytes(e_data, byteorder = 'big')
blue = blue // 128
i2c.write(1, b'\xf2\x83') #Mode Control F
i2c.delay = 1
f_data = i2c.ioctl_read(0, 2)
switch = int.from_bytes(f_data, byteorder = 'big')
switch = switch //128
# print (red,' ',green,' ',blue,' ', switch)
if (switch > 150): # > 150 ok
while switch > 150:
i2c.write(1, b'\xf2\x83') #Mode Control F
i2c.delay = 1
f_data = i2c.ioctl_read(0, 2)
switch = int.from_bytes(f_data, byteorder = 'big')
switch = switch //128
# altx = x
# alty = y
i = 0
print(' Neustart ')
ret, bg = cap0.read()
if (gain == 5000):
gain = 500
center_coordinate = (1000,150) # Kreis
radius = 60
color = (0,0,255)
thickness = -1
bg = cv2.circle(bg, center_coordinate, radius, color, thickness)
else:
gain = 5000
center_coordinate = (1000,150) # Kreis
radius = 60
color = (0,255,0)
thickness = -1
bg = cv2.circle(bg, center_coordinate, radius, color, thickness)
#############################
center_coordinate = (iny, inx) # Kreis
radius = 10
thickness = -1
if (i == 3): # Differenzierer reset
savered = red
savegreen = green
saveblue = blue
mod_red=(savered-red) * f +128
if mod_red > 255 or mod_red < 0:
savered = red
print (' rot neu ')
mod_green=(savegreen-green) * f +128
if mod_green > 255 or mod_green < 0:
savegreen = green
print (' grün neu ')
mod_blue =(saveblue-blue) * f +128
if mod_blue > 255 or mod_blue < 0:
saveblue = blue
print (' blau neu ')
# print (mod_red,' ',mod_green,' ',mod_blue,' Mod ')
if (switch < 10): # Spur löschen, nur aktuellen Punkt zeigen
bg = cv2.circle(bg, alt_center_coordinate, radius, black, thickness)
# hier Differenzierer reset ?
color = (mod_green, mod_red, mod_blue)
bg = cv2.circle(bg, center_coordinate, radius, color, thickness) #plot aktuellen Punkt
alt_center_coordinate = center_coordinate
start_point = (1160, 50) # Hintergrund Anz Sat
end_point = (1500, 270)
color = (0, 100, 150)
thickness = -1
bg = cv2.rectangle(bg, start_point, end_point, color, thickness)
font = cv2.FONT_HERSHEY_SIMPLEX
org = (1180, 100)
fontScale = 1.5
color = (255, 255, 255)
thickness = 3
sats = str(anzsat)
satsout = ('GPS: ' + sats)
bg = cv2.putText(bg, satsout , org, font, fontScale, color, thickness, cv2.LINE_AA)
org = (1180, 150)
fontScale = 1.5
color = (255, 255, 255)
thickness = 3
sats = str(anzsatgl)
satsout = ('GLONASS: ' + sats)
bg = cv2.putText(bg, satsout , org, font, fontScale, color, thickness, cv2.LINE_AA)
org = (1180, 200)
fontScale = 1.5
color = (255, 255, 255)
thickness = 3
sats = str(anzsatga)
satsout = ('Galileo: ' + sats)
bg = cv2.putText(bg, satsout , org, font, fontScale, color, thickness, cv2.LINE_AA)
org = (1180, 250)
fontScale = 1.5
color = (255, 255, 255)
thickness = 3
sats = str(anzsatgb)
satsout = ('Beidou: ' + sats)
bg = cv2.putText(bg, satsout , org, font, fontScale, color, thickness, cv2.LINE_AA)
counter += 1
print("Sekunde: ", time.strftime("%S"),"Count:",counter)
cv2.imshow(winname, bg)
keyCode = cv2.waitKey(30) & 0xFF
if keyCode == 27:
break
cap0.release()
cap1.release()
cv2.destroyAllWindows()