Sunday 24 January 2016

Raspberry Pi gesture controlled Minecraft X-Wing (revisited)

figure 1
This post builds on two earlier posts and tries to address some of the very useful comments from people who have tried this. I hope this helps.

Overall the project builds on an earlier project to get a simple X-Wing into Minecraft on a Raspberry Pi.  The goal was get Python to build and move the X-Wing. Details of this project can be found here.

Main revision: In this project and the earlier one is based around Python 3 running the Raspbian 'Jessie' November version OS. Also additional libraries may need to be add to get the minecraftstuff (such as ShapeBlock() and MinecraftShape()). Details on how to obtain and install these can be found at  http://www.stuffaboutcode.com/2013/11/coding-shapes-in-minecraft.html .

In this post the additional of Pirmoroni's Skywriter is included to allow movements of a hand or a finger to enable the X-Wing to take-off, land, move forward or backward.

It builds on ideas from the book Adventures in Minecraft on using Python and Minecraft using a Raspberry Pi.


figure 2
The Skywriter  is a Raspberry Pi HAT (see figure 2) that allows positional information of the hand just above the board. In this project it is detecting flicks of the hand up, down, or across the board to determine the direction of motion.

Before you start, to use the Skywriter, in the terminal you need to add curl -sSL get.pimoroni.com/skywriter | bash

To start with we just placed the X-Wing above the player by placing blocks in the shape (roughly) of the X-Wing based around the method MinecraftShape (see Chapter 8 of Adventures in Minecraft ).



