Created
May 21, 2020 07:01
-
-
Save bricklife/017937aab080c860eee6b732c77dd76c to your computer and use it in GitHub Desktop.
Send command to SPIKE from EV3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env pybricks-micropython | |
from pybricks.hubs import EV3Brick | |
from pybricks.tools import wait | |
from pybricks.parameters import Button | |
from connection import SpikePrimeStreamReader | |
# Add send_command method into SpikePrimeStreamReader class | |
def send_command(self, command): | |
self.sock.send((command + '\r').encode()) | |
SpikePrimeStreamReader.send_command = send_command | |
# Beep! | |
ev3 = EV3Brick() | |
ev3.speaker.beep() | |
# Create the connection. See README.md to find the address for your SPIKE hub. | |
spike = SpikePrimeStreamReader('40:BD:32:42:CB:37') | |
while True: | |
while not any(ev3.buttons.pressed()): | |
wait(10) | |
if Button.LEFT in ev3.buttons.pressed(): | |
spike.send_command('{"i":"AAA","m":"scratch.motor_start","p":{"port":"A","speed":-75,"stall":true}}') | |
if Button.RIGHT in ev3.buttons.pressed(): | |
spike.send_command('{"i":"AAA","m":"scratch.motor_start","p":{"port":"A","speed":75,"stall":true}}') | |
if Button.CENTER in ev3.buttons.pressed(): | |
spike.send_command('{"i":"AAA","m":"scratch.motor_stop","p":{"port":"A","stop":1}}') | |
while any(ev3.buttons.pressed()): | |
wait(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just added examples here: https://gist.github.com/bricklife/13c7fe07c3145dd94f4f23d20ccf5a79