Pages: [1]   Go Down
Author Topic: Problems with interfacing Arduino with Matlab  (Read 514 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
First, I want to point that i have scoured the internet looking for an answer before posting here.
I guess im just missing something there. Ive started a project about modeling control systems with unknown parameters,
DC motors in particular:

Code:
#include <Encoder.h>

Encoder motor(2,3);// 2=white, 3=blue
long pos=0;
int pwmPin = 11;     // PWM Motor driver (/D2 pin pin on MC338870 motor board)
int motorPin1 = 7;   // Motor pin 1 (IN1 on MC338870 motor board)
int motorPin2 = 8;   // Motor pin 2 (IN2 on MC338870 motor board)
long destination=100000;
long error=0;
long input=0;
int flag=0;

void setup() {
  pinMode(pwmPin, OUTPUT); // initialize the digital pin as an output:
  pinMode(motorPin1, OUTPUT); // initialize the digital pin as an output:
  pinMode(motorPin2, OUTPUT); // initialize the digital pin as an output:
  Serial.begin(9600); 
  motor.write(0); 
 }

void loop(){
  while (flag==0) {    //initiate loop only after receiving any serial input
    if (Serial.available()>0){
      flag=Serial.read();
      delay(100);     
    }
  }   
 pos=motor.read();//read position from encoder;
  if (millis()%300==0) {
    Serial.println(pos);     
    Serial.println(error);   
    Serial.println(input);
  } 
 error=destination-pos;
 input=error;
 if (input>250) input=250;// saturation
 if (input<-250) input=-250; 
 if (error > 0)
   {   
    digitalWrite(motorPin1,LOW);
    digitalWrite(motorPin2,HIGH);
    analogWrite(pwmPin,input);
   }
if (error<0)
   {
    input *= -1;   
    digitalWrite(motorPin1,HIGH);
    digitalWrite(motorPin2,LOW);
    analogWrite(pwmPin,input);
   }
 if (error==0) analogWrite(pwmPin,0);
}

Needless to say, this is the very basic, no-controller code i made just for testing the serial connection.
Im trying to write the output to matlab so i can plot it and measure dynamic-response / steady-state parameters.
Now i have 2 problems (maybe they're connected, maybe not):
1. If i open the IDE serial monitor and send any char to the arduino, the program starts as should be.
But when i close the serial monitor (at any point), the program stops (as if i have pressed the reset button). why is that?
2. using fprintf via Matlab, i send numbers, chars, or even strings to the Arduino serial port, and even though the RX led
blinks, the program will not start (as it did when sending bytes through the IDE serial monitor). Am i doing something wrong?
Should i end tranmission from matlab with a certain terminator?

Matlab code (very simple, very basic):
Code:
s = serial('COM13');
fopen(s);
fprintf(s,'a'); %or whatever instead of 'a'
fclose(s);


Already tried it without closing the port object, or with using fscanf command after fprintf.
The latter one just prompts the timeout error.

I would be glad if anyone can help....
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Encoder motor(2,3);// 2=white, 3=blue
You have an instance of the Encoder class called motor?

Next think you know, you'll have an instance of the Stepper class called encoder.

Quote
Ive started a project about modeling control systems with unknown parameters,
DC motors in particular:
I don't understand this. DC motors don't have unknown parameters. The parameters, from the Arduino's perspective, are quite well known - direction and speed.

Code:
  motor.write(0); 
How do you write to an encoder?

Quote
1. If i open the IDE serial monitor and send any char to the arduino, the program starts as should be.
But when i close the serial monitor (at any point), the program stops (as if i have pressed the reset button). why is that?
Because opening and closing the serial port resets the Arduino. There are ways to permanently or temporarily disable this, but, in general, it's a good thing to have happen.

Code:
2. using fprintf via Matlab, i send numbers, chars, or even strings to the Arduino serial port, and even though the RX led
blinks, the program will not start
You never send numbers using fprintf(). You send a string representation of the number. 0 and 1 can not be sent using fprintf(). '0' and '1' can. But, you aren't testing for '0' or '1'. You are testing for 0 and 1.

Quote
Matlab code (very simple, very basic):
That code opens the serial port, resetting the Arduino. Then, without waiting for the Arduino to be ready, it sends some data. Then, it closes the serial port, resetting the Arduino again.

Quote
Am i doing something wrong?
That would be my guess. Of course, I don't know anything about Matlab, so take this into consideration.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, about calling the encoder instance "motor" - that is a copy-paste from the library example, not my idea.
doesnt really matter, as long as i know what i mean.
About the unknown parameters, i wont even attemp to explain what i meant in detail.
motor.write(0) resets the encoder count to 0 (again, taken directly from the example).
The specific encoder i work with "remembers" the last position it was on when turned off.
About sending numbers, they were sent via fwrite (binary). thats why i used 'a' in fprintf, same thing for the desired outcome.

Nonetheless, you did answer my questions and i thank you for that. Didnt know both about the closing of the serial monitor and the matlab opening of the port that they reset the arduino program. I will attempt to tackle this....

Edit: You are correct, i added a pause in Matlab and it worked fine. thank you.

Have a great week!
« Last Edit: September 30, 2013, 07:22:37 am by idantim » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
doesnt really matter, as long as i know what i mean.
It does when you want other people to help you with your code.

Quote
About the unknown parameters, i wont even attemp to explain what i meant in detail.
That's fine, since it was really irrelevant.

Quote
The specific encoder i work with "remembers" the last position it was on when turned off.
That's cool.

Quote
Edit: You are correct, i added a pause in Matlab and it worked fine. thank you.
Excellent.
Logged

Pages: [1]   Go Up
Jump to: