Nuevo Proyecto

por | 01/23/2015

Una parte de este proyecto se centra en un modulo GPS ( GY-GPS6MV2 / NEO-6M ) y un GY-86 ( MS5611 + HMC5883l + MPU6050 ). Ya dedicare un post de que son cada uno y como es el tema de la conexión a una RPI ( Raspberry Pi modelo B ); ahora lo que les quiero pasar es un poco de codigo como hice andar todo esto, yo lo llame «All in One».

Utilice Python, i2c y gpsd:
apt-get install gpsd gpsd-clients python-gps i2c-tools python-smbus

Y el código maestro es ( aio.py ):

#!/usr/bin/python

## GENERAL

import time
import smbus
import math
import os

os.system('clear')

print
print "------------------------------------------------------"
print "*** ALL IN ONE by Sepa ***"
print "*** GPS + MPU6050 + HMC5883l + MS5611 ***"
print "------------------------------------------------------"
print
print

## MPU6050

# Power management registers
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c

def read_byte(adr):
return bus.read_byte_data(address, adr)

def read_word(adr):
high = bus.read_byte_data(address, adr)
low = bus.read_byte_data(address, adr+1)
val = (high << 8) + low return val def read_word_2c(adr): val = read_word(adr) if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val

def dist(a,b):
return math.sqrt((a*a)+(b*b))

def get_y_rotation(x,y,z):
radians = math.atan2(x, dist(y,z))
return -math.degrees(radians)

def get_x_rotation(x,y,z):
radians = math.atan2(y, dist(x,z))
return math.degrees(radians)

bus = smbus.SMBus(1)
address = 0x68

bus.write_byte_data(address, power_mgmt_1, 0)

print
print "Giroscopo "
print "------------------"

gyro_xout = read_word_2c(0x43)
gyro_yout = read_word_2c(0x45)
gyro_zout = read_word_2c(0x47)

print "gyro_xout: ", gyro_xout, " scaled: ", (gyro_xout / 131)
print "gyro_yout: ", gyro_yout, " scaled: ", (gyro_yout / 131)
print "gyro_zout: ", gyro_zout, " scaled: ", (gyro_zout / 131)

print
print "Acelerometro"
print "---------------------"

accel_xout = read_word_2c(0x3b)
accel_yout = read_word_2c(0x3d)
accel_zout = read_word_2c(0x3f)

accel_xout_scaled = accel_xout / 16384.0
accel_yout_scaled = accel_yout / 16384.0
accel_zout_scaled = accel_zout / 16384.0

print "accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled
print "accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled
print "accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled

print "x rotation: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
print "y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)

## MS5611

# bus = smbus.SMBus(1)

bus.write_byte(0x77, 0x48)
time.sleep(0.05)

D1 = bus.read_i2c_block_data(0x77, 0x00)
time.sleep(0.05)

bus.write_byte(0x77, 0x58)
time.sleep(0.05)
D2 = bus.read_i2c_block_data(0x77, 0x00)
time.sleep(0.05)

D1 = D1[0] * 65536 + D1[1] * 256.0 + D1[2]
D2 = D2[0] * 65536 + D2[1] * 256.0 + D2[2]

C1 = bus.read_i2c_block_data(0x77, 0xA2) #Pressure Sensitivity
time.sleep(0.05)
C2 = bus.read_i2c_block_data(0x77, 0xA4) #Pressure Offset
time.sleep(0.05)
C3 = bus.read_i2c_block_data(0x77, 0xA6) #Temperature coefficient of pressure sensitivity
time.sleep(0.05)
C4 = bus.read_i2c_block_data(0x77, 0xA8) #Temperature coefficient of pressure offset
time.sleep(0.05)
C5 = bus.read_i2c_block_data(0x77, 0xAA) #Reference temperature
time.sleep(0.05)
C6 = bus.read_i2c_block_data(0x77, 0xAC) #Temperature coefficient of the temperature
time.sleep(0.05)

C1 = C1[0] * 256.0 + C1[1]
C2 = C2[0] * 256.0 + C2[1]
C3 = C3[0] * 256.0 + C3[1]
C4 = C4[0] * 256.0 + C4[1]
C5 = C5[0] * 256.0 + C5[1]
C6 = C6[0] * 256.0 + C6[1]

dT = D2 - C5 * 2**8
TEMP = 2000 + dT * C6 / 2**23

TEMP_F = TEMP/100.0 * 9.0/5 + 32

print
print "Temperatura y presion"
print "---------------------"

print "Temperature: ", TEMP/100.0
print "Temperature F: ", round(TEMP_F, 2)

OFF = C2 * 2**16 + (C4 * dT) / 2**7
SENS = C1 * 2**15 + (C3 * dT) / 2**8
P = (D1 * SENS / 2**21 - OFF) / 2**15

print "Pressure: ", P/100.0

print "Pressure Adjusted: ", round(P/100.0 + 55.5, 2)

## HMC5883l

# bus = smbus.SMBus(1)
address = 0x68

def read_byte(adr):
return bus.read_byte_data(address, adr)

def read_word(adr):
high = bus.read_byte_data(address, adr)
low = bus.read_byte_data(address, adr+1)
val = (high << 8) + low return val def read_word_2c(adr): val = read_word(adr) if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val

def write_byte(address, adr, value):
bus.write_byte_data(address, adr, value)

write_byte(address, 0, 0b01110000) # Set to 8 samples @ 15Hz
write_byte(address, 1, 0b00100000) # 1.3 gain LSb / Gauss 1090 (default)
write_byte(address, 2, 0b00000000) # Continuous sampling

scale = 0.92

x_out = read_word_2c(3) * scale
y_out = read_word_2c(7) * scale
z_out = read_word_2c(5) * scale

bearing = math.atan2(y_out, x_out)
if (bearing < 0):
bearing += 2 * math.pi

print
print "Compas"
print "---------------------"

print ("Bearing: ", math.degrees(bearing))

## GPS

print
print "GPS"
print "---------------------"

from gps import *
session = gps() # assuming gpsd running with default options on port 2947
session.stream(WATCH_ENABLE|WATCH_NEWSTYLE)
while True:
try:
report = session.next()
if report['class'] == 'TPV':
if hasattr(report, 'track'):
print "Vel: ", report.speed * 3.6
print "Lat: ", report.lat
print "Lon: ", report.lon
print "URL: https://www.google.com.ar/maps/dir/%.7f,%.7f" % (report.lat,report.lon)
print "Alt: ", report.alt
print "Trk: ", report.track
print "Clm: ", report.climb
break
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None

print
print
print "------------------------------------------------------"
print

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.