#!/usr/bin/env python
# i2c_ADXL345.py este es el accelerómetro de 3 ejes
# 2015-04-01
# Public Domain
import time
import struct
import sys
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
if sys.version > '3':
buffer = memoryview
BUS=0
ADXL345_I2C_ADDR=0x53
#RUNTIME=60.0 This is the original line.
#RUNTIME=0.10 / This was the new line
pi=pigpio.pi() # open local Pi
h = pi.i2c_open(BUS, ADXL345_I2C_ADDR)
if h >= 0: # Connected OK?
# Initialise ADXL345.
pi.i2c_write_byte_data(h, 0x2d, 0) # POWER_CTL reset.
pi.i2c_write_byte_data(h, 0x2d, 8) # POWER_CTL measure.
pi.i2c_write_byte_data(h, 0x31, 0) # DATA_FORMAT reset.
pi.i2c_write_byte_data(h, 0x31, 11) # DATA_FORMAT full res +/- 16g.
read = 0
# start_time = time.time() ?part of the RUNTIME?
# while (time.time()-start_time) < RUNTIME: ?part of the RUNTIME?
# 0x32 = X LSB, 0x33 = X MSB
# 0x34 = Y LSB, 0x35 = Y MSB
# 0x36 = Z LSB, 0x37 = Z MSB
# < = little endian
(s, b) = pi.i2c_read_i2c_block_data(h, 0x32, 6)
if s >= 0:
(x, y, z) = struct.unpack('<3h', buffer(b))
print("{} {} {}".format(x, y, z))
read += 1
pi.i2c_close(h)
pi.stop()
print()
以上代码生成
265 -17 -34
作为输出。我的问题是我如何更改代码,以便只有&#34; 256&#34;被生产?包含此值的变量的名称是什么?这些似乎是非常基本的问题,但由于我不是程序员,我试图一次采取这一步骤。我正努力适应&#34;这段代码让我可以将它替换为Jay Dosher的Sun位置代码,但由于他使用的是另一个IMU Joan,来自Raspberry Pi,请编写上述代码来阅读我们的IMU。
#!/usr/bin/env python
# i2c_ADXL345.py this is the 3 axis accelerometer
# 2015-04-01
# Public Domain
# Note - Don't forget to run pigpiod first
import time
import struct
import sys
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
if sys.version > '3':
buffer = memoryview
BUS=0
ADXL345_I2C_ADDR=0x53
#RUNTIME=60.0 This is the original line.
#RUNTIME=0.10 / This was the new line
pi=pigpio.pi() # open local Pi
h = pi.i2c_open(BUS, ADXL345_I2C_ADDR)
if h >= 0: # Connected OK?
# Initialise ADXL345.
pi.i2c_write_byte_data(h, 0x2d, 0) # POWER_CTL reset.
pi.i2c_write_byte_data(h, 0x2d, 8) # POWER_CTL measure.
pi.i2c_write_byte_data(h, 0x31, 0) # DATA_FORMAT reset.
pi.i2c_write_byte_data(h, 0x31, 11) # DATA_FORMAT full res +/- 16g.
read = 0
# start_time = time.time() ?part of the RUNTIME?
# while (time.time()-start_time) < RUNTIME: ?part of the RUNTIME?
# 0x32 = X LSB, 0x33 = X MSB
# 0x34 = Y LSB, 0x35 = Y MSB
# 0x36 = Z LSB, 0x37 = Z MSB
# < = little endian
(s, b) = pi.i2c_read_i2c_block_data(h, 0x32, 6)
if s >= 0:
(x, y, z) = struct.unpack('<3h', buffer(b))
# (x) = struct.unpack('<3h', buffer(b)) # Let's experiment again.
# print("{} {} {}".format(x, y, z))
print("{}".format(x))
read += 1
pi.i2c_close(h)
pi.stop()
print()
这是新代码,现在只生成254.我们剩下的问题是:具有此值的变量的名称是什么?
答案 0 :(得分:1)
在这种情况下,变量的名称为x
,如(x, y, z) = struct.unpack('<3h', buffer(b))
中所定义。