2 Copyright (C) 2021-2022 Torsten Fietzek; Helge Ülo Dinkelbach
4 JointPopulation.py is part of the ANNarchy iCub interface
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.
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.
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/].
20import ANN_iCub_Interface
21from ANNarchy.core.Neuron
import Neuron
23 from ANNarchy.intern.SpecificPopulation
import SpecificPopulation
24 from ANNarchy.intern.Messages
import _error
27 from ANNarchy.core.SpecificPopulation
import SpecificPopulation
28 from ANNarchy.core.Global
import _error
33 ANNarchy population class to connect with the iCub joint control, e. g. arm or head.
34 Readout the angles from the ANNarchy Population and send it to the iCub.
37 def __init__(self, geometry=None, neuron=Neuron(equations=
"r = 0.0"), ip_address=
"0.0.0.0", port=50010, copied=
False, name=
None):
38 """Init the JointControl population.
41 geometry (tuple, optional): ANNarchy population geometry. Defaults to None.
42 neuron (Neuron, optional): ANNarchy Neuron, being used in the population. Defaults to Neuron(equations="r = 0.0").
43 ip_address (str, optional): ip-address of the gRPC connection. Need to fit with the respective joint writer module. Defaults to "0.0.0.0".
44 port (int, optional): port of the gRPC connection. Need to fit with the respective joint reader module. Defaults to 50010.
45 copied (bool, optional): ANNarchy specific parameter. Defaults to False.
46 name (str, optional): individiual name for the population. Defaults to None.
48 SpecificPopulation.__init__(self, geometry=geometry, neuron=neuron, copied=copied, name=name)
55 SpecificPopulation._init_attributes(self)
58 self.cyInstance.set_port(self.
_port)
62 return JointControl(geometry=self.geometry, neuron=self.neuron_type, ip_address=self.
_ip_address, port=self.
_port, copied=
True, name=self.name)
67 return self.cyInstance.get_ip_address()
69 _error(
"Read-out IP address is only valid after compile().")
74 self.cyInstance.set_ip_address(value)
76 _error(
"Changing IP address is only valid after compile() or constructor.")
81 return self.cyInstance.get_port()
83 _error(
"Read-out port is only valid after compile().")
88 self.cyInstance.set_port(value)
90 _error(
"Changing port is only valid after compile() or constructor.")
94 read out code for iCub through gRPC
96 include_paths = ANN_iCub_Interface.__path__[0]+
"/grpc/"
98 self._specific_template[
'include_additional'] =
"""// grpc stuff
99#include "ANN_iCub_Interface/grpc/WriteOutputServer.h"
102 self._specific_template[
'declare_additional'] =
"""
103 std::string ip_address;
105 WriteOutputServerInstance<PopStruct%(id)s>* joint_server=nullptr;
106 std::thread server_thread;
109 if (joint_server != nullptr) {
110 joint_server->shutdown();
111 server_thread.join();
114 """ % {
'id': self.id}
115 self._specific_template[
'access_additional'] =
"""
116 // Joint Source ip address
117 void set_ip_address(std::string value) {
120 std::string get_ip_address() {
125 void set_port(unsigned int value) {
128 unsigned int get_port() {
133 if (joint_server == nullptr) {
134 joint_server = new WriteOutputServerInstance<PopStruct%(id)s>(ip_address, port, new WriteOutputServiceImpl<PopStruct%(id)s>(this) );
135 this->server_thread = std::thread(&WriteOutputServerInstance<PopStruct%(id)s>::wait, this->joint_server);
137 std::cerr << "Population %(name)s already connected" << std::endl;
140 """ % {
'id': self.id,
'name': self.name}
141 self._specific_template[
'export_additional'] =
"""
142 # IP Address and port
143 void set_ip_address(string)
144 string get_ip_address()
145 void set_port(unsigned int)
146 unsigned int get_port()
149 self._specific_template[
'wrapper_access_additional'] =
"""
150 # IP Address and port
151 def set_ip_address(self, value):
152 pop%(id)s.set_ip_address(value.encode('utf-8'))
153 def get_ip_address(self):
154 return pop%(id)s.get_ip_address()
155 def set_port(self, value):
156 pop%(id)s.set_port(value)
158 return pop%(id)s.get_port()
165 SpecificPopulation._instantiate(self, cython_module)
168 """Connect the population to the gRPC socket. Need to be called once after compile."""
176 ANNarchy population class to connect with the iCub joint readout, e. g. arm or head.
177 Readout the angles from the iCub and set it as population activation.
180 def __init__(self, geometry=None, joints=None, encoded=False, ip_address="0.0.0.0", port=50005, copied=False, name=None):
181 """Init the JointReadout population.
184 geometry (tuple, optional): ANNarchy population geometry. Defaults to None.
185 joints (list, optional): Specify the joints, which should be accessed. Defaults to None.
186 encoded (bool, optional): Specify if the joint angles should be encoded with a population coding. Defaults to False.
187 ip_address (str, optional): ip-address of the gRPC connection. Need to fit with the respective joint reader module. Defaults to "0.0.0.0".
188 port (int, optional): port of the gRPC connection. Need to fit with the respective joint reader module. Defaults to 50005.
189 copied (bool, optional): ANNarchy specific parameter. Defaults to False.
190 name (str, optional): individiual name for the population. Defaults to None.
192 SpecificPopulation.__init__(self, geometry=geometry, neuron=Neuron(equations=
"r = 0.0"), copied=copied, name=name)
200 SpecificPopulation._init_attributes(self)
202 self.cyInstance.set_joints(self.
_joints)
203 self.cyInstance.set_encoded(self.
_encoded)
205 self.cyInstance.set_port(self.
_port)
213 return self.cyInstance.get_ip_address()
215 _error(
"Read-out IP address is only valid after compile().")
220 self.cyInstance.set_ip_address(value)
222 _error(
"Changing IP address is only valid after compile() or constructor.")
227 return self.cyInstance.get_port()
229 _error(
"Read-out port is only valid after compile().")
234 self.cyInstance.set_port(value)
236 _error(
"Changing port is only valid after compile() or constructor.")
241 return self.cyInstance.get_joints()
243 _error(
"Read-out joints is only valid after compile().")
248 self.cyInstance.set_joints(value)
250 _error(
"Changing joints is only valid after compile() or constructor.")
255 return self.cyInstance.get_encoded()
257 _error(
"Read-out encoded is only valid after compile().")
262 self.cyInstance.set_encoded(value)
264 _error(
"Changing encoded is only valid after compile() or constructor.")
268 read out code for iCub through gRPC
270 include_paths = ANN_iCub_Interface.__path__[0]+
"/grpc/"
272 self._specific_template[
'include_additional'] =
"""// grpc stuff
273#include "ANN_iCub_Interface/grpc/ProvideInputClient.h"
275 self._specific_template[
'declare_additional'] =
"""
276 std::string ip_address;
278 ClientInstance* joint_source=nullptr;
279 std::vector<int> joints;
282 self._specific_template[
'access_additional'] =
"""
283 // Joint Source ip address
284 void set_ip_address(std::string value) {
287 std::string get_ip_address() {
292 void set_port(unsigned int value) {
295 unsigned int get_port() {
299 // Joint Source joint selection
300 void set_joints(std::vector<int> value) {
303 std::vector<int> get_joints() {
307 // Joint Source joint encoding
308 void set_encoded(bool value) {
316 if (joint_source == nullptr) {
317 joint_source = new ClientInstance(ip_address, port);
319 std::cerr << "Population %(name)s already connected" << std::endl;
322 """ % {
'name': self.name}
323 self._specific_template[
'export_additional'] =
"""
324 # IP Address and port
325 void set_ip_address(string)
326 string get_ip_address()
327 void set_port(unsigned int)
328 unsigned int get_port()
329 void set_joints(vector[int])
330 vector[int] get_joints()
331 void set_encoded(bool)
335 self._specific_template[
'wrapper_access_additional'] =
"""
336 # IP Address and port
337 def set_ip_address(self, value):
338 pop%(id)s.set_ip_address(value.encode('utf-8'))
339 def get_ip_address(self):
340 return pop%(id)s.get_ip_address()
341 def set_port(self, value):
342 pop%(id)s.set_port(value)
344 return pop%(id)s.get_port()
345 def set_joints(self, value):
346 pop%(id)s.set_joints(value)
347 def get_joints(self):
348 return pop%(id)s.get_joints()
349 def set_encoded(self, value):
350 pop%(id)s.set_encoded(value)
351 def get_encoded(self):
352 return pop%(id)s.get_encoded()
358 self._specific_template[
'update_variables'] =
"""
361 r = joint_source->retrieve_alljoints(encoded);
365 self._specific_template[
'update_variables'] =
"""
368 r = joint_source->retrieve_singlejoint(joints[0], encoded);
372 self._specific_template[
'update_variables'] =
"""
375 r = joint_source->retrieve_multijoints(joints, encoded);
381 SpecificPopulation._instantiate(self, cython_module)
384 """Connect the population to the gRPC socket. Need to be called once after compile."""
_instantiate(self, cython_module)
__init__(self, geometry=None, neuron=Neuron(equations="r = 0.0"), ip_address="0.0.0.0", port=50010, copied=False, name=None)
_instantiate(self, cython_module)
__init__(self, geometry=None, joints=None, encoded=False, ip_address="0.0.0.0", port=50005, copied=False, name=None)