The code for this project is included in the EZ-B SDK. It was written in C# and is a Windows Form application. It also contains the web server for remote control.
The Project is titled Robot Vision Tracking in the EZ-B SDK Sample.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EZ_B;
namespace Tutorial_11___Follow_Red_Object {
public partial class Form1 : Form {
EZCommunicator _ezb = new EZCommunicator();
public Form1() {
InitializeComponent();
comboBox1.Items.Clear();
foreach (DirectX.Capture.VideoCaptureDevice videoCaptureDevice in _ezb.Camera.GetVideoCaptureDeviceList())
comboBox1.Items.Add(videoCaptureDevice.Name);
comboBox2.Items.Clear();
comboBox2.Items.AddRange(_ezb.GetAvailableCommunicationPorts().ToArray());
comboBox3.Items.Clear();
foreach (Camera.ColorEnum item in Enum.GetValues(typeof(Camera.ColorEnum)))
comboBox3.Items.Add(item);
comboBox3.SelectedIndex = 0;
timer1.Interval = 200;
timer1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e) {
if (_ezb.IsConnected) {
_ezb.Disconnect();
} else {
if (comboBox2.SelectedItem == null)
_ezb.AutoConnect();
else
_ezb.Connect(comboBox2.SelectedItem.ToString());
}
if (_ezb.IsConnected) {
button1.Text = "Disconnect";
_ezb.Servo.SetServoPosition(Servo.ServoPortEnum.RB3, Servo.SERVO_CENTER);
} else {
button1.Text = "Connect";
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
string videoCaptureDeviceName = comboBox1.SelectedItem.ToString();
_ezb.Camera.StartCamera(_ezb.Camera.GetVideoCaptureDevicebyName(videoCaptureDeviceName), panel1, panel2);
}
private void button2_Click(object sender, EventArgs e) {
_ezb.ShowDebugWindow();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (_ezb.IsConnected)
_ezb.Servo.ReleaseAllServos();
_ezb.Camera.StopCamera();
}
private void timer1_Tick(object sender, EventArgs e) {
if (!_ezb.Camera.IsActive)
return;
Camera.ColorEnum color = (Camera.ColorEnum)comboBox3.SelectedItem;
ObjectLocation objectLocation = _ezb.Camera.GetObjectLocation(
color,
Camera.SearchObjectSize.Med,
(byte)trackBar1.Value);
if (!objectLocation.isFound)
return;
if (_ezb.IsConnected) {
int position = _ezb.Servo.GetServoPosition(Servo.ServoPortEnum.RB3);
if (objectLocation.horizontalLocation == ObjectLocation.HorizontalLocationEnum.Left)
position++;
else if (objectLocation.horizontalLocation == ObjectLocation.HorizontalLocationEnum.Right)
position--;
_ezb.Servo.SetServoPosition(Servo.ServoPortEnum.RB3, position);
}
string logMsg = string.Format("Object found at {0} {1} ({2}x{3})",
objectLocation.horizontalLocation,
objectLocation.verticalLocation,
objectLocation.FoundX,
objectLocation.FoundY);
tbLog.AppendText(logMsg);
tbLog.AppendText(Environment.NewLine);
}
}
}
|