步驟1:在MATLAB
打開您的MATLAB,然后鍵入命令:“ guide”
如果一切正常,將打開一個(gè)窗口供您設(shè)計(jì)布局。如果無法獲取該窗口,請(qǐng)檢查您的MATLAB安裝中是否包含該模塊。我的MATLAB版本是R2012b,安裝了默認(rèn)設(shè)置和軟件包。
讓我們假設(shè)您在輸入“指南”后會(huì)感覺很好。放置窗口組件如下:
-1切換按鈕
-2靜態(tài)文本
按圖片所示排列布局(實(shí)際上,布局要只要您易于使用和閱讀,就可以通過修改屬性檢查器中的“字符串”值來更改每個(gè)對(duì)象中的文本(任何您想要的內(nèi)容)(選擇對(duì)象-右鍵單擊-屬性檢查器,或雙擊)
然后,保存該GUI圖形文件。
步驟2:編寫代碼
ARDUINO代碼
arduino的代碼與您在此處可以找到的代碼基本相同:https://www.instructables.com/id/Infrared-Tachomete 。..但因?yàn)檫@里我僅想要顯示rpm值(而不是rps值以及所有的“ rps”和“ rpm”文本),所以我編輯了一些行(那些具有Serial.print()的行,因?yàn)橐郧霸摮绦蛑荚陲@示讀數(shù)在記事本式串行監(jiān)視器上,但現(xiàn)在我們只需要rpm值即可輸入到靜態(tài)文本字符串中)。好的,為了方便快捷,我將代碼復(fù)制到此處,您可以自行檢查以與之前的代碼進(jìn)行比較。請(qǐng)記住,arduino代碼的主要目的只是將值傳遞給串行comm,因此該程序僅作為示例,如果您有自己的程序?qū)⑷魏巫x取到的傳感器的值打印到串行,然后忽略此操作即可。
int sensorvalue;
int state1 = HIGH;
int state2;
float rps;
float rpm;
long prevMillis = 0;
long interval = 100;
long currentTime;
long prevTime = 1;
long diffTime;
int sensorthreshold = 30; // this value indicates the limit reading between dark and light,
// it has to be tested as it may change acording on the
// distance the leds are placed.
// to see what number is good, check the sensorvalue variable value
// as printed out in the serial monitor
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT); // assign pin 13 led as indicator because we cannot se the IR light
}
void loop()
{
sensorvalue = analogRead(0); // read from pin 0
if(sensorvalue 《 sensorthreshold)
state1 = HIGH;
else
state1 = LOW;
digitalWrite(13,state1); // as iR light is invisible for us, the led on pin 13
// indicate the state of the circuit.
if(state2!=state1){ //counts when the state change, thats from (dark to light) or
//from (light to dark), remember that IR light is invisible for us.
if (state2》state1){
currentTime = micros(); // Get the arduino time in microseconds
diffTime = currentTime - prevTime; // calculate the time difference from the last sensors meet-up
rps = 1000000/diffTime; // calculate how many rev per second - good to know
rpm = 60000000/diffTime; // calculate how many rev per minute
unsigned long currentMillis = millis();
// print to serial at every interval - defined at the variables declaration
if(currentMillis - prevMillis 》 interval){ // see if now already an interval long
prevMillis = currentMillis;
Serial.println(rpm); // this line is edited from the code in the prev instructable
}
prevTime = currentTime;
}
state2 = state1;
}
/* only for testing to determine the sensorthreshold value
delay(500);
Serial.println(sensorvalue);
*/
}
MATLAB代碼
在MATLAB GUI布局設(shè)計(jì)窗口中,單擊“查看-編輯器”(或在工具欄中找到?jīng)]有手的紙和鉛筆的圖片)。將打開一個(gè)編輯器窗口,其中已經(jīng)編寫了一些代碼,MATLAB為您編寫了它們,沒問題。只為切換按鈕編寫回調(diào)函數(shù),其余代碼可以保留不變。就我而言,我將切換按鈕命名為OnOffToggle,因此編寫代碼的函數(shù)是函數(shù)OnOffToggle_Callback(hObject,eventdata,handles)。 rpmdata,所以我們只想將rpm數(shù)據(jù)打印到屏幕上即可。還有一件事,請(qǐng)確保在編寫代碼時(shí)將arduino連接到正確的COM端口。在這段代碼中,我寫了COM3,因?yàn)槲覍rduino連接到了COM3。
然后保存您的m文件。
下面是完整代碼(僅編輯OnOffToggle_Callback函數(shù)):
function varargout = gui(varargin)
% GUI MATLAB code for gui.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI(‘CALLBACK’,hObject,eventData,handles,。..) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI(‘Property’,‘Value’,。..) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE‘s Tools menu. Choose “GUI allows only one
% instance to run (singleton)”。
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help gui
% Last Modified by GUIDE v2.5 14-Mar-2015 01:06:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(’gui_Name‘, mfilename, 。..
’gui_Singleton‘, gui_Singleton, 。..
’gui_OpeningFcn‘, @gui_OpeningFcn, 。..
’gui_OutputFcn‘, @gui_OutputFcn, 。..
’gui_LayoutFcn‘, [] , 。..
’gui_Callback‘, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before gui is made visible.
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to gui (see VARARGIN)
% Choose default command line output for gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function currentEdit_Callback(hObject, eventdata, handles)
function currentEdit_CreateFcn(hObject, eventdata, handles)
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
set(hObject,’BackgroundColor‘,’white‘);
end
function OnOffToggle_Callback(hObject, eventdata, handles)
button_state = get(hObject,’Value‘);
if button_state == get(hObject,’Max‘)
set(handles.OnOffToggle,’String‘,’Stop‘);
drawnow;
i=2;
while i 》 1
rpmdata = serial(’COM3‘,’BaudRate‘,9600); % this Baud rate should be the same as that in Arduino code
fclose(instrfindall);
fopen(rpmdata);
b = fscanf(rpmdata);
set(handles.textCurrent,’String‘,b);
drawnow;
delete(rpmdata)
if get(hObject,’Value‘) == get(hObject,’Min‘)
break
end
end
set(handles.OnOffToggle,’String‘,’Start‘);
drawnow;
rpmdata = serial(’COM3‘,’BaudRate‘,9600);
fclose(rpmdata);
end
步驟3:運(yùn)行Rpm Reader
完成代碼后,連接arduino,然后轉(zhuǎn)動(dòng)旋轉(zhuǎn)并運(yùn)行程序(編輯器或布局編輯器窗口上的綠色三角形類似游戲的按鈕)。程序的一個(gè)窗口將會(huì)出現(xiàn)(我的如圖所示),單擊切換按鈕,您將在那里看到車輪的當(dāng)前轉(zhuǎn)速。
責(zé)任編輯:wv
-
matlab
+關(guān)注
關(guān)注
182文章
2963瀏覽量
230199 -
Arduino
+關(guān)注
關(guān)注
187文章
6464瀏覽量
186684 -
RPM
+關(guān)注
關(guān)注
0文章
44瀏覽量
17701
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論