ronex_motor_control.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 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         # Wait until there's one ronex.
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()


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