ANNarchy iCub Interface 1.1.1
Loading...
Searching...
No Matches
SkinPopulation.py
1"""
2 Copyright (C) 2021-2022 Torsten Fietzek; Helge Ülo Dinkelbach
3
4 SkinPopulation.py is part of the ANNarchy iCub interface
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 The ANNarchy iCub interface is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this headers. If not, see [http://www.gnu.org/licenses/].
18 """
19import ANN_iCub_Interface
20from ANNarchy.core.Neuron import Neuron
21try:
22 from ANNarchy.intern.SpecificPopulation import SpecificPopulation
23 from ANNarchy.intern.Messages import _error
24
25except:
26 from ANNarchy.core.SpecificPopulation import SpecificPopulation
27 from ANNarchy.core.Global import _error
28
29
30class SkinPopulation(SpecificPopulation):
31 """
32 ANNarchy population class to connect with the iCub skin.
33 Readout the skin sensor data and set it as population activation.
34 """
35
36 def __init__(self, geometry=None, skin_section="", ip_address="0.0.0.0", port=50015, copied=False, name=None):
37 """Init the SkinPopulation.
38
39 Args:
40 geometry (tuple, optional): ANNarchy population geometry. Defaults to None.
41 skin_section (str, optional): Specify the respective skin section of the used arm. Defaults to "".
42 ip_address (str, optional): ip-address of the gRPC connection. Need to fit with the respective kinematic reader module. Defaults to "0.0.0.0".
43 port (int, optional): port of the gRPC connection. Need to fit with the respective kinematic reader module. Defaults to 50015.
44 copied (bool, optional): ANNarchy specific parameter. Defaults to False.
45 name (str, optional): individiual name for the population. Defaults to None.
46 """
47 SpecificPopulation.__init__(self, geometry=geometry, neuron=Neuron(equations="r = 0.0"), copied=copied, name=name)
48
49 self._skin_section = skin_section
50 self._ip_address = ip_address
51 self._port = port
52
54 SpecificPopulation._init_attributes(self)
55
56 self.cyInstance.set_ip_address(self._ip_address)
57 self.cyInstance.set_port(self._port)
58
59 def _copy(self):
60 return SkinPopulation(geometry=self.geometry, ip_address=self._ip_address, port=self._port, copied=True, name=self.name)
61
62 @property
63 def ip_address(self):
64 if self.initialized:
65 return self.cyInstance.get_ip_address()
66 else:
67 _error("Read-out IP address is only valid after compile().")
68
69 @ip_address.setter
70 def ip_address(self, value):
71 if self.initialized:
72 self.cyInstance.set_ip_address(value)
73 else:
74 _error("Changing IP address is only valid after compile() or constructor.")
75
76 @property
77 def port(self):
78 if self.initialized:
79 return self.cyInstance.get_port()
80 else:
81 _error("Read-out port is only valid after compile().")
82
83 @port.setter
84 def port(self, value):
85 if self.initialized:
86 self.cyInstance.set_port(value)
87 else:
88 _error("Changing port is only valid after compile() or constructor.")
89
90 def _generate(self):
91 """
92 read out code for iCub through gRPC
93 """
94 include_paths = ANN_iCub_Interface.__path__[0]+"/grpc/"
95
96 self._specific_template['include_additional'] = """// grpc stuff
97#include "ANN_iCub_Interface/grpc/ProvideInputClient.h"
98"""
99 self._specific_template['declare_additional'] = """
100 std::string ip_address;
101 unsigned int port;
102 ClientInstance* skin_source=nullptr;
103 """
104 self._specific_template['access_additional'] = """
105 // Skin Source ip address
106 void set_ip_address(std::string value) {
107 ip_address = value;
108 }
109 std::string get_ip_address() {
110 return ip_address;
111 }
112
113 // Skin Source port
114 void set_port(unsigned int value) {
115 port = value;
116 }
117 unsigned int get_port() {
118 return port;
119 }
120
121 void connect() {
122 if (skin_source == nullptr) {
123 skin_source = new ClientInstance(ip_address, port);
124 } else {
125 std::cerr << "Population %(name)s already connected" << std::endl;
126 }
127 }
128 """% {'name': self.name}
129 self._specific_template['export_additional'] = """
130 # IP Address and port
131 void set_ip_address(string)
132 string get_ip_address()
133 void set_port(unsigned int)
134 unsigned int get_port()
135 void connect()
136"""
137 self._specific_template['wrapper_access_additional'] = """
138 # IP Address and port
139 def set_ip_address(self, value):
140 pop%(id)s.set_ip_address(value.encode('utf-8'))
141 def get_ip_address(self):
142 return pop%(id)s.get_ip_address()
143 def set_port(self, value):
144 pop%(id)s.set_port(value)
145 def get_port(self):
146 return pop%(id)s.get_port()
147 def connect(self):
148 pop%(id)s.connect()
149""" %{'id': self.id}
150
151 if self._skin_section == "arm":
152 self._specific_template['update_variables'] = """
153 r = skin_source->retrieve_skin_arm();
154 """
155 elif self._skin_section == "forearm":
156 self._specific_template['update_variables'] = """
157 r = skin_source->retrieve_skin_forearm();
158 """
159 elif self._skin_section == "hand":
160 self._specific_template['update_variables'] = """
161 r = skin_source->retrieve_skin_hand();
162 """
163
164 def _instantiate(self, cython_module):
165 # initializes cython wrapper
166 SpecificPopulation._instantiate(self, cython_module)
167
168 def connect(self):
169 """Connect the population to the gRPC socket. Need to be called once after compile."""
170 # create socket and connect
171 if self.initialized:
172 self.cyInstance.connect()
__init__(self, geometry=None, skin_section="", ip_address="0.0.0.0", port=50015, copied=False, name=None)