-
Notifications
You must be signed in to change notification settings - Fork 1
/
seed_s7.py
336 lines (291 loc) · 9.72 KB
/
seed_s7.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created On: 2023-03-27
# SPDX-FileCopyrightText: Copyright (c) 2023 Shanghai Bosch Rexroth Hydraulics & Automation Ltd.
# SPDX-License-Identifier: MIT
#
import socket
import sys
import struct
import argparse
from datetime import datetime
import time
import logging
import requests
import snap7
# logger = logging.getLogger(__name__)
now = datetime.now()
date_time = now.strftime("%d-%m-%Y-%H-%M-%S")
# Locator
user_name = "admin"
password = "123456"
LOCATOR_ADDRESS = "127.0.0.1"
LOCATOR_BINARY_PORT = 9011
LOCATOR_JSON_RPC_PORT = 8080
URL = "http://" + LOCATOR_ADDRESS + ":" + str(LOCATOR_JSON_RPC_PORT)
# ClientLocalizationPoseDatagram data structure (see API manual)
UNPACKER = struct.Struct("<ddQiQQddddddddddddddQddd")
sessionId = "" # ROKIT Locator JSON RPC session ID
# Siemens S7-1200
PLC_ADDRESS = "192.168.0.235"
PLC_PORT = 102
PLC_RACK = 0
PLC_SLOT = 1
seed_num = 8 # number of seeds stored in DB
DB_NUMBER = 10000 # Siemens S7 data block number
ROW_SIZE = 28 # bytes that a row/seed resides
POSE_SIZE = 24 # bytes that Pose2D resides in a row/seed
# row/seed specification in a data block
layout = """
0.0 enforceSeed BOOL
0.1 uncertainSeed BOOL
2 x LREAL
10 y LREAL
18 a LREAL
26.0 recordSeed BOOL
26.1 setSeed BOOL
"""
def readCurrentPoseFromLocator() -> dict:
# Creating a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connecting to the server
server_address = (LOCATOR_ADDRESS, LOCATOR_BINARY_PORT)
logging.info("connecting to Locator %s : %s ..." % (server_address))
try:
sock.connect(server_address)
logging.info("Connected.")
except socket.error as e:
logging.error(str(e.message))
logging.error("Connection to Locator failed...")
return
# read the socket
data = sock.recv(UNPACKER.size)
# upack the data (= interpret the datagram)
unpacked_data = UNPACKER.unpack(data)
logging.debug(unpacked_data)
# create a json row
jsonRow = {
"timestamp": datetime.fromtimestamp(unpacked_data[1]).strftime(
"%d-%m-%Y-%H-%M-%S"
),
"x": unpacked_data[6],
"y": unpacked_data[7],
# 'yaw': math.degrees(unpacked_data[8]),
"yaw": unpacked_data[8],
"localization_state": unpacked_data[3],
}
sock.close()
logging.debug(jsonRow)
return jsonRow
def clientLocalizationSetSeed(
sessionId: str,
x: float,
y: float,
a: float,
enforceSeed: bool = False,
uncertainSeed: bool = False,
):
headers = {
"Content-Type": "application/json; charset=utf-8",
}
payload = {
"id": 111,
"jsonrpc": "2.0",
"method": "clientLocalizationSetSeed",
"params": {
"query": {
"sessionId": sessionId,
"enforceSeed": enforceSeed,
"uncertainSeed": uncertainSeed,
"seedPose": {"x": x, "y": y, "a": a},
}
},
}
logging.info(f"x={x}, y={y}, a={a}")
response = requests.post(url=URL, json=payload, headers=headers)
logging.debug(response.json())
def sessionLogin() -> str:
headers = {
"Content-Type": "application/json; charset=utf-8",
}
payload = {
"id": 101,
"jsonrpc": "2.0",
"method": "sessionLogin",
"params": {
"query": {
"timeout": { # timeout, not timestamp
"valid": True,
"time": 60, # Integer64
"resolution": 1, # real_time = time / resolution
},
"userName": user_name,
"password": password,
}
},
}
logging.debug(payload)
response = requests.post(url=URL, json=payload, headers=headers)
logging.debug(response.json())
sessionId = response.json()["result"]["response"]["sessionId"]
return sessionId
def sessionLogout(sessionId: str = None):
headers = {
"Content-Type": "application/json; charset=utf-8",
}
payload = {
"id": 103,
"jsonrpc": "2.0",
"method": "sessionLogout",
"params": {"query": {"sessionId": sessionId}},
}
response = requests.post(url=URL, json=payload, headers=headers)
logging.debug(response.json())
def run():
client = snap7.client.Client()
client.connect(PLC_ADDRESS, PLC_RACK, PLC_SLOT, PLC_PORT)
all_data_a = client.db_read(1, 0, ROW_SIZE * seed_num)
db1_a = snap7.util.DB(
db_number=DB_NUMBER,
bytearray_=all_data_a,
specification=layout,
row_size=ROW_SIZE,
size=seed_num,
)
seed_a = db1_a.export()
while True:
time.sleep(0.5)
all_data_b = client.db_read(1, 0, ROW_SIZE * seed_num)
db1_b = snap7.util.DB(
db_number=DB_NUMBER,
bytearray_=all_data_b,
specification=layout,
row_size=ROW_SIZE,
size=seed_num,
)
seed_b = db1_b.export()
for i in range(seed_num):
if not seed_a[i]["recordSeed"] and seed_b[i]["recordSeed"]:
# # pose 0 in DB stores current pose
# # write pose 0 to pose i in the data block
# client.db_write(
# db_number=1,
# start=i*ROW_SIZE+2,
# data=all_data_b[2:2+POSE_SIZE] # Pose2D in the data block
# )
# read current pose from Locator and write it to pose i in the data block
pose = readCurrentPoseFromLocator()
assert pose["localization_state"] >= 2, "NOT_LOCALIZED"
logging.info("LOCALIZED")
logging.info(pose)
pose_ba = struct.pack(">ddd", pose["x"], pose["y"], pose["yaw"])
client.db_write(
db_number=DB_NUMBER,
start=i * ROW_SIZE + 2,
# data=pose_ba+bytearray([0b00000000])
data=pose_ba,
)
# reset recordSeed
client.db_write(
db_number=DB_NUMBER,
start=i * ROW_SIZE + 2 + POSE_SIZE,
data=bytearray([0b00000000]),
)
# client.wait_as_completion(5000)
logging.info(f"Seed {i} recorded.")
break
if not seed_a[i]["setSeed"] and seed_b[i]["setSeed"]:
setSeed(
x=seed_b[i]["x"],
y=seed_b[i]["y"],
a=seed_b[i]["a"],
enforceSeed=seed_b[i]["enforceSeed"],
uncertainSeed=seed_b[i]["uncertainSeed"],
)
logging.info(f"Seed {i} set.")
client.db_write(
db_number=DB_NUMBER,
start=i * ROW_SIZE + 2 + POSE_SIZE,
data=bytearray([0b00000000]),
)
break
seed_a = seed_b
def cancel(received_signal):
"""
Callback method for signal handler
:param received_signal: Interrupt Signal number
:param frame: Stack frame object
"""
def recordSeed(station: int):
pass
def setSeed(x, y, a, enforceSeed, uncertainSeed):
sessionId = sessionLogin()
logging.info(sessionId)
clientLocalizationSetSeed(
sessionId=sessionId,
x=x,
y=y,
a=a,
enforceSeed=enforceSeed,
uncertainSeed=uncertainSeed,
)
sessionLogout(sessionId)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="works as a protocol converter between Siemens S7-1200 and ROKIT Locator",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"--seed_num", type=int, default=seed_num, help="number of seeds"
)
parser.add_argument(
"--plc_address", type=str, default=PLC_ADDRESS, help="IP address of PLC"
)
parser.add_argument("--plc_port", type=int, default=PLC_PORT, help="port of PLC")
parser.add_argument(
"--locator_address",
type=str,
default=LOCATOR_ADDRESS,
help="address of Locator",
)
parser.add_argument(
"--locator_binary_port",
type=int,
default=LOCATOR_BINARY_PORT,
help="binary port of Locator",
)
parser.add_argument(
"--locator_json_rpc_port",
type=int,
default=LOCATOR_JSON_RPC_PORT,
help="JSON RPC port of Locator",
)
args = parser.parse_args()
if args.seed_num:
seed_num = args.seed_num
if args.plc_address:
PLC_ADDRESS = args.plc_address
if args.plc_port:
r = range(1, 65535)
if args.plc_port not in r:
raise argparse.ArgumentTypeError("Value has to be between 1 and 65535")
PLC_PORT = args.plc_port
logging.info(f"PLC address: {PLC_ADDRESS}")
logging.info(f"PLC port: {PLC_PORT}")
logging.info("Locator host address: " + LOCATOR_ADDRESS)
logging.info("Locator bianry port: " + str(LOCATOR_BINARY_PORT))
format = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S")
while True:
try:
time.sleep(0.5) # give 0.5s for the KeyboardInterrupt to be caught
run()
except KeyboardInterrupt:
# press ctrl+c to stop the program
sys.exit("The program exits as you press ctrl+c.")
except Exception as e:
logging.error(sys.exc_info())
logging.exception(e)
logging.error("Some exceptions arise. Restart run()...")
# finally: