2
$\begingroup$

I have a servo motor (http://robokits.download/documentation/RMCS220x_DCServo_Driver.pdf). I wrote a code in Arduino to rotate it in some defined angles and position. Code is given below:

#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10, TX = digital pin 11
// I have Arduino Uno so I created extra RX and TX to send and receive data. 
// Becuase using inbuilt RX0 and TX0, I was unable to transfer data to motor 
// and get feedack in PC
SoftwareSerial serial1(10, 11); 

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

void loop()
{

  if (Serial.available() > 0)
  {
    delay(5);
    serial1.println(Serial.readString());
  }


  if (serial1.available() > 0)
  {
    delay(5);
    Serial.println(serial1.readString());
    delay(5);
  }
}

Using this code what I am able to do is, in terminal I enter some value say "G400" or "R821" etc. and motor rotate accordingly. But, this is not my aim. I don't want to put values manually, instead I used matlab script which give me some angle after calculations. I have to send this value to motor. Say, after calculations, matlab gives 26.4 degree, then I have input value to motor 26.4/0.2 = 132 counts i.e. "G132". Value changes every time for next calculation it may be 40 degree. What should be the coding for this in Arduino as well as in MATLAB.

Thanks.

$\endgroup$
0
$\begingroup$

After trying so many times, I came out with a solution to operate above-mentioned motor through Matlab. Here, I will rotate single motor only but in actual I have to rotate 4 motors for my robotic arm. So here is my arduino mega code to receive data from Matlab.

int doi;
char d[5];
int d1;int d2;int d3;int d4;
String stringG1 ; String stringG2 = "G"; String stringG3 = "G"; String stringG4 = "G";
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  }
void loop()
{

  if(Serial.available()>0)
  {
    d1 = Serial.parseFloat();d2 = Serial.parseFloat();d3 = Serial.parseFloat();d4 = Serial.parseFloat();


    stringG1 = "G" + String(d1);
    Serial1.println(stringG1);

    if (Serial1.available()>0)
    {
      Serial.print(Serial1.readString());
    }  

  }
}

In above code, I used d1,d2,d3, and d4 because I have to send four angle values to four DC motor. But at this moment I have only 1 motor with me so I tested this code for single motor only. In future, I will extend this code to rotate all four motors.

Matlab side code to tansfer angle values to Arduino Mega is:

portName = 'COM9';
s = serial(portName,'BaudRate',9600,'Terminator','LF');
s.timeout = 1;
try 
    try
        fopen(s);
    catch
        delete(s);
        fopen(s);
    end
catch
    disp('Unable to open the port ');
end
%%
angle = [250,200,50,120];
    %dataOut = angle;
    fprintf(s,'%d\n',angle);
   % fread(s)


%%

    try
       fclose(s);
       try
           delete(instrfind('port','COM9'));
       catch
       end
catch
    disp('Unable to close the port ');
    end

In above Matlab code one most important thing is be noted is that this piece of code

portName = 'COM9';
    s = serial(portName,'BaudRate',9600,'Terminator','LF');
    s.timeout = 1;
    try 
        try
            fopen(s);
        catch
            delete(s);
            fopen(s);
        end
    catch
        disp('Unable to open the port ');
    end 

will take some time to established connection between Matlab and Arduino Mega. If you try to run whole code at once it will do nothing. To run above code first we have to establish connection between Matlab and Arduino. For that first run only above piece of code. Then run

angle = [250,200,50,120];
        %dataOut = angle;
        fprintf(s,'%d\n',angle);
       % fread(s)

as many times as you want to change the values in angle array.

To close the connection between Matlab and Arduino run the last remaining piece of code. Look carefully, I have inserted %% in Matlab code to break down code in sections which eases me to run code section wise using run section command in Matlab.

Any suggestions to improve code will be most welcomed.

Thanks.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.