ANNarchy iCub Interface 1.1.1
Loading...
Searching...
No Matches
KinematicPopulation.py
1"""
2 Copyright (C) 2021-2022 Torsten Fietzek; Helge Ülo Dinkelbach
3
4 KinematicPopulation.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 KinematicForward(SpecificPopulation):
31 """
32 ANNarchy population class to connect with the iCub forward kinematics.
33 Readout the angles from the iCub, compute the forward kinematics and set it as population activation.
34 """
35
36 def __init__(self, geometry=(3,), ip_address="0.0.0.0", port=50020, copied=False, name=None):
37 """Init the KinematicForward Population.
38
39 Args:
40 geometry (tuple, optional): ANNarchy population geometry. Defaults to (3,).
41 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".
42 port (int, optional): port of the gRPC connection. Need to fit with the respective kinematic reader module. Defaults to 50020.
43 copied (bool, optional): ANNarchy specific parameter. Defaults to False.
44 name (str, optional): individiual name for the population. Defaults to None.
45 """
46 SpecificPopulation.__init__(self, geometry=geometry, neuron=Neuron(equations="r = 0.0"), copied=copied, name=name)
47
48 self._ip_address = ip_address
49 self._port = port
50
52 SpecificPopulation._init_attributes(self)
53
54 self.cyInstance.set_ip_address(self._ip_address)
55 self.cyInstance.set_port(self._port)
56
57 def _copy(self):
58 return KinematicForward(geometry=self.geometry, ip_address=self._ip_address, port=self._port, copied=True, name=self.name)
59
60 @property
61 def ip_address(self):
62 if self.initialized:
63 return self.cyInstance.get_ip_address()
64 else:
65 _error("Read-out IP address is only valid after compile().")
66
67 @ip_address.setter
68 def ip_address(self, value):
69 if self.initialized:
70 self.cyInstance.set_ip_address(value)
71 else:
72 _error("Changing IP address is only valid after compile() or constructor.")
73
74 @property
75 def port(self):
76 if self.initialized:
77 return self.cyInstance.get_port()
78 else:
79 _error("Read-out port is only valid after compile().")
80
81 @port.setter
82 def port(self, value):
83 if self.initialized:
84 self.cyInstance.set_port(value)
85 else:
86 _error("Changing port is only valid after compile() or constructor.")
87
88 def _generate(self):
89 """
90 read out code for iCub through gRPC
91 """
92 include_paths = ANN_iCub_Interface.__path__[0]+"/grpc/"
93
94 self._specific_template['include_additional'] = """// grpc stuff
95#include "ANN_iCub_Interface/grpc/ProvideInputClient.h"
96"""
97 self._specific_template['declare_additional'] = """
98 std::string ip_address;
99 unsigned int port;
100 ClientInstance* kinematic_source=nullptr;
101 """
102 self._specific_template['access_additional'] = """
103 // Kinematic Source ip address
104 void set_ip_address(std::string value) {
105 ip_address = value;
106 }
107 std::string get_ip_address() {
108 return ip_address;
109 }
110
111 // Kinematic Source port
112 void set_port(unsigned int value) {
113 port = value;
114 }
115 unsigned int get_port() {
116 return port;
117 }
118
119 void connect() {
120 if (kinematic_source == nullptr) {
121 kinematic_source = new ClientInstance(ip_address, port);
122 } else {
123 std::cerr << "Population %(name)s already connected" << std::endl;
124 }
125 }
126 """ % {'name': self.name}
127 self._specific_template['export_additional'] = """
128 # IP Address and port
129 void set_ip_address(string)
130 string get_ip_address()
131 void set_port(unsigned int)
132 unsigned int get_port()
133 void connect()
134"""
135 self._specific_template['wrapper_access_additional'] = """
136 # IP Address and port
137 def set_ip_address(self, value):
138 pop%(id)s.set_ip_address(value.encode('utf-8'))
139 def get_ip_address(self):
140 return pop%(id)s.get_ip_address()
141 def set_port(self, value):
142 pop%(id)s.set_port(value)
143 def get_port(self):
144 return pop%(id)s.get_port()
145 def connect(self):
146 pop%(id)s.connect()
147""" %{'id': self.id}
148
149 self._specific_template['update_variables'] = """
150 r = kinematic_source->retrieve_kinematic_hand();
151 """
152
153 def _instantiate(self, cython_module):
154 # initializes cython wrapper
155 SpecificPopulation._instantiate(self, cython_module)
156
157 def connect(self):
158 """Connect the population to the gRPC socket. Need to be called once after compile."""
159 # create socket and connect
160 if self.initialized:
161 self.cyInstance.connect()
__init__(self, geometry=(3,), ip_address="0.0.0.0", port=50020, copied=False, name=None)