pybricks / support

Pybricks support and general discussion
MIT License
107 stars 6 forks source link

[Feature] Add example for IR Seeker #1498

Open laurensvalk opened 6 months ago

laurensvalk commented 6 months ago

Hi I am going to use Pybricks blocks with my students for RoboCup Junior Australia Competition. In the soccer comp we will be using the IR sensor from https://buildingblockrobotics.com/products/ir-seeker

It has a sample to read in the ball but could you add the additional blocks to set up please.

Originally posted by a Facebook user via our page

laurensvalk commented 6 months ago

I think we can start by explaining how you can do it by using that sample in Python and importing it into a block program.

BertLindeman commented 6 months ago

I think we can start by explaining how you can do it by using that sample in Python and importing it into a block program.

Is there already doc about the import into a block program? Do you want everyone to know that trick? I could make an example here from the IR-seeker example:

from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port

device = PUPDevice(Port.A)

while True:
    values = device.read(5)
    if len(values) > 0:
        print("Strength", values[0]) #return the signal strength
    if len(values) > 1:
        print("Direction", values[1]) #return the direction of the ball 0 - 12

No IR-seeker in my inventory, but I have a color sensor, and as documented it acts like a color sensor.

laurensvalk commented 6 months ago

Thanks Bert. I received the sensor although the ball seems to be out of stock in most places. I found that it is able to produce values using an old radio remote, so I'll use that for basic testing.

Do you want everyone to know that trick?

Sure! There's a few usability questions to sort out still though, so I haven't posted about it extensively.

Basically, importing functions works great, but right now the main block program may either be fully async or not, and the imported function has to match or it will not work as expected.

laurensvalk commented 6 months ago

As I was writing that, I think a reasonable solution that doesn't require too much magic behind the scenes is #1499.

Beginning teams can just write synchronous code, but more advanced teams or third party manufacturers can use this to make classes that work either way without having the end user having to think about it.