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.