00001 #!/usr/bin/env python 00002 ########################################################################### 00003 # This software is graciously provided by HumaRobotics 00004 # under the Simplified BSD License on 00005 # github: git@www.humarobotics.com:baxter_tasker 00006 # HumaRobotics is a trademark of Generation Robots. 00007 # www.humarobotics.com 00008 00009 # Copyright (c) 2013, Generation Robots. 00010 # All rights reserved. 00011 # www.generationrobots.com 00012 # 00013 # Redistribution and use in source and binary forms, with or without 00014 # modification, are permitted provided that the following conditions are met: 00015 # 00016 # 1. Redistributions of source code must retain the above copyright notice, 00017 # this list of conditions and the following disclaimer. 00018 # 00019 # 2. Redistributions in binary form must reproduce the above copyright notice, 00020 # this list of conditions and the following disclaimer in the documentation 00021 # and/or other materials provided with the distribution. 00022 # 00023 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00025 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00026 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 00027 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00029 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00030 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00031 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00033 # THE POSSIBILITY OF SUCH DAMAGE. 00034 # 00035 # The views and conclusions contained in the software and documentation are 00036 # those of the authors and should not be interpreted as representing official 00037 # policies, either expressed or implied, of the FreeBSD Project. 00038 00039 import rospy 00040 from modbus.modbus_wrapper_server import ModbusWrapperServer 00041 from std_msgs.msg import Int32MultiArray as HoldingRegister 00042 00043 if __name__=="__main__": 00044 rospy.init_node("modbus_server") 00045 port = 1234 # custom modbus port without requirement of sudo rights 00046 # port = 502 # default modbus port 00047 if rospy.has_param("~port"): 00048 port = rospy.get_param("~port") 00049 else: 00050 rospy.loginfo("For not using the default port %d, add an arg e.g.: '_port:=1234'",port) 00051 # Init modbus server with specific port 00052 mws = ModbusWrapperServer(port) 00053 # Stop the server if ros is shutdown. This should show that the server is stoppable 00054 rospy.on_shutdown(mws.stopServer) 00055 # Starts the server in a non blocking call 00056 mws.startServer() 00057 print "Server started" 00058 00059 ############### 00060 # Example 1 00061 # write to the Discrete Input 00062 mws.setDigitalInput(0,1) # args: address , value. sets address to value 00063 # Example 2 00064 # read from clients coil output 00065 print "waiting for line 0 to be set to True" 00066 result = mws.waitForCoilOutput(0,5) # args: address,timeout in sec. timeout of 0 is infinite. waits until address is true 00067 if result: 00068 print "got line 0 is True from baxter" 00069 else: 00070 print "timeout waiting for signal on line 0" 00071 00072 00073 ############### 00074 # Example 3 00075 # Listen for the writeable modbus registers in any node 00076 def callback(msg): 00077 rospy.loginfo("Modbus register have been written: %s",str(msg.data)) 00078 rospy.sleep(2) 00079 sub = rospy.Subscriber("modbus_server/read_from_registers",HoldingRegister,callback,queue_size=500) 00080 ############### 00081 00082 00083 ############### 00084 # Example 4 00085 # Publisher to write first 20 modbus registers from any node 00086 pub = rospy.Publisher("modbus_server/write_to_registers",HoldingRegister,queue_size=500) 00087 rospy.sleep(1) 00088 msg = HoldingRegister() 00089 msg.data = range(20) 00090 msg2 = HoldingRegister() 00091 msg2.data = range(20,0,-1) 00092 00093 while not rospy.is_shutdown(): 00094 pub.publish(msg) 00095 rospy.sleep(1) 00096 pub.publish(msg2) 00097 rospy.sleep(1) 00098 ################ 00099 rospy.spin() 00100 00101 mws.stopServer()