Skip to content

Instantly share code, notes, and snippets.

@bjpirt
Created February 10, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjpirt/9666d8c623cb98e755c92f1fbeeb6118 to your computer and use it in GitHub Desktop.
Save bjpirt/9666d8c623cb98e755c92f1fbeeb6118 to your computer and use it in GitHub Desktop.
Controlling the MeArm Pi from Minecraft
import os
import math
import pigpio
#import RPi.GPIO as GPIO
pi = pigpio.pi()
class Servo:
def __init__(self, config):
self.pin = config['pin']
self.min = config['min']
self.max = config['max']
self.minAngle = config['minAngle']
self.maxAngle = config['maxAngle']
def moveTo(self, angle):
self.moveToAngle(angle)
def moveBy(self, angle):
newAngle = self.currentAngle + angle
self.moveToAngle(newAngle)
def moveToCentre(self):
centre = self.minAngle + (self.maxAngle - self.minAngle)/2
self.moveToAngle(centre)
def moveToAngle(self, angle):
if angle > self.maxAngle:
angle = self.maxAngle
if angle < self.minAngle:
angle = self.minAngle
self.currentAngle = angle
self.updateServo()
def updateServo(self):
pulseWidth = math.floor(self.min + ((float(self.currentAngle - self.minAngle) / float(self.maxAngle - self.minAngle)) * (self.max - self.min)));
pi.set_servo_pulsewidth(self.pin, pulseWidth)
class MeArm:
def __init__(self):
self.base = Servo({'pin': 4, 'min': 530, 'max': 2400, 'minAngle': -90, 'maxAngle': 90});
self.lower = Servo({'pin': 17, 'min': 530, 'max': 1450, 'minAngle': 0, 'maxAngle': 90});
self.upper = Servo({'pin': 22, 'min': 530, 'max': 2000, 'minAngle': 0, 'maxAngle': 135});
self.grip = Servo({'pin': 10, 'min': 1400, 'max': 2400, 'minAngle': 0, 'maxAngle': 90});
def moveToCentres(self):
self.base.moveToCentre()
self.lower.moveToCentre()
self.upper.moveToCentre()
self.grip.moveToCentre()
import RPi.GPIO as GPIO
import mcpi.minecraft as minecraft
import mcpi.block as block
import mcpi.vec3 as vec3
from mearm import MeArm
mc = minecraft.Minecraft.create()
active = False
arm = MeArm()
arm.moveToCentres()
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
controls = []
oldBlocks = []
class ControlBlock:
def __init__(self, servo, amount, x, y, z, colour):
mc.setBlocks(x, y, z, x, y+3, z, block.WOOL.id, colour)
self.servo = servo
self.amount = amount
self.x = x
self.z = z
def trigger(self):
servo = getattr(arm, self.servo)
servo.moveBy(self.amount*6)
def buildBase(width=10):
print "Build Base"
w = width/2
pos = mc.player.getPos()
mc.setBlocks(pos.x-w, pos.y-1, pos.z-w, pos.x+w, pos.y-2, pos.z+w, block.IRON_BLOCK.id)
mc.setBlocks(pos.x-w, pos.y, pos.z-w, pos.x+w, pos.y+20, pos.z+w, block.AIR.id)
def buildControls():
print "Build Controls"
currentPos = mc.player.getPos()
x, y, z = currentPos
x = int(x)
y = int(y)
z = int(z)
controls.append(ControlBlock('base', -1, x+2, y, z+1, 1))
controls.append(ControlBlock('base', 1, x+2, y, z-1, 1))
mc.setBlock(x+2, y, z, block.TORCH.id)
controls.append(ControlBlock('lower', -1, x+1, y, z+2, 5))
controls.append(ControlBlock('lower', 1, x-1, y, z+2, 5))
mc.setBlock(x, y+1, z+2, block.TORCH.id)
controls.append(ControlBlock('upper', 1, x-2, y, z+1, 3))
controls.append(ControlBlock('upper', -1, x-2, y, z-1, 3))
mc.setBlock(x-2, y+2, z, block.TORCH.id)
controls.append(ControlBlock('grip', 1, x+1, y, z-2, 10))
controls.append(ControlBlock('grip', -1, x-1, y, z-2, 10))
mc.setBlock(x, y+3, z-2, block.TORCH.id)
def identifyBlock(pos):
x,y,z = pos
for control in controls:
if x == control.x and z == control.z:
return control
return None
def start():
print "Start controls"
buildBase()
buildControls()
def end():
print "End controls"
buildBase()
while True:
if not GPIO.input(5) and not active:
active = True
start()
if not GPIO.input(6) and active:
active = False
end()
#Get the block hit events
blockHits = mc.events.pollBlockHits()
# if a block has been hit
if blockHits and len(controls) > 0:
# for each block that has been hit
for blockHit in blockHits:
myblock = identifyBlock(blockHit.pos)
if myblock:
myblock.trigger()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment