artificial-intelligence-class / spherov2.py

Unofficial Python API for all Sphero toys
MIT License
64 stars 24 forks source link

BOLT connection and API issue #23

Closed filterbank10210 closed 2 years ago

filterbank10210 commented 2 years ago

Hi, just bought a BOLT in the hope my daughter will pick it up to learn some programming. Was hoping to impress her abit more with python scripting... but yeah... I come here begging for help :P

With the below version: OS: windows 10 Python version: 3.10.4 (IDLE shell) Bleak: 0.19.0 (and the dependencies) SPHEROV2: 0.11.4

I faced the below issues:

  1. toy initialization: toy=scanner.find_BOLT() worked, but toy.__adapter always set to None ... so any use of toy there after seems to fail ... (e.g. the spin(360,1) example)
  2. after toy.enter()... it seems to resolve the point 1 issue, but then I see timeout error. From here I am not sure how to proceed.

If there is any pointers or reference for me to look up or debug ... it is much appreciated.

semi-noop :P mostly used matplotlib for my plotting works... IO connectivity is kinda new to me ... like to use this opportunity to learn and improve myself too ... before I try to impress my daughter :>

NB: with the Sphero-Edu app... i can use the BOLT normally, so I am ure it is just the python env issue.

superfashi commented 2 years ago

Hi, have you tried to use the Edu API? For example:

from spherov2 import scanner
from spherov2.sphero_edu import SpheroEduAPI

toy = scanner.find_toy()
with SpheroEduAPI(toy) as api:
    api.spin(360, 1)
filterbank10210 commented 2 years ago

Good day, superfashi

ok.. I did it this way toy=scanner.find_BOLT() api=SpheroEduAPI(toy) api.spin(360,1) error message: RuntimeError: Use toys in context manager

I tried using the above "with ... " statement ... it worked.. I do not need to use the enter() step, but it is unreliable. Some time it will give the following error: asyncio.exceptions.TimeoutError (the second problem I saw) then a retry with the same statement worked, but another following same statement may fail again with timeout.

This is still better then yesterday already. at least spin(360,1) can work.. and now I can explore the scripting more :> thanks

It is still abit weird to me as I thought that both the "with" style and the object api style should behave the same?

Sincerely appreciate your kind patience.

Best Regards filterbank10210

superfashi commented 2 years ago

So the API is designed that when you use with statement (context manager), it will connect to the toy and release resources when you exit. You have to use it with with, basically.

filterbank10210 commented 2 years ago

OIC

thanks very much for the pointers!

Will close this topic then.

sincerely appreciated.

filterbank10210 commented 2 years ago

learnt the context handling herein.