Thursday, 25 October 2012

RASPBERRY PI STEPPER MOTOR USING C# AND MONO.


With the aid of the libpigpio shared C library from David's blog and the mono runtime I spent a morning messing around my Pi and Stepper Motor running using c#. Using the libpiggio and the c# wrapper (also by David) it is makes controlling the GPIO pins for output easy. I found I had to compile the library on the pi, copying the provided .so file did not seem to work but the instructions are provided in the libpigpio.h file so it is not to much of an upheaval, you need to have gcc installed though. Once that was working and tested with one of the examples provided, I put together a class which lets me easily control the motor. The resulting code is shown below:


using System;
using LibPiGpio;
using System.Collections;
using System.Threading;

namespace ConsoleApplication1
{
    class Stepper
    {
        private int pina=0;
        private  int pinb=0;
        private  int pinc=0;
        private  int pind = 0;
        private  string[] graycode=new string[8]{"1000","1100","0100","0110","0010","0011","0001,","1001"};
        private string[] reversegraycode = new string[8] { "1001", "0001", "0011", "0010", "0110", "0100", "1100,", "1000" };
       public Stepper(int setpina, int setpinb, int setpinc, int setpind)
        {
            pina = setpina;
            pinb = setpinb;
            pinc = setpinc;
            pind = setpind;
            RpiGpio.SetOutputPins(new[] { setpina, setpinb, setpinc, setpind });
            RpiGpio.Pins[pina] = false;
            RpiGpio.Pins[pinb] = false;
            RpiGpio.Pins[pinc] = false;
            RpiGpio.Pins[pind] = false;
           

           
       }

        public void forward(int delay)
        {

            foreach (String value in graycode)
            {

                setPin(pina, value[0]);
                setPin(pinb, value[1]);
                setPin(pinc, value[2]);
                setPin(pind, value[3]);


                Thread.Sleep(delay);

            }
        }

        public void reverse(int delay)
        {
            foreach (String value in reversegraycode)
            {

                setPin(pina, value[0]);
                setPin(pinb, value[1]);
                setPin(pinc, value[2]);
                setPin(pind, value[3]);


                Thread.Sleep(delay);

            }
        }

        private void setPin(int pin, char value)
        {
            if (value == '0')
            {
                RpiGpio.Pins[pin] = false;

            }
            else
            {
                RpiGpio.Pins[pin] = true;

            }

        }


    }
}
The below code shows Stepper.cs in use.

using System;
using LibPiGpio;
using System.Threading;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            // setup the stepper motor and specify which GPIO pins to use.
            Stepper motor = new Stepper(24, 25, 8, 7);

            Console.WriteLine("Stepper Motor stepup complete");
            Console.WriteLine("Press up or Down Arrow keys to drive motor forwards or reverse.");
           
            // start checking for key presses
            ConsoleKeyInfo keyinfo;
            do
            {
                keyinfo = Console.ReadKey();
                

                if (keyinfo.Key.ToString()=="UpArrow"){
                    motor.forward(1);
                
                };

                if (keyinfo.Key.ToString() == "DownArrow")
                {
                    motor.reverse(1);

                };
            }
            while (keyinfo.Key != ConsoleKey.X);
         
        }
    
    
    }


}

The Visual Studio c# Project can be downloaded from https://github.com/qubecad/Csharp.git

Tuesday, 23 October 2012

Raspberry Pi Stepper Motor Control using Java.

I came across the Pi4J Project today. The project is working on a library to allow Pi programming from Java. It is not at version 1 yet so still quiet new but the library lets you access the GPIO port on your Pi and join in some of the fun I thought you needed to use python for.

My Pi is currently connected to a stepper motor so I thought I would have ago at getting it to run from java,the below code is the result. Once I had the pi setup and proved the java setup was working correctly I used Eclipse on my Windows PC to write my code/compile and WinSCP to transfer it to the Pi. 

The Pi4J project have a number of examples on their site and their control example (http://pi4j.com/example/control.html) was the starting point for my code. 

I am using a 28BYJ-48  stepper motor and ULN 2003 driver board which I picked up from ebay awhile ago.

Update: The latest version of Pi4J now includes a dedicated stepper motor class and example code.

The eclipse project can be download from: http://github.com/qubecad/hmm-pi.git


package com.qubecad.pi.stepper;

import java.util.HashMap;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

public class PiStepper {

 /**
  * 
  * Driving a Stepper Motor From Java 
  * 
  * @param args
  * @throws InterruptedException 
  */
 public static void main(String[] args) throws InterruptedException {


  // create gpio controller
  GpioController gpio = GpioFactory.getInstance();

  // Set up the pins and set low to start
  System.out.print("Setting up GPIO Pins for ouput");
  GpioPinDigitalOutput pina = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_05, "Pin A", PinState.LOW);
  GpioPinDigitalOutput pinb = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_06, "Pin B", PinState.LOW);
  GpioPinDigitalOutput pinc = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_10, "Pin C", PinState.LOW);
  GpioPinDigitalOutput pind = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_11, "Pin D", PinState.LOW);


  HashMap driveLogic=new HashMap();

  driveLogic.put(0, "1001");
  driveLogic.put(1, "0001");
  driveLogic.put(2, "0011");
  driveLogic.put(3, "0010");
  driveLogic.put(4, "0110");
  driveLogic.put(5, "0100");
  driveLogic.put(6, "1100");
  driveLogic.put(7, "1000");

  System.out.print("Driving motor");
  while (true) {
   for (int i = 1; i < driveLogic.size(); i++) {

    String grayCode = (String) driveLogic.get(i);
    setPin(pina,grayCode.charAt(0));
    setPin(pinb,grayCode.charAt(1));
    setPin(pinc,grayCode.charAt(2));
    setPin(pind,grayCode.charAt(3));

    Thread.sleep(10);
   }
  }

 }

 /**
  * 
  * Sets the passed pin Low or High depending on the passed value.
  * 
  * @param pin
  * @param value
  */
 private static void setPin(GpioPinDigitalOutput pin,char value){
  if (value == '0') {

   pin.low();
  } else {

   pin.high();
  }
 }

}

Using the Raspberry Pi remotely from a Windows PC.


Having a Raspberry pi and desktop/Laptop PC can lead to a desktop covered in keyboards and mice. To reduce this (and stop confusion over which mouse or keyboard you should be using)  you can access the pi remotely using putty and a X server. 

This guide assumes you have used the standard Raspbian image from (http://www.raspberrypi.org) which already has ssh configured. 


Install Putty (http://www.chiark.greenend.org.uk/~sgtatham/putty/) 

and 

Xming (http://sourceforge.net/projects/xming/) on your windows PC

Start Xming by launching XLaunch option from start menu 



Set the display settings to 'one window', if your select multiple each program you launch on your pi with run in it's own window. Make a note of the display number you will need this later.



Leave as default and press 'Next'.



  Leave as default and press 'Next'.



Save your configuration and press 'Finish'.



Start putty and configure a session to connect via ssh on port 22 using the ip address of your pi.



under connections/SSH/X11 tick the "Enable x11 forwarding" and set the X display location to
 localhost: (in this case 0 noted earlier).



Click on the session option and save your configuration.

Click open to connect to your Raspberry pi and log in. You now have a terminal access on your pi and use it as you would the LxTerminal.



Once logged in you can either start individual applications or a complete desktop session.

To use IDLE type idle

 or to start a full desktop session enter startlxde





Monday, 28 February 2011

Mobile Development

At Christmas I released my Droid 2 CAD app on Android Marketplace, this has now had nearly 5000 downloads and clocked up some 16,000 user sessions. The app is not a game but a GPS point recording tool. But for someone who only dabbles in software development the opportunities the current mobile app trend offers are amazing, 5000 downloads may not be much compared to apps like angry birds but I had an idea, I coded and published it and other people actually wanted to download and try it. I released my app for free with no adverts, next time I may looking into this more but when I released it I didn't think anyone would be interested it and I would be adding to the bloat in the marketplace.


One highlight this year so far has has been having to use Google translate to be able to read the feedback for the app because people i can not even speak to liked my app enough to leave positive feedback. That is something that I thought would never happen.

I am now not sure what to try next with the release of Android Honeycomb means a whole new array of devices is just waiting to be developed on.

And it is not just limited to mobile phone development, technologies like Microsoft's XNA studio and Xbox Live Indie Games are allowing homegrown game writers to create, publish and get paid for their own games both on the XBox 360 and Windows phone 7 (Check out Team Mango at www.team-mango.com ).


If you have an idea and are interested these are good times.



(This started out as a comment on the Guardian Tech Blog )
Map screen taken from collected anonymous data using www.flurry.com

Sunday, 16 January 2011

Progress on the VR player

Still progressing the radio player app, now revised the playing media info and added a read only twitter feed.
Thinking of looking into tabs but not sure how that will work on small sized screens.

Wednesday, 12 January 2011

New android project: the Jode player

just a quick post to show the small project that i have been using to try and improve my android screen layouts.
vectisradio.com is my local web radio station so what better than to try and use android 2.2s new streaming media player to create a small radio player.



I am in no way linked to vectis radio in anyway.

Tuesday, 28 December 2010

adding google bookmarks from ipad/iphone safari

Just worked this out based on the tweet this options in tweetings for tweeting from inside safari.

if you create a new bookmark to any page then edit the address of the bookmark to contain the following:

javascript:window.location='http:///www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+window.location+'&title='+document.title

change the title of the bookmark to something like "add to google bookmarks"

now when you want to add a page to google bookmarks you can just press the new bookmark and you will be presented with the add a google bookmark form, complete this and press add and you are done.

press back a few times to get back to the page you were viewing.

update: just found a similar way of doing it that was already posted on a forum which works better, see it here