【问题标题】:how to send Data from matlab GUI to arduino?如何将数据从 matlab GUI 发送到 arduino?
【发布时间】:2016-05-24 09:04:11
【问题描述】:

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

1- arduino 代码(发射器):

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 代码“问题出在这里”:

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);
    }
}
}

请帮忙..

【问题讨论】:

    标签: matlab arduino


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 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
    【解决方案2】:

    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
    

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 2017-06-27
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多