Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
ros2-scripts
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Elphel
ros2-scripts
Commits
7613fecc
Commit
7613fecc
authored
Dec 15, 2018
by
Oleg Dzhimiev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
testing
parent
c198abeb
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
154 additions
and
30 deletions
+154
-30
ros2_client.py
src/init/ros2_client.py
+47
-2
ros2_init.py
src/init/ros2_init.py
+1
-3
ros2_master.py
src/init/ros2_master.py
+52
-16
ros2_slave.py
src/init/ros2_slave.py
+54
-9
No files found.
src/init/ros2_client.py
View file @
7613fecc
...
...
@@ -6,6 +6,25 @@ import rclpy
from
elphel_interfaces.srv
import
StrReqStrRes
import
ros2_config
import
sys
import
html
# etree
#a = '<document><cmd>&</cmd></document>'
#xml = ET.Element("document")
#ET.SubElement(xml, "cmd").text = "begin & end"
#a = ET.tostring(xml)
#print(a)
#e = ET.fromstring(a)
#print(e)
#print(html.escape(e.find("cmd").text))
#sys.exit()
rclpy
.
init
()
node
=
rclpy
.
create_node
(
'some_client_node'
)
...
...
@@ -16,8 +35,33 @@ while not cli.wait_for_service(timeout_sec=1.0):
print
(
'Service is not available, waiting '
+
str
(
wcnt
)
+
'...'
)
wcnt
+=
1
###############################################################################
print
(
"TEST1"
)
req
=
StrReqStrRes
.
Request
()
req
.
request
=
'''<document>
<cmd>state</cmd>
</document>
'''
cli
.
call
(
req
)
cli
.
wait_for_future
()
res
=
cli
.
response
.
response
node
.
get_logger
()
.
info
(
"cmd response:"
)
node
.
get_logger
()
.
info
(
"
\n
"
+
res
)
###############################################################################
print
(
"TEST2"
)
req
=
StrReqStrRes
.
Request
()
req
.
request
=
"wget -qO- -o /dev/null 'http://localhost/parsedit.php?immediate&TRIG'"
req
.
request
=
'''<document>
<cmd>wget -qO- -o /dev/null 'http://localhost/parsedit.php?immediate&TRIG'</cmd>
</document>
'''
cli
.
call
(
req
)
cli
.
wait_for_future
()
...
...
@@ -25,7 +69,8 @@ cli.wait_for_future()
res
=
cli
.
response
.
response
node
.
get_logger
()
.
info
(
"cmd response:"
)
node
.
get_logger
()
.
info
(
res
)
node
.
get_logger
()
.
info
(
"
\n
"
+
res
)
###############################################################################
node
.
destroy_node
()
rclpy
.
shutdown
()
\ No newline at end of file
src/init/ros2_init.py
View file @
7613fecc
...
...
@@ -5,9 +5,7 @@ import ros2_config
cfg
=
ros2_config
.
get
()
print
(
cfg
)
if
cfg
[
'role'
]
==
'master'
:
if
cfg
[
'role'
]
==
'master'
:
subprocess
.
Popen
([
'python3'
,
'ros2_master.py'
])
subprocess
.
Popen
([
'python3'
,
'ros2_slave.py'
])
src/init/ros2_master.py
View file @
7613fecc
...
...
@@ -2,44 +2,81 @@
import
asyncio
import
xml.etree.ElementTree
as
ET
import
xml.dom.minidom
as
minidom
import
rclpy
from
elphel_interfaces.srv
import
StrReqStrRes
import
ros2_config
import
urllib.parse
import
html
TIMEOUT
=
5
# async coroutine for cmdall_callback
async
def
pass_cmd
(
target
,
cmd
):
#node = rclpy.create_node(target+'_client
')
cli
=
node
.
create_client
(
StrReqStrRes
,
target
+
'/cmd
'
)
slave
=
cfg
[
'slaves'
][
target
]
cli
=
node
.
create_client
(
StrReqStrRes
,
slave
+
'/cmd'
)
wcnt
=
0
while
not
cli
.
wait_for_service
(
timeout_sec
=
1.0
):
print
(
'Service is not available, waiting '
+
str
(
wcnt
)
+
'...'
)
wcnt
+=
1
if
wcnt
==
TIMEOUT
:
break
# if n/a
if
not
cli
.
wait_for_service
(
timeout_sec
=
1.0
):
return
"<response><node>"
+
target
+
"</node><state>dead</state></response>"
# if alive
req
=
StrReqStrRes
.
Request
()
req
.
request
=
cmd
req
.
request
=
'<request><cmd>'
+
cmd
+
'</cmd></request>'
cli
.
call
(
req
)
cli
.
wait_for_future
()
cmdres
=
cli
.
response
.
response
return
cmdres
def
parse_command
(
string
):
msg
=
{}
e
=
ET
.
fromstring
(
string
)
tmp
=
e
.
find
(
'cmd'
)
if
tmp
!=
None
:
# have to escape: &
msg
[
'cmd'
]
=
html
.
escape
(
tmp
.
text
)
else
:
msg
[
'cmd'
]
=
'state'
tmp
=
e
.
findall
(
'target'
)
if
not
tmp
:
msg
[
'targets'
]
=
clients
return
msg
# callback for broadcast commands
def
cmd_callback
(
req
,
res
):
cmd
=
req
.
request
tasks
=
[
pass_cmd
(
c
,
cmd
)
for
c
in
clients
]
# default function
cmd_func
=
pass_cmd
print
(
"The raw command is:"
)
print
(
req
.
request
)
q
=
parse_command
(
req
.
request
)
print
(
"parsed command:"
)
print
(
q
)
if
q
[
'cmd'
]
==
'state'
:
cmd_func
=
pass_cmd
#if q['cmd']=='status':
# cmd_func = cmd_get_status
tasks
=
[
cmd_func
(
cfg
[
'slaves'
][
c
],
q
[
'cmd'
])
for
c
in
q
[
'targets'
]]
results
=
event_loop
.
run_until_complete
(
asyncio
.
gather
(
*
tasks
))
print
(
"Printing results:"
)
print
(
results
)
res
.
response
=
'
\n
'
.
join
(
results
)
...
...
@@ -54,7 +91,6 @@ cfg = ros2_config.get()
nodename
=
cfg
[
'self'
]
+
'_master'
for
k
,
v
in
cfg
[
'slaves'
]
.
items
():
print
(
k
+
' '
+
v
)
clients
.
append
(
k
)
rclpy
.
init
()
...
...
@@ -62,7 +98,7 @@ node = rclpy.create_node(nodename)
event_loop
=
asyncio
.
get_event_loop
()
print
(
'Starting master services:'
)
print
(
'
\n
Starting master services:'
)
print
(
' '
+
nodename
+
'/cmd'
)
s1
=
node
.
create_service
(
StrReqStrRes
,
nodename
+
'/cmd'
,
cmd_callback
)
...
...
src/init/ros2_slave.py
View file @
7613fecc
...
...
@@ -9,16 +9,38 @@ import ros2_config
TIMEOUT
=
5
def
parse_command
(
string
):
msg
=
{}
e
=
ET
.
fromstring
(
string
)
tmp
=
e
.
find
(
'cmd'
);
if
tmp
!=
None
:
msg
[
'cmd'
]
=
tmp
.
text
else
:
msg
[
'cmd'
]
=
'state'
return
msg
# slave callbacks
def
cmd_callback
(
req
,
res
):
cmd
=
req
.
request
# call to localhost here, get response and send it back
try
:
output
=
subprocess
.
check_output
(
cmd
,
shell
=
True
)
except
subprocess
.
CalledProcessError
:
output
=
"<error>-1</error>"
res
.
response
=
'<cmd><node>'
+
nodename
+
'</node><status>'
+
str
(
output
)
+
'</status></cmd>'
q
=
parse_command
(
req
.
request
)
print
(
"parsed command by slave:"
)
print
(
q
)
if
q
[
'cmd'
]
==
'state'
:
res
.
response
=
'<response><node>'
+
nodename
+
'</node><state>idle</state></response>'
else
:
# call to localhost here, get response and send it back
try
:
output
=
subprocess
.
check_output
(
q
[
'cmd'
],
shell
=
True
)
except
subprocess
.
CalledProcessError
:
output
=
"<error>CalledProcessError</error>"
.
encode
()
res
.
response
=
'<response><node>'
+
nodename
+
'</node><state>'
+
output
.
decode
()
+
'</state></response>'
return
res
...
...
@@ -30,7 +52,7 @@ nodename = cfg['self']
rclpy
.
init
()
node
=
rclpy
.
create_node
(
nodename
)
print
(
'Starting slave services:'
)
print
(
'
\n
Starting slave services:'
)
print
(
' '
+
nodename
+
'/cmd'
)
s
=
node
.
create_service
(
StrReqStrRes
,
nodename
+
'/cmd'
,
cmd_callback
)
...
...
@@ -39,4 +61,27 @@ rclpy.spin(node)
# Destroy the node explicitly
# (optional - Done automatically when node is garbage collected)
node
.
destroy_node
()
rclpy
.
shutdown
()
\ No newline at end of file
rclpy
.
shutdown
()
#xml = ET.Element("response")
##ET.SubElement(doc, "field1", name="blah").text = "some value1"
##ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"
#for k,v in cfg['slaves'].items():
#xml_node = ET.SubElement(xml, "node")
#ET.SubElement(xml_node, "name").text = k
#ET.SubElement(xml_node, "id").text = v
#if v in nodelist:
#s = "online"
#else:
#s = "offline"
#ET.SubElement(xml_node, "status").text = s
#xml_str = ET.tostring(xml).decode()
#xml_str = minidom.parseString(xml_str).toprettyxml(indent=" ")
#return xml_str
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment