00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 import time, os, copy
00033 import serial, numpy as np
00034 import threading
00035 from threading import RLock
00036
00037 class AMTI_force:
00038 def __init__(self, dev_name="/dev/robot/force_plate0", broadcast=True):
00039 self.dev_name = dev_name
00040 self.serial_dev = open_serial(dev_name, 57600)
00041 self.offset = None
00042 if not self._reset():
00043 while not self._reset():
00044 time.sleep(50/1000.0)
00045 self.last_value = self._read_once()
00046
00047 def read(self):
00048 reading = self._read_once()
00049 while (reading == self.last_value).all():
00050 time.sleep(8/1000.0)
00051 reading = self._read_once()
00052 return reading
00053
00054 def _reset(self):
00055 self.serial_dev.write('R')
00056 self.serial_dev.flushInput()
00057 self.serial_dev.flushOutput()
00058 self.serial_dev.write('Q')
00059 x = self.serial_dev.read(1)
00060 print 'AMTI_force._reset: resetting.'
00061 return x == 'U'
00062
00063 def _read_once(self):
00064
00065 while True:
00066 x = self.serial_dev.read(24)
00067 if len(x) > 0:
00068 ret, error = stream_to_values(x)
00069 ret[0,0] = 4.448221628254617 * ret[0,0]
00070 ret[1,0] = 4.448221628254617 * ret[1,0]
00071 ret[2,0] = 4.448221628254617 * ret[2,0]
00072 ret[3,0] = 4.448221628254617 * 0.0254 * ret[3,0]
00073 ret[4,0] = 4.448221628254617 * 0.0254 * ret[4,0]
00074 ret[5,0] = 4.448221628254617 * 0.0254 * ret[5,0]
00075 if error:
00076 time.sleep(50/1000.0)
00077 self._reset()
00078 else:
00079 ft_vector = ret
00080 ft_vector[0,0] = -ft_vector[0,0] * 1.8
00081 ft_vector[1,0] = ft_vector[1,0] * 1.7
00082 ft_vector[2,0] = -ft_vector[2,0]
00083 ft_vector[3,0] = -ft_vector[3,0]
00084 ft_vector[5,0] = -ft_vector[5,0]
00085 return ft_vector
00086
00087
00088 COEFF_MAT = np.matrix([[ 0.0032206,-0.0000321,0.0003082,-0.0032960,-0.0000117,-0.0002678,0.0032446,0.0000940,0.0001793,-0.0031230,-0.0000715,-0.0002365],
00089 [ -0.0001470,0.0032134,0.0004389,0.0000222,0.0032134,0.0003946,0.0000684,-0.0031486,0.0000523,-0.0000209,-0.0031797,-0.0001470],
00090 [ -0.0006416,-0.0005812,-0.0087376,-0.0006207,-0.0005215,-0.0086779,-0.0006731,-0.0008607,-0.0087900,-0.0005766,-0.0007237,-0.0084300],
00091 [ 0.0000000,0.0000000,-0.0279669,0.0000000,0.0000000,-0.0269454,0.0000000,0.0000000,0.0281477,0.0000000,0.0000000,0.0268347],
00092 [ 0.0000000,0.0000000,0.0273877,0.0000000,0.0000000,-0.0269034,0.0000000,0.0000000,0.0278664,0.0000000,0.0000000,-0.0272117],
00093 [ -0.0123918,0.0124854,0.0000917,0.0126818,-0.0124854,0.0000911,0.0124843,-0.0122338,0.0000923,-0.0120165,0.0123544,0.0000885]])
00094
00095 def open_serial(dev_name, baudrate):
00096 serial_dev = serial.Serial(dev_name, timeout=4.)
00097 serial_dev.setBaudrate(baudrate)
00098 serial_dev.setParity('N')
00099 serial_dev.setStopbits(1)
00100 serial_dev.open()
00101
00102 serial_dev.flushOutput()
00103 serial_dev.flushInput()
00104 serial_dev.write('R')
00105 time.sleep(1.)
00106 if(serial_dev == None):
00107 raise RuntimeError("AMTI_force: Serial port not found!\n")
00108 return serial_dev
00109
00110 def stream_to_values(data):
00111 val = np.matrix(np.zeros((12,1)))
00112 val[0,0] = ord(data[0]) + 256*(ord(data[1])%16)
00113 val[1,0] = ord(data[2]) + 256*(ord(data[3])%16)
00114 val[2,0] = ord(data[4]) + 256*(ord(data[5])%16)
00115 val[3,0] = ord(data[6]) + 256*(ord(data[7])%16)
00116 val[4,0] = ord(data[8]) + 256*(ord(data[9])%16)
00117 val[5,0] = ord(data[10]) + 256*(ord(data[11])%16)
00118 val[6,0] = ord(data[12]) + 256*(ord(data[13])%16)
00119 val[7,0] = ord(data[14]) + 256*(ord(data[15])%16)
00120 val[8,0] = ord(data[16]) + 256*(ord(data[17])%16)
00121 val[9,0] = ord(data[18]) + 256*(ord(data[19])%16)
00122 val[10,0] = ord(data[20]) + 256*(ord(data[21])%16)
00123 val[11,0] = ord(data[22]) + 256*(ord(data[23])%16)
00124 error = ord(data[1])/16 != 0 or ord(data[3])/16 != 1 or ord(data[5])/16 != 2 or \
00125 ord(data[7])/16 != 3 or ord(data[9])/16 != 4 or ord(data[11])/16 != 5 or\
00126 ord(data[13])/16 != 6 or ord(data[15])/16 != 7 or ord(data[17])/16 != 8 or \
00127 ord(data[19])/16 != 9 or ord(data[21])/16 != 10 or ord(data[23])/16 != 11
00128
00129 return COEFF_MAT * val, error
00130