Python Sonrach do Róbónna#

Réamhrá#

Tá Mótar Iontógála, Mótar Láimhe, agus Braiteoir Fís IQ AI sa Laoch Bot, Flop.

Tá gach ordú caighdeánach VEXcode VR Python ar fáil lena n-úsáid sa chlós súgartha IQ 26-27 Leibhéal Suas.

Seo thíos liosta de na horduithe Python uile atá ar fáil atá sainiúil do Robots:

Gluaiseacht – Bog agus rianaigh mótair an róbait.

  • Gníomhartha

    • casadh – Casann sé mótar i dtreo amháin go deo.

    • sníomh_le_haghaidh – Casann sé mótar ar feadh líon áirithe céimeanna nó casadh.

    • casadh_go_suíomh – Casann sé mótar go suíomh ar leith.

    • stop – Cuireann sé stop le mótar ó bheith ag sníomh.

  • Socruithe

    • set_velocity – Socraíonn sé cé chomh tapa agus a rothlóidh mótar.

    • set_timeout – Socraíonn sé cé chomh fada a dhéanfaidh mótar iarracht gluaiseacht a chríochnú.

    • set_position – Athraíonn sé suíomh reatha an mhótair go luach nua.

  • Luachanna

    • is_done – Tugann sé seo ar ais an bhfuil an mótar críochnaithe ag bogadh.

    • ag_sníomh – Tugann sé seo ar ais an bhfuil an mótar ag sníomh.

    • suíomh – Tugann sé seo suíomh reatha an mhótair ar ais.

    • treoluas – Tugann sé seo léargas ar luas rothlaithe an mhótair.

Fís AI – Gabh agus anailísigh rudaí ag baint úsáide as Braiteoir Fís AI IQ.

  • Gabhálaithe

    • take_snapshot – Scagtar fráma reatha an braiteora go síniú sonrach agus cuirtear tacar de réada braite ar ais.

  • Airíonna

    • .width – Tugann sé leithead an réada braite i bpicteilíní ar ais.

    • .airde – Tugann sé airde an réada braite ar ais i bpicteilíní.

    • .centerX – Tugann sé seo comhordanáid-x lár an réada braite ar ais.

    • .centerY – Tugann sé seo comhordanáid y lár an réada braite ar ais.

    • .originX – Tugann sé seo comhordanáid-x chúinne uachtarach na láimhe clé de bhosca teorann an réada braite ar ais.

    • .originY – Tugann sé seo comhordanáid y chúinne uachtarach na láimhe clé de bhosca teorann an réada braite ar ais.

    • .id – Tugann sé seo ID an Aicmithe AI ​​a braitheadh ​​ar ais.

Úsáideann na samplaí ar an leathanach seo suíomh tosaigh réamhshocraithe an Chlós Súgartha.

Gluaiseacht#

Tá dhá mhótar atá sainiúil don róbat ag an Flop: intake_motor agus arm_motorÚsáideann an dá cheann orduithe mótair caighdeánacha.

Tairiseacha treorach:

  • intake_motor: FORWARD = iontógáil (bailíonn); REVERSE = aschur (díbirt)

  • arm_motor: FORWARD = suas (ardaíonn); REVERSE = síos (íslionn)

casadh#

Casann spin mótar sa treo tugtha go deo. Leanfaidh an mótar ag rothlú go dtí go dtabharfar ordú eile dó.

Usage:
motor.spin(direction)

Paraiméadair

Cur síos

direction

An treo le casadh: FORWARD nó REVERSE.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

casadh_do#

spin_for rothlaíonn mótar ar feadh achar sonrach. Fanann an tionscadal go dtí go mbeidh an mótar críochnaithe sula ritheann an chéad ordú eile mura ritear wait=False.

Usage:
motor.spin_for(direction, amount, unit)

Paraiméadair

Cur síos

direction

An treo le casadh: FORWARD nó REVERSE.

amount

An fad le casadh. Is féidir leis seo a bheith ina shlánuimhir nó ina dheachúil.

unit

An t-aonad tomhais: DEGREES nó TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

casadh_go_suíomh#

spin_to_position Casann mótar go suíomh ar leith. Fanann an tionscadal go dtí go mbeidh an mótar críochnaithe sula ritheann an chéad ordú eile mura ritear wait=False.

Usage:
motor.spin_to_position(position, unit)

Paraiméadair

Cur síos

position

An suíomh le casadh chuige. Is féidir leis seo a bheith ina shlánuimhir nó ina dheachúil.

unit

