Error using sim.edit_transfer_function() in the VC


#1

Hi :smile:

I am having problem editing a transfer function in the VC. I can easily edit other transfer functions, but I cannot do the same with this one.
That’s what I write in the VC terminal:

from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach
vc = VirtualCoach(environment=‘local’)
sim = vc.launch_experiment(‘my_experiment_name’)
my_tf = sim.get_transfer_function(‘my_tf_name’)
sim.edit_transfer_function(‘new_tf_name’, my_tf)

Using this command, I usually have no problem with other transfer functions, but in this case I get this error:

In [10]: sim.edit_transfer_function(‘tf_CPG_input’, etf_CPG_input)
INFO: [2018-03-19 10:37:55,355 - Simulation (fable_locomotion_myGA - localhost #0)] Attempting to retrieve transfer-function
INFO: [2018-03-19 10:37:55,368 - Simulation (fable_locomotion_myGA - localhost #0)] Attempting to set Transfer Function tf_CPG_input

UnicodeEncodeError Traceback (most recent call last)
in ()
----> 1 sim.edit_transfer_function(‘tf_CPG_input’, etf_CPG_input)

/home/elisa/Documents/NRP/VirtualCoach/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/simulation.pyc in edit_transfer_function(self, transfer_function_name, transfer_function)
504 “”"
505 self.__set_script(‘transfer-function’, transfer_function,
–> 506 script_name=transfer_function_name)
507
508 def add_transfer_function(self, transfer_function):

/home/elisa/Documents/NRP/VirtualCoach/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/simulation.pyc in __set_script(self, script_type, script, script_name, new)
424 url = ‘%s/%s/%s’ % (self.__sim_url,
425 self.__config[‘simulation-scripts’][script_type], script_name)
–> 426 res, _ = self.__oidc_client.request(url, method=‘PUT’, body=script)
427 if res[‘status’] != str(httplib.OK):
428 raise Exception(“Unable to set %s, HTTP status %s”

/home/elisa/.opt/platform_venv/lib/python2.7/site-packages/bbp_client/oidc/client.pyc in request(self, uri, method, body, headers, params)
408 uri += ‘?’ + params
409
–> 410 resp, content = self.http.request(uri, method, body, headers)
411 _verify_request(uri, resp.status, content)
412 return resp, content

/usr/lib/python2.7/dist-packages/httplib2/init.pyc in request(self, uri, method, body, headers, redirections, connection_type)
1605 content = “”
1606 else:
-> 1607 (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
1608 except Exception, e:
1609 if self.force_exception_to_status_code:

/usr/lib/python2.7/dist-packages/httplib2/init.pyc in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
1347 auth.request(method, request_uri, headers, body)
1348
-> 1349 (response, content) = self._conn_request(conn, request_uri, method, body, headers)
1350
1351 if auth:

/usr/lib/python2.7/dist-packages/httplib2/init.pyc in _conn_request(self, conn, request_uri, method, body, headers)
1270 if hasattr(conn, ‘sock’) and conn.sock is None:
1271 conn.connect()
-> 1272 conn.request(method, request_uri, body, headers)
1273 except socket.timeout:
1274 raise

/usr/lib/python2.7/httplib.pyc in request(self, method, url, body, headers)
1055 def request(self, method, url, body=None, headers={}):
1056 “”“Send a complete request to the server.”""
-> 1057 self._send_request(method, url, body, headers)
1058
1059 def _set_content_length(self, body, method):

/usr/lib/python2.7/httplib.pyc in _send_request(self, method, url, body, headers)
1095 for hdr, value in headers.iteritems():
1096 self.putheader(hdr, value)
-> 1097 self.endheaders(body)
1098
1099 def getresponse(self, buffering=False):

/usr/lib/python2.7/httplib.pyc in endheaders(self, message_body)
1051 else:
1052 raise CannotSendHeader()
-> 1053 self._send_output(message_body)
1054
1055 def request(self, method, url, body=None, headers={}):

/usr/lib/python2.7/httplib.pyc in _send_output(self, message_body)
899 #message_body was not a string (i.e. it is a file) and
900 #we must run the risk of Nagle
–> 901 self.send(message_body)
902
903 def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):

/usr/lib/python2.7/httplib.pyc in send(self, data)
871 datablock = data.read(blocksize)
872 else:
–> 873 self.sock.sendall(data)
874
875 def _output(self, s):

/usr/lib/python2.7/socket.pyc in meth(name, self, *args)
226
227 def meth(name,self,*args):
–> 228 return getattr(self._sock,name)(*args)
229
230 for _m in _socketmethods:

UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xe8’ in position 3826: ordinal not in range(128)

If it could help, I attach here below, the transfer function tf_CPG_input.py which is giving me problems

Imported Python Transfer Function

import sys
sys.path.append("/home/elisa/Documents/NRP/Experiments/fable_locomotion_my")

import hbp_nrp_cle.tf_framework as nrp
from hbp_nrp_cle.robotsim.RobotInterface import Topic
from hbp_nrp_excontrol.logs import clientLogger
from std_msgs.msg import Float64

import os

A1 = 10.5
A2 = 10.5
A3 = 10.5
A4 = 10.5
f1 = 0.5
f2 = 0.5
f3 = 0.5
f4 = 0.5
phi1 = 0.5
phi2 = 0.5
phi3 = 0.5
phi4 = 0.5

@nrp.MapVariable(‘traj’)

@nrp.MapVariable(‘A1’, initial_value = {A1})
@nrp.MapVariable(‘A2’, initial_value = {A2})
@nrp.MapVariable(‘A3’, initial_value = {A3})
@nrp.MapVariable(‘A4’, initial_value = {A4})
@nrp.MapVariable(‘f1’, initial_value = {f1})
@nrp.MapVariable(‘f2’, initial_value = {f2})
@nrp.MapVariable(‘f3’, initial_value = {f3})
@nrp.MapVariable(‘f4’, initial_value = {f4})
@nrp.MapVariable(‘phi1’, initial_value = {phi1})
@nrp.MapVariable(‘phi2’, initial_value = {phi2})
@nrp.MapVariable(‘phi3’, initial_value = {phi3})
@nrp.MapVariable(‘phi4’, initial_value = {phi4})

@nrp.MapVariable(‘TimeAngleInd’, initial_value = 0)

@nrp.MapRobotPublisher(“leg1_motor1”, Topic(’/robot/leg1_joint_1/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg1_motor2”, Topic(’/robot/leg1_joint_2/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg2_motor1”, Topic(’/robot/leg2_joint_1/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg2_motor2”, Topic(’/robot/leg2_joint_2/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg3_motor1”, Topic(’/robot/leg3_joint_1/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg3_motor2”, Topic(’/robot/leg3_joint_2/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg4_motor1”, Topic(’/robot/leg4_joint_1/cmd_pos’, std_msgs.msg.Float64))
@nrp.MapRobotPublisher(“leg4_motor2”, Topic(’/robot/leg4_joint_2/cmd_pos’, std_msgs.msg.Float64))

@nrp.Neuron2Robot()

def tf_CPG_input(t, TimeAngleInd, A1, A2, A3, A4, f1, f2, f3, f4, phi1, phi2, phi3, phi4, traj, leg1_motor1, leg1_motor2, leg2_motor1, leg2_motor2, leg3_motor1, leg3_motor2, leg4_motor1, leg4_motor2):

import math
from math import sin as sin
    

x = [] 
x = range(0, 3600) 


pi = 3.14
traj = [0.0] * 4


clientLogger.info(type(A1.value))
a1 = float(list(A1.value)[0])	
clientLogger.info(type(a1))
a2 = float(list(A2.value)[0])	
a3 = float(list(A3.value)[0])	
a4 = float(list(A4.value)[0])		
F1 = float(list(f1.value)[0])	
F2 = float(list(f2.value)[0])	
F3 = float(list(f3.value)[0])	
F4 = float(list(f4.value)[0])	
Phi1 = float(list(phi1.value)[0])	
Phi2 = float(list(phi2.value)[0])	
Phi3 = float(list(phi3.value)[0])	
Phi4 = float(list(phi4.value)[0])



A = [a1, a2, a3, a4]
f = [F1, F2, F3, F4] 
phi = [Phi1, Phi2, Phi3, Phi4] 


for i in range(0, 4):  # over the DOFs
	traj[i] = A[i] * math.sin(math.radians(2 * pi * f[i] * x[TimeAngleInd.value] + phi[i])) # compute the instant value, not all the signal



leg1_motor1.send_message(1.57)  # fixed due to robot configuration
leg1_motor2.send_message(traj[0])
leg2_motor1.send_message(1.57)  # fixed due to robot configuration
leg2_motor2.send_message(traj[1])
leg3_motor1.send_message(1.57)  # fixed due to robot configuration
leg3_motor2.send_message(traj[2])
leg4_motor1.send_message(1.57)  # fixed due to robot configuration
leg4_motor2.send_message(traj[3])	

TimeAngleInd.value = TimeAngleInd.value + 1		

if (TimeAngleInd.value == 3600):
	TimeAngleInd.value = 0	

Thank you a lot.
Kind regards,

Elisa


#2

Hi Elisa!

The error message you posted are from the virtual coach side I assume, but you should look at the ones from the cle-start side: it seems that setting this transfer function makes the simulation crash.

Have you tried to start the experiment manually and load the transfer function from the web interface? What happens then?

UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xe8’ in position 3826: ordinal not in range(128)

This sounds like a string encoding error, maybe you can try with:

sim.edit_transfer_function('new_tf_name', my_tf.encode('utf-8'))

Check out this stackoverflow post. Also make sure there is no weird unicode character in the TF.

Cheers,
Jacques

##PS:
For readability, you can format the code blocks (or error messages) with the following syntax on the forum:
```python
some python code
```

Which gives:

from hbp_nrp_virtual_coach.virtual_coach import VirtualCoachvc = VirtualCoach(environment='local') 
sim = vc.launch_experiment('my_experiment_name')
my_tf = sim.get_transfer_function('my_tf_name')
sim.edit_transfer_function('new_tf_name', my_tf)