2016-04-14 12 views
-1
#!/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ですか?出力として

265 -17を産生-34

。私の質問は、 "256"だけが生成されるようにコードを変更する方法です。この値を含む変数の名前は何ですか?これらはかなり基本的な質問であるようですが、私はプログラマーではないので、私は一度にこの一歩を踏み出そうとしています。私はSunの場所のJay Dosherのコードに置き換えることができるようにこのコードを "適応"しようとしていますが、彼はIMUを読むために上のコードを親切に作ったRaspberry Piの別のIMU Joanを使用しています。

#!/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

私は一度もPythonコードを書いていませんが、 'print {" {} {} "。format(x、y、z))'は疑わしく見えます。私は 'print(" {} "。format(x))'に変更して、何が起こるかを見てみよう。 – mah

+0

それは私が試したものです!私はスクリプトをチェックし、それは私が変更した最後の行でした。それは(264、-14、-33)を生成する。それは3つの軸を生み出すだけでなく、パラセシスでそれらを囲んでいます。 – frazelle09

+0

Oho!私は、この行(x、y、z)= struct.unpack( '<3h'、バッファ(b))をコメントアウトされた状態(#)に戻しました。したがって、残りの唯一の質問は、この値を持つ変数の名前は何ですか? – frazelle09

答えて

1

この場合の変数名はで、(x, y, z) = struct.unpack('<3h', buffer(b))で定義されています。

関連する問題