101 LED

LEDをOpenRTMで点滅させる

#!/usr/bin/env python
# coding: utf-8
# Python 

import RPi.GPIO as GPIO
import time
import sys
import RTC
import OpenRTM_aist

faboled_spec = ["implementation_id", "FaBoLED",
              "type_name",         "FaBoLEDComponent",
              "description",       "FaBo LED component",
              "version",           "1.0",
              "vendor",            "GClue, Inc.",
              "category",          "fabo",
              "activity_type",     "DataFlowComponent",
              "max_instance",      "10",
              "language",          "Python",
              "lang_type",         "script",
              ""]
LEDPIN = 4
toggle = False

class FaBoLED(OpenRTM_aist.DataFlowComponentBase):

    def __init__(self, manager):
        print "__init__"
        OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)

        return

    def onInitialize(self):
        print "onInitialize"

        GPIO.setwarnings(False)
        GPIO.setmode( GPIO.BCM )
        GPIO.setup( LEDPIN, GPIO.OUT )
        self.toggle = True
        return RTC.RTC_OK

    def onExecute(self, ec_id):
        print "onExcute"
        self.toggle = not(self.toggle)
        GPIO.output( LEDPIN, self.toggle )
        time.sleep(1.0)

        return RTC.RTC_OK

def FaBoLEDInit(manager):
    print "FaBoLEDInit"
    profile = OpenRTM_aist.Properties(defaults_str=faboled_spec)
    manager.registerFactory(profile,
                            FaBoLED,
                            OpenRTM_aist.Delete)    

def MyModuleInit(manager):
    print "MyModuleInit"
    FaBoLEDInit(manager)

    # Create a component
    comp = manager.createComponent("FaBoLED")

def main():
    print "main"
    mgr = OpenRTM_aist.Manager.init(sys.argv)
    mgr.setModuleInitProc(MyModuleInit)
    mgr.activateManager()
    mgr.runManager()

if __name__ == "__main__":
    main()

Boolean型のInPortを追加する

#!/usr/bin/env python
# coding: utf-8
# Python 

import RPi.GPIO as GPIO
import time
import sys
import RTC
import OpenRTM_aist

faboled_spec = ["implementation_id", "FaBoLED",
              "type_name",         "FaBoLEDComponent",
              "description",       "FaBo LED component",
              "version",           "1.0",
              "vendor",            "GClue, Inc.",
              "category",          "fabo",
              "activity_type",     "DataFlowComponent",
              "max_instance",      "10",
              "language",          "Python",
              "lang_type",         "script",
              ""]
LEDPIN = 4
toggle = False

class FaBoLED(OpenRTM_aist.DataFlowComponentBase):

    def __init__(self, manager):
        print "__init__"
        OpenRTM_aist.DataFlowComponentBase.__init__(self, manager)

        self._ledFlag     = RTC.TimedBoolean(RTC.Time(0,0),0)
        self._ledFlagIn     = OpenRTM_aist.InPort("LED", self._ledFlag)
        self.addInPort("LED", self._ledFlagIn)

        return

    def onInitialize(self):
        print "onInitialize"

        GPIO.setwarnings(False)
        GPIO.setmode( GPIO.BCM )
        GPIO.setup( LEDPIN, GPIO.OUT )
        self.toggle = True
        return RTC.RTC_OK

    def onExecute(self, ec_id):
        print "onExcute"
        self.toggle  = self._ledFlag.read()
        GPIO.output( LEDPIN, self.toggle.data)
        time.sleep(0.1)

        return RTC.RTC_OK

def FaBoLEDInit(manager):
    print "FaBoLEDInit"
    profile = OpenRTM_aist.Properties(defaults_str=faboled_spec)
    manager.registerFactory(profile,
                            FaBoLED,
                            OpenRTM_aist.Delete)    

def MyModuleInit(manager):
    print "MyModuleInit"
    FaBoLEDInit(manager)

    # Create a component
    comp = manager.createComponent("FaBoLED")

def main():
    print "main"
    mgr = OpenRTM_aist.Manager.init(sys.argv)
    mgr.setModuleInitProc(MyModuleInit)
    mgr.activateManager()
    mgr.runManager()

if __name__ == "__main__":
    main()