arduino and matlab

Hello,

I'm creating a robot that can take pictures and analyse strawberries.
Taking a picture and the analysing is written in matlab and it works.
The next step is to open the ".exe file of matlab" with the arduino mega 2560.
I have no idea how to start with that?
Can somebody help me?
Controlling (turn on/off) LED's with matlab is easy, you can find it on the link below.

But I have to work reversed.
Arduino have to call the matlab file and depending on the result of the analysis of the picture in matlab,
will Arduino give some other orders to the engines of the robot.
So concrete:
Arduino have to call the matlab file that I have created.

You can go about this several different ways. You can have your matlab program always running and send data to the arduino, or you can have a secondary program running, any language works I like java because its free and fairly easy, that takes commands from the arduino and starts the matlab program. Then your matlab code does its thing and sends the data to your com port.

s=serial('com1');
fopen(s);
fwrite(s,'12345');
fclose(s);

Thanks laadams85.

I have found how to make a .exe file of a matlab program.
So the goal of my project is to run the .exe file (who is written in matlab) when arduino says "run .exe file".
The first question is:
Can I say to arduino run the .exe file of matlab and how does it works?
(Hopefully it can)

When arduino is capable to start a .exe, the matlab code for detecting a strawberry have to begin running.
When matlab has detected a strawberry, then I want to give a high bit on a digital pin on arduino.
The second question is:
Can matlab gives a signal to give a high bit on arduino?

Thanks

The arduino can't operate on your computer per se, this would be a major security issue. So the first thing to do is connect your arduino to your computer. The easiest way to do this is to use the usb connection. This will show up as a 'com' port on your computer. When you use serial.print() on the arduino, it will print to the 'com' port on your computer.
Now for this to be useful something needs to be listening (polling) that 'com' port on your computer. This can be pretty much anything, but like I said, I like using java. This program will wait for the execute command from your ardiuno. Once it receives the command it will start up your matlab routine and wait for the matlab output. It will then send this output back to the arduino. From their the arduino can do what it needs to do.

Anther way to do this is to modify your matlab code to always be running. Then your matlab code handles listening to the serial port and writing to the serial port. To me this would be the better implementation and would be less error prone.

If your PC is running Microsoft Windows then you can use Gobetwino to run commands on the PC and read/write files on the PC. That would enable you to get a working solution without having to write your own PC application. Just one caveat - Gobetwino doesn't seem to like being run for extended periods in my experience. If this is something that you expect to start and leave running for days, you might be better off looking for other options. I guess you plan to analyse a sequence of items and have the Arduino present them for imaging and then dispose of them. In that case I think I'd prefer to put the main control logic in a custom PC application which uses the Arduino to operate servos/motors and read position sensors, invokes matlab (or whatever else) to do your image analysis and (somehow) get the results of that analysis. This approach would involve writing more code, but would enable much more complex behaviour such as recording the images and the results of your analysis.

There was a bug in GoBetwino that showed its ugly face when running the program for extended time. This has been fixd in the version you can download now.

It was a stupid error with the two textareas in the main window that would end up holding so much text data that it made GoBetwino (and sometimes Windows) run extreamly slow. In the current version this has been fixed.

I want to start with your second idea to communicate between matlab and arduino.
How Can I modify that my matlab code always runs?
And what's then the code for arduino to make the communication?

Thanks al lot

laadams85:
Anther way to do this is to modify your matlab code to always be running. Then your matlab code handles listening to the serial port and writing to the serial port. To me this would be the better implementation and would be less error prone.

I have already downloaded Gobetwino, but it's look complicated.
Up to now,I haven't find something about running a .exe file with Gobetwino.
Is it possible? And how to start with that?
Thanks a lot

PeterH:
If your PC is running Microsoft Windows then you can use Gobetwino to run commands on the PC and read/write files on the PC. That would enable you to get a working solution without having to write your own PC application. Just one caveat - Gobetwino doesn't seem to like being run for extended periods in my experience. If this is something that you expect to start and leave running for days, you might be better off looking for other options. I guess you plan to analyse a sequence of items and have the Arduino present them for imaging and then dispose of them. In that case I think I'd prefer to put the main control logic in a custom PC application which uses the Arduino to operate servos/motors and read position sensors, invokes matlab (or whatever else) to do your image analysis and (somehow) get the results of that analysis. This approach would involve writing more code, but would enable much more complex behaviour such as recording the images and the results of your analysis.

Have you gotten a chance to read the manual? You'll need to create a new command in Gobetwino, either and SPRID command type or a SPWEX command type depending on if you want it to block or not. I would guess you want it to block, it will make things easier later, so use the SPWEX command type. Then from the arduino

Now from what I can see this will not give you the output from the matlab process. What you'll need to do is in matlab create a routine that checks to see if your output fill exists, and if it does delete it. Then create the file again and spit out the output from matlab into that file. In Gobetwino create an RFLIN command type to read a line from that file.

int serInLen = 25;
char serInString[25];
int returnValue = 0;
void setup() 
{ 
  Serial.begin(9600); 
}
void loop(){
  Serial.println("#S|NAME|[]#");        // start NAME the SPWEX command you created
  readSerialString(serInString, 5000);  // wait  5  seconds (max) for answer from Gobetwino (=process ID)
  if (serInString[0]=='0'){  //do code if program ran correctly
    Serial.println("#S|NAME|[NUM]#"); // start NAME the RFLIN command you created and read line NUM
    readSerialString(serInString, 5000);
    if(serInString[0]=='-' && serInString[1]=='1'){ //file does not exist
    }else if(serInString[0]=='-' && serInString[1]=='2'){ //the line number is greater than number of lines in file
    }else if(serInString[0]=='-' && serInString[1]=='3'){ //program threw an exception
    }else{
      //process serInString however you want
    }
  }else if(serInString[0]=='-' && serInString[1]=='1'){ //error in program path parameter
  }else if(serInString[0]=='-' && serInString[1]=='2'){ //program didn't end in time
  }else if(serInString[0]=='-' && serInString[1]=='3'){ //program threw an exception
  }
}

With the easy code below and Gobetwino, can I open my .exe file of matlab.
It works perfect, thanks a lot for helping.
Now the next step is give one value to arduino.
Is this the right solution?
First, I have to save the matlab value (that I need) in a .txt - file.
Then, I have to read that value with the function RFLIN of Gobetwino.
Right?
Of is there a better solution?
Do someone know how to save one value in a txt-file with matlab?

This italicized text show the matlab code:
whitecount = sum(sum(BW)); %counter of the white pixels
if whitecount > 100
_ (I want to sent te value of whitecount to arduino)_
end

The bold text is the value that I want to give to Arduino.
Any ideas?

void setup()
{
}

void loop()
{
  Serial.begin(9600);
  Serial.println("#S|OPNMATLAB|[]#");
  delay(8000);
}

laadams85:
Have you gotten a chance to read the manual? You'll need to create a new command in Gobetwino, either and SPRID command type or a SPWEX command type depending on if you want it to block or not. I would guess you want it to block, it will make things easier later, so use the SPWEX command type. Then from the arduino

Now from what I can see this will not give you the output from the matlab process. What you'll need to do is in matlab create a routine that checks to see if your output fill exists, and if it does delete it. Then create the file again and spit out the output from matlab into that file. In Gobetwino create an RFLIN command type to read a line from that file.