Monday, 17 April 2017

Blog 7: What can "UCreate" with a Micro:bit and a Rasperry Pi 3 (Minecraft random radio 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.

This builds on the previous blog:
 http://warksjammy.blogspot.co.uk/2017/04/blog-6-what-can-ucreate-with-microbit.html


Which created random bouncing Minecraft character triggered by a Micro:bit. This extends this by using two Micro:bits one remains connected to the Pi and listens for messages from the second Micro:bit which runs off batteries. If you press "a" on the battery run Micro:bit then it sends a message via the radio module. The Pi connected Micro:bit then receives the message and then sends the message to the Python script through the serial connection. In a nutshell its a radio controlled bouncing character. 



Here is a picture of the set up


Here is the updated version of the code the two Micro:bits:

from microbit import*
import random
import radio

radio.on()

REFRESH = 500

JumpStatus = ["Jump","Donotjump"]

def get_data():
    if button_a.is_pressed():
        a = random.choice(JumpStatus)
        radio.send(random.choice(JumpStatus))  # a-ha
   

def run():
    while True:
        sleep(REFRESH)
        get_data()
        # Read any incoming messages.
        incoming = radio.receive()
        if incoming == 'Jump':
            display.scroll("Jump")
            print('Jump')

        elif incoming == 'Donotjump':
            display.scroll("Do not jump")
            print('Donotjump')

#display.show('J')

run()


Code to go on Python on the Pi
Python 2.7 needs to be used

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

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()
    data_s = data.rstrip().split(" ")
    JumpStatus = data_s[0]
    print JumpStatus
    return JumpStatus

mc = Minecraft.create()
try:
    playerPos = mc.player.getTilePos()
    while True:
        JumpStatus = read_microbit_data()
        if JumpStatus == "Jump":
            pos = mc.player.getPos()
            #change this to teleport
            mc.player.setPos(random.randint(-100,100), random.randint(1,45),random.randint(-100,100))
            msg = JumpStatus
            mc.postToChat(msg)  
finally:
    sleep(1)
    s.close()


No comments:

Post a Comment