Replies: 8 comments 8 replies
-
|
Are you able to reproduce the same behavior in a smaller program? |
Beta Was this translation helpful? Give feedback.
-
|
Apparently no. I tried two very simple programs, one receiver and one sender, with parts of the program above, but without the HandleComms object, and I could not replicate. Then I tried to put the HandleComms and it started to seem to have delays like the program above... but I quit, because the program above is much easier to debug than having two separate programs... One could think it is related with tasks, but with City and Prime everything works fine. |
Beta Was this translation helpful? Give feedback.
-
|
Today I did some more tests with this setup, and for some reason, I went to a different room in my apartment. And there there was no problem! Coming back to the initial room and the problem is there! It seems this is related with the location where I test it! But with City and Prime I could not reproduce the problems… |
Beta Was this translation helpful? Give feedback.
-
|
I am sorry if I didn't said anything else, but the problem vanished... I cannot replicate it anymore. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, As the main failure I'm getting is from broadcast or observe generating the error "OSError: This resource cannot be used in two tasks at once." or one hub trying to get an acknowledgment for it's location broadcast message, but only getting the location broadcast from the other hub, I've been searching for any discussion of hub to hub comms and multitasking. _ which brought me here. However the code example above is quite complex and I'm struggling to establish what's happening when. Are there any good resources/examples for explaining broadcast/observe, especially if they include multitasking. As for example code - mine is harder to follow than the example above... |
Beta Was this translation helpful? Give feedback.
-
|
Hi! Bidirectional broadcasting is a difficult process but it works. The code above was written when I was starting to use it. It tries to illustrate a strange situation which turned out to be very difficult to replicate... If I remember, the Anyway, broadcasting and tasks... here are some thoughts.
Here is some example code for a simple communications object. You make the class Queue:
def __init__(self):
self.fifo=[]
def append(self, element):
self.fifo.append(element)
def get(self):
if len(self.fifo) == 0:
return None
return self.fifo.pop(0)
def next(self):
if len(self.fifo) == 0:
return None
return self.fifo[0]
class CommunicationsHandler:
# This class is supposed to be a singleton but nothing is done to ensure it.
COMMS_WAIT_TIME=const(300)
def __init__(self, hub, observe_ch):
# hub =TechnicHub() or something like that
self.hub=hub
self.send_counter=1
self.observe_ch=observe_ch
self.rec_last_counter=None
self.rec_queue=Queue()
self.send_queue=Queue()
async def loop(self):
while True:
await wait(10)
rec=self.hub.ble.observe(self.observe_ch)
if rec is not None and rec[0] != self.rec_last_counter:
self.rec_last_counter=rec[0]
self.rec_queue.append(rec[1:]) # Remove counter
while self.send_queue.next():
to_send=self.send_queue.get()
await self.hub.ble.broadcast(to_send)
await wait(self.COMMS_WAIT_TIME) # To be sure it is broadcasted properly, give it some time
def send(self, value): # value must be a tupple
self.send_queue.append((self.send_counter,) + value) # add counter
self.send_counter+=1
def next_received(self):
return self.rec_queue.next()
def get_received(self):
return self.rec_queue.get()Hope it helps! |
Beta Was this translation helpful? Give feedback.
-
|
Yes, do a limited number of tasks since the beginning of the program. Send and acknowledge receive won’t work with this broadcast stuff, broadcast was simply not done for that. Put yourself in the train’s "mind". To take the decision of going to a station does not require to acknowledge to the other train that you have received its location (I assume...). |
Beta Was this translation helpful? Give feedback.
-
|
I don't see it that way: every train should be always broadcasting and observing at the same time. This means that train A is assured that train B received its location because train B acts like A does…. Always broadcasting and always observing. So, acknowledgment is implicit and it happens ”always”. This makes the communications layer much simpler. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everybody!
I wanted to test multitasking and bidirectional TechnicHub hub to hub communication.
So I did a fancy test program that launches several threads and uses one hub button to change the state of the other hub.
The same program runs on each hub, the program detects that there is a motor connected to port A or B, to setup the communication channels.
Almost everything works fine, in fact it seems a funny demo program but:
The very first time I click on one of the hubs, the message is received by the other hub only after about 8s.
Then, everything seems to work fine in that direction. The same problem in the opposite direction.
I also tried with CityHubs and PrimeHubs and it seems to be a problem only related with TechnicHub message reception.
The program is below. The comments includes further details.
Is this a real issue?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions