Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 from rospy import get_param, loginfo, Subscriber, Publisher, init_node, is_shutdown, sleep
00021 from sr_ronex_msgs.msg import PWM, GeneralIOState
00022
00023 class SrRonexFindGeneralIOModule(object):
00024 """
00025 This class demonstrates how to find the General I/O module with the given ronex_id.
00026 """
00027
00028 def __init__(self, ronex_id):
00029 self.ronex_id = ronex_id
00030
00031 def get_path(self):
00032 """
00033 Get the path of the General I/O module with the given ronex_id.
00034 Note that None is returned if the module is not found.
00035 """
00036
00037
00038 while True:
00039 try:
00040 get_param("/ronex/devices/0/ronex_id")
00041 break
00042 except:
00043 loginfo("Waiting for the ronex to be loaded properly.")
00044 sleep(0.05)
00045
00046 """
00047 Retrieve all the ronex parameter ids from the parameter server.
00048 If there are three General I/O modules, then ronex_param_ids is [0,1,2].
00049 Note that the id starts from zero. And the size of the returned variable
00050 is equal to the number of General I/O modules.
00051 """
00052 for device in get_param("/ronex/devices").itervalues():
00053 if self.ronex_id == device["ronex_id"]:
00054 return device["path"]
00055
00056 def general_io_state_cb(data, controller):
00057 controller.analog = data.analogue[0]
00058
00059 class SpeedController():
00060 def __init__(self):
00061 path = SrRonexFindGeneralIOModule('10').get_path()
00062 topic = path + "/state"
00063 self.analog = 0.0
00064 self.subscriber = Subscriber(topic, GeneralIOState, general_io_state_cb, self)
00065 topic = path + "/command/pwm/0"
00066 self.publisher = Publisher(topic, PWM, latch=True)
00067
00068 def execute(self):
00069 while not is_shutdown():
00070 self.publisher.publish(PWM(pwm_period=6400, pwm_on_time_1=0, pwm_on_time_0=self.analog))
00071 sleep(0.01)
00072
00073 init_node('sr_speed_controller')
00074 controller = SpeedController()
00075 controller.execute()