Commit c198abeb authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

some changes

parent 71134888
[Dolphin]
PreviewsShown=true
Timestamp=2018,12,12,15,36,33
SortRole=date
Timestamp=2018,12,13,15,55,15
Version=3
ViewMode=1
......@@ -9,7 +9,7 @@ import ros2_config
rclpy.init()
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
while not cli.wait_for_service(timeout_sec=1.0):
......
......@@ -4,47 +4,34 @@ import os
import re
import xml.etree.ElementTree as ET
def is_unique_name(a,name):
for i in a:
if i['name']==name:
return False
return True
NODEPREFIX = 'n'
# get function
def get(f="ros2_config.xml"):
cfg = []
cfg = {}
prefix = "cam_"
mac = get_mac(capitalize=False)
name = prefix+mac
role = "unknown"
mac = get_mac(last_three_octets=False,capitalize=False)
cfg['self'] = NODEPREFIX+mac
cfg['role'] = None
if os.path.isfile(f):
e = ET.parse(f).getroot()
tmp = e.find('prefix')
if tmp!=None:
name = tmp.text+mac
e = ET.parse(f).getroot()
tmp = e.find('role')
tmp = e.find('self')
if tmp!=None:
role = tmp.text
# self goes first
cfg.append({'name':name,
'role':role})
tmp = tmp.get('role')
if tmp!=None:
cfg['role'] = tmp
# drop non-unique names
for s in e.findall('node'):
cfg['slaves'] = {}
for s in e.findall('slave'):
tmp_name = s.get('name')
tmp_role = s.get('role')
tmp_name = s.get('class')
tmp_mac = s.get('id')
if is_unique_name(cfg,tmp_name):
cfg.append({'name': tmp_name,
'role': tmp_role})
cfg['slaves'][tmp_name] = tmp_mac
# will not overwrite role for non-unique nodes
return cfg
......
<?xml version="1.0" standalone="yes"?>
<ros2>
<prefix>cam_</prefix>
<role>not_master</role>
<self role='' />
</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
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_slave.py'])
......@@ -8,30 +8,13 @@ import ros2_config
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 def pass_cmd(target,cmd):
node = rclpy.create_node(target+'_client')
cli = node.create_client(StrReqStrRes,target+'/cmd')
#node = rclpy.create_node(target+'_client')
slave = cfg['slaves'][target]
cli = node.create_client(StrReqStrRes,slave+'/cmd')
wcnt = 0
while not cli.wait_for_service(timeout_sec=1.0):
......@@ -51,7 +34,7 @@ async def pass_cmd(target,cmd):
# callback for broadcast commands
def cmdall_callback(req,res):
def cmd_callback(req,res):
cmd = req.request
tasks = [pass_cmd(c,cmd) for c in clients]
......@@ -68,21 +51,20 @@ def cmdall_callback(req,res):
clients = []
cfg = ros2_config.get()
nodename = cfg[0]['name']
nodename = cfg['self']+'_master'
for i in cfg:
clients.append(i['name'])
for k,v in cfg['slaves'].items():
print(k+' '+v)
clients.append(k)
rclpy.init()
node = rclpy.create_node(nodename+"_master")
node = rclpy.create_node(nodename)
event_loop = asyncio.get_event_loop()
print("Starting master services:")
#print(" master/signup")
#s1 = node.create_service(StrReqStrRes, 'master/signup', signup_callback)
print(" master/cmd_all")
s2 = node.create_service(StrReqStrRes, 'master/cmd_all', cmdall_callback)
print('Starting master services:')
print(' '+nodename+'/cmd')
s1 = node.create_service(StrReqStrRes, nodename+'/cmd', cmd_callback)
rclpy.spin(node)
......
......@@ -25,15 +25,14 @@ def cmd_callback(req,res):
# global
cfg = ros2_config.get()
nodename = cfg[0]['name']
nodename = cfg['self']
rclpy.init()
node = rclpy.create_node(nodename)
# now start cmd service
print("Starting slave services:")
print(" "+nodename+'/cmd')
s3 = node.create_service(StrReqStrRes, nodename+'/cmd', cmd_callback)
print('Starting slave services:')
print(' '+nodename+'/cmd')
s = node.create_service(StrReqStrRes, nodename+'/cmd', cmd_callback)
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