demo.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2010, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of Willow Garage, Inc. nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 import bluetooth
00036 
00037 
00038 def print_hex(data):    
00039     for i,s in enumerate(data):
00040         print "%02x" % ord(s),
00041         if i%16 == 15:
00042             print
00043     print
00044 
00045 def print_hex_msg(data):
00046     count = 0
00047     for s in data:
00048         val = ord(s)
00049         if val == 2:
00050             count = 0
00051             print '\n\n'
00052         if val == 3:
00053             count = 0
00054             print '\n\n'
00055         print ("%02x" % val),
00056         count += 1
00057         if count == 16:
00058             count = 0
00059             print
00060     for s in data:
00061         val = ord(s)
00062         if val == 2:
00063             count = 0
00064             print '\n\n'
00065         if val == 3:
00066             count = 0
00067             print '\n\n'
00068         print ("%02x" % val),
00069         count += 1
00070         if count == 16:
00071             count = 0
00072             print
00073 
00074     print    
00075 
00076 def convert_str(data):
00077     result = []
00078     for c in data:    
00079         result.append(ord(c))
00080     return result
00081 
00082 def convert_hex(data):
00083     s = ""
00084     for val in data:
00085         s+=chr(val)
00086     return s
00087 
00088 def hex_to_short(data):
00089     return (data[0] << 8) | data[1]
00090 
00091 seqnum = 1
00092 def make_msg(typ, body):
00093     global seqnum
00094     data = [ 0 for i in range(8)]
00095     length = len(body)+8  # header length (8) + body length
00096     data[0] = (length >> 8) & 0xFF
00097     data[1] = length & 0xFF
00098     data[2] = 0 #checksum placeholder    
00099     data[3] = (seqnum >> 8) & 0xFF
00100     data[4] = seqnum & 0xFF
00101     seqnum+=1
00102     data[5] = 0 #resend?
00103     data[6] = (typ >> 8) & 0xFF
00104     data[7] = (typ & 0xFF)
00105     data += body
00106     data[2] = (-sum(data)) & 0xff
00107     return [2] + data + [3]
00108 
00109 def make_action_msg(action,speed,stops,do_blowout,mix_cycles):
00110     body = [ 0 for i in range(6) ] 
00111     body[0] = action
00112     body[1] = speed
00113     body[2] = (stops>>8)&0xff
00114     body[3] = stops&0xff
00115     body[4] = do_blowout
00116     body[5] = mix_cycles
00117     return make_msg(4,body)
00118 
00119 def make_voyager_move(position):
00120     body = [ (position>>8)&0xff, position&0xff ]
00121     return make_msg(12,body)
00122 
00123 class PipetteOutputHeader:
00124     def __init__(self,data):
00125         data = data[1:-2]
00126         self.length = (data[0] << 8) | (data[1])
00127         if (sum(data)&0xFF) != 0:
00128             print sum(data)
00129             print (sum(data) & 0xff)
00130             print "bad checksum"
00131             
00132         self.checksum = data[2]
00133         self.seqnum = (data[3] << 8) | (data[4])        
00134         self.resend = data[5]
00135         self.msg_type = (data[6] << 8) | (data[7])
00136         self.error_code = data[8]
00137         self.body = data[9:]
00138         print "Body = ", self.body
00139 
00140     def __str__(self):
00141         s = "PipetteOutputHeader\n"
00142         s += " length %d\n" % self.length
00143         s += " cksum  0x%02x\n" % self.checksum
00144         s += " seqnum %d\n" % self.seqnum
00145         s += " resend %d\n" % self.resend
00146         s += " type   %d\n" % self.msg_type
00147         s += " error  %d\n" % self.error_code
00148         s += " body   "
00149         for v in self.body:
00150             s += "%02x" % v
00151         s += '\n'
00152         return s
00153 
00154 class PipetteInfoOutput:
00155     def __init__(self, hdr):
00156         self.hdr = hdr
00157         body = hdr.body
00158         print " owner : '%s'" % convert_hex(body[0:11+1])
00159         print " serial : '%s'" % convert_hex(body[12:23+1])
00160         print " fwmajor : ", hex_to_short(body[24:25+1])
00161         print " fwminor : ", hex_to_short(body[26:27+1])
00162         print " motorsteps millions : ", hex_to_short(body[58:61+1])
00163         print " motorsteps          : ", hex_to_short(body[62:65+1]) 
00164 
00165 #print make_msg(1, [])
00166 #print convert_hex([2, 0, 0, 1, 0, 0, 0, 0, 1, 3])
00167 
00168 def send_msg(sock,data):
00169     bytes_sent = sock.send( convert_hex(data) )
00170     if bytes_sent != len(data):
00171         raise RuntimeError("Sent only %d of %d bytes"%(bytes_sent, len(data)))
00172 
00173 def recv_msg(sock):
00174     data = []
00175     while (len(data) == 0) or (data[-1] != chr(3)):
00176         data += sock.recv(1024)
00177         #print "Recieved :",  convert_str(data)
00178         #print_hex(data)
00179 
00180     print "Recieved : %d bytes" % (len(data))
00181     print_hex(data)
00182     h = PipetteOutputHeader(convert_str(data))
00183     print h
00184     return h
00185 
00186 
00187 bd_addr = "00:80:98:e6:2b:30"
00188 port = 1
00189 
00190 #data = make_msg(1, [])
00191 #print data
00192 
00193 sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
00194 sock.connect((bd_addr, port))
00195 #bluetooth.set_packet_timeout(bd_addr, int(0))
00196 
00197 print "Connected"
00198 data = sock.recv(1024)
00199 print "Recieved",  convert_str(data)
00200 
00201 send_msg(sock, make_msg(1, []))
00202 h = recv_msg(sock)
00203 i = PipetteInfoOutput(h)
00204 
00205 
00206 #def make_action_msg(action,speed,stops,do_blowout,mix_cycles):
00207 #send_msg(sock,make_action_msg(7,50,0,1,0))
00208 #recv_msg(sock)
00209 
00210 #send_msg(sock,make_voyager_move(0))
00211 #recv_msg(sock)
00212     
00213 #send_msg(sock,make_voyager_move(1000))
00214 #recv_msg(sock)
00215 
00216 
00217 sock.close()


pipette_driver
Author(s): Kevin Watts
autogenerated on Tue Dec 10 2013 15:49:44