Sanitka

Zo stránky Robotický krúžok
Verzia z 17:05, 11. október 2024, ktorú vytvoril Palo (diskusia | príspevky)
(rozdiel) ← Staršia verzia | Aktuálna úprava (rozdiel) | Novšia verzia → (rozdiel)
Skočit na navigaci Skočit na vyhledávání

Diaľkový ovládač jedného Spike z druhého Spike pomocou PyBricks, ktorý dovoľuje posielať BlueTooth správy.

sanitka.py

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.robotics import DriveBase
from pybricks.parameters import Port, Side, Stop, Color, Direction 
from pybricks.tools import wait 

hub = PrimeHub(observe_channels=[1])

left_motor = Motor(Port.B, Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.A)

drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=192)

while True:
    data = hub.ble.observe(1) 

    if data is None:
        # No data has been received in the last 1 second.
        hub.light.on(Color.RED)

    else:
        
        # Data was received and is less that one second old.
        hub.light.on(Color.GREEN)
        ta, tc = data 

        if ta:
             drive_base.drive(180, -90)
        elif tc:
            drive_base.drive(180, 90)
        else:
            drive_base.drive(180, 0)
        wait(50)


ovladac.py

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ForceSensor
from pybricks.parameters import  Port, Side, Stop, Color
from pybricks.tools import wait, StopWatch

hub = PrimeHub(broadcast_channel=1)

print("ahoj")

hub.light.on(Color.GREEN)
wait(100)
hub.light.on(Color.RED)
wait(50)

print("joha")

hub.light.on(Color.GREEN)
wait(100)
hub.light.on(Color.RED)
wait(50)

print("cau")

hub.light.on(Color.GREEN)
wait(100)
hub.light.on(Color.RED)
wait(50)

print("uac")

TA = ForceSensor(Port.A)
TC = ForceSensor(Port.B)

while True:
  #print TA: " + str(TA.pressed()) + ", TC: " + str(TC.pressed()) + ",
  data = ( TA.pressed(), TC.pressed())
  hub.ble.broadcast(data)
  wait(20)