Monday 17 April 2017

Blog 6: What can "UCreate" with a Micro:bit and a Rasperry Pi 2 (Minecraft random serial bounce)

Last summer I created some resources to use with Micro:bit/s for CPC and over the next few months and I will try and share the best bits with the kind permission of CPC. 

You can go and buy a Micro:bit project kit from here: http://cpc.farnell.com/bbc-micro-bit-kits

This one uses the Micro:bit and Raspberry Pi. The Micro:bit uses a serial connection to the Pi to send data. The credit for this goes to Martin O'Hanlon and David Whale.

The Micro:bit sends messages to the Pi when the "a" button is pressed, which then interprets that instruction and randomly bounces the Minecraft character to a random height.

Here is a picture of the set up



Here is the code for the Micro:bit:


Written by David Whale and Martin O’Hanlon edited by Chris Penn

from microbit import*
import random

REFRESH = 500
Message = "Bounce"
def get_data():
    yValue = random.randint(2,20)  
    a, b = button_a.was_pressed(), button_b.was_pressed()
   
    print(a, b, Message, yValue )

def run():
    while True:
        sleep(REFRESH)
        get_data()
        if button_a.is_pressed():
            display.scroll(Message)
run()

Python code to go on the Raspberry Pi
N.b. This needs to be run in Python 2.7

"""
Code written by Martin O'Hanlon in the following blog post:
http://www.stuffaboutcode.com/2016/03/microbit-get-data-from-usb.html
"""

import time
import serial
from mcpi.minecraft import Minecraft
from time import sleep
from mcpi import block as block
import random


PORT = "/dev/ttyACM0"
BAUD = 115200

s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity   = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE
#read the first line and flush any bad data
s.readline()

def read_microbit_data():
    #read a line from the microbit,
    data = s.readline()
    #split the microbit data into x, y, z, a, b
    data_s = data.rstrip().split(" ")
    a = True if data_s[0] == "True" else False
    b = True if data_s[1] == "True" else False
    Message = data_s[2]
    yValue = data_s[3]
    return a,b,Message,yValue

mc = Minecraft.create()
try:
    playerPos = mc.player.getTilePos()
    while True:
        a,b,Message,yValue = read_microbit_data()
        if a == True:
            pos = mc.player.getPos()

            mc.player.setPos(pos.x,yValue,pos.z)
            msg = Message+""+yValue
            mc.postToChat(msg)  
finally:
    sleep(1)
    s.close()


Instructions 

1. Having flashed your Micro:bit script and leaving your Micro:bit plugged into your Pi.
2. Open Minecraft, create a new world, ensure you can see the character like in the picture above, minimise the screen. 
3. Run the Python code in 2.7 from above. 
4. Press the A button on the Micro:bit, look at Minecraft and you should see the character jump up at a random height between 1 and 20!
5. Try it again.

Enjoy!


Why not try and extend the Python code to Teleport Steve

Updated Python code needs to be run in Python 2.7
Updated code is in red



import time

"""
Code written by Martin O'Hanlon in the following blog post:
http://www.stuffaboutcode.com/2016/03/microbit-get-data-from-usb.html
"""
import serial
from mcpi.minecraft import Minecraft
from time import sleep
from mcpi import block as block
import random


PORT = "/dev/ttyACM0"
BAUD = 115200

s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity   = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE
#read the first line and flush any bad data
s.readline()

def read_microbit_data():
    #read a line from the microbit,
    data = s.readline()
    #split the microbit data into x, y, z, a, b
    data_s = data.rstrip().split(" ")
    a = True if data_s[0] == "True" else False
    b = True if data_s[1] == "True" else False
    Message = data_s[2]
    yValue = data_s[3]
    return a,b,Message,yValue

mc = Minecraft.create()
try:
    playerPos = mc.player.getTilePos()
    while True:
        a,b,Message,yValue = read_microbit_data()
        if a == True:
            pos = mc.player.getPos()
            #change this to teleport


            mc.player.setPos(random.randint(-100,100), yValue,random.randint(-100,100))
            msg = Message+""+yValue
            mc.postToChat(msg)  
finally:
    sleep(1)
    s.close()

No comments:

Post a Comment