0

I'm working on a project that uses MPU 6050 and GPS together embedded in raspberry pi device. However, if I try to run the gps code alone or the code for MPU 6050 (accelerometer/gyroscope) alone, it works.

But the problem is when I try to run the code for GPS and MPU 6050 together in one program. It only runs the code for mpu 6050 and ignore many of the code and outputs of the gps.

I think the problem is related to the interfacing of both the gps and mpu 6050.

So, I hope I get any help ... thanks in advance ..

import gyro_acc 
#import speed
import sys, time, requests, jsonify 
import serial
import pynmea2


# defining the api-endpoint 
# API_ENDPOINT = "https://gp-anamoly-api.herokuapp.com/create/anomaly"

# initialize MPU
gyro_acc.MPU_Init()

print(" ::: Start Reading ::: ")

# tracker for the number of potholes/cracks
i = 0
port="/dev/ttyS0"
ser = serial.Serial(port, baudrate=9600, timeout=0.2)
dataout = pynmea2.NMEAStreamReader()
while True:

    # read data
    Gx, Gy, Gz, Ax, Ay, Az = gyro_acc.mpu_read()

    # print("Gx={:.2f} deg/s | Gy={:.2f} deg/s | Gz={:.2f} deg/s".format(Gx, Gy, Gz)) + "|| Ax={:.2f} m/s^2 | Ay={:.2f} m/s^2 | Az={:.2f} m/s^2".format(Ax, Ay, Az))
    print("Gx={:.2f} deg/s | Gy={:.2f} deg/s | Gz={:.2f} deg/s".format(Gx, Gy, Gz))
    print("Ax={:.2f} m/s^2 | Ay={:.2f} m/s^2 | Az={:.2f} m/s^2".format(Ax, Ay, Az))

    # get the location and speed from gps data
    
    
    newdata=ser.readline()

    if newdata[0:6] == "$GPRMC":
        newmsg=pynmea2.parse(newdata)
        lat=newmsg.latitude
        lng=newmsg.longitude
        gps = "Latitude=" + str(lat) + " and Longitude=" + str(lng)
        print(gps)

    if newdata[0:6] == "$GPVTG":
        newmsg=pynmea2.parse(newdata)
        speed = newmsg.spd_over_grnd_kmph
        speed_output = "Speed= {}KM/H".format(speed)
        print(speed_output)

    # separator
    print("===========================")

    
    # print data dynamically
    # sys.stdout.write("\rGx={:.2f} deg/s | Gy={:.2f} deg/s | Gz={:.2f} deg/s".format(Gx, Gy, Gz) \
    #     + " || Ax={:.2f} m/s^2 | Ay={:.2f} m/s^2 | Az={:.2f} m/s^2".format(Ax, Ay, Az) + "\n"
    #     + "Speed: {:.1f} KM/H".format(vehicle_speed))
    # sys.stdout.flush()

    # print speed dynamically
    # sys.stdout.write("\rSpeed: {:.1f} KM/H".format(vehicle_speed))
    # sys.stdout.flush()


    # check if it detect pothole/crack and save to database
    if Az > 13.25:
        # print pothole detected
        # sys.stdout.write("\n")
        print("---------------------------")
        print("Pothole/Crack Detected (No.{})".format(i))
        print("---------------------------")
        i += 1


    # sleep for one second
    time.sleep(1)

the library gyro_acc is another I have in the project.

  • Part of your problem is probably `time.sleep(1)`. That won't work with the GPS receiver, as it generates (depending on the type and the configured messages) several dozen messages per second. You'll loose most of them. – PMF Nov 13 '21 at 12:18
  • It might be due to that, but we removed it, and still the same. Also, I use the time sleeping in the GPS program only and it works perfectly. – Faisal Aldhuwayhi Nov 13 '21 at 13:26
  • What is the actual output you're getting? What parts are missing? – PMF Nov 13 '21 at 13:34
  • the speed part and the latitude and longitude part. They are not printing periodically as the mpu 6050 data. They are not following the order of mpu 6050 data. – Faisal Aldhuwayhi Nov 13 '21 at 21:53
  • This is difficult to get right, I suppose. You either need two threads or you need to change the loop (in particular the delays) such that they don't interfere with each other. – PMF Nov 14 '21 at 13:14

0 Answers0