-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Powered Up Hub disconnects before leaving "run"? #6
Comments
This looks like an unsolicited disconnect. This is something I see occasionally (but quite often) with my technic hub too, usually when hub gets too far from my laptop. Unfortunately, technic hub is the only hub I have so I cannot try. These days I'm working on providing support for unsolicited disconnects, once finished, will push (I have to do it before I return to day-to-day work process) This should solve a number of odd things I've seen with technic hub. I'd be rather interested if you can try... |
The train was around 50 cm away from the Laptop with USB dongle for bluetooth.
Yes, of course =). Which other things have you fixed? |
@janvrany Do you have a preview of your current work? |
@justxi Pushed it to https://github.com/janvrany/bricknil/tree/wip/devel (branch
|
@janvrany Thanks. I have successfully set up a chroot, created ebuilds for Gentoo (https://github.com/justxi/bricks/) and all compiles fine with python 3.8. Next step is to test the train with your changes. |
@justxi Cool, let me know if you need any help! |
@janvrany The deprecation warning says to use "run" instead of "start", but maybe you want to fix that:
|
Is it correct that you moved the "run" method out of the class? So the above program should look like this:
But know I get the the following eror:
Which python version are you using? Another question, is it with your changes still possible to connect to more than one hub? |
Maybe this is also interesting: https://docs.python.org/3/whatsnew/3.8.html#asyncio |
Okay, I tried to build a simple example like yours and run into all sorts of issues. I messed it up and will need more time to fix it. Thanks for spotting this! Good catch!
3.9.1
Yeah, should be, though I cannot try as I have only one. Moreover, it should be possible to control both (all) hubs using a single program (that was one of the motivations for this refactoring) |
Do you have the problems with python 3.9.1 also?
Never mind, I´m interested in getting my train running =). |
@justxi |
@justxi |
it seems that this patch https://github.com/justxi/bricks/blob/master/dev-python/bricknil-janvrany/files/bricknil-run.patch fixes the problem above, but now I get:
Any idea? |
I had a look on bleak and that is fixed also ... But now I get:
|
Sorry, this was a mistake on my site... But it seems that the hub disconnects again ...
Any idea? |
@justxi : Yes, that is exactly what I see here (on my system). No idea so far, I'm sorry. |
@janvrany It seems that the hub disconnects always after one command, but this is just a guess... The "good" thing is, that it seems to be reproduceble... as well as on your and my system... |
@janvrany Interestingly the hub does not disconnect if no command is send to it. Instead of changing the speed of the motor, I "await" only a "sleep(1)" or repeated "sleep(1)" in a for-loop. I tested this for up to 100 seconds. |
I think this is a fault in my code, see:
This shows that |
@janvrany In one test I thought that adding an "await sleep(5)" at the beginning of "program" seems to make it better... but this was not reproducable. |
@janvrany Another question is, why do we see:
As far as I understand it, it should be called in the "finally" block when the block "try: await program()" leaves. |
@justxi: looks like I have fixed it, will push to |
Due to a slip in hub connection logic it could happen a peripheral was deemed as attached even before an 'attach' message has been sent and received. If in that case hub has been sent a command, it disconnects (apparrently). The problem was that peripheral's port not being `None` was used as an indication whether or not a peripheral is attached (or not). But this is WRONG, since when peripheral is attached like: @Attach(CPlusXLMotor, name='front_drive', port=0) @Attach(CPlusXLMotor, name='rear_drive', port=1) class XtremeOffRoader(CPlusHub): (that is, specifying port explicitly), the port is indeed not `None`! This commit fixes this by encapsluating the query into new `_is_attached()` method. This should fix issue #6.
@justxi: Pushed, see https://github.com/janvrany/bricknil/tree/wip/devel Could you please checkout https://github.com/janvrany/bricknil/tree/wip/devel and try again? Thanks for the patience! |
Hmm, maybe it is too late... but with this program
I get this:
It seems that the hub still disconnects. |
btw... I will go to C3 now and continue in the evening =). |
Given that changes in |
@janvrany |
@janvrany Bad: The PoweredUpHub still disconnects after 2-3 seconds and only one command is possible. |
Hmm...disappointing. Can you please post again complete log with new version, the script and perhaps what exactly you call "PoweredUpHub" (the LEGO number - I'm not very familiar with LEGO - I only have the technic hub from 4x4). |
In the Lego City trains (e.g. 60197) https://www.lego.com/en-gb/product/hub-88009 is used. I will add the script and the output later. Thanks for your support. |
This is the script:
|
And the output:
|
If the second set_speed command is executed (uncommented), this is the result:
|
It is very strange, the "Technic hub" and the "move hub" (Boost) seem to work; all starts, executes and terminates as expected. I tested the train hub with the Android app and it works as expected also. I use Gentoo Linux, Kernel 5.10.4, bluez 5.55 and python 3.7.9. There must be something different.... but what? |
@janvrany I installed the latest version of bricknil from virantha (0.9.3) and its dependencies using python 3.7. So there must be something broken regarding the "PoweredUpHub" aka. "TwoPortHub" in your current version or a dependency. |
@justxi |
I think thats a good idea. |
Do you remember which version of bleak you have used at this point: f433a4c ? |
I also have the Train hub, and I just tested the code from @justxi above ( #6 (comment) ), it seems to be working for me, also when uncommenting the second set_speed line. I am running on windows 10, python 3.9.1. |
However, if I add a VisionSensor: import logging
from asyncio import sleep
import bricknil
from bricknil import attach, run
from bricknil.hub import PoweredUpHub
from bricknil.sensor import TrainMotor, VisionSensor
@attach(TrainMotor, name='motor', port=0)
@attach(VisionSensor, name='train_sensor', capabilities=['sense_color'], port=1)
class Train(PoweredUpHub):
async def train_sensor_change(self):
print(self.train_sensor.sense_color)
train = Train('My Train')
async def program_train():
train.message_info('program_train')
for idx in range(5):
train.message_info('0: wait '+str(idx))
await sleep(1)
train.message_info('set_speed 50')
await train.motor.set_speed(50)
await sleep(3)
await train.motor.set_speed(0)
for idx in range(10):
train.message_info('1: wait '+str(idx))
await sleep(1)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
bricknil.run(program_train()) I get the following exception:
And the log:
|
Attaching a sensor works as expected with the Control+ hub, using as a basis the example from #7 |
I did the test with 6ed897e (slightly modified, due to bleak API change), without the sensor it works, but with the sensor I still get:
With dd752e4, It works without the sensor, with sensor no motor is ever started but also no exception is raised. |
That means that the change from curio to asyncio still works (b9e5e37)? |
No, with that commit I couldn't get it to work at all, with or without sensor. Same with the one before that, where adafruit was removed. I didn't try trouble shooting for too long though. |
@janvrany I installed bricknil from this repository (devel branch) with commit id f433a4c using bleak-0.6.1. Running the train_ramp.py example, I get this error:
So the first commit after forking from virantha seems to introduce something which let the "train" hub fail. |
I have been going back to testing this: And actually I now realized that sometimes even the most recent commit works, even with the sensor, using most recent bleak. However, it is not at all consistent, sometimes I get streaks of it working 3-4 times in a row but then it stops working for another 4 times. If it works, I've tested it for >1 minute, so it seems to be stable if it works. I've tested many factors, but so far I got only circumstantial correlation with some things, nothing yet guaranteed it working. I've tested 2 hubs. At one point it seemed like new batteries made it better, and letting a previous program finish normally without forcing it to quit while still connected seemed to have increased the chances, but like I said, nothing guaranteed it working. |
Which version of bleak are you using? |
It must've been bricknil-bleak 0.3.1, since I was approaching this from 0.9.3 |
@janvrany
Hi,
while your implementation works fine for the "Powered Up Technic Hub", I have a problem with the "Powered Up Hub".
Using this program:
I get:
I tried to change the LED color and I can only change the color once.
It seems that the hub disconnects or is disconnected before "run" leaves.
Do you have an idea why this happens?
Or do you have/had a similiar problem?
The text was updated successfully, but these errors were encountered: