help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "real" time inputs?


From: Lisa Thierbach
Subject: Re: "real" time inputs?
Date: Thu, 21 Jul 2011 20:26:20 -0400



On Jul 21, 2011, at 11:05 AM, Doug Stewart <address@hidden> wrote:



2011/7/21 Lisa Thierbach <address@hidden>
Thanks so much for your help. My Arduino board is now happily sending data into Octave.

Lisa

On Jul 18, 2011, at 4:33 PM, Jordi GutiƩrrez Hermoso <address@hidden> wrote:

> On 18 July 2011 12:44, Lisa Thierbach <address@hidden>
> wrote:
>> I'd like to read in data from the serial port (USB actually) on my
>> Mac. It is possible I could go through some other code that reads
>> the data and then passes it to Octave. But how do I pass data into
>> Octave from another source in real time?
>
> Depending exactly on how you read your data, one possibility is to use
> pipes:
>
>     http://www.gnu.org/software/octave/doc/interpreter/Controlling-Subprocesses.html#doc_002dpopen
>
> You can also use the sockets package, which can work with local
> sockets, not necessarily over a network:
>
>     http://octave.sourceforge.net/sockets/index.html
>
> Both of these mimic closely their standard Unix counterparts. You
> should consult documentation for pipes or sockets to understand more
> about how to use these.
>
> HTH,
> - Jordi G. H.
_______________________________________________
Help-octave mailing list
address@hidden
https://mailman.cae.wisc.edu/listinfo/help-octave

I was wondering how to do this--- So could you explain what you did?

Doug

Doug,

I'd love to, but a warning, it is long.


Here is what I did to get data from my Arduino board to talk to Octave. I'm sure there are better ways, but at least this works. I'd love to hear better ways to do this. I was a bit hampered in that I'm running things on an old Mac with an old operating system, so I can't install the latest Octave or the sockets package. The following I did quickly as a proof of concept. Now I need to get to work making everything do what I really wanted it to.

For the Arduino code, I just pulled Example 7 from:

I set up a simple circuit using a light sensitive resister. 

Then I used a program written in Processing (see processing.org). For this I pulled out what I wanted from Example 08A from the website above and put in a println.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import processing.serial.*;

Serial port;
int i;
String buffer = ""; // Accumulates characters coming from Arduino
int light = 0;  // light level measured by the lamp

void setup()
{
  println("in setup");

  String arduinoPort = Serial.list()[0];
  port = new Serial(this, arduinoPort, 9600); // connect to Arduino

}

void draw()
{

  if (port.available() > 0) { // check if there is data waiting
    int inByte = port.read(); // read one byte
    if (inByte != 10) { // if byte is not newline
      buffer = buffer + char(inByte); // just add it to the buffer
    }
    else {

      // newline reached, let's process the data
      if (buffer.length() > 1) { // make sure there is enough data

        // chop off the last character, it's a carriage return
        // (a carriage return is the character at the end of a
        // line of text)
        buffer = buffer.substring(0,buffer.length() -1);
 
        // turn the buffer from string into an integer number
        light = int(buffer);
        println(light);

        // clean the buffer for the next read cycle
        buffer = "";

        // We're likely falling behind in taking readings
        // from Arduino. So let's clear the backlog of
        // incoming sensor readings so the next reading is
        // up-to-date.
        port.clear();
      }
    }
  }
}

++++++++++++++++++++++++++++++++++++++++++++++++++++

Again, I didn't write the above code, so don't give me credit for it. It came from the makezine website. The processing gui has an option to make a stand alone application from the code, which I did. As a warning, the code produces a window containing a useless gray box. Someday, I intend for it to show the incoming data, but for now it is just a gray box. I then called this application from Octave and read in the results:

++++++++++++++++++++++++++++++++++++++++++++++++++++


skip = 50;

buffer = 1024;



fid = popen("./get_data/application.linux32/get_data", "r");


for(i = 1:skip)

    s = fgets(fid);

if(s == -1)

        fprintf(stdout, "Problem reading in data");

return

end

end


[d, count] = fscanf(fid, "%d", buffer);


plot(1:buffer, d)


return;

  


++++++++++++++++++++++++++++++++++++++++++++++++++++

Skipping some lines at the beginning is necessary because the processing application spits out some lines of text before the actual code begins to run. By far, the biggest issue I had was with the processing application in that the serial libraries were having conflicts with each other and not able to write into the directories they wanted to. I fixed the problem by downloading the binaries of the libraries here:


make sure to get version rxtx-2.1-7-bins-r2

Then installing it following the instructions:


For the issue with file permissions with the serial locks I had to give myself write permission to the directories:
/var/spool/uucp
/var/lock


I hope this helps, or gives someone an idea of where to go next. Once I get this code doing something more interesting, I can put it on my website.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]