force_sensor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # FSRobo-R Package BSDL
5 # ---------
6 # Copyright (C) 2019 FUJISOFT. All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification,
9 # are permitted provided that the following conditions are met:
10 # 1. Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation and/or
14 # other materials provided with the distribution.
15 # 3. Neither the name of the copyright holder nor the names of its contributors
16 # may be used to endorse or promote products derived from this software without
17 # specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # ---------
30 
31 from info_catch_client import InfoCatchClient
32 
33 class ForceSensor(object):
34  MA_BUFFER_SIZE = 4
35  class Status(object):
36  NO_IF = 0x00 # インターフェイス基板なし
37  VALID = 0x1F # 力覚センサ有効
38  ERR_MASK = 0x17 # エラー判定のためのマスク
39  ERR_NO_RESPONSE = 0x10 # 力覚センサ応答なし
40  ERR_FRAME_1 = 0x11 # フレームズレ1
41  ERR_FRAME_2 = 0x12 # フレームズレ1
42  ERR_DATA = 0x13 # データ異常
43 
44  def __init__(self):
45  self._fx = 0
46  self._fy = 0
47  self._fz = 0
48  self._mx = 0
49  self._my = 0
50  self._mz = 0
51  self._ma_buf = [[0.0] * 6] * self.MA_BUFFER_SIZE
52  self._ma_index = 0
53 
54  def read(self, info):
55  self._status = info[InfoCatchClient.Label.F000][0]
56  #self._zero_point = [float(x) for x in shm_read(0x0710,6).split(',')]
57  self._zero_point = [8192] * 6
58  self._raw_val = info[InfoCatchClient.Label.F200][0:6]
59  self._gain = info[InfoCatchClient.Label.F300][0:6]
60  #self._status = int(shm_read(0x1800,1))
61  #self._zero_point = [float(x) for x in shm_read(0x1c00,6).split(',')]
62  #self._raw_val = [float(x) for x in shm_read(0x1d00,6).split(',')]
63  #self._gain = [float(x) for x in shm_read(0x1e00,6).split(',')]
64 
65 
66  if all(self._gain):
67  val = []
68  for i in range(6):
69  val.append((self._raw_val[i] - self._zero_point[i]) / self._gain[i])
70  ma_val = self._calc_ma(val)
71  self._fx = ma_val[0]
72  self._fy = ma_val[1]
73  self._fz = ma_val[2]
74  self._mx = ma_val[3]
75  self._my = ma_val[4]
76  self._mz = ma_val[5]
77 
78  def _calc_ma(self, val):
79  self._ma_buf[self._ma_index] = val
80  ma_val = []
81 
82  for i in range(6):
83  avg = sum([x[i] for x in self._ma_buf]) / self.MA_BUFFER_SIZE
84  ma_val.append(avg)
85 
86  self._ma_index = (self._ma_index + 1) % self.MA_BUFFER_SIZE
87 
88  return ma_val
89 
90 
91  def is_valid(self):
92  return self._status == self.Status.VALID
93 
94  @property
95  def raw_fx(self):
96  return self._raw_val[0]
97 
98  @property
99  def raw_fy(self):
100  return self._raw_val[1]
101 
102  @property
103  def raw_fz(self):
104  return self._raw_val[2]
105 
106  @property
107  def raw_mx(self):
108  return self._raw_val[3]
109 
110  @property
111  def raw_my(self):
112  return self._raw_val[4]
113 
114  @property
115  def raw_mz(self):
116  return self._raw_val[5]
117 
118  @property
119  def fx(self):
120  return self._fx
121 
122  @property
123  def fy(self):
124  return self._fy
125 
126  @property
127  def fz(self):
128  return self._fz
129 
130  @property
131  def mx(self):
132  return self._mx
133 
134  @property
135  def my(self):
136  return self._my
137 
138  @property
139  def mz(self):
140  return self._mz
141 
142 
143 if __name__ == '__main__':
144  fs = ForceSensor()
145  fs.read()
146  print('F x: {} y: {} z: {}'.format(fs.fx, fs.fy, fs.fz))
147  print('M x: {} y: {} z: {}'.format(fs.mx, fs.my, fs.mz))
def read(self, info)
Definition: force_sensor.py:54
def _calc_ma(self, val)
Definition: force_sensor.py:78


fsrobo_r_driver
Author(s): F-ROSROBO
autogenerated on Sun Feb 9 2020 03:58:29