sr_ronex_flash_LED_with_PWM.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # ####################################################################
00004 # Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00005 #
00006 # This library is free software; you can redistribute it and/or
00007 # modify it under the terms of the GNU Lesser General Public
00008 # License as published by the Free Software Foundation; either
00009 # version 3.0 of the License, or (at your option) any later version.
00010 #
00011 # This library is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 # Lesser General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU Lesser General Public
00017 # License along with this library.
00018 # ####################################################################
00019 
00020 import roslib
00021 import rospy
00022 from time import sleep
00023 from sr_ronex_msgs.msg import PWM
00024 
00025 roslib.load_manifest('sr_ronex_examples')
00026 
00027 # --------------------------------------------------------------------------------
00028 
00029 # Flash a LED light with PWM.
00030 
00031 
00032 def flashLED(topic):
00033     pwm_period = 320
00034     # Start with a 100% duty cycle.
00035     pwm_on_time_0 = pwm_period
00036     # The second output is not used.
00037     pwm_on_time_1 = 0
00038 
00039     pub = rospy.Publisher(topic, PWM)
00040     while not rospy.is_shutdown():
00041         # Flash the light...
00042         pwm_on_time_0 -= 10
00043         if pwm_on_time_0 < 0:
00044             pwm_on_time_0 = pwm_period
00045 
00046         # Set the PWM data.
00047         pwm = PWM()
00048         pwm.pwm_period = pwm_period
00049         pwm.pwm_on_time_0 = pwm_on_time_0
00050         pwm.pwm_on_time_1 = pwm_on_time_1
00051 
00052         pub.publish(pwm)
00053         rospy.sleep(0.01)
00054 
00055 # --------------------------------------------------------------------------------
00056 
00057 """
00058 This class demonstrates how to find the General I/O module with the given ronex_id.
00059 """
00060 
00061 
00062 class SrRonexFindGeneralIOModule(object):
00063 
00064     def __init__(self, ronex_id):
00065         self.ronex_id = ronex_id
00066 
00067     """
00068     Get the path of the General I/O module with the given ronex_id.
00069     Note that None is returned if the module is not found.
00070     """
00071     def get_path(self):
00072         """
00073         Find the ronexes present on the system.
00074         """
00075         # Wait until there's one ronex.
00076         while True:
00077             try:
00078                 rospy.get_param("/ronex/devices/0/ronex_id")
00079                 break
00080             except:
00081                 rospy.loginfo("Waiting for the ronex to be loaded properly.")
00082                 sleep(0.1)
00083 
00084         """
00085         Retrieve all the ronex parameter ids from the parameter server.
00086         If there are three General I/O modules, then ronex_param_ids is [0,1,2].
00087         Note that the id starts from zero. And the size of the returned variable
00088         is equal to the number of General I/O modules.
00089         """
00090         ronex_param_ids = rospy.get_param("/ronex/devices")
00091         for key in ronex_param_ids:
00092             if self.ronex_id == ronex_param_ids[key]["ronex_id"]:
00093                 path = ronex_param_ids[key]["path"]
00094                 return path
00095 
00096 # --------------------------------------------------------------------------------
00097 
00098 """
00099 Assume that your RoNeX consists of a Bridge (IN) module, and one or multiple General I/O module(s).
00100 This example demonstrates how to flash a LED light with pulse-width modulation (PWM).
00101 """
00102 if __name__ == "__main__":
00103     rospy.init_node('sr_ronex_flash_LED_with_PWM')
00104 
00105     print "Please configure D0 pin of your ronex module as output (you can use plugin Dynamic Reconfigure in rqt): \n"
00106 
00107     # Note that you may have to set the value of ronex_id,
00108     # depending on which General I/O board the LED is connected to.
00109     ronex_id = raw_input("Please enter the ronex id: ")
00110     findModule = SrRonexFindGeneralIOModule(str(ronex_id))
00111     path = findModule.get_path()
00112 
00113     if path is not None:
00114         # Always use the first digital I/O channel to flash the LED light.
00115         # For example "/ronex/general_io/1" + "/command/pwm/0".
00116         topic = path + "/command/pwm/0"
00117         rospy.loginfo("topic = %s.", topic)
00118         try:
00119             flashLED(topic)
00120         except rospy.ROSInterruptException:
00121             pass
00122     else:
00123         rospy.loginfo("Failed to find the General I/O module with the given ronex_id %s.", ronex_id)
00124 
00125 # --------------------------------------------------------------------------------


sr_ronex_examples
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless, Yi Li
autogenerated on Thu Jun 6 2019 21:22:11