How to save all data from sensor into textpad?

Digital Fiber, Photoelectric, Laser Range, Optical, Temperature, Rotary Encoder, Ultrasonic, Gas, Gyro, Accelerometer, FlexiBend, Flexiforce, Compass......

How to save all data from sensor into textpad?

Postby anwar » Sat Mar 20, 2010 2:33 pm

Hello.

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

}




Image
anwar
Greenhorn
 
Posts: 3
Joined: Sat Mar 20, 2010 2:09 pm

Re: How to save all data from sensor into textpad?

Postby waiweng83 » Mon Mar 22, 2010 10:11 am

hmm...I'm sorry but I dont really understand your question. What do you mean by "I would like to know how to save all this data at once like a database"? The sensor is returning the distance data at a point one by one, but how is it possible to save all at once? you need to save the points one by one also isn't it? Another thing is if you want to use Matlab to process the data, why do you need to save it to the file first? Why don't you straight away use the Matlab to grab the data from the serial port?
With the power of dream, nothing is impossible...
User avatar
waiweng83
Moderator
 
Posts: 171
Joined: Wed Apr 15, 2009 2:17 pm

Re: How to save all data from sensor into textpad?

Postby anwar » Tue Mar 23, 2010 2:51 pm

Thanx for the reply waiweng83.

Yes, the sensor is returning the distance data at a point one by one. If saving all the point at once is not possible, can you tell me how to save the point one by one, please..

The reasons I need to save it the file so that I will able to select certain points only from the whole points available. (For example point no 150 to point no 250, not the whole point which is from 0 to 681).

Right now I'm using this sensor with 2 USB port connectivity (port 14) which one USB port for data transfer and another USB port for power supply.
How to grab the data from serial port using MATLAB? If possible, please tell me how.

Thank you very much.
anwar
Greenhorn
 
Posts: 3
Joined: Sat Mar 20, 2010 2:09 pm

Re: How to save all data from sensor into textpad?

Postby waiweng83 » Wed Mar 24, 2010 10:59 am

I think for your application, you don't need to write to the file in order to select the data you want. As far as I know, you can directly use MATLAB to read the data from the virtual COM Port. But I got no idea how to do it cause I'm not familiar with MATLAB. Maybe you can ask our beloved Mr. Google :)

One of the problem if you write the data to a file and use MATLAB to read from that file is, the file cannot be read and written at the same time. When the software is writing to a file, that file is not accessible by other program. You need to close the file 1st, then only other program can read it.
With the power of dream, nothing is impossible...
User avatar
waiweng83
Moderator
 
Posts: 171
Joined: Wed Apr 15, 2009 2:17 pm

Re: How to save all data from sensor into textpad?

Postby anwar » Wed Mar 24, 2010 12:59 pm

I had asked my lecturer yesterday, and he told me that is possible to communicate to USB port using MATLAB.
but he told me the communication would be a lot easier if the data is transferred through serial port RS 232.

One of the problem if you write the data to a file and use MATLAB to read from that file is, the file cannot be read and written at the same time. When the software is writing to a file, that file is not accessible by other program. You need to close the file 1st, then only other program can read it.


can we access (read) the file then just save it first?


~im blur of choosing which way and method. and the expectation of this project is so damn high. how rookie like me can achieve it.. hmm..
anwar
Greenhorn
 
Posts: 3
Joined: Sat Mar 20, 2010 2:09 pm

Re: How to save all data from sensor into textpad?

Postby sich » Fri Mar 26, 2010 1:17 pm

From your C# program, you're accessing the sensor using virtual com port (as waiweng83 said). Although you're using USB cable to connect the sensor to your computer, it doesn't mean that you need to take care of USB protocol to access the sensor. Actually what the sensor appear in your computer is a virtual com port (practically same as serial port). So what you need to do is assume that you're transferring data through serial port and forget about the USB stuff. The sensor has help you to 'bypass' the USB part.

Then about getting data n saving it to a file or process it directly in MATLAB, do you mind to tell us more about the whole project? What's the application? Where will possibly the end product be applied? It's better for the forumers to have a clearer idea on what you want to achieve before giving their advice.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm


Return to Sensor

Who is online

Users browsing this forum: No registered users and 1 guest

cron