I have problem with my script in Python 3 (on Raspberry Pi). Im trying to build a simple Drone with it. I have problem with 2 threads: one for Gyro and one for GPS. Threads only check data and save it in sqlite3 database. I use that kind of technic in several projects, and never had any prolbem with that. But now I cand figure it out, why app crashes after seweral seconds with Segmetation Fault error. Im building in with PyQt5. I have 2 other thread, one is for stream pi camera feed to http, other is for port listening (for control commands) Here is the code:
In main function I start thread like that:
#Thread Gyro
t3 = threading.Thread(target=worker_gyro)
t3.daemon = True
threads.append(t3)
t3.start()
#Thread GPS
t4 = threading.Thread(target=worker_gps)
t4.daemon = True
threads.append(t4)
t4.start()
and the functions are:
def worker_gps():
while True:
for new_data in gps_socket:
if new_data:
data_stream.unpack(new_data)
gps1=str((data_stream.lat))
gps2=str((data_stream.lon))
gps3=str((data_stream.alt))
gps4=str((data_stream.speed))
gps5=str((data_stream.climb))
conn = sqlite3.connect("baza_danych.db", isolation_level=None)
cur = conn.cursor()
try:
cur.execute('UPDATE gps set wartosc="' + gps1 + '" where nazwa="Latitude"')
cur.execute('UPDATE gps set wartosc="' + gps2 + '" where nazwa="Longitude"')
cur.execute('UPDATE gps set wartosc="' + gps3 + '" where nazwa="Wysokosc"')
cur.execute('UPDATE gps set wartosc="' + gps4 + '" where nazwa="Szybkosc"')
cur.execute('UPDATE gps set wartosc="' + gps5 + '" where nazwa="Climb"')
except sqlite3.Error as err:
print('Query Failed: %s\nError: %s' % (conn, str(err)))
conn.close()
time.sleep(2)
def worker_gyro():
while True:
gyroskop_xout = read_word_2c(0x43)
gyroskop_yout = read_word_2c(0x45)
gyroskop_zout = read_word_2c(0x47)
gyroskop_xout_przeskalowane = gyroskop_xout / 131
gyroskop_yout_przeskalowane = gyroskop_yout / 131
gyroskop_zout_przeskalowane = gyroskop_zout / 131
akcelerometr_xout = read_word_2c(0x3b)
akcelerometr_yout = read_word_2c(0x3d)
akcelerometr_zout = read_word_2c(0x3f)
akcelerometr_xout_przeskalowany = akcelerometr_xout / 16384.0
akcelerometr_yout_przeskalowany = akcelerometr_yout / 16384.0
akcelerometr_zout_przeskalowany = akcelerometr_zout / 16384.0
btemp = read_word_2c(0x41)
btemp = (int(btemp) / 100)
rotacjax = get_x_rotation(akcelerometr_xout_przeskalowany, akcelerometr_yout_przeskalowany,
akcelerometr_zout_przeskalowany)
rotacjay = get_y_rotation(akcelerometr_xout_przeskalowany, akcelerometr_yout_przeskalowany,
akcelerometr_zout_przeskalowany)
conn = sqlite3.connect("baza_danych.db", isolation_level=None)
cur = conn.cursor()
try:
cur.execute('UPDATE gyro set wartosc="' + str(gyroskop_xout)+'" where nazwa="gyro_x"')
cur.execute('UPDATE gyro set wartosc="' + str(gyroskop_yout) + '" where nazwa="gyro_y"')
cur.execute('UPDATE gyro set wartosc="' + str(gyroskop_zout) + '" where nazwa="gyro_z"')
cur.execute('UPDATE gyro set wartosc="' + str(gyroskop_xout_przeskalowane) + '" where nazwa="gyro_x_skala"')
cur.execute('UPDATE gyro set wartosc="' + str(gyroskop_yout_przeskalowane) + '" where nazwa="gyro_y_skala"')
cur.execute('UPDATE gyro set wartosc="' + str(gyroskop_zout_przeskalowane) + '" where nazwa="gyro_z_skala"')
cur.execute('UPDATE gyro set wartosc="' + str(akcelerometr_xout) + '" where nazwa="acce_x"')
cur.execute('UPDATE gyro set wartosc="' + str(akcelerometr_yout) + '" where nazwa="acce_y"')
cur.execute('UPDATE gyro set wartosc="' + str(akcelerometr_zout) + '" where nazwa="acce_z"')
cur.execute('UPDATE gyro set wartosc="' + str(akcelerometr_xout_przeskalowany) + '" where nazwa="acce_x_sklala"')
cur.execute('UPDATE gyro set wartosc="' + str(akcelerometr_yout_przeskalowany) + '" where nazwa="acce_y_sklala"')
cur.execute('UPDATE gyro set wartosc="' + str(akcelerometr_zout_przeskalowany) + '" where nazwa="acce_z_sklala"')
cur.execute('UPDATE gyro set wartosc="' + str(rotacjax) + '" where nazwa="rotacja_x"')
cur.execute('UPDATE gyro set wartosc="' + str(rotacjay) + '" where nazwa="rotacja_y"')
cur.execute('UPDATE gyro set wartosc="' + str(btemp) + '" where nazwa="temp"')
except sqlite3.Error as err:
print('Query Failed: %s\nError: %s' % (conn, str(err)))
conn.close()
time.sleep(1)