figure 3

  • Find the position of the player;
  • To avoid building on top the player the starting position of the X-Wing is set by:
    • add 5 to the x position of the player;
    • add 10 to the y position of the player(The bit I have to keep reminding myself is the y-axis is vertical.);
    • add 5 to the z position of the player;
  • Using these values build using, Wool blocks, the X-Wing - 0 for white, and 14 for red blocks;
  • If a flick starts at the top of the board (or "north") this moves the X-Wing down towards the ground;
  • If a flick starts at the bottom of the board (or "south") this moves the X-Wing vertically up;
  • If a flick starts on the right of the board (or "east") the X-Wing moves backwards horizontally;
  • if a flick starts on the left of the board (or "west") the X-Wing moves forward.
    from mcpi.minecraft import Minecraft
    from mcpi import block
    import mcpi.minecraftstuff as minecraftstuff
    import time
    import skywriter
    import signal

    mc=Minecraft.create()
    xPos=mc.player.getTilePos()
    xPos.x=xPos.x+5
    xPos.y=xPos.y+5
    xPos.z=xPos.z+5

    xWingBlocks=[
    minecraftstuff.ShapeBlock(0,0,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(-1,0,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(-2,0,0,block.WOOL.id,14),
    minecraftstuff.ShapeBlock(-3,0,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(1,0,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(0,1,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(1,1,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(2,0,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(2,1,0,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(1,2,-1,block.WOOL.id,14),
    minecraftstuff.ShapeBlock(1,2,1,block.WOOL.id,14),
    minecraftstuff.ShapeBlock(1,-1,-1,block.WOOL.id,14),
    minecraftstuff.ShapeBlock(1,-1,1,block.WOOL.id,14),
    minecraftstuff.ShapeBlock(1,3,-2,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(1,3,2,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(1,-2,-2,block.WOOL.id,0),
    minecraftstuff.ShapeBlock(1,-2,2,block.WOOL.id,0)]

    xWingShape=minecraftstuff.MinecraftShape(mc,xPos,xWingBlocks)

    @skywriter.flick()
    def flick(start,finish):
      if start=="south":
        for count in range(1,10):
          time.sleep(0.1)
          xWingShape.moveBy(0,1,0)
      if start=="west":
        for count in range(1,10):
          time.sleep(0.1)
          xWingShape.moveBy(-1,0,0)
      if start=="east":
        for count in range(1,10):
          time.sleep(0.1)
          xWingShape.moveBy(1,0,0)
      if start=="north":
        for count in range(1,10):
          time.sleep(0.1)
          xWingShape.moveBy(0,-1,0)

    signal.pause()




    For more details on Minecraft and Python I would suggest going to http://www.stuffaboutcode.com/2013/11/coding-shapes-in-minecraft.html especially on how to download the software to implement MinecraftShape. 


    If you do use or modify this please leave a comment, I would love to see what others do with it.







    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.

    Sunday 17 January 2016

    Raspberry Pi - Python Junkbot



    Junkbots (http://junkbots.blogspot.co.uk/) are based around using  materials such as drink's cans, broken propellors and motors to produce something that moves by vibration. Previous designs be found at:



    The latest tweak to the Raspberry Pi based Junkbot design is to use the combination of Python and Pimoroni's Explorer HAT PRO to control it. Explorer HAT Pro is a good choice, it can control two motors with a library provided to simplify the programming. For this the Junkbot was the one shown above: a drinks can, pen, LEGO bits, motor and broken propellor.

    Before the Explorer HAT can be used the library needs to be installed via the Terminal and the instructions below

    curl get.pimoroni.com/explorerhat | bash

    Python code to control the junkbot is shown below.


    import explorerhat
    from time import sleep

    def spin1(duration):
        explorerhat.motor.one.forward(100)
        sleep(duration)
        explorerhat.motor.one.stop()

    def spin2(duration):
        explorerhat.motor.one.backward(100)
        sleep(duration)
        explorerhat.motor.one.stop()

    spin1(1)
    spin2(1)

    Essentially the code spins the junkbot one way and then the other.





    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.

    Thursday 7 January 2016

    Robots behaving...

    Reblogged from: http://scott-ltattempts.blogspot.co.uk/2011/05/robot-behaviours.html


    Behaviour based robots was used in the teaching as way of getting the students to think out AI a little deeper and in particular Do we need Human Level intelligence? or rather Do we always need to aim for Human Level Intelligence?

    Lego Mindstorms robot are a good vehicle for students to start trying out idea around behaviour-based robotics. They are inexpensive, programmable and with the LeJOS software installed on them; have behaviours built into the programming which is done in Java.

    A good example to use comes from Bagnall's book (B Bagnall (2002) Core Lego Mindstorms: 

    Programming the RCX in Java , ISBN: 978-0130093646)


    code 1: HitWall

    //Taken from Bagnall (2002)
    import josx.robotics.*;
    import josx.platform.rcx.*;
    public class HitWall implements Behavior
    {
    public boolean takeControl()
    {
    return Sensor.S2.readBooleanValue();
    }
    public void suppress()
    {
    Motor.A.stop();
    Motor.C.stop();
    }
    public void action()
    {
    Motor.A.backward();
    Motor.C.backward();
    try{Thread.sleep(1000);}catch(Exception e){}
    Motor.A.stop();
    try{Thread.sleep(300);}catch(Exception e){}
    Motor.C.stop();
    }
    }

    Code 2: DriveForward

    //Taken from Bagnall (2002)

    import josx.robotics.*;
    import josx.platform.rcx.*;
    public class DriveForward implements Behavior
    {
    public boolean takeControl()
    {
    return true;
    }
    public void suppress()
    {
    Motor.A.stop();
    Motor.C.stop();
    }
    public void action()
    {
    Motor.A.forward();
    Motor.C.forward();
    }
    }



    Code 3: Bumper Car

    import josx.robotics.*;
    public class BumperCar
    {
    public static void main(String [] args)
    {
    Behavior b1=new DriveForward();
    Behavior b2=new HitWall();
    Behavior [] bArray ={b1,b2};
    Arbitrator arby=new Arbitrator(bArray);
    arby.start();
    }
    }

    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.

    Sunday 3 January 2016

    Raspberry Pi gesture controlled Minecraft X-Wing

    figure 1
    This post builds on an earlier project to get a simple X-Wing into Minecraft on a Raspberry Pi.  The goal was get Python to build and move the X-Wing. Details of this project can be found here.

    In this post the additional of Pirmoroni's Skywriter HAT included to allow movements of a hand to enable the X-Wing to take-off, land, move forward or backward.

    It builds on ideas from the book Adventures in Minecraft on using Python and Minecraft using a Raspberry Pi.


    figure 2
    The Skywriter is a Raspberry Pi HAT (see figure 2) that allows positional information of the hand just above the board. In this project it is detecting flicks of the hand up, down, or across the board to determine the direction of motion.

    Before you start, to use the Skywriter, in the terminal you need to add curl -sSL get.pimoroni.com/skywriter | bash

    To start with we just placed the X-Wing above the player by placing blocks in the shape (roughly) of the X-Wing based around the method MinecraftShape (see Chapter 8 of Adventures in Minecraft ).



    figure 3
    • Find the position of the player;
    • To avoid building on top the player the starting position of the X-Wing is set by:
      • add 5 to the x position of the player;
      • add 10 to the y position of the player(The bit I have to keep reminding myself is the y-axis is vertical.);
      • add 5 to the z position of the player;
    • Using these values build using, Wool blocks, the X-Wing - 0 for white, and 14 for red blocks;
    • If a flick starts at the top of the board (or "north") this moves the X-Wing down towards the ground;
    • If a flick starts at the bottom of the board (or "south") this moves the X-Wing vertically up;
    • If a flick starts on the right of the board (or "east") the X-Wing moves backwards horizontally;
    • if a flick starts on the left of the board (or "west") the X-Wing moves forward.
      from mcpi.minecraft import Minecraft
      from mcpi import block
      import mcpi.minecraftstuff as minecraftstuff
      import time
      import skywriter
      import signal

      mc=Minecraft.create()
      xPos=mc.player.getTilePos()
      xPos.x=xPos.x+5
      xPos.y=xPos.y+5
      xPos.z=xPos.z+5

      xWingBlocks=[
      minecraftstuff.ShapeBlock(0,0,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(-1,0,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(-2,0,0,block.WOOL.id,14),
      minecraftstuff.ShapeBlock(-3,0,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(1,0,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(0,1,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(1,1,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(2,0,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(2,1,0,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(1,2,-1,block.WOOL.id,14),
      minecraftstuff.ShapeBlock(1,2,1,block.WOOL.id,14),
      minecraftstuff.ShapeBlock(1,-1,-1,block.WOOL.id,14),
      minecraftstuff.ShapeBlock(1,-1,1,block.WOOL.id,14),
      minecraftstuff.ShapeBlock(1,3,-2,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(1,3,2,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(1,-2,-2,block.WOOL.id,0),
      minecraftstuff.ShapeBlock(1,-2,2,block.WOOL.id,0)]

      xWingShape=minecraftstuff.MinecraftShape(mc,xPos,xWingBlocks)

      @skywriter.flick()
      def flick(start,finish):
        if start=="south":
          for count in range(1,10):
            time.sleep(0.1)
            xWingShape.moveBy(0,1,0)
        if start=="west":
          for count in range(1,10):
            time.sleep(0.1)
            xWingShape.moveBy(-1,0,0)
        if start=="east":
          for count in range(1,10):
            time.sleep(0.1)
            xWingShape.moveBy(1,0,0)
        if start=="north":
          for count in range(1,10):
            time.sleep(0.1)
            xWingShape.moveBy(0,-1,0)
      signal.pause()




      For more details on Minecraft and Python I would suggest going to http://www.stuffaboutcode.com/2013/11/coding-shapes-in-minecraft.html especially on how to download the software to implement MinecraftShape. 
      If you do use or modify please leave a comment, I would love to see what others do with it.






      All views are those of the author and should not be seen as the views of any organisation the author is associated with.


      Wednesday 30 December 2015

      Daleks, cameras, and a mutant rabbit.

      A little more detail on my experience of PiCademy and some of the code developed (and I apologies it is not well developed).

      Programming LEDs and Motors through either the the GPIO or using an HAT (see the images below) is just what I enjoy the most.

      To have a go, you may have to have the following:




      In the above image was my attempt at a simple 'Dalek' - essentially a cup and straw, with a wheeled motor inside. Controlled using python,  Pi through an Explorer HAT PRO . It essentially moved in a circle either clockwise (button 1 on the explorehat) or anti-clockwise (button 2).


      import explorerhat
      from time import sleep
      from random import randint

      def wheel(channel, event):
          duration = randint(1,2)
          print(duration)
          explorerhat.motor.one.forward(100)
          sleep(duration)
          explorerhat.motor.one.stop()

      def wheel2(channel, event):
          duration = randint(1,2)
          print(duration)
          explorerhat.motor.one.backward(100)
          sleep(duration)
          explorerhat.motor.one.stop()
          

      explorerhat.touch.one.pressed(wheel)
      explorerhat.touch.two.pressed(wheel2)

      It needs a lot more work, not least of which is a moving head under seperate motor control but it is a start.


      Playing with the PiCamera and a button attached to the GPIO, I came up with a simple system that everytime the button is pressed a image is captured this was based on the activities and worksheets at PiCademy. The extra was the tweak concerning providing a different filename each time. Essentially:

      •  create a string with most of the filename and path ('/home/pi/Desktop/image'); 
      • include a count of how many pictures have been taken and convert that to a string (str(count)); 
      • add the file extension ('.jpg');
      • combine them and use them as the filename.
            str1='/home/pi/Desktop/image'+str(count)+'.jpg
            camera.capture(str1)


      The whole code is shown here.


      from time import sleep
      from picamera import PiCamera
      from gpiozero import Button

      camera = PiCamera()
      button = Button(17)
      str1=[]
      count=1

      while True:
          camera.start_preview(alpha=192)
          button.wait_for_press()
          str1='/home/pi/Desktop/image'+str(count)+'.jpg'
          camera.capture(str1)
          count=count+1
          camera.stop_preview()


      Rise of Rabbitsapien - A team of us put together a project of a robot with a rabbit (no other soft toys were available) with a Passive IR sensor in its belly; that carries out a set routine when movement is detected.




      It was also great to come away with some many resources both physical and activities. Thank you to the Pi Foundation for such a good experience.



      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.

      Saturday 26 December 2015

      BB-8, Droid I was looking for... - Tynkering

      In a recent post controlling the Sphero BB-8 with the Tickle App was discussed.

      This is not the only alternative software, the Tynker App can also control it.


      This is also a graphical drag and drop programming tool, that you can connect certain 'toys' to. Though the App itself is about developing programming skills.










      The Sphero BB-8 Droid can be connected to Tynker (or how I did it anyway) by:

          • Clicking on the Create button on the opening screen;
          • Clicking on Blank Template;
          • Deleting the 'Actor' that has there and clicking on the + button in the top right hand corner of the screen;
          • Clicking on connected toys and selecting the grey ball;
          • On the main screen it should say spherobot with a code button at the side, click on the button;
          • You should get a screen with some code for changing the colour shown and then moving in a square- you can change this for your own code.
      Not all the commands, listed down the side, will work with the BB-8 - I restricted myself to the ones under common (star in the list).

      The Tynker app is a nice tool anyway with lots of games related activities to try. Having the ability to connect and program certain devices is a benefit.


      If you have comments or experiences with Tynker, Sphero BB-8 or Tickle app please add them.





      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.

      Friday 25 December 2015

      BB-8, this is the Droid I was looking for...




      Previously I have shown the Sphero BB-8 rolling around the room under its own control

      One of the features of the Sphero BB-8 Droid  is it programmable either by its own downloadable software but also by one of my favourite apps - TickleApp which has been discussed in previous posts. This app allows control of a quite an impressive range of devices using the same interface. Examples, some of which have been discussed previously (e.g Parrot Minidrone or Dash and Dot), are shown below. 






















































      The App uses an graphical programming interface similar to Scratch or Blockly to produce code. The example here it a very simple one of:

      • Spin twice for one second;
      • Move roughly in a square;
      • If the BB-8 collides (or is bumped) it is set to spin twice for a second.  

      Ok, not the most sophisticated bit of coding; but it does demonstrates the simplicity of controlling this robot with the app. 

      Sphero BB-8 Droid is great fun, and with the head appearing to float over the body and face in the direction of movement it is hard resist. The video below shows it action.





       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.

      Monday 21 December 2015

      Playing with Aldebaran's NAO - walking and talking.

      Ok, I need to read the manual! Managed today to play with Aldebaran NAO again and was struggling to get it to interact - this is the should have read the manual bit, it was all in there.


      • Mistake number 1 - I hadn't set a channel for all the apps so it was reacting to sounds and movement but not much more. So I set it.
      • Mistake number 2 - not understanding the meaning of the changes in the colour of the eyes, when the eyes go blue NAO is listening.


      Now  it does what I was after - to be lead by the hand using the follow me app and react to some vocal commands. The video below shows "Red" in action.



      I would be interest in others experiences with these robots, if you would like please add your comments below.


      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.

      Thursday 17 December 2015

      Aldebaran NAO 'Red' in Teaching

      Photo by John Sinclair

      I had my first opportunity today to try an Aldebaran NAO robot as a teaching tool in an AI class today. The session was an end of term activity around summarising what we did in the AI class so far and questions. 

      A question came up around AI and it's impact on society. Perfect opportunity to bring in a social robot - especially as a precursor for when we include a session on social robotics next term.


      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.

      Sunday 13 December 2015

      Playing with Aldebaran NAO

      This is just a short post, as well as being able to go to Picademy this week (http://robotsandphysicalcomputing.blogspot.co.uk/2015/12/picademy-7-8th-december-2015.html); I have been fortunate to be able to borrow an Aldebaran NAO robot (https://www.aldebaran.com/en) for the weekend to play with.




      This is an extermly cool robot, straight out of the box, tracking movement and dynamic balancing. Hopefully, more on this in future posts.

      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.

      Saturday 12 December 2015

      Experience at Raspberry Pi Cademy 7-8th December 2015

      I was fortunate to get a place at Picademy (#picademy) this week. It was a fantastic opportunity and great fun, especially as most of it was about Physical computing.





      In the screenshot above I was playing SonicPi (http://sonic-pi.net/) programming music (or trying to create music in my case). If you haven't had a go at throughly recommend it. It is great that SonicPi is available on the Mac and PC as well. 

      Playing with connecting Python and Minecraft is very engaging and fun, but programming LEDs and Motors through either the the GPIO or using an HAT (see the images below) is just what I enjoy the most.


      In the above image was my attempt at a simple 'Dalek' - essentially a cup and straw, with a wheeled motor inside. Controlled using python,  Pi through an ExplorerHat. It essentially moved in a circle either clockwise or anti-clockwise.

      Rise of Rabbitsapien - A team of us put together a project of a robot with a rabbit (no other soft toys were available) with a Passive IR sensor in its belly; that carries out a set routine when movement is detected.




      It was also great to come away with some many resources both physical and activities. Thank you to the Pi Foundation for such a good experience.




      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.

      Tuesday 1 December 2015

      Controlling junk with LEGO

      Up to this point the junk bot building has largely being about building a moving (or drawing) 'bot' moved by vibration - limited control, but fun. A Nuffield funded bursary student, Hayden Tetley,  has being working within staff from the University of Northampton on whether LEGO 8547: Mindstorms NXT 2.0: Robot or Raspberry Pi based solutions can be incorporated with the bot to add some control of the movement (still by vibration).


      Idea One 

      Is to add a LEGO NXT brick, to move a junkbot similar.The motor and broken propeller combination in the earlier junkbots is replaced with the NXT brick and LEGO motor. A good potential feature is it a self-contained unit with power and control together, as well as being potentially fairly simple to set-up. This is the focus of this post. 

      Here are some videos showing idea one in action using LEGO motors, brick and the software that comes with the LEGO 8547: Mindstorms NXT 2.0: Robot :





      For more information on how this was done go to: http://legojunkbots.weebly.com/uploads/3/7/2/2/37227791/nuffield_nxt_mindstorms.docx or http://legojunkbots.weebly.com/

      Idea Two

      Is to do a similar approach as idea one but keep the motor and broken propeller combination but control the motors via a Raspberry Pi. This is discussed in another post http://robotsandphysicalcomputing.blogspot.co.uk/2015/07/raspberry-pi-controlled-robot-from-junk.html

      Details of the work will be published on the Junkbots Blog (htttp://junkbots.blogspot.co.uk/ ) as the project progresses.




      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.

      Top posts on this blog in March 2024

      The Top 10 viewed post on this blog in March 2024. Covering areas such as small robots, augmented reality, Scratch programming, robots. Micr...