• How to read gyroscope input (Raspberry Pi)?
    4 replies, posted
Hello everybody, I need some help. How do I successfully read gyroscope input using Raspberry Pi 3B running Phyton 3 on latest Raspbian, connected to a gyroscope? My gyroscope model : Sunfounder GY-521 MPU-6050 https://www.sunfounder.com/arduino-gy-521-mpu-6050-module-3-axial-gyroscope-accelerometer.html I tried following the tutorial on this site Measuring Rotation and acceleration with the Raspberry Pi But the output doesnt show any gyroscope readings... No error generated. I did get the same output as that tutorial using: sudo i2cdetect -y 1 But when I used the code below, there is no output Any idea to get this working? Thanks, any help is appreciated. #!/usr/bin/python import smbus import math import pprint # 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() 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)
does this bit work when you tried it in the tutorial? "If the gyroscope is properly connected, you will see this output (if you have other I2C modules connected, their hex addresses should be displayed):"
I've gotten it to work on the Beaglebone Black before, but I'm not familiar with Raspberry Pi or Python either, I'm afraid. But it appears your code does differ from the code they provide, as yours has parenthesis: 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) where-as the example you've linked to doesn't, not sure if that matters? 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)
Yeah it turn out its my syntax error with the 'print' command. Sorry for sending you guys on a wild goose chase.
Python 3 requires parentheses on print calls, python 2 doesn't.
Sorry, you need to Log In to post a reply to this thread.