0

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)
Torxed
  • 22,866
  • 14
  • 82
  • 131
Avatarus
  • 141
  • 4
  • 15
  • did you check memory? Look at this thread: https://stackoverflow.com/a/10035594/2127451 – Koxo Jan 31 '19 at 08:11
  • thx, using Your gdb method, problem is with Thread 1 "python3" received signal SIGSEGV, Segmentation fault. 0x6db2963c in qt_convert_rgb888_to_rgb32(unsigned int*, unsigned char const*, int) () from /usr/lib/arm-linux-gnueabihf/libQt5Gui.so.5 – Avatarus Jan 31 '19 at 11:03
  • This might be related to how you're doing the GUI and not so much the threads themselves. For instance, if you're trying to update values in the graphical environment from a thread and that makes things wonk out. Run a profiler and see which execution causes it (`python -m trace --trace script.py`) – Torxed Jan 31 '19 at 11:58
  • my problem is with another part of script: def setFrame(self, frame): if(frame.isNull()): pass else: pixmap = QPixmap.fromImage(frame) #<----- that couses Error self.label.setPixmap(pixmap) – Avatarus Jan 31 '19 at 12:40

0 Answers0