lundi 8 août 2016

How To Make Your Own Robot Controlled With C#, Followed By A WebCam.

How To Make Your Own Robot Controlled With C#, Followed By A WebCam. 

Hello my dears followers, Like it is mentionned in the article title. Today i m going to demonstrate how we could make an intelligent robot, Controlled by your own C# application made in Visual Studio 2012, Followed by a WebCam.





To Make a full project like this one you have many choices to choose. 
The Hard part of this project is done using an Atmega 328p like a principal MCU. You could use any other MCU, For exemple A PIC like PIC16F877 is able also to replace the Atmega328p.
We know that the principal MCU based on the Arduino UNO is ATmega328 so You Could also use Arduino to get the robot working.

ATmega 328p pins 



   
Those Kind of project based essentially an serial communication. so you have just to use a MCU able to support UART.
There is a step in my project based in PWM so the MCU used have to support PWM also.
And for those how dont know what that means UART and PWM they have just to take a look here:

I choosed directly the ATmega 328p because this one is easier in programming and cheap in price. 
Résultat de recherche d'images pour "atmega328"
ATmega 328p


The robot move in 4 directions using a 4 simple DC motors, and those motors are controlled by 2 L293D.
The camera is just placed in the first servomotor. and this one is place in other one to get sure that the position where the camera is putted could  cover every point. 
Résultat de recherche d'images pour "servo motor web cam"
Placing the WebCam

I showed in this article programming ATmega like an Arduino how to program an ATmega32p like programming a simple arduino. this solution is used today also and this is the full program placed in the MCU memory to make the robot moving.



/*  Control and follow your robot using c# application and webcam 
 *  Project done by : Aymen Lachkhem
 *  Aymenlachkem@gmail.com
 *  Blog site : letselectronic.blogspot.com
 */
#include <Servo.h>
Servo myservo1;
Servo myservo2;
void setup() {
         pinMode(A0, OUTPUT);
         pinMode(A1, OUTPUT);
         pinMode(A3, OUTPUT);
         pinMode(A4, OUTPUT);
         pinMode(2, OUTPUT);
         pinMode(3, OUTPUT);
         pinMode(4, OUTPUT);
         pinMode(5, OUTPUT);
         pinMode(6, OUTPUT);
         pinMode(7, OUTPUT);         
         pinMode(11, OUTPUT);
         pinMode(12, OUTPUT);
         digitalWrite(A0,LOW);
         digitalWrite(A1,LOW);
         digitalWrite(A3,LOW);
         digitalWrite(A4,LOW);
         digitalWrite(2,LOW);
         digitalWrite(3,LOW);
         digitalWrite(4,LOW);
         digitalWrite(5,LOW);
         digitalWrite(6,LOW);
         digitalWrite(7,LOW);
         digitalWrite(11,LOW);
         digitalWrite(12,LOW);
           myservo1.attach(9);
           myservo2.attach(10);
            int i1 = 0;
            int i2 = 90;
         myservo1.write(i1);
         myservo2.write(i2);
}

void loop() {       
  blinker();
  Serial.begin(9600);  
   if (Serial.available() > 0) {
    char a = Serial.read();
if ( a == '1'){robot_left(); }  
if ( a == '2'){robot_up();}
if ( a == '3'){robot_right();}
if ( a == '4'){robot_down();}
if ( a == '5'){camera2_down();}
if ( a == '6'){camera1_up();}
if ( a == '7'){camera2_up();}
if ( a == '8'){camera1_down();}
}}
void blinker()
{ 
  digitalWrite(A3,HIGH);
  digitalWrite(A4,LOW);
  delay(100);
  digitalWrite(A3,LOW);
  digitalWrite(A4,HIGH);
  delay(100);
}  
void robot_left()
{
  digitalWrite(A1,HIGH);
  digitalWrite(A0,LOW);
  digitalWrite(2,HIGH);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  digitalWrite(6,LOW);
  digitalWrite(7,HIGH);
}
void robot_up()
{
  digitalWrite(A1,HIGH);
  digitalWrite(A0,LOW);
  digitalWrite(2,HIGH);
  digitalWrite(3,LOW);
  digitalWrite(4,HIGH);
  digitalWrite(5,LOW);
  digitalWrite(6,HIGH);
  digitalWrite(7,LOW);  
}  
void robot_right()
{
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  digitalWrite(3,HIGH);
  digitalWrite(2,LOW);
  digitalWrite(4,HIGH);
  digitalWrite(5,LOW);
  digitalWrite(6,HIGH);
  digitalWrite(7,LOW);  
}
void robot_down()
{
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  digitalWrite(3,HIGH);
  digitalWrite(2,LOW);
  digitalWrite(5,HIGH);
  digitalWrite(4,LOW);
  digitalWrite(7,HIGH);
  digitalWrite(6,LOW);  
}

void camera2_down()
{
  int i3 = myservo2.read();
  myservo2.write(i3 - 1);
}
void camera2_up()
{
  int i4 = myservo2.read();
  myservo2.write(i4 + 1);
}
void camera1_down()
{
  int i5 = myservo1.read();
  myservo1.write(i5 - 1);
}
void camera1_up()
{
  int i6 = myservo1.read();
  myservo1.write(i6 + 1);
}


   


This is The Complet Circuit that you have to draw.

Full Circuit




The Hard part of the project is just done so let's talk about the soft part.
We have here to build an Application based in serial communication able to connect directly the robot.
There is many solution to do this, for exemple Labview is one of the best interfacing system also Visual Basic or C#.
I  used C# to do this. A simple application in one only panel with a serial communication platform.
Many Buttons to control the robot and the camera.







How The Application Work ?

This is simple, We have just to add a serial platform to the app. This one will always ask for the port com and the baud rate. Since the moment your choose your serial parametres the fonctionnemnet of buttons starts. 
Every button you will click will send a code to the MCU and exactly like this that's working.

Résultat de recherche d'images pour "serial communication"



How the WebCam Works ?

In fact, there is a direct library you have to add to your visual studio named AFroge.net Direct Download. After adding this one to your visual studio you have to add this two reference to your project.






The rest is easy, you have just to give the application the permission to look for webcam devices plug in your computer, starts showing what she see, Capture button to save directly the view.

This is the full program written with C# in Visual Studio 2012.

/* Robot controlled by C# , Follewed by a Webcam
Letselectronic.blogspot.com
aymenlachkem@gmail.com
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using System.IO.Ports;
using System.Diagnostics;


namespace Camera_Project_Lachkhem
{
    public partial class Form1 : Form
    {
        VideoCaptureDevice capture;

        public Form1()
        {
            InitializeComponent();
            getAvailablePorts();


            FilterInfoCollection info = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (info != null)
            {
                capture = new VideoCaptureDevice(info[0].MonikerString);
                capture.NewFrame += (s, e) => pictureBox1.Image = (Bitmap)e.Frame.Clone();
                capture.Start();
            }
        }
        void getAvailablePorts()
        {
            string[] Ports = SerialPort.GetPortNames();
            portcom.Items.AddRange(Ports);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.Save("snapchot.png", System.Drawing.Imaging.ImageFormat.Png);
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (capture != null && capture.IsRunning)
            {
                capture.SignalToStop();
                capture = null;

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void connecter_Click(object sender, EventArgs e)
        {
            {
                try
                {
                    if (portcom.Text == "" || baudrate.Text == "")
                    {
                        textbox1.Text = "Please select port setting ? ";
                    }
                    else
                    {

                        serialPort1.PortName = portcom.Text;
                        serialPort1.BaudRate = Convert.ToInt32(baudrate.Text);
                        progressBar1.Value = 100;
                        textbox1.Text = "Connected";


                    }
                }
                catch (UnauthorizedAccessException)
                {
                    baudrate.Text = "Unauthorized Access";
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            progressBar1.Value = 0;
        }

        private void Contact_Me_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://https://letselectronic.blogspot.com/p/blog-page.html/");
            Process.Start(startInfo);
        }

        private void Visit_My_Blog_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://https://letselectronic.blogspot.com/p/blog-page.html/");
            Process.Start(startInfo);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            var myLines = new List<string>();

            myLines.Add("Hello, This Windows application give their Users the ability to control serially or with wirless communication a Robot Followed By a WebCam.");
            myLines.Add("- The MCU used is The high-performance Atmel 8-bit AVR RISC-based microcontroller ATmega 328 ");
            myLines.Add("- The Control Application was made in Visual Studio 2012, written using C# ");
            

            myLines.Add("----------------------------------------- Blog: Letselectronic.blogspot.com ----------------------------------------------------------------------");
            myLines.Add("----------------------------------------- Contact: Aymenlachkem@gmail.com ------------------------------------------------------------------");

            textBox2.Lines = myLines.ToArray();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("1");
            serialPort1.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("2");
            serialPort1.Close();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("3");
            serialPort1.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("4");
            serialPort1.Close();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("6");
            serialPort1.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("5");
            serialPort1.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("7");
            serialPort1.Close();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("8");
            serialPort1.Close();
        }

        private void turn_on_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("9");
            serialPort1.Close();
        }

        private void turn_off_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("a");
            serialPort1.Close();
        }
    }
}






Built your application, Get your file.exe and the Setup. Like always this is a video for more details.

I have all files of this project collected in compressed directory so just contact me to have your free files.



                                                                 See you Soon AYMEN LACHKHEM

   





Aucun commentaire:

Enregistrer un commentaire