An t-aonad tomhais: DEGREES nó TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_to_position(1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

stad#

stop stopann mótar ó sníomh.

Usage:
motor.stop()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(1, SECONDS)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

socraigh_luas#

set_velocity socraíonn sé cé chomh tapa agus a rothlóidh mótar mar chéatadán ó 0 go 100.

Usage:
motor.set_velocity(velocity, unit)

Paraiméadair

Cur síos

velocity

Luas an mhótair mar chéatadán ó 0 go 100.

unit

An t-aonad tomhais: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.set_velocity(100, PERCENT)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

socraigh_am_scoite#

set_timeout socraíonn sé cé mhéad soicind a dhéanfaidh mótar iarracht gluaiseacht a chríochnú sula stopann sé.

Usage:
motor.set_timeout(timeout, unit)

Paraiméadair

Cur síos

timeout

An líon soicindí a dhéanfaidh an mótar iarracht gluaiseacht a chríochnú.

unit

An t-aonad tomhais: SECONDS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_timeout(2, SECONDS)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

socraigh_suíomh#

Athraíonn set_position suíomh reatha an mhótair go luach nua, ag athshocrú a ionchódóra.

Usage:
motor.set_position(position, unit)

Paraiméadair

Cur síos

position

An luach suímh nua.

unit

An t-aonad tomhais: DEGREES nó TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_position(0, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

is_done#

is_done luach Booleach ar ais a léiríonn an bhfuil an mótar críochnaithe lena ghluaiseacht.

  • True – Tá an mótar críochnaithe ag bogadh.

  • False – Tá an mótar fós ag gluaiseacht.

Usage:
motor.is_done()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS, wait=False)
    while not arm_motor.is_done():
        wait(5, MSEC)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

ag_sníomh#

is_spinning luach Booleach ar ais a léiríonn an bhfuil an mótar ag sníomh faoi láthair.

  • True – Tá an mótar ag sníomh.

  • False – Níl an mótar ag sníomh.

Usage:
motor.is_spinning()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    while intake_motor.is_spinning():
        wait(5, MSEC)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

seasamh#

Tugann suíomh position suíomh reatha an mhótair ar ais.

Usage:
motor.position(unit)

Paraiméadair

Cur síos

unit

An t-aonad tomhais: DEGREES nó TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    while arm_motor.position(DEGREES) < 360:
        arm_motor.spin(FORWARD)
        wait(2, MSEC)
    arm_motor.stop()
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

luas#

velocity a thugann ar ais cé chomh tapa agus atá an mótar ag sníomh faoi láthair mar chéatadán ó -100 go 100.

Usage:
motor.velocity(unit)

Paraiméadair

Cur síos

unit

An t-aonad tomhais: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(0.2, SECONDS)
    brain.screen.print(intake_motor.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

Fís AI#

tóg_pictiúr#

Scagann take_snapshot sonraí ó fhráma Braiteoir Fís IQ AI go síniú aonair — cur síos sábháilte ar rud is féidir leis an braiteoir a aithint, amhail eilimint chluiche ar an bpáirc — agus tugann sé tuple ar ais.

Stórálann an tuple réada in ord ón gceann is mó go dtí an ceann is lú de réir leithead, ag tosú ag innéacs 0. Is féidir rochtain a fháil ar airíonna gach réada trína innéacs a úsáid. Seoltar tuple folamh ar ais mura mbraitear aon réada comhoiriúnacha.

Usage:
ai_vision.take_snapshot(signature)

Paraiméadair

Cur síos

signature

Scagann sé an tacar sonraí chun sonraí an tsínithe tugtha amháin a áireamh. Is iad na sínithe atá ar fáil:

  • AiVision.ALL_AIOBJS - Braitheann sé gach Mála Pónaire.
  • AiVision.ALL_TAGS - Braitheann sé AprilTags, atá le fáil ar thaobhanna na gCúl.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Turn to face a red Bean Bag
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn(LEFT)
                elif ai_object.centerX > 180:
                    drivetrain.turn(RIGHT)
                else:
                    drivetrain.stop()
                    return
                break

# VR threads — Do not delete
vr_thread(main)

Airíonna#

Tá seacht n-airíonna san áireamh le gach réad atá stóráilte i dtuple tar éis take_snapshot a úsáid.

Déanann gach luach maoine seachas .id cur síos ar shuíomh agus méid an réada braite i radharc Braiteoir Fís IQ AI ag an nóiméad a úsáideadh take_snapshot Tomhaistear na luachanna seo i bpicteilíní, bunaithe ar réiteach 320 faoi 240 picteilín an braiteora.

.leithead#

Tugann .width leithead an réada braite i bpicteilíní ar ais, ar slánuimhir é idir 1 agus 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.width > 200:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

airde#

.height tugann sé airde an réada braite i bpicteilíní ar ais, ar slánuimhir é idir 1 agus 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.height > 110:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

.centerX#

.centerX seolann sé comhordanáid-x lár an réada braite i bpicteilíní ar ais, ar slánuimhir idir 0 agus 320 é.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the Bean Bag centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.centerY#

.centerY seolann sé comhordanáid-y lár an réada braite i bpicteilíní ar ais, ar slánuimhir idir 0 agus 240 é.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.centerY)
            break

# VR threads — Do not delete
vr_thread(main)

.originX#

.originX seolann sé comhordanáid-x chúinne uachtarach na láimhe clé de bhosca teorann an réada braite i bpicteilíní, ar slánuimhir idir 0 agus 320 í.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the left edge centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.originX < 100:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.bunúsY#

Tugann .originY comhordanáid-y chúinne uachtarach na láimhe clé de bhosca teorann an réada braite ar ais i bpicteilíní, arb é slánuimhir idir 0 agus 240 é.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag's top edge
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.originY)
            break

# VR threads — Do not delete
vr_thread(main)

.id#

.id tugann sé aitheantóir an Aicmithe AI ​​a braitheadh ​​ar ais mar shlánuimhir.

Aicmiú AI

aitheantas

Mála Pónairí Gorma

0

Mála Pónaire Dearg

1

Mála Pónaire Buí

2

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Pick up the Bean Bag if it's red
    drivetrain.set_drive_velocity(30, PERCENT)
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            intake_motor.spin(FORWARD)
            drivetrain.drive_for(FORWARD, 150, MM)
            break

# VR threads — Do not delete
vr_thread(main)


def main():
    # Stop when you see the Red Goal
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.turn(RIGHT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)
        for ai_object in ai_objects:
            if ai_object.id == 1:
                drivetrain.stop()
                brain.screen.print("Found the Red Goal!")
                return

# VR threads — Do not delete
vr_thread(main)