sr_ronex_spi_dw1000.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 import roslib; roslib.load_manifest('sr_ronex_examples')
00021 import rospy
00022 from time import sleep
00023 from sr_ronex_msgs.srv import SPI
00024 
00025 class DW1000SpiInterface(object):
00026     """
00027     """
00028 
00029     def __init__(self, ):
00030         """
00031         """
00032         self.spi_srvs_ = []
00033 
00034         for spi_out_index in range(0, 4):
00035             self.spi_srvs_.append(rospy.ServiceProxy("/ronex/spi/35/command/passthrough/"+str(spi_out_index), SPI))
00036 
00037     def read_register(self, spi_out_index, address, data_packet):
00038         """
00039         Reads from the DW1000 register.
00040 
00041         @spi_out_index: which spi are you using (0 to 3)
00042         @address: trying to read from which address
00043         @data_packet: the packet that will be sent
00044 
00045         @return values read from SPI, None if failed
00046         """
00047         return_data_packet = []
00048 
00049         if address < 64:
00050             data_packet.insert(0, address)
00051         else:
00052             rospy.logerr("Address provided ("+str(address)+") is > 64")
00053             return None
00054 
00055         try:
00056             resp = self.spi_srvs_[spi_out_index](data_packet)
00057         except rospy.ServiceException, e:
00058             rospy.logerr("Service call failed: %s"%e)
00059             return None
00060 
00061         #removing the first item from the received packet of data
00062         return_data_packet = resp.data[1:]
00063 
00064         print "SPI[",spi_out_index,"]: reading ",self.hexify_list(return_data_packet), " from address: ",address
00065 
00066         return return_data_packet
00067 
00068     def write_register(self, spi_out_index, address, data_packet):
00069         """
00070         Writes to the DW1000 register.
00071 
00072         @spi_out_index: which spi are you using (0 to 3)
00073         @address: trying to write to which address
00074         @data_packet: the packet of data you want to write
00075 
00076         @return True if success, False otherwise
00077         """
00078         print "SPI[",spi_out_index,"]: writing ", self.hexify_list(data_packet),"] to address: ",address
00079         if address < 64:
00080             data_packet.insert(0, address + 0x80)
00081         else:
00082             rospy.logerr("Address provided ("+str(address)+") is > 64")
00083             return False
00084 
00085         try:
00086             resp = self.spi_srvs_[spi_out_index](data_packet)
00087         except rospy.ServiceException, e:
00088             rospy.logerr("Service call failed: %s"%e)
00089             return False
00090 
00091         print " ... OK data written"
00092         return True
00093 
00094     def hexify_list(self, l):
00095         hex_l = []
00096         for item in l:
00097             hex_l.append( str(hex(int(item))) )
00098         return hex_l
00099 
00100 
00101 if __name__ == "__main__":
00102     rospy.init_node('sr_ronex_spi_dw1000')
00103 
00104     dw1000 = DW1000SpiInterface()
00105 
00106     print "--- new write"
00107     success = dw1000.write_register(0, 0x03, [1,1,1,1])
00108     packet = dw1000.read_register(0, 0x03, [0,1,2,3])
00109 
00110     print "--- new write"
00111     success = dw1000.write_register(0, 0x03, [0x01,0x20,0x30, 0x40])
00112     packet = dw1000.read_register(0, 0x03, [1,1,1,1])
00113 
00114 
00115 
00116 


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