Nasc Teachtaireachta#
Réamhrá#
Ligeann MessageLink do dhá V5 Brain cumarsáid a dhéanamh trí theachtaireachtaí gearra teaghrán a sheoladh le sonraí beaga roghnacha. Tá sé deartha le haghaidh comhordú róbat go róbat — amhail gnáthaimh a thosú, torthaí braiteoirí a roinnt, nó iompraíochtaí a spreagadh ar róbat eile.
Cuireann gach úsáid a bhaintear as send teachtaireacht le scuaine an V5 Brain nasctha, agus léitear an scuaine isteach ar dtús, amach ar dtús (FIFO). Má sheoltar il-theachtaireachtaí sula n-úsáideann an V5 Brain eile receive, stórálfar agus cuirfear ar ais iad ceann ar cheann san ord inar seoladh iad. Ós rud é gur féidir le teachtaireachtaí scuaine a dhéanamh, d’fhéadfadh sé go gcruthófaí riaráiste má sheoltar an stádas céanna arís agus arís eile i ngach lúb; i gcás loighic ríthábhachtach ó thaobh ama de, seoltar ach amháin nuair a athraíonn luachanna.
Tábhachtach: Caithfidh an dá róbat tionscadail a úsáideann MessageLink a bheith ag rith ag an am céanna, nó ní sheolfar/ní gheofar aon teachtaireachtaí.
Úsáideann an leathanach seo link mar shamplaainm MessageLink Cuir d’ainm cumraithe féin ina áit de réir mar is gá.
Seo thíos liosta de na modhanna atá ar fáil:
is_linked– Tugann sé seo ar ais an bhfuil an Inchinn seo ceangailte go gníomhach lena Inchinn phéireáilte.send– Seolann sé teachtaireacht chuig an Inchinn phéireáilte.receive– Fanann sé leis an gcéad teachtaireacht isteach eile agus seolann sé ar ais í.received– Cláraíonn sé feidhm le glaoch uirthi aon uair a fhaightear teachtaireacht nua.
Cruthaigh Nasc#
Cruthaigh MessageLink ar an dá V5 Brains chun cainéal cumarsáide a bhunú eatarthu—gan sreang le Raidióin Róbat V5 nó trí Chábla Cliste sreangaithe—ionas gur féidir leis na róbónna teachtaireachtaí a sheoladh chuig a chéile.
MessageLink(smartport, name, linktype, wired)
Paraiméadar |
Cur síos |
|---|---|
|
An Port Cliste a úsáidtear don nasc seo—an port a bhfuil an Raidió Róbat V5 ceangailte leis (gan sreang) nó a bhfuil an Cábla Cliste breiseánaithe isteach ann (sreangaithe). Scríofa mar |
|
An t-ainm uathúil le tabhairt don nasc seo mar shreang. |
|
An ról a úsáidfidh an Inchinn seo don phéire nasc seo. Ní mór Bainisteoir amháin agus Oibrí amháin a bheith i ngach péire:
|
|
Roghnach. Cibé acu an bhfuil an nasc seo sreangaithe nó gan sreang:
|
Cód do Róbat 1
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)Cód do Róbat 2
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
Modhanna atá ar Fáil#
Nuair a bheidh do MessageLink socraithe agus na Brains nasctha, is féidir leat na modhanna thíos a úsáid chun teachtaireachtaí a sheoladh, a fháil agus a fhreagairt.
is_linked#
Tugann an modh is_linked ar ais cibé an bhfuil na V5 Brains ar MessageLink péireáilte le chéile.
True– Tá an dá inchinn V5 péireáilte agus ag cumarsáid ar an nasc seo.False– Níl an dá Inchinn V5 péireáilte ar an nasc seo.
Nóta: Is dea-chleachtas é a chinntiú i gcónaí go bhfuil na V5 Brains nasctha ag tús tionscadail sula ritheann tú aon chód eile.
Usage:
link.is_linked()
Paraiméadair |
Cur síos |
|---|---|
Níl aon pharaiméadair ag an modh seo. |
Cód do Róbat 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)Cód do Róbat 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
send#
send seolann teaghrán agus/nó slánuimhir agus/nó snámhphointe chuig an V5 Brain nasctha eile.
Usage:
send(string, integer, float)
Paraiméadair |
Cur síos |
|---|---|
|
An teaghrán le seoladh chuig an V5 Brain nasctha eile. |
|
Roghnach. Slánuimhir le seoladh chuig an V5 Brain nasctha eile. |
|
Roghnach. Snámhphointe le seoladh chuig an V5 Brain nasctha eile. Déantar snámhphointí a fhormáidiú go 4 dheachúlacha; má sholáthraítear níos lú deachúlacha, cuirtear nialais ina dhiaidh sin leis go dtí go dtaispeántar 4. |
Mura seoltar ach string , faigheann an V5 Brain nasctha eile an teaghrán cruinn sin.
Má chuirtear integer agus/nó float san áireamh, faigheann an Brain glactha teaghrán ionchódaithe aonair a chomhcheanglaíonn na réimsí go léir san ord seo: .string_integer_float
Sampla: Ag baint úsáide as link.send(“string”, 1, 2.55) mar .string_1_2.5500.
Cód do Róbat 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)Cód do Róbat 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
Cód do Róbat 1
# Tell the other robot where the screen was pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") brain.screen.next_row() brain.screen.print("Tap to send circles") # Send the coordinates whenever the screen is pressed while True: if brain.screen.pressing(): x = brain.screen.x_position() y = brain.screen.y_position() link.send("touch", x, y) wait(10, MSEC)Cód do Róbat 2
# Draw a circle where Robot 1's screen was pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") while True: # Get the full encoded string circle_coordinates = link.receive(200) if circle_coordinates: # Split the encoded string by _ parts = circle_coordinates.split("_") if len(parts) == 3: # Convert the coordinates back into numbers x = float(parts[1]) y = float(parts[2]) brain.screen.set_fill_color(Color.WHITE) brain.screen.draw_circle(x, y, 20) wait(10, MSEC)
receive#
Seolann receive an chéad teachtaireacht eile sa scuaine ón V5 Brain nasctha eile. Léitear teachtaireachtaí in ord FIFO (an ceann is sine gan léamh ar dtús). Má tá an scuaine folamh nuair a ghlaotar receive , fanann sé go dtí an t-am críochnaithe receive le haghaidh teachtaireacht nua. Mura dtagann aon teachtaireacht isteach sa fhuinneog sin, seolann receive timeoutar ais, agus fanann aon teachtaireacht a sheoltar ina dhiaidh sin sa scuaine le léamh an chéad uair eile a úsáidtear None.
Más rud é nár sheol an V5 Brain nasctha eile ach teaghrán string, receive seolann sé an teaghrán beacht sin ar ais.
Má sheol an V5 Brain nasctha eile slánuimhir agus/nó snámhphointe, tugann receive teaghrán ionchódaithe aonair ar ais a chomhcheanglaíonn na réimsí go léir san ord seo: .string_integer_float
Mar shampla, má úsáideann an V5 Brain nasctha eile link.send(“string”, 1, 2.55), ansin má úsáideann tú link.receive() , cuirtear teaghrán ionchódaithe ar ais amhail .string_1_2.5500. Is féidir leat an teaghrán ionchódaithe seo a roinnt (féach na samplaí thíos) chun ainm an teachtaireachta, an tslánuimhir, agus na luachanna snámhphointe a bhaint amach.
Más fearr leat scoilt/parsáil láimhe a sheachaint, bain úsáid as received chun láimhseálaí a chlárú le haghaidh ainm teachtaireachta ar leith; faigheann an láimhseálaí na luachanna seolta mar argóintí.
Usage:
receive(timeout)
Paraiméadair |
Cur síos |
|---|---|
|
Roghnach. Cé chomh fada i milleasoicindí a fhanfaidh |
Cód do Róbat 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)Cód do Róbat 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
Cód do Róbat 1
# Send a math problem for the other Brain to solve # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ link.send("add", 7, 2.5)Cód do Róbat 2
# Solve the math problem that the other Brain sends # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() math_problem = link.receive() if math_problem: # Split the encoded string by _ parts = math_problem.split("_") if len(parts) == 3: # Remove the . from the beginning of the string operator = "".join([c for c in parts[0] if c.isalpha()]) # Separate out the numbers for the operation a = int(parts[1]) b = float(parts[2]) # Use what operator was sent if operator == "add": result = a + b elif operator == "subtract": result = a - b # If neither add or subtract is sent, do nothing else: pass brain.screen.print("Result = ", result)
received#
received cláraíonn feidhm le glaoch uirthi aon uair a fhaigheann an V5 Brain teachtaireacht sheolta.
Usage:
link.received(message, arg)
Paraiméadair |
Cur síos |
|---|---|
|
Ainm an teachtaireachta (teaghrán) le meaitseáil. Nuair a bhíonn ainm teachtaireachta a fuarthas ag teacht leis an luach seo, glaotar ar an bhfeidhm |
|
Feidhm [Feidhm atá sainithe roimhe seo a ritheann nuair a fhaigheann an V5 Brain teachtaireacht a mheaitseálann |
received glao ar ais a ghlaoitear le ceithre argóint:
Síniú Athghlao:
callback(message, linkname, index, value)
Argóint |
Cur síos |
|---|---|
|
Ainm an teachtaireachta a fuarthas (mar shampla, |
|
Ainm an naisc a bhfuarthas an teachtaireacht uaidh. |
|
An luach slánuimhir a sheoltar leis an teachtaireacht (más soláthraítear é). |
|
An luach snámhphointe a sheoltar leis an teachtaireacht (más soláthraítear é). |
Cód do Róbat 1
# Send a math problem for the other Brain to solve # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ link.send("add", 7, 2.5)Cód do Róbat 2
# Solve the math problem that the other Brain sends # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() # add if "add" is received def add_cmd(message, linkname, a, b): result = a + b brain.screen.print("Result = ", result) # subtract if ""subtract" is received def subtract_cmd(message, linkname, a, b): result = a - b brain.screen.print("Result = ", result) # Create event handlers for "add" and "subtract" link.received("add", add_cmd) link.received("subtract", subtract_cmd)