comModbusRtu.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Robotiq, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Robotiq, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Copyright (c) 2012, Robotiq, Inc.
00034 # Revision $Id$
00035 #
00036 # Modifed from the orginal comModbusTcp by Kelsey Hawkins @ Georgia Tech
00037 
00038 
00039 
00040 """@package docstring
00041 Module comModbusRtu: defines a class which communicates with Robotiq Grippers using the Modbus RTU protocol. 
00042 
00043 The module depends on pymodbus (http://code.google.com/p/pymodbus/) for the Modbus RTU client.
00044 """
00045 
00046 from pymodbus.client.sync import ModbusSerialClient
00047 from math import ceil
00048 
00049 class communication:    
00050 
00051    def __init__(self):
00052       self.client = None
00053       
00054    def connectToDevice(self, device):
00055       """Connection to the client - the method takes the IP address (as a string, e.g. '192.168.1.11') as an argument."""
00056       self.client = ModbusSerialClient(method='rtu',port=device,stopbits=1, bytesize=8, baudrate=115200, timeout=0.2)
00057       if not self.client.connect():
00058           print "Unable to connect to %s" % device
00059           return False
00060       return True
00061 
00062    def disconnectFromDevice(self):
00063       """Close connection"""
00064       self.client.close()
00065 
00066    def sendCommand(self, data):   
00067       """Send a command to the Gripper - the method takes a list of uint8 as an argument. The meaning of each variable depends on the Gripper model (see support.robotiq.com for more details)"""
00068       #make sure data has an even number of elements   
00069       if(len(data) % 2 == 1):
00070          data.append(0)
00071 
00072       #Initiate message as an empty list
00073       message = []
00074 
00075       #Fill message by combining two bytes in one register
00076       for i in range(0, len(data)/2):
00077          message.append((data[2*i] << 8) + data[2*i+1])
00078 
00079       #To do!: Implement try/except 
00080       self.client.write_registers(0x03E8, message, unit=0x0009)
00081 
00082    def getStatus(self, numBytes):
00083       """Sends a request to read, wait for the response and returns the Gripper status. The method gets the number of bytes to read as an argument"""
00084       numRegs = int(ceil(numBytes/2.0))
00085 
00086       #To do!: Implement try/except 
00087       #Get status from the device
00088       response = self.client.read_holding_registers(0x07D0, numRegs, unit=0x0009)
00089 
00090       #Instantiate output as an empty list
00091       output = []
00092 
00093       #Fill the output with the bytes in the appropriate order
00094       for i in range(0, numRegs):
00095          output.append((response.getRegister(i) & 0xFF00) >> 8)
00096          output.append( response.getRegister(i) & 0x00FF)
00097       
00098       #Output the result
00099       return output


robotiq_modbus_rtu
Author(s): Nicolas Lauzier (Robotiq inc.), Kelsey Hawkins
autogenerated on Thu Aug 27 2015 14:44:34