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


sr_ronex_examples
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless, Yi Li
autogenerated on Fri Aug 28 2015 13:12:34