Pages: [1]   Go Down
Author Topic: Arduino, matlab and servo control  (Read 7807 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to interface my mega 2560 with a matlab. I just have to move two servo motors based on values sent by matlab. Unfortunately I have had zero luck and am unable to debug. Could someone look at the code and maybe lead me towards the problem?
[Arduino][http://pastebin.com/Pg5xb5h6]
[Matlab][http://pastebin.com/chQ2K3p6]


Thank you in advance

* twoservo.m (0.66 KB - downloaded 145 times.)
* UWFLO.ino (13.02 KB - downloaded 105 times.)
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13707
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


interfacing matlab has been discussed several times on the forum. Have you tried to search in the upper right corner?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13707
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The .ino file looks rather complex, can you explain the meaning of the states?

You can compare val like this

if (val < 'a') { ....

makes it a bit more readable imho
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

hi there!

i am also searching for how to set up my arduino with matlab.

i want to use matlab to controll a servo, connected to a arduino uno.

i read every post i could find in this forum about "matlab servo", but nothing i found was helpful.

i also searched the internet, and this is what i get so far:

MATLAB CODE:
Code:
%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);
fprintf(arduino,'%s','120'); %Write 120 to ComPort
fwrite(arduino,120);
pause(0.02)


fclose(arduino);

delete(arduino)
clear arduino

fprintf(arduino,'%s','120');
fwrite(arduino,120);
those are two ways i found to send data, but both of them dont work....



ARDUINO CODE:
Code:
/*
Matlab-Arduino Servo Controller
*/


#include <Servo.h>


//DECLARATION---------------------
Servo servo1;



//SETUP----------------------------
void setup() {
  
  Serial.begin(9600);
    
  servo1.attach(6);
  
  
}


//LOOP--------------------------------
void loop() {

 if( Serial.available() )
    {
        int x = Serial.read();
        servo1.write(x);
    }
  
}

It seems Matlab isnt sending the right data.
If is send data, arduino RX Led is blinking, but nothing happens.
SO it gets something, but not the angle (ie. 120) to controll the servo.

i hope somebody can help me with that problem.

thank you!

---moe

EDIT:

ok, i found the error!!!!

before u send data to serial with matlab you have to give arduino a little break!
just insert a:


pause(0.5) before the write commando

like this!

Code:
%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);
pause(0.5) % this is very important! arduino needs a little time to initialise the fopen command!

for i = 0:179  % Runs your servo from 0-179 degree
fwrite(arduino,i);
pause(0.02)
end

fclose(arduino);

delete(arduino)
clear arduino
« Last Edit: March 12, 2012, 06:31:25 pm by moe69 » Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's because the Arduino resets when the serial port is opened.
Logged

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

ok, now i got another problem.

i want to sent not only numbers to arduino with matlab.

i want to send following code:

p7m45g

this means: servo on pin 7 with 45 degree

how can i send this with matlab?

my code is:

Code:
%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);

pause(0.1) % this is very important! arduino needs a little time to initialise the fopen command, because it resets it!

fprintf(arduino,'%s','p7m20g')

fclose(arduino);
delete(arduino)
clear arduino

but it doesnt work, any ideo?

---moe


edit:

ok, solved it.
you have to set the pause after fopen longer.
a pause of 1.7 seconds worked
is there any other way to solve this?

Code:
%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',115200); % Set ComPort

fopen(arduino);

pause(1.7) % this is very important! arduino needs a little time to initialise the fopen command, because it resets it!

fprintf(arduino,'%s','p7m45g') %servo on pin 7 to 45 degree
fprintf(arduino,'%s','p6m135g') % servo on pin 6 to 135 degree

fclose(arduino);
delete(arduino)
clear arduino
« Last Edit: March 13, 2012, 04:06:02 pm by moe69 » Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. You can disable the auto-reset - something else to search for.
Logged

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

thank you.

it seems to disable auto reset on a arduino uno, you have to use a 10uF capacitor (Resetpin- 10uF - Groundpin).
i will give it a try, when im home again.

---moe
Logged

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

Hi,
   Im new about the arduino language. I have tried control the LED by using the matlab through Arduino, it work well to me, but cannot control even move the servo motor as i refer to the arduino forum the previous forum about arduino matlab servo.
There are my steps and code in order to control and interface.
1.dowload the arduino i/o support package and installed it.
2.connect the arduino to PC via ubs port
3.dowload and install the arduino software and 'upload' the adiosrv into arduino duemilanove (also try in romeo) board
4.type arduino in the matlab and it connect successful.
5.and then type "a=arduino('COM8')", "a.servoAttach(1)"and "a.servoWrite(1,50)" to move the servo motor 1(pin 9) from 0 to 50 degree.

but it seem does not work at all.
my question are:
1.is there any steps that i miss out?

2.any software is required others than what i mention above?

3.the romeo or the duemilanove that assign to "a.servoAttach(1)" is represented pin 9 and "a.servoAttach(2)"

Logged

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

help needed,this code below is for one servo ,suppose if i have 5 servo how do i change the void loop?
Code:
Serial.begin(9600);
   
  servo1.attach(6);
   
   
}


//LOOP--------------------------------
void loop() {

 if( Serial.available() )
    {
        int x = Serial.read();
        servo1.write(x);
    }
 
}
Logged

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

Quote
suppose if i have 5 servo how do i change the void loop?
You need to send more data. You need to send which servo the data is for as well as the data for the servo.

Then, you read two bytes of data - servo # and servo val - and move the corresponding servo. And, hope like hell no serial data ever gets lost. Don't forget that you need to wait for two or more bytes, not one or more, too.
Logged

0
Offline Offline
Tesla Member
***
Karma: 143
Posts: 9601
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Multi servo test code.

Code:
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

Friends for matlab and arduino interfacing my article written at i have provided steps and images to reduce difficulty I hope you all will be benifited

thanks:

http://arduinointerfacing.blogspot.in/
Logged

Pages: [1]   Go Up
Jump to: