-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_trajectory_of_agent.py
155 lines (134 loc) · 4.31 KB
/
get_trajectory_of_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Example on how to get a complete trajectory of a SHERPA agent
#
import zmq
import sys
import time
import datetime
import json
import os
### Variables ###
objectAttribute = "sherpa:agent_name"
agent = "fw0"
fbxPath = os.environ['FBX_MODULES']
fbxPath = fbxPath + "/lib/"
if len(sys.argv) > 2:
objectAttribute = sys.argv[1]
agent = sys.argv[2]
print("Reqesting pose history for agent : %s " % (agent))
### Communication Setup ###
port = "22422"
context = zmq.Context()
def querySWM(queryMessage):
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:%s" % port)
socket.send_string(json.dumps(queryMessage))
queryResult = socket.recv_json()
print ("Query: " + str(queryMessage))
print ("Query result: " + str(queryResult))
if queryResult['querySuccess']:
return queryResult
else:
return ""
def updateSWM(updateMessage):
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:%s" % port)
socket.send_string(json.dumps(updateMessage))
updateResult = socket.recv_json()
print ("Update: " + str(updateMessage))
print ("Update result: " + str(updateResult))
if updateResult['updateSuccess']:
print ("[DCM updateSWM:] updateSuccess = " + str(updateResult['updateSuccess']))
return updateResult
else:
return ""
def functionBlockOperation(operation):
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:%s" % port)
socket.send_string(json.dumps(operation))
operationResult = socket.recv_json()
print ("Operation: " + str(operation))
print ("Operation result: " + str(operationResult))
if operationResult['operationSuccess']:
print ("[DCM updateSWM:] operationSuccess = " + str(operationResult['operationSuccess']))
return operationResult
else:
return ""
### Query to get Ids of object and reference frame ####
# Get root node Id as reference
getRootNodeMsg = {
"@worldmodeltype": "RSGQuery",
"query": "GET_ROOT_NODE"
}
result = querySWM(getRootNodeMsg)
rootId = result["rootId"]
print("rootId = %s " % rootId)
referenceId = rootId
# Get object Id
getNodes = {
"@worldmodeltype": "RSGQuery",
"query": "GET_NODES",
"attributes": [
{"key": objectAttribute, "value": agent },
]
}
result = querySWM(getNodes)
ids = result["ids"]
print("ids = %s " % ids)
if (len(ids) > 0):
### Prepare poisehistory functionblock ###
blockName = "posehistory"
unloadFunctionBlock = {
"@worldmodeltype": "RSGFunctionBlock",
"metamodel": "rsg-functionBlock-schema.json",
"name": blockName,
"operation": "UNLOAD",
"input": {
"metamodel": "rsg-functionBlock-path-schema.json",
"path": fbxPath,
"comment": "path is the same as the FBX_MODULES environment variable appended with a lib/ folder"
}
}
functionBlockOperation(unloadFunctionBlock)
loadFunctionBlock = {
"@worldmodeltype": "RSGFunctionBlock",
"metamodel": "rsg-functionBlock-schema.json",
"name": blockName,
"operation": "LOAD",
"input": {
"metamodel": "rsg-functionBlock-path-schema.json",
"path": fbxPath,
"comment": "path is the same as the FBX_MODULES environment variable appended with a lib/ folder"
}
}
functionBlockOperation(loadFunctionBlock)
### Request history ###
getPoseHistoryQuery = {
"@worldmodeltype": "RSGFunctionBlock",
"metamodel": "rsg-functionBlock-schema.json",
"name": blockName,
"operation": "EXECUTE",
"input": {
"metamodel": "fbx-posehistory-input-schema.json",
"id": ids[0],
"idReferenceNode": rootId
}
}
result = functionBlockOperation(getPoseHistoryQuery)
print("Recieved pose history:")
print(result["output"]["history"])
## For comparison - below is an example how to get a single pose rather than a full history:
#currentTimeStamp = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
#print(currentTimeStamp)
#
#getPose = {
# "@worldmodeltype": "RSGQuery",
# "query": "GET_TRANSFORM",
# "id": ids[0],
# "idReferenceNode": rootId,
# "timeStamp": {
# "@stamptype": "TimeStampDate",
# "stamp": currentTimeStamp,
# }
#}
#result = querySWM(getPose)
#print("Received reply for object pose: %s " % str(result))