Go Down

Topic: controlling arduino with software like matlab etc (Read 2015 times) previous topic - next topic


Oct 27, 2009, 02:09 pm Last Edit: Oct 27, 2009, 02:11 pm by Pilkaster Reason: 1
I'm building a robot-arm with 4 dc motors and two arduino usb boards (duemilanove). I can control two and two motors on each arduino with for example tera term but I need to control and calculate the motors together to get a robot arm.

I know matlab can communicate with several serial ports (COM) on hardware like:

s = serial('COM1');

but I dont know how to communicate and control the digital pins on the arduinos with matlab. Especially if I allready have some setup-code in the arduino processing language and only want to control the motors together from a more high-level programming/script language.

I have used the arduino processing language to control the motors until now that I need a more complex algorithm to control all the 4 motors together in an arm.

I know about the arduinoTarget scripts for matlab but I had some problems with the installationscript and it looked like the scripts only was for one arduino at the time.

I'm open for suggestions on other software to use for this purpuse but I think matlab is pretty good on calculations as well when I have to calculate the forward and inversekinematics for the robotic arm.

Please help!


Nov 02, 2009, 03:10 pm Last Edit: Nov 02, 2009, 03:10 pm by jpgr87 Reason: 1
The easiset way is to write a program for the Arduino that listens for commands over the Serial port.  When the Arduino receives the correct commands, it should move motors in your arm accordingly.  Once that works. you can use MATLAB (or any other language) to send these commands over the Serial port.


I now know how to send simple data fram matlab to arduino but the problem now is that I don't know how to separate the commands/strings from matlab in arduino. i. e.

s1 = serial('COM3', 'BaudRate', 9600, 'Parity', 'none', 'DataBits', 8);
fprintf(s1, 'Hello World!') - or if I'm going to control 4 motors:
fprintf(s1, 100(motorspeed), 1(as in motor no. 1), 0(as in direction 1 of 2 on motor 1)) and perhaps
fscanf(s1) - if I'm going to read something from the arduino back to matlab

In Arduino - I know the setup() and loop() methods, this is just some of the code:
char info;
     info = Serial.read();
 Serial.println('I want to send something back to matlab');

I can for instance send the char '8' from matlab to arduino and let the motors run when char info detects the char '8' but that solves nothing because then all my motors will run and I will not be able to control them separately. I'm thinking on splitting the string I get from matlab and separate the words/integers to each motor but I don't know if it's possible yet.


It's possible. You'll need to define the format of the string. For instance, you could do something like this:


This would set a speed, select a motor, and define a direction. Send this from matlab.

Then, on the Arduino, in loop, check for serial data. If any is present, read a byte. Decide what that byte means:

Code: [Select]

int speed = 0;
int direction = 0;
int motor = 0;

  case 'S':
     // read until you find a ;
     // if not a :
     // speed *= 10;
     // speed += byte - '0';
  case 'M':
     // similar logic for motor, except
     // you probably will not have 10 or more
     // motors, so you could just expect one byte
  case 'D':
     // similar logic for direction

After the switch block ends, do something with the values in speed, direction, and motor.
The art of getting good answers lies in asking good questions.

Go Up