1
- from __future__ import absolute_import
2
- from __future__ import print_function
3
-
4
- import asyncio
5
- import logging
6
- import sys
7
-
8
- from pyrevolve .sdfbuilder import SDF
9
- from pyrevolve .sdfbuilder .sensor import Sensor
10
-
11
- from ..spec import BodyAnalysisResponse
1
+ from pyrevolve .custom_logging .logger import logger
2
+ from pyrevolve .SDF .math import Vector3
3
+ from pyrevolve .spec import BodyAnalysisResponse
12
4
from .connect import connect , RequestHandler
13
5
14
6
# Message ID sequencer, start at some high value to prevent ID clashes
@@ -23,68 +15,13 @@ def _msg_id():
23
15
return r
24
16
25
17
26
- # Prevent the trollius logging warning
27
- kl = logging .getLogger ("trollius" )
28
- kl .addHandler (logging .StreamHandler (sys .stdout ))
29
-
30
-
31
- def get_analysis_robot (robot , builder ):
32
- """
33
- Creates an SDF model suitable for analysis from a robot object
34
- and a builder.
35
- :param robot:
36
- :type robot: Robot
37
- :param builder:
38
- :type builder: BodyBuilder
39
- :return:
40
- """
41
- model = builder .sdf_robot (
42
- robot = robot ,
43
- controller_plugin = None ,
44
- name = "analyze_bot" ,
45
- analyzer_mode = True )
46
- model .remove_elements_of_type (Sensor , recursive = True )
47
- sdf = SDF ()
48
- sdf .add_element (model )
49
- return sdf
50
-
51
-
52
- def analyze_body (sdf , address = ("127.0.0.1" , 11346 )):
53
- """
54
- Single body analyzer. Opens a new connection, analyzes the
55
- body, and returns the result. If you already have a manager
56
- running doing other things, create an instance of `BodyAnalyzer`
57
- instead.
58
-
59
- :param sdf: SDF object consisting of BodyPart
60
- instances.
61
- :type sdf: SDF
62
- :param address: Tuple of the hostname and port where the analyzer
63
- resides. Note that the default is one up from the default
64
- Gazebo port, since it is meant to be used with the
65
- `run-analyzer.sh` tool.
66
- :type address: (str, int)
67
- :return:
68
- :rtype: (bool, (float, float, float))
69
- """
70
- response_obj = [None ]
71
-
72
- async def internal_analyze ():
73
- analyzer = await (BodyAnalyzer .create (address ))
74
- response_obj [0 ] = await (analyzer .analyze_sdf (sdf ))
75
-
76
- loop = asyncio .get_event_loop ()
77
- loop .run_until_complete (internal_analyze ())
78
- return response_obj [0 ]
79
-
80
-
81
18
class BodyAnalyzer (object ):
82
19
"""
83
20
Class used to make body analysis requests.
84
21
"""
85
22
_PRIVATE = object ()
86
23
87
- def __init__ (self , _private , address ):
24
+ def __init__ (self , _private , address , port ):
88
25
"""
89
26
Private constructor - use the `create` coroutine instead.
90
27
@@ -97,43 +34,46 @@ def __init__(self, _private, address):
97
34
"the `create` coroutine." )
98
35
99
36
self .address = address
37
+ self .port = port
100
38
self .manager = None
101
39
self .request_handler = None
102
40
103
41
@classmethod
104
- async def create (cls , address = ( "127.0.0.1" , 11346 ) ):
42
+ async def create (cls , address , port ):
105
43
"""
106
44
Instantiates a new body analyzer at the given address.
107
45
108
- :param address: host, port tuple .
109
- :type address: (str, int)
46
+ :param address: hostname .
47
+ :param port: host port.
110
48
:return:
111
49
"""
112
- self = cls (cls ._PRIVATE , address )
113
- await ( self ._init () )
50
+ self = cls (cls ._PRIVATE , address , port )
51
+ await self ._init ()
114
52
return self
115
53
116
54
async def _init (self ):
117
55
"""
118
56
BodyAnalyzer initialization coroutine
119
57
:return:
120
58
"""
121
- self .manager = await ( connect (self .address ) )
59
+ self .manager = await connect (self .address )
122
60
self .request_handler = await (
123
61
RequestHandler .create (self .manager , msg_id_base = _msg_id ()))
124
62
125
- async def analyze_robot (self , robot , builder , max_attempts = 5 ):
63
+ async def disconnect (self ):
64
+ await self .manager .stop ()
65
+ await self .request_handler .stop ()
66
+
67
+ async def analyze_robot (self , robot , max_attempts = 5 ):
126
68
"""
127
69
Performs body analysis of a given Robot object.
128
70
:param robot:
129
71
:type robot: Robot
130
- :param builder:
131
- :type builder: Builder
132
72
:param max_attempts:
133
73
:return:
134
74
"""
135
- sdf = get_analysis_robot ( robot , builder )
136
- ret = await ( self .analyze_sdf (sdf , max_attempts = max_attempts ) )
75
+ sdf = robot . to_sdf ( pose = Vector3 ( 0 , 0 , 0 ) )
76
+ ret = await self .analyze_sdf (sdf , max_attempts = max_attempts )
137
77
return ret
138
78
139
79
async def analyze_sdf (self , sdf , max_attempts = 5 ):
@@ -156,7 +96,7 @@ async def analyze_sdf(self, sdf, max_attempts=5):
156
96
break
157
97
158
98
if not msg :
159
- # Error return
99
+ logger . error ( "analyze_sdf returned not valid message" )
160
100
return None
161
101
162
102
if msg .HasField ("boundingBox" ):
0 commit comments