コード例 #1
0
class GroveGasSensorMQ2:
    def __init__(self, channel):
        self.channel = channel
        self.adc = ADC()

    @property
    def MQ2(self):
        value = self.adc.read(self.channel)
        return value
コード例 #2
0
from adc import ADC
adc = ADC(11, mosi=10, miso=9, ss=8)

for i in range(5):
    print adc.read(0)

import Adafruit_GPIO.SPI as SPI
#
# spi = SPI.SpiDev(0,0)
# spi.open()
# spi.set_clock_hz(5000)
# spi.set_mode(0)
# spi.set_bit_order(0)
コード例 #3
0
class PiSmart(_Basic_class):
    _class_name = 'PiSmart'

    ON = 1
    OFF = 0

    SERVO_POWER_OFF = 0x20
    SERVO_POWER_ON = 0x21
    SPEAKER_POWER_OFF = 0x22
    SPEAKER_POWER_ON = 0x23
    MOTOR_POWER_OFF = 0x24
    MOTOR_POWER_ON = 0x25
    SET_POWER_DC = 0X26  # power_type = 0, DC power
    SET_POWER_2S = 0X27  # power_type = 1, Li-po battery 2s
    SET_POWER_3S = 0X28  # power_type = 2, Li-po battery 3s

    POWER_TYPE = 0X1d  # send this number to ask arduino return power-type

    def __init__(self):
        self.logger_setup()
        self.speaker_volume = 70
        self.capture_volume = 100
        tmp = self.power_type
        sleep(0.01)  # when arduino open debug, sleep(> 0.3)
        self.adc = ADC(5)  # Power voltage channel: 5
        self._debug('Init PiSmart object complete')

    def servo_switch(self, on_off):
        self.pwm_switch(on_off)

    def pwm_switch(self, on_off):
        if on_off not in (0, 1):
            raise ValueError(
                "On_off set must be .ON(1) or .OFF(0), not \"{0}\".".format(
                    on_off))
        if on_off == 1:
            self._write_sys_byte(self.SERVO_POWER_ON)
            self._debug('Servo switch ON')
        else:
            self._write_sys_byte(self.SERVO_POWER_OFF)
            self._debug('Servo switch OFF')

    def motor_switch(self, on_off):
        if on_off not in (0, 1):
            raise ValueError(
                "On_off set must be .ON(1) or .OFF(0), not \"{0}\".".format(
                    on_off))
        if on_off == 1:
            self._write_sys_byte(self.MOTOR_POWER_ON)
            self._debug('Motor switch ON')
        else:
            self._write_sys_byte(self.MOTOR_POWER_OFF)
            self._debug('Motor switch OFF')

    def speaker_switch(self, on_off):
        if on_off not in (0, 1):
            raise ValueError(
                "On_off set must in .ON(1) or .OFF(0), not \"{0}\".".format(
                    on_off))
        if on_off == 1:
            self._write_sys_byte(self.SPEAKER_POWER_ON)
            self._debug('Speaker switch ON')
        else:
            self._write_sys_byte(self.SPEAKER_POWER_OFF)
            self._debug('Speaker switch OFF')

    @property
    def power_type(self):
        self._power_type = self._read_sys_byte(self.POWER_TYPE)
        self._debug('Get power type from bottom board')
        return self._power_type

    @power_type.setter
    def power_type(self, power_type):
        if power_type not in ['2S', '3S', 'DC']:
            raise ValueError(
                'Power type only support: "2S", "3S" Li-po battery or "DC" power, not \"{0}\".'
                .format(power_type))
        else:
            self._power_type = power_type
            if power_type == '2S':
                self._write_sys_byte(self.SET_POWER_2S)
                self._debug('Set power type to 2S Li-po battery')
            elif power_type == '3S':
                self._write_sys_byte(self.SET_POWER_3S)
                self._debug('Set power type to 3S Li-po battery')
            elif power_type == 'DC':
                self._write_sys_byte(self.SET_POWER_DC)
                self._debug('Set power type to DC power')

    @property
    def power_voltage(self):
        A7_value = self.adc.read()
        if A7_value:
            A7_volt = float(A7_value) / 1024 * 5
            battery_volt = round(A7_volt * 14.7 / 4.7, 2)
            self._debug('Read battery: %s V' % battery_volt)
            return battery_volt
        else:
            return False

    @property
    def speaker_volume(self):
        return self._speaker_volume

    @speaker_volume.setter
    def speaker_volume(self, value):
        if value > 100:
            value = 100
            self._warning('Value is over 100, set to 100')
        if value < 0:
            value = 0
            self._warning('Value is less than 0, set to 0')
        # gain(dB) = 10 * log10(volume)
        #self._debug('speaker percentage = %s' % value)
        self._speaker_volume = self._map(value, 0, 100, 0, 75)
        self._debug('speaker percentage = %s' % value)
        #self._speaker_volume = self._map(value, 0, 100, ((10.0**(-102.39/10))-1), ((10.0**(4.0/10))-1))
        #self._speaker_volume = int(math.log10(self._speaker_volume) * 100) * 10
        #self._debug('speaker dB = %s' % self._speaker_volume)
        cmd = "sudo amixer -M sset 'PCM' %d%%" % self._speaker_volume
        self.run_command(cmd)

    @property
    def capture_volume(self):
        return self._capture_volume

    @capture_volume.setter
    def capture_volume(self, value):
        if value not in range(0, 101):
            raise ValueError(
                "Volume should be in [0, 100], not \"{0}\".".format(value))
        self._capture_volume = value
        cmd = "sudo amixer -M -c 1 sset 'Mic' %d%%" % (self._capture_volume)
        self.run_command(cmd)
        return 0

    def _get_capture_volume_id(self):
        all_controls = self.run_command("sudo amixer -c 1 controls")
        all_controls = all_controls.split('\n')
        capture_volume = ''
        capture_volume_id = ''
        for line in all_controls:
            if 'Mic Capture Volume' in line:
                capture_volume = line
        capture_volume = capture_volume.split(',')
        for variable in capture_volume:
            if 'numid' in variable:
                capture_volume_id = variable
        capture_volume_id = capture_volume_id.split('=')[1]
        return int(capture_volume_id)

    def _get_capture_volume_max(self, numid):
        all_values = self.run_command("sudo amixer -c 1 cget numid=%s" % numid)
        all_values = all_values.split('\n')
        values = all_values[1]
        values = values.split(',')
        for value in values:
            if 'max' in value:
                max_value = value
        max_value = max_value.split('=')[1]

        return int(max_value)

    @property
    def cpu_temperature(self):
        raw_cpu_temperature = commands.getoutput(
            "cat /sys/class/thermal/thermal_zone0/temp")
        cpu_temperature = float(raw_cpu_temperature) / 1000
        #cpu_temperature = 'Cpu temperature : ' + str(cpu_temperature)
        return cpu_temperature

    @property
    def gpu_temperature(self):
        raw_gpu_temperature = commands.getoutput(
            '/opt/vc/bin/vcgencmd measure_temp')
        gpu_temperature = float(
            raw_gpu_temperature.replace('temp=', '').replace('\'C', ''))
        #gpu_temperature = 'Gpu temperature : ' + str(gpu_temperature)
        return gpu_temperature

    @property
    def ram_info(self):
        p = os.popen('free')
        i = 0
        while 1:
            i = i + 1
            line = p.readline()
            if i == 2:
                return line.split()[1:4]

    @property
    def ram_total(self):
        ram_total = round(int(self.ram_info[0]) / 1000, 1)
        return ram_total

    @property
    def ram_used(self):
        ram_used = round(int(self.ram_info[1]) / 1000, 1)
        return ram_used

    @property
    def disk_space(self):
        p = os.popen("df -h /")
        i = 0
        while 1:
            i = i + 1
            line = p.readline()
            if i == 2:
                return line.split()[1:5]

    @property
    def disk_total(self):
        disk_total = float(self.disk_space[0][:-1])
        return disk_total

    @property
    def disk_used(self):
        disk_used = float(self.disk_space[1][:-1])
        return disk_used

    @property
    def cpu_usage(self):
        return str(
            os.popen(
                "top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())

    def end(self):
        self.servo_switch(self.OFF)
        self.pwm_switch(self.OFF)
        self.motor_switch(self.OFF)
        self.speaker_switch(self.OFF)
コード例 #4
0
class PiSmart(_Basic_class):
    _class_name = 'Amateur PiSmart'

    ON = 1
    OFF = 0

    def __init__(self, *item):
        self._ps            = pismart.PiSmart()
        self._ps.DEBUG = 'error'
        self.servo_switch   = self._ps.servo_switch
        self.pwm_switch     = self._ps.pwm_switch
        self.motor_switch   = self._ps.motor_switch
        self.speaker_switch = self._ps.speaker_switch
        if len(item) == 0:
            self.ALL_init()
        elif item == "manual":
            pass

    def ALL_init(self):
        self.ADC_init()
        self.Servo_init()
        self.LED_init()
        self.Motor_init()
        self.TTS_init()
        self.STT_init()

    def ADC_init(self):
        from adc import ADC
        self._A0 = ADC(0)
        self._A1 = ADC(1)
        self._A2 = ADC(2)
        self._A3 = ADC(3)
        self._A4 = ADC(4)
        self._A0.DEBUG = 'error'
        self._A1.DEBUG = 'error'
        self._A2.DEBUG = 'error'
        self._A3.DEBUG = 'error'
        self._A4.DEBUG = 'error'

    def Servo_init(self):
        from servo import Servo
        self._servo0 = Servo(0)
        self._servo1 = Servo(1)
        self._servo2 = Servo(2)
        self._servo3 = Servo(3)
        self._servo4 = Servo(4)
        self._servo5 = Servo(5)
        self._servo6 = Servo(6)
        self._servo7 = Servo(7)
        self.servo_switch(self.ON)
        self._servo0.DEBUG = 'error'
        self._servo1.DEBUG = 'error'
        self._servo2.DEBUG = 'error'
        self._servo3.DEBUG = 'error'
        self._servo4.DEBUG = 'error'
        self._servo5.DEBUG = 'error'
        self._servo6.DEBUG = 'error'
        self._servo7.DEBUG = 'error'

    def PWM_init(self):
        from pwm import PWM
        self._pwm0 = PWM(0)
        self._pwm1 = PWM(1)
        self._pwm2 = PWM(2)
        self._pwm3 = PWM(3)
        self._pwm4 = PWM(4)
        self._pwm5 = PWM(5)
        self._pwm6 = PWM(6)
        self._pwm7 = PWM(7)
        self.pwm_switch(self.ON)
        self._pwm0.DEBUG = 'error'
        self._pwm1.DEBUG = 'error'
        self._pwm2.DEBUG = 'error'
        self._pwm3.DEBUG = 'error'
        self._pwm4.DEBUG = 'error'
        self._pwm5.DEBUG = 'error'
        self._pwm6.DEBUG = 'error'
        self._pwm7.DEBUG = 'error'

    def LED_init(self):
        from led import LED
        self._led = LED()
        self._led.DEBUG = 'error'

    def Motor_init(self):
        from motor import Motor
        self._motor_a = Motor(Motor.MotorA)
        self._motor_b = Motor(Motor.MotorB)
        self._motor_a.DEBUG = 'error'
        self._motor_b.DEBUG = 'error'
        self.motor_switch(self.ON)

    def TTS_init(self):
        from tts import TTS
        self._tts = TTS()
        self._tts.DEBUG = 'error'
        self.speaker_switch(self.ON)
        self.speaker_volume = 100

    def STT_init(self):
        from stt import STT
        self._stt = STT('dictionary', name_calling=False, timeout=10.0, dictionary_update=True)
        self._stt.DEBUG = 'error'
        self.capture_volume = 100

    def ADC_end(self):
        pass
    
    def Servo_end(self):
        self.servo_switch(self.OFF)

    def PWM_end(self):
        self.pwm_switch(self.OFF)

    def Motor_end(self):
        self._motor_a.stop()
        self._motor_b.stop()
        self.motor_switch(self.OFF)

    def LED_end(self):
        self.LED = 0

    def TTS_end(self):
        self._tts.end()
        self.speaker_switch(self.OFF)

    def STT_end(self):
        self._stt.end()


    def end(self):
        self.ADC_end()
        self.LED_end()
        self.Motor_end()
        self.Servo_end()
        self.PWM_end()
        self.STT_end()

    @property
    def power_type(self):
        return self._ps.power_type
    @power_type.setter
    def power_type(self, value):
        self._ps.power_type = value
    @property
    def power_voltage(self):
        return self._ps.power_voltage
    @power_voltage.setter
    def power_voltage(self, value):
        self._ps.power_voltage = value
    @property
    def speaker_volume(self):
        return self._ps.speaker_volume
    @speaker_volume.setter
    def speaker_volume(self, value):
        self._ps.speaker_volume = value
    @property
    def capture_volume(self):
        return self._ps.capture_volume
    @capture_volume.setter
    def capture_volume(self, value):
        self._ps.capture_volume = value
    @property
    def cpu_temperature(self):
        return self._ps.cpu_temperature

    @property
    def A0(self):
        return self._A0.read()
    @property
    def A1(self):
        return self._A1.read()
    @property
    def A2(self):
        return self._A2.read()
    @property
    def A3(self):
        return self._A3.read()
    @property
    def A4(self):
        return self._A4.read()

    @property
    def Servo0(self):
        return self._servo0.angle
    @Servo0.setter
    def Servo0(self, angle):
        self._servo0.angle = angle
    @property
    def Servo1(self):
        return self._servo1.angle
    @Servo1.setter
    def Servo1(self, angle):
        self._servo1.angle = angle
    @property
    def Servo2(self):
        return self._servo2.angle
    @Servo2.setter
    def Servo2(self, angle):
        self._servo2.angle = angle
    @property
    def Servo3(self):
        return self._servo3.angle
    @Servo3.setter
    def Servo3(self, angle):
        self._servo3.angle = angle
    @property
    def Servo4(self):
        return self._servo4.angle
    @Servo4.setter
    def Servo4(self, angle):
        self._servo4.angle = angle
    @property
    def Servo5(self):
        return self._servo5.angle
    @Servo5.setter
    def Servo5(self, angle):
        self._servo5.angle = angle
    @property
    def Servo6(self):
        return self._servo6.angle
    @Servo6.setter
    def Servo6(self, angle):
        self._servo6.angle = angle
    @property
    def Servo7(self):
        return self._servo7.angle
    @Servo7.setter
    def Servo7(self, angle):
        self._servo7.angle = angle

    @property
    def PWM0(self):
        return self._pwm0.value
    @PWM0.setter
    def PWM0(self, value):
        self._pwm0.value = value
    @property
    def PWM1(self):
        return self._pwm1.value
    @PWM1.setter
    def PWM1(self, value):
        self._pwm1.value = value
    @property
    def PWM2(self):
        return self._pwm2.value
    @PWM2.setter
    def PWM2(self, value):
        self._pwm2.value = value
    @property
    def PWM3(self):
        return self._pwm3.value
    @PWM3.setter
    def PWM3(self, value):
        self._pwm3.value = value
    @property
    def PWM4(self):
        return self._pwm4.value
    @PWM4.setter
    def PWM4(self, value):
        self._pwm4.value = value
    @property
    def PWM5(self):
        return self._pwm5.value
    @PWM5.setter
    def PWM5(self, value):
        self._pwm5.value = value
    @property
    def PWM6(self):
        return self._pwm6.value
    @PWM6.setter
    def PWM6(self, value):
        self._pwm6.value = value
    @property
    def PWM7(self):
        return self._pwm7.value
    @PWM7.setter
    def PWM7(self, value):
        self._pwm7.value = value

    @property
    def LED(self):
        return self._led.brightness
    @LED.setter
    def LED(self, value):
        self._led.brightness = value    

    @property
    def MotorA(self):
        return self._motor_a.speed
    @MotorA.setter
    def MotorA(self, value):
        self._motor_a.speed = value
    @property
    def MotorB(self):
        return self._motor_b.speed
    @MotorB.setter
    def MotorB(self, value):
        self._motor_b.speed = value

    @property
    def MotorA_reversed(self):
        return self._motor_a.is_reversed
    @MotorA_reversed.setter
    def MotorA_reversed(self, value):
        self._motor_a.is_reversed = value
    @property
    def MotorB_reversed(self):
        return self._motor_b.is_reversed
    @MotorB_reversed.setter
    def MotorB_reversed(self, value):
        self._motor_b.is_reversed = value

    @property
    def Say(self):
        return self._tts.say
    @Say.setter
    def Say(self, words):
        self._tts.say = words

    @property
    def listen(self):
        return self._stt.recognize()

    @property
    def heard(self):
        return self._stt.heard

    @property
    def result(self):
        return self._stt.result