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