Commit c198abeb authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

some changes

parent 71134888
[Dolphin] [Dolphin]
PreviewsShown=true PreviewsShown=true
Timestamp=2018,12,12,15,36,33 SortRole=date
Timestamp=2018,12,13,15,55,15
Version=3 Version=3
ViewMode=1 ViewMode=1
...@@ -9,7 +9,7 @@ import ros2_config ...@@ -9,7 +9,7 @@ import ros2_config
rclpy.init() rclpy.init()
node = rclpy.create_node('some_client_node') node = rclpy.create_node('some_client_node')
cli = node.create_client(StrReqStrRes, 'master/cmd_all') cli = node.create_client(StrReqStrRes, 'n000e64101698_master/cmd')
wcnt = 0 wcnt = 0
while not cli.wait_for_service(timeout_sec=1.0): while not cli.wait_for_service(timeout_sec=1.0):
......
...@@ -4,47 +4,34 @@ import os ...@@ -4,47 +4,34 @@ import os
import re import re
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
def is_unique_name(a,name): NODEPREFIX = 'n'
for i in a:
if i['name']==name:
return False
return True
# get function # get function
def get(f="ros2_config.xml"): def get(f="ros2_config.xml"):
cfg = [] cfg = {}
prefix = "cam_" mac = get_mac(last_three_octets=False,capitalize=False)
mac = get_mac(capitalize=False) cfg['self'] = NODEPREFIX+mac
name = prefix+mac cfg['role'] = None
role = "unknown"
if os.path.isfile(f): if os.path.isfile(f):
e = ET.parse(f).getroot()
tmp = e.find('prefix') e = ET.parse(f).getroot()
if tmp!=None:
name = tmp.text+mac
tmp = e.find('role') tmp = e.find('self')
if tmp!=None: if tmp!=None:
role = tmp.text tmp = tmp.get('role')
if tmp!=None:
# self goes first cfg['role'] = tmp
cfg.append({'name':name,
'role':role})
# drop non-unique names cfg['slaves'] = {}
for s in e.findall('node'): for s in e.findall('slave'):
tmp_name = s.get('name') tmp_name = s.get('class')
tmp_role = s.get('role') tmp_mac = s.get('id')
if is_unique_name(cfg,tmp_name): cfg['slaves'][tmp_name] = tmp_mac
cfg.append({'name': tmp_name,
'role': tmp_role})
# will not overwrite role for non-unique nodes # will not overwrite role for non-unique nodes
return cfg return cfg
......
<?xml version="1.0" standalone="yes"?> <?xml version="1.0" standalone="yes"?>
<ros2> <ros2>
<prefix>cam_</prefix> <self role='' />
<role>not_master</role>
</ros2> </ros2>
<!--
<self> - info for this camera's node
attrs:
'role' - master or any string for not master
'system' - not used, TBA
example:
<self role='' />
<node> - info of other nodes, so this node will know other nodes 'by name'
attrs:
'class' - class TODO: allow multiple classes separated by comma or space
'id' - other node id - when composing - have to know prefix and mac
example:
<node class='left' id='n000e64101698'/>
prefixes are used because nodes names must start from a letter.
-->
\ No newline at end of file
...@@ -5,7 +5,9 @@ import ros2_config ...@@ -5,7 +5,9 @@ import ros2_config
cfg = ros2_config.get() cfg = ros2_config.get()
if cfg[0]['role']=='master': print(cfg)
if cfg['role']=='master':
subprocess.Popen(['python3','ros2_master.py']) subprocess.Popen(['python3','ros2_master.py'])
subprocess.Popen(['python3','ros2_slave.py']) subprocess.Popen(['python3','ros2_slave.py'])
...@@ -8,30 +8,13 @@ import ros2_config ...@@ -8,30 +8,13 @@ import ros2_config
TIMEOUT = 5 TIMEOUT = 5
# master callbacks
def signup_callback(req,res):
name = req.request
print('Sign up from '+name)
if name in clients:
print('Slave name is not unique. So sad.')
else:
clients.append(name)
print('List of registered slaves: '+str(clients))
res.response = name
return res
# async coroutine for cmdall_callback # async coroutine for cmdall_callback
async def pass_cmd(target,cmd): async def pass_cmd(target,cmd):
node = rclpy.create_node(target+'_client') #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 wcnt = 0
while not cli.wait_for_service(timeout_sec=1.0): while not cli.wait_for_service(timeout_sec=1.0):
...@@ -51,7 +34,7 @@ async def pass_cmd(target,cmd): ...@@ -51,7 +34,7 @@ async def pass_cmd(target,cmd):
# callback for broadcast commands # callback for broadcast commands
def cmdall_callback(req,res): def cmd_callback(req,res):
cmd = req.request cmd = req.request
tasks = [pass_cmd(c,cmd) for c in clients] tasks = [pass_cmd(c,cmd) for c in clients]
...@@ -68,21 +51,20 @@ def cmdall_callback(req,res): ...@@ -68,21 +51,20 @@ def cmdall_callback(req,res):
clients = [] clients = []
cfg = ros2_config.get() cfg = ros2_config.get()
nodename = cfg[0]['name'] nodename = cfg['self']+'_master'
for i in cfg: for k,v in cfg['slaves'].items():
clients.append(i['name']) print(k+' '+v)
clients.append(k)
rclpy.init() rclpy.init()
node = rclpy.create_node(nodename+"_master") node = rclpy.create_node(nodename)
event_loop = asyncio.get_event_loop() event_loop = asyncio.get_event_loop()
print("Starting master services:") print('Starting master services:')
#print(" master/signup") print(' '+nodename+'/cmd')
#s1 = node.create_service(StrReqStrRes, 'master/signup', signup_callback) s1 = node.create_service(StrReqStrRes, nodename+'/cmd', cmd_callback)
print(" master/cmd_all")
s2 = node.create_service(StrReqStrRes, 'master/cmd_all', cmdall_callback)
rclpy.spin(node) rclpy.spin(node)
......
...@@ -25,15 +25,14 @@ def cmd_callback(req,res): ...@@ -25,15 +25,14 @@ def cmd_callback(req,res):
# global # global
cfg = ros2_config.get() cfg = ros2_config.get()
nodename = cfg[0]['name'] nodename = cfg['self']
rclpy.init() rclpy.init()
node = rclpy.create_node(nodename) node = rclpy.create_node(nodename)
# now start cmd service print('Starting slave services:')
print("Starting slave services:") print(' '+nodename+'/cmd')
print(" "+nodename+'/cmd') s = node.create_service(StrReqStrRes, nodename+'/cmd', cmd_callback)
s3 = node.create_service(StrReqStrRes, nodename+'/cmd', cmd_callback)
rclpy.spin(node) rclpy.spin(node)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment