how to make your own robot controlled with c followed by a web cam

11
How To Make Your Own Robot Controlled With C#, Followed By A WebCam. letselectronic.blogspot.com /2016/08/how-to-make-your-own-robot-controlled.html 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. 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: Pulse With Modulation UART 1/11

Upload: aymen-lachkhem

Post on 15-Feb-2017

20 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: how to make your own robot controlled with c followed by a web cam

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

letselectronic.blogspot.com /2016/08/how-to-make-your-own-robot-controlled.html

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 couldmake an intelligent robot, Controlled by your own C# application made in Visual Studio 2012, Followed by aWebCam.

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 getthe robot working.

Those Kind of project based essentially an serial communication. so you have just to use a MCU able to supportUART.

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:

Pulse With ModulationUART

1/11

Page 2: how to make your own robot controlled with c followed by a web cam

ATmega 328p pins

ATmega 328p

I choosed directly the ATmega 328p because this one is easier in programming and cheap in price.

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 positionwhere the camera is putted could cover every point.

I showed in this article programming ATmega like an Arduino how to program an ATmega32p like programming asimple arduino. this solution is used todayalso and this is the full program placed inthe MCU memory to make the robotmoving.

/* Control and follow your robot using c# application and webcam * Project done by : Aymen Lachkhem * [email protected] * Blog site : letselectronic.blogspot.com 2/11

Page 3: how to make your own robot controlled with c followed by a web cam

Placing the WebCam

* 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()

3/11

Page 4: how to make your own robot controlled with c followed by a web cam

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(){

4/11

Page 5: how to make your own robot controlled with c followed by a web cam

Full Circuit

{ int i6 = myservo1.read(); myservo1.write(i6 + 1);}

This is The Complet Circuit that you have to draw.

TheHardpartofthe

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 orC#.

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.

5/11

Page 6: how to make your own robot controlled with c followed by a web cam

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 thebaud 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.

How the WebCam Works ?

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

6/11

Page 7: how to make your own robot controlled with c followed by a web cam

The rest is easy, you have just to give the application the permission to look for webcam devices plug in yourcomputer, 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 [email protected]*/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();

7/11

Page 8: how to make your own robot controlled with c followed by a web cam

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);

8/11

Page 9: how to make your own robot controlled with c followed by a web cam

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: [email protected] ------------------------------------------------------------------");

textBox2.Lines = myLines.ToArray(); }

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

9/11

Page 10: how to make your own robot controlled with c followed by a web cam

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();

10/11

Page 11: how to make your own robot controlled with c followed by a web cam

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

11/11