ADCPi-如何在没有root的情况下运行?

时间:2014-08-24 10:07:46

标签: python raspberry-pi

我有一个Pi模型A,运行最新版本的Raspbian。插入其中的是带有各种模拟传感器的ADC-Pi(https://www.abelectronics.co.uk/products/3/Raspberry-Pi/17/ADC-Pi-V2---Raspberry-Pi-Analogue-to-Digital-converter)。

运行演示代码(如下所示) 如果我使用'./adc_demo.py'它工作正常 如果我使用'sudo python3 adc_demo.py',我会收到错误'导入错误:没有名为quick2wire.i2c的模块'。 我能做什么,所以我可以使用后一种语句来运行它?我有另一个脚本通过pi上的GPIO引脚运行电机,需要以root身份运行 - 我正在尝试将两个脚本合并在一起。

adc_demo.py

#!/usr/bin/env python3
# read abelectronics ADC Pi board inputs
# uses quick2wire from http://quick2wire.com/
# See http://elinux.org/RPi_ADC_I2C_Python for full setup instructions

import quick2wire.i2c as i2c

import re
import time

adc_address1 = 0x68
adc_address2 = 0x69

adc_channel1 = 0x98
adc_channel2 = 0xB8
adc_channel3 = 0xD8
adc_channel4 = 0xF8


for line in open('/proc/cpuinfo').readlines():
    m = re.match('(.*?)\s*:\s*(.*)', line)
    if m:
        (name, value) = (m.group(1), m.group(2))
        if name == "Revision":
           if value [-4:] in ('0002', '0003'):
                i2c_bus = 0
            else:
                i2c_bus = 1
            break

with i2c.I2CMaster(i2c_bus) as bus:

    def getadcreading(address, channel):
        bus.transaction(i2c.writing_bytes(address, channel))
        time.sleep(0.05)
        h, l, r = bus.transaction(i2c.reading(address,3))[0]
        time.sleep(0.05)
        h, l, r = bus.transaction(i2c.reading(address,3))[0]

        t = (h << 8) | l
        v = t * 0.000154
        if v < 5.5:
            return v
        else: # must be a floating input
            return 0.00

while True:

    print("1: %f" % getadcreading(adc_address1, adc_channel1))
    print("2: %f" % getadcreading(adc_address1, adc_channel2))
    print("3: %f" % getadcreading(adc_address1, adc_channel3))
    print("4: %f" % getadcreading(adc_address1, adc_channel4))

    print("5: %f" % getadcreading(adc_address2, adc_channel1))
    print("6: %f" % getadcreading(adc_address2, adc_channel2))
    print("7: %f" % getadcreading(adc_address2, adc_channel3))
    print("8: %f" % getadcreading(adc_address2, adc_channel4))

    time.sleep(1)

0 个答案:

没有答案