Showing posts with label robot arm. Show all posts
Showing posts with label robot arm. Show all posts

Sunday, 19 November 2017

Micro:bit Robot Arm

In this post, I am discussing using a recently bought  CBIS BBC micro:bit RobotArm, but play with it using Python (or rather micropython).




Set Up
Not a lot to set up really. The base and the arm are separate and are attached with four screws (so you will need a screwdriver). 

The most difficult bit is the wiring the arm to the circuit/breakout board on the base, but instructions are available through CBiS Resource portal http://portal.cbis.education/teacher/hardware. You will need a log-in for this. Also on that site, there is an example Microsoft Blocks code which includes some instructions on inserting the microbit as well - the micro:bit goes in buttons side facing upwards.


Code
Taking the values from the instruction sheet for setting it - the micro:bit key bit - the following pins were selected.
Base                     Pins 0 and 1
Shoulder               Pins 8 and 12
Elbow                   Pins 2 and 13
Wrist                    Pins 14 and 15
Gripper                 Pins 16 and 11 - this is the only one I haven't got working yet.

So to test it out, a simple bit of code to drive each motor in both directions was produced and is shown below.



This is good fun to play with. The use of the micro:bit is a good idea because it's ease of use to plug-in and play with.

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

Thursday, 10 August 2017

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:
Command
Explanation
from mcpi.minecraft import Minecraft
Import modules from Minecraft folder
import minecraft.block as block
Imports Block Module
minecraft.Minecraft.create()
Used for creating a connection, modifying players and blocks and capturing events with the Minecraft World. This command uses the default address and port.
mc.player.getPos()
Return current position of player (as a float)
mc.postToChat()
Post message in brackets to Minecraft Chat
mc.setBlock(x,y,z,id)
Sets block (id) at position x, y, z
.setPos(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.
BlockEvent.pos
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.
Dictionaries
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:
Commands[k]
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
print(pos)

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
            print("")
            xyz = [blockHit.pos.x,blockHit.pos.y,blockHit.pos.z]
            print(xyz)
            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"]:
                OpenGripper(0.5)
            elif xyz == Dict["Grip Close"]:
                CloseGripper(0.5)
            elif xyz == Dict["Rotate Clockwise"]:
                RotateBaseClockwise(0.5)
            elif xyz == Dict["Rotate AntiClockwise"]:
                RotateBaseAntiClockwise(0.5)
            elif xyz == Dict["Shoulder Up"]:
                ShoulderUp(0.5)
            elif xyz == Dict["Shoulder Down"]:
                ShoulderDown(0.5)
            elif xyz == Dict["Elbow Up"]:
                ElbowUp(0.5)
            elif xyz == Dict["Elbow Down"]:
                ElbowDown(0.5)
            elif xyz == Dict["Wrist Up"]:
                WristUp(0.5)
            elif xyz == Dict["Wrist Down"]:
                WristDown(0.5)
            elif xyz == Dict["Light On"]:
                GripLight(1,1)
            elif xyz == Dict["Light Off"]:
                GripLight(0,1)




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

Who wants to produce AI produced cartoon strips

Question: How easy is it produce a comic/cartoon using genetative AI? Let's start with  using ChatGPT4o to produce cartoons. The idea wa...