Minecraft controlled Raspberry Pi Robot Arm

Guest Blogger Hiren Mistry, Nuffield Research Placement Student working at the University of Northampton. Nuffield Research Placement scheme provides students in their first year of a post16 course to work with STEM professionals http://www.nuffieldfoundation.org/nuffield-research-placements.

How to use Minecraft to move a USB Robot Arm on a Raspberry Pi
By Hiren Mistry
Currently, I am taking part in a Nuffield Foundation Research Placement at the University of Northampton, researching how to control a CBIS Robot Arm using a Raspberry Pi. This part of the project aims to learn more about using Python in Minecraft and how I can control the Robot Arm using Minecraft. When the player hits a specific block, the Robot Arm will move in a specific way.
Minecraft is an adventure game where players are put in a randomly-generated world and can create their own structures and contraptions out of textured cubes.
Here are the main commands used in Python for Minecraft Pi:
from mcpi.minecraft import Minecraft
Import modules from Minecraft folder
import minecraft.block as block
Imports Block Module
Used for creating a connection, modifying players and blocks and capturing events with the Minecraft World. This command uses the default address and port.
Return current position of player (as a float)
Post message in brackets to Minecraft Chat
Sets block (id) at position x, y, z
Moves player to position x, y, z
blockEvents = mc.events.pollBlockHits()
for blockEvent in blockEvents:
This returns any block event hits since the last time the command was run.
Returns the position of the block that was hit

Before developing the main program which controls the Robot Arm, I wrote some basic programs which enabled me to learn more about how Python interacts with Minecraft Pi. One of the programs I created leaves a trails of blocks behind the player. Below is the code with explanations for each line. It works by using a While loop and continually checks to know if the player has moved. If the player has moved then a block will be placed. The block number (38) can be changed. 

For a list of block numbers and more information, go to http://www.stuffaboutcode.com/p/minecraft-api-reference.html

import mcpi.minecraft as minecraft #imports minecraft modules
import mcpi.block as blocksw #imports block modules
import time #imports time module needed for time.sleep()
mc = minecraft.Minecraft.create()#creates connection to mc game
while True:#Starts while loop
    pos = mc.player.getTilePos()#Gets position of player
    time.sleep(0.1)#Sleeps for 0.1 seconds
    newpos = mc.player.getTilePos()#Gets new position of player
    if newpos != pos: #if the position has changed
        mc.setBlock(pos,38)#Set Block 38 down on previous block

Robot Arm Program in Minecraft
Whilst researching into Minecraft Pi I had the concept that the user would press a button in Minecraft and a Minecraft ‘Sign Post’ would allow the user to know what that Minecraft button does. However, due to Minecraft Pi limitations Buttons and Signposts (with text) do not exist so therefore they cannot be used. As a solution, I produced a model of the Robot Arm in Minecraft (See Picture below), which would enable the user to move specific parts of the Robot Arm.

The torches are the blocks which will activate the Robot Arm movements (Except for the Grip Open/Close and the Rotate Clockwise/Anticlockwise blocks).
Limitations: As only one block can be hit at once, only one command can be out put to the Robot Arm. Therefore, multiple commands are not available.
An easier way of storing data, such as the co-ordinates of each block, was to use a dictionary. A dictionary contains keys which are assigned to values. For example, Dictionary = {"Key1":Value1, "Key2":Value2}would store two keys where "Key1" is assigned to Value1 and "Key2" is assigned to Value2.
A more efficient way of storing the commands and co-ordinates would be to store the commands in another dictionary with the same key name. E.g.
Coordinates = {"Shoulder Up":[77, 67, 1], "Shoulder Down":[74,67,1]}
Commands = {"Shoulder Up":ShoulderUp(), "Shoulder Down":ShoulderDown()}
I could then search through all the keys and if the coordinates of the block hit equalled the value of a key, then the corresponding command would be executed. Using code such as,
For k, v in Coordinates:
If BlockHit == v:
However, this code causes the an error (ValueError: too many values to unpack) as the Python code has to go through all the keys and values until a match is made. Therefore, I have decided to continue with the simpler code which uses many IF statements to decide which command to use.

Final Program

Below is the code which allows the player to hit blocks which control the Robot Arm.
Once Minecraft is running, press "TAB" and run the Python code and then switch back to the Minecraft Window.
Note: When hitting blocks in Minecraft, the sword has to be used to activate commands.
import mcpi.minecraft as minecraft #Imports Minecraft Module
import mcpi.block as blocksw #Imports Block Module
import time
from Maincommands import * #Imports Commands from Maincommands.py file
mc = minecraft.Minecraft.create() # Creates connection to Minecraft
mc.postToChat("Robot Arm Control")#Post message to Minecraft chat
pos = mc.player.getTilePos()#Gets position of player

while True:
    blockHits = mc.events.pollBlockHits()#Get the block hit events
    if blockHits:# if a block has been hit
       for blockHit in blockHits:   # for each block that has been hit
            xyz = [blockHit.pos.x,blockHit.pos.y,blockHit.pos.z]
            Dict = {"Grip Open":[74,12,-70],"Grip Close":[72,12,-70], "Rotate AntiClockwise":[77,12,-63], "Rotate Clockwise":[77,12,-67],"Shoulder Up":[77, 13, -64], "Shoulder Down":[77, 13, -66], "Elbow Up":[77, 16, -64], "Elbow Down":[77, 16, -66], "Wrist Up":[77, 18, -64], "Wrist Down":[77, 18, -66], "Light On":[75, 19, -65], "Light Off":[76, 19, -65]}# Dictionary with Command and coordinates of block
            # Outputs command depending on block hit
            if xyz == Dict["Grip Open"]:
            elif xyz == Dict["Grip Close"]:
            elif xyz == Dict["Rotate Clockwise"]:
            elif xyz == Dict["Rotate AntiClockwise"]:
            elif xyz == Dict["Shoulder Up"]:
            elif xyz == Dict["Shoulder Down"]:
            elif xyz == Dict["Elbow Up"]:
            elif xyz == Dict["Elbow Down"]:
            elif xyz == Dict["Wrist Up"]:
            elif xyz == Dict["Wrist Down"]:
            elif xyz == Dict["Light On"]:
            elif xyz == Dict["Light Off"]:

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon