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

1 comment:

Bharat said...

Please provide me steps how to register the c language file in gac or do i have to create dll ?