Last active
April 26, 2018 00:57
-
-
Save sqamara/0ac31944eb0a58d3cc43660a0d4efaed to your computer and use it in GitHub Desktop.
MPU 6050 with Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import pygame | |
import urllib | |
from OpenGL.GL import * | |
from OpenGL.GLU import * | |
from OpenGL.GLUT import * | |
from math import radians | |
from pygame.locals import * | |
import sys | |
SCREEN_SIZE = (800, 600) | |
SCALAR = .5 | |
SCALAR2 = 0.2 | |
def resize(width, height): | |
glViewport(0, 0, width, height) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(45.0, float(width) / height, 0.001, 10.0) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
gluLookAt(0.0, 1.0, -5.0, | |
0.0, 0.0, 0.0, | |
0.0, 1.0, 0.0) | |
def init(): | |
glEnable(GL_DEPTH_TEST) | |
glClearColor(0.0, 0.0, 0.0, 0.0) | |
glShadeModel(GL_SMOOTH) | |
glEnable(GL_BLEND) | |
glEnable(GL_POLYGON_SMOOTH) | |
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST) | |
glEnable(GL_COLOR_MATERIAL) | |
glEnable(GL_LIGHTING) | |
glEnable(GL_LIGHT0) | |
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.3, 0.3, 0.3, 1.0)); | |
def read_values(): | |
link = "http://127.0.0.1:8080" # Change this address to your settings | |
f = urllib.urlopen(link) | |
myfile = f.read() | |
return myfile.split(" ") | |
def run(): | |
pygame.init() | |
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE | OPENGL | DOUBLEBUF) | |
resize(*SCREEN_SIZE) | |
init() | |
clock = pygame.time.Clock() | |
angle = 0 | |
glutInit(sys.argv) | |
while True: | |
then = pygame.time.get_ticks() | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
return | |
if event.type == KEYUP and event.key == K_ESCAPE: | |
return | |
values = read_values() | |
x_angle = values[0] | |
y_angle = values[1] | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glColor((1.,1.,1.)) | |
glLineWidth(1) | |
glBegin(GL_LINES) | |
glEnd() | |
glPushMatrix() | |
glRotate(float(x_angle), 1, 0, 0) | |
glRotate(-float(y_angle), 0, 0, 1) | |
#glutWireTeapot(1) | |
glutSolidTeapot(1) | |
glPopMatrix() | |
pygame.display.flip() | |
if __name__ == "__main__": | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import smbus | |
import math | |
# Register | |
power_mgmt_1 = 0x6b | |
power_mgmt_2 = 0x6c | |
def read_byte(reg): | |
return bus.read_byte_data(address, reg) | |
def read_word(reg): | |
h = bus.read_byte_data(address, reg) | |
l = bus.read_byte_data(address, reg+1) | |
value = (h << 8) + l | |
return value | |
def read_word_2c(reg): | |
val = read_word(reg) | |
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) # bus = smbus.SMBus(0) fuer Revision 1 | |
address = 0x68 # via i2cdetect | |
# Aktivieren, um das Modul ansprechen zu koennen | |
bus.write_byte_data(address, power_mgmt_1, 0) | |
print "Gyroskop" | |
print "--------" | |
gyroskop_xout = read_word_2c(0x43) | |
gyroskop_yout = read_word_2c(0x45) | |
gyroskop_zout = read_word_2c(0x47) | |
print "gyroskop_xout: ", ("%5d" % gyroskop_xout), " skaliert: ", (gyroskop_xout / 131) | |
print "gyroskop_yout: ", ("%5d" % gyroskop_yout), " skaliert: ", (gyroskop_yout / 131) | |
print "gyroskop_zout: ", ("%5d" % gyroskop_zout), " skaliert: ", (gyroskop_zout / 131) | |
print "Beschleunigungssensor" | |
print "---------------------" | |
beschleunigung_xout = read_word_2c(0x3b) | |
beschleunigung_yout = read_word_2c(0x3d) | |
beschleunigung_zout = read_word_2c(0x3f) | |
beschleunigung_xout_skaliert = beschleunigung_xout / 16384.0 | |
beschleunigung_yout_skaliert = beschleunigung_yout / 16384.0 | |
beschleunigung_zout_skaliert = beschleunigung_zout / 16384.0 | |
print "beschleunigung_xout: ", ("%6d" % beschleunigung_xout), " skaliert: ", beschleunigung_xout_skaliert | |
print "beschleunigung_yout: ", ("%6d" % beschleunigung_yout), " skaliert: ", beschleunigung_yout_skaliert | |
print "beschleunigung_zout: ", ("%6d" % beschleunigung_zout), " skaliert: ", beschleunigung_zout_skaliert | |
print "X Rotation: " , get_x_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert) | |
print "Y Rotation: " , get_y_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import web | |
import smbus | |
import math | |
import time | |
urls = ( | |
'/', 'index' | |
) | |
# Power management registers | |
power_mgmt_1 = 0x6b | |
power_mgmt_2 = 0x6c | |
gyro_scale = 131.0 | |
accel_scale = 16384.0 | |
address = 0x68 # This is the address value read via the i2cdetect command | |
def read_all(): | |
raw_gyro_data = bus.read_i2c_block_data(address, 0x43, 6) | |
raw_accel_data = bus.read_i2c_block_data(address, 0x3b, 6) | |
gyro_scaled_x = twos_compliment((raw_gyro_data[0] << 8) + raw_gyro_data[1]) / gyro_scale | |
gyro_scaled_y = twos_compliment((raw_gyro_data[2] << 8) + raw_gyro_data[3]) / gyro_scale | |
gyro_scaled_z = twos_compliment((raw_gyro_data[4] << 8) + raw_gyro_data[5]) / gyro_scale | |
accel_scaled_x = twos_compliment((raw_accel_data[0] << 8) + raw_accel_data[1]) / accel_scale | |
accel_scaled_y = twos_compliment((raw_accel_data[2] << 8) + raw_accel_data[3]) / accel_scale | |
accel_scaled_z = twos_compliment((raw_accel_data[4] << 8) + raw_accel_data[5]) / accel_scale | |
return (gyro_scaled_x, gyro_scaled_y, gyro_scaled_z, accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
def twos_compliment(val): | |
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) | |
def get_z_rotation(x,y,z): | |
radians = math.atan2(z, dist(x,y)) | |
return math.degrees(radians) | |
bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards | |
class index: | |
def GET(self): | |
now = time.time() | |
K = 0.98 | |
K1 = 1 - K | |
time_diff = 0.01 | |
(gyro_scaled_x, gyro_scaled_y, gyro_scaled_z, accel_scaled_x, accel_scaled_y, accel_scaled_z) = read_all() | |
last_x = get_x_rotation(accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
last_y = get_y_rotation(accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
last_z = get_z_rotation(accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
gyro_offset_x = gyro_scaled_x | |
gyro_offset_y = gyro_scaled_y | |
gyro_offset_z = gyro_scaled_z | |
gyro_total_x = (last_x) - gyro_offset_x | |
gyro_total_y = (last_y) - gyro_offset_y | |
gyro_total_z = (last_z) - gyro_offset_z | |
for i in range(0, int(0.5 / time_diff)): | |
time.sleep(time_diff - 0.005) | |
(gyro_scaled_x, gyro_scaled_y, gyro_scaled_z, accel_scaled_x, accel_scaled_y, accel_scaled_z) = read_all() | |
gyro_scaled_x -= gyro_offset_x | |
gyro_scaled_y -= gyro_offset_y | |
gyro_scaled_z -= gyro_offset_z | |
gyro_x_delta = (gyro_scaled_x * time_diff) | |
gyro_y_delta = (gyro_scaled_y * time_diff) | |
gyro_z_delta = (gyro_scaled_z * time_diff) | |
gyro_total_x += gyro_x_delta | |
gyro_total_y += gyro_y_delta | |
gyro_total_z += gyro_z_delta | |
rotation_x = get_x_rotation(accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
rotation_y = get_y_rotation(accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
rotation_z = get_z_rotation(accel_scaled_x, accel_scaled_y, accel_scaled_z) | |
last_x = K * (last_x + gyro_x_delta) + (K1 * rotation_x) | |
last_y = K * (last_y + gyro_y_delta) + (K1 * rotation_y) | |
last_z = K * (last_z + gyro_z_delta) + (K1 * rotation_z) | |
rotation = str(rotation_x)+" "+str(rotation_y)+" "+str(rotation_z) | |
return rotation | |
if __name__ == "__main__": | |
# Now wake the 6050 up as it starts in sleep mode | |
bus.write_byte_data(address, power_mgmt_1, 0) | |
app = web.application(urls, globals()) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment