I am using sample code for URG-04LX (laser rangefinder) sensor.
using this sample code I was able to display the data from scans area.
the data is in image and represent by point from 0 up to 682.
in each of this points there is distance data.
For example at point no 150 the distance is 300mm, point no 450 distance is 2000mm, point no 23 the distance is 453 mm and so on. there's 682 point data were given by the sensor.
I would like to know how to save all this data at once like a database. so that I may use it with other program such as MATLAB or some other programs.
I had searched 'file processing' method and found no clue.
Please give some opinion how can I save all the data given by the sensor at once.
Below is the sample code I used:
- CODE: SELECT_ALL_CODE
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 System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
namespace hokuyo_URG04LX
{
public partial class Form1 : Form
{
UrgCtrl.UrgCtrl urgctrl = new UrgCtrl.UrgCtrl();
Graphics picbox;
//initialization
public Form1()
{
int i;
InitializeComponent();
//initialize comboBox1 items.
for (i = 1; i <= 99; i += 1)
{
comboBox1.Items.Add(i);
}
//initialize picbox
picbox = pictureBox1.CreateGraphics();
}
//enable checkbox checked
private void checkBox1_Click(object sender, EventArgs e)
{
//byte[] data;
if (urgctrl.IsConnected() == false)
{
MessageBox.Show("Please connect to a COM port.");
checkBox1.Checked = false;
return;
}
while (checkBox1.Checked == true)
{
Application.DoEvents();
if (urgctrl.IsConnected() == false)
{
MessageBox.Show("Please connect to a COM port.");
checkBox1.Checked = false;
return;
}
update_laser_range_finder_sensor();
Thread.Sleep(100);
}
picbox.Clear(Color.White);
}
//connect/disconnect button
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Connect")
{
if (comboBox1.SelectedIndex < 0)
{
MessageBox.Show("Please select a COM port.");
return;
}
if (urgctrl.Connect(comboBox1.SelectedIndex + 1, 19200) == false)
{
MessageBox.Show("Connection fail.");
return;
}
button1.Text = "Disconnect";
}
else
{
urgctrl.Disconnect();
button1.Text = "Connect";
}
}
// To update Laser Range Finder sensor value to picture box.
public void update_laser_range_finder_sensor()
{
int[] data = new int[urgctrl.MaxBufferSize];
int i;
Point[] output_point = new Point[682];
//capture data
urgctrl.Capture(data);
//calculate point
for (i = 0; i <= 681; i += 1)
{
output_point[i] = calculate_point((data[i] / 10), (urgctrl.Index2Radian(i + 44) * 180.0 / Math.PI));
}
//clear picbox
picbox.Clear(Color.White);
draw_outlines(Pens.Blue, output_point);
//starting line
draw_line_from_center(Pens.Black, 1000, (urgctrl.Index2Radian(44) * 180.0 / Math.PI));
//center line
draw_line_from_center(Pens.Gray, 1000, (urgctrl.Index2Radian(384) * 180.0 / Math.PI));
//ending line
draw_line_from_center(Pens.Black, 1000, (urgctrl.Index2Radian(725) * 180.0 / Math.PI));
//read data
textBox1.Text = Convert.ToString(trackBar1.Value);
textBox2.Text =Convert.ToString (data[trackBar1 .Value]);
}
// Draw outlines
public void draw_outlines(Pen pen, Point[] output_point)
{
picbox.DrawLines(pen, output_point);
}
// Draw a line from center of picbox
public void draw_line_from_center(Pen pen, int lenght, double angle)
{
Point center_point = new Point(300, 300);
picbox.DrawLine(pen, center_point, calculate_point(lenght, angle)); //draw line
}
// Calculate point base on lenght from center and the angle of the point.
public Point calculate_point(int lenght, double angle)
{
Point point = new Point();
angle = angle / 180 * (float)Math.PI; //convert to radian
point.X = Convert.ToInt16(300 + lenght * (float)Math.Sin(angle)); //convert to x and y point
point.Y = Convert.ToInt16(300 - lenght * (float)Math.Cos(angle));
return point;
}
}
}