如何将数据从matlab GUI发送到arduino?

时间:2015-03-20 22:28:22

标签: matlab arduino

我正在使用MATLAB GUI进行远程控制 但我没有MATLAB的经验 这是代码,但没有实现:

1- arduino code(发射器):

int matlabData;
const int APIN=13;
const int BPIN=12;

#include <VirtualWire.h>

char *controller;

void setup() {

  Serial.begin(9600);
  pinMode(APIN,OUTPUT);

  vw_set_ptt_inverted(true); // 
  vw_set_tx_pin(BPIN);
  vw_setup(4000);// SPEED OF DATA TRANSFER KBPS
}


void GET_ACTION(){
if(Serial.available()>0) // if there is data to read
matlabData=Serial.read(); // read data

if(matlabData==1){
//FORWARD
controller="F";
Serial.println("FORWARD");

}


 if(matlabData==2){
//BACKWARD
controller="B";
Serial.println("BACKWARD");

}


if(matlabData==3){
  //RIGHT
   controller="R";
  Serial.println("RIGHT");
}


if(matlabData==4){
  //LEFT
   controller="L";
  Serial.println("LEFT");

}


if(matlabData==5) {
  //STOP
   controller="S";
  Serial.println("STOP");
}
}
}

void SEND_RF(){
vw_send((uint8_t *) controller, strlen(controller));
 vw_wait_tx(); // WAIT UNTIL THE WHOLE MESSAGE IS GONE
 digitalWrite(APIN,1);
delay(50); 
 digitalWrite(APIN,0);

}
void loop(){
GET_ACTION();
SEND_RF();
//delay(1000);
}

2 MATLAB Code&#34;这里的问题&#34; :

clear all
clc
answer=1; % this is where we'll store the user's answer
arduino=serial('COM10','BaudRate',9600); % create serial communication object on port COM10

fopen(arduino); % initiate arduino communication

while answer
fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino


function pushbutton1_Callback(hObject, eventdata, handles)
 answer= 1;

function pushbutton2_Callback(hObject, eventdata, handles)
answer= 2;

function pushbutton3_Callback(hObject, eventdata, handles)
 answer= 3;

function pushbutton4_Callback(hObject, eventdata, handles)
answer= 4;

function pushbutton5_Callback(hObject, eventdata, handles)
answer= 5;

end

fclose(arduino); % end communication with arduino

3-arduino代码(接收方):

const int APIN=13;
const int BPIN=12;

#include <VirtualWire.h>

#include "L298_MOTOR.h"

L298_MOTOR L298(5,4,6,7);

void setup()
{
    Serial.begin(9600);
     L298.ENABLE_ACTIVE(11,10);
  L298.ENABLE_A('ON'); 
  L298.STOP();
    vw_set_ptt_inverted(true); // REQUIRED FOR DR3100
    vw_set_rx_pin(BPIN);
    vw_setup(4000);  // BITS PER SEC
    pinMode(APIN, OUTPUT);

    vw_rx_start();       // START THE RECEIVER PLL RUNNING
}
    void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // NON-BLOCKING
    {
      if(buf[0]=='F'){
        //FORWARD
           L298.BACKWARD(150);
  //       SERIAL.PRINTLN("FORWARD");
   digitalWrite(APIN,1);
      }   
        if(buf[0]=='B'){
        //BACKWARD
        L298.FORWARD(150);
    //    SERIAL.PRINTLN("BACKWARD");


   digitalWrite(APIN,1);
      }   
        if(buf[0]=='R'){
          //RIGHT
           L298.TurnLEFT(140);
      //     SERIAL.PRINTLN("RIGHT");

  digitalWrite(APIN,0);
    }
     if(buf[0]=='L'){
          //LEFT
           L298.TurnRIGHT(140);
        //   SERIAL.PRINTLN("LEFT");

  digitalWrite(APIN,0);
    }
   if(buf[0]=='S'){
          //STOP'
           L298.STOP();
          //  SERIAL.PRINTLN("STOP");

  digitalWrite(APIN,0);
    }
}
}

请帮助..

2 个答案:

答案 0 :(得分:0)

1-不要在循环中定义回调函数。

2-变量答案不是全局的。您可以使用setappdata设置答案变量。

3-您的代码正在尽快发送变量答案!不要这样做,你必须在你的while循环中使用sleep(TIME)

如果您不需要连续数据流,请避免使用while并在回调中调用fprintf。

答案 1 :(得分:0)

MinaKamel感谢您的帮助,但您的意思是成为这样的代码:

 function pushbutton1_Callback(hObject, eventdata, handles)


setappdata(handles.pushbutton1,'Forward',1)


answer=getappdata(handles.pushbutton1,'Forward');


function pushbutton2_Callback(hObject, eventdata, handles)


setappdata(handles.pushbutton2,'Backward',2)


answer=getappdata(handles.pushbutton2,'Backward');


while answer

 fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino

answer ;

sleep(1000) ;


end
相关问题