Error opening a text file using fopen
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
disp('Setting up serial communication...');
% Determine which COM port is for microcontroller and change
a = arduino('COM4','Uno');
% Open serial COM port for communication
fopen(a);
set(a,'Timeout',10);
prev_left = [0 0 0 0];
prev_right = [0 0 0 0];
prev_left_flag=0;
prev_right_flag =0;
prev_flag = 2;
check_left = 0;
check_right = 0;
check_straight_right = 0;
check_straight_left = 0;
check_left_right = 0;
check_right_left = 0;
採用された回答
Walter Roberson
2017 年 9 月 27 日
It is not necessary to fopen() an arduino object: the arduino() function does any necessary opening of the device.
13 件のコメント
NURUL NABILAH
2017 年 10 月 1 日
so for arduino does not any necessary? am i right?
NURUL NABILAH
2017 年 10 月 1 日
sorry for disturbing you sir. sir can you help me for correcting my coding because my driver motor is not moving.
Walter Roberson
2017 年 10 月 1 日
The code you posted does not read anything from the device and does not send anything to the device.
NURUL NABILAH
2017 年 10 月 3 日
編集済み: Walter Roberson
2017 年 10 月 3 日
%%Initialization of variables and serial communication
clc;
clear all;
close all;
delete(instrfind);
% Set up the COM port for serial communication
disp('Setting up serial communication...');
% Determine which COM port is for microcontroller and change
s = serial('COM4','Parity','none','FlowControl','none','BaudRate',9600);
% Open serial COM port for communication
fopen(s);
set(s,'Timeout',10);
prev_left = [0 0 0 0];
prev_right = [0 0 0 0];
prev_left_flag=0;
prev_right_flag =0;
prev_flag = 2;
check_left = 0;
check_right = 0;
check_straight_right = 0;
check_straight_left = 0;
check_left_right = 0;
check_right_left = 0;
%%Image Capture and Eye Detection
vid=videoinput('winvideo',1); start(vid)
while(vid.FramesAcquired<=25)
I = getsnapshot(vid);
I = rgb2gray(I);
I = imadjust(I);
I = adapthisteq(I);
I = imrotate(I,180);
faceDetector = vision.CascadeObjectDetector('LeftEyeCART');
j=0;
left_flag=0;
right_flag=0;
bboxes = step(faceDetector, I);
[m,n] = size(bboxes);
IFaces = insertObjectAnnotation(I, 'rectangle', bboxes, 'Eye');
imshow(IFaces), title('Detected eye');
%%Image Processing
TF = isempty(bboxes);
if (TF==1)
disp('nothing');
k=1;
else
k=0;
end
for i=1:1:m
if (bboxes(i,3) < 150)
% display('invalid eye');
elseif (bboxes(i,3) > 300)
% display('invalid eye');
else
j=j+1;
eye(j,:) = bboxes(i,:);
end
end
if (j>0)
for (i=1:1:j)
if(eye(i,1)>300) && (eye(i,1)<600)
left_eye = eye(i,:);
disp('Left:');
disp(left_eye);
left_flag=1;
elseif(eye(i,1)>600) && (eye(i,1)<900)
right_eye = eye(i,:);
disp('Right:');
disp(right_eye);
right_flag=1;
end
end
%%Movement Detection
if((left_flag==1)&& (prev_left_flag ==1))
prev_left_flag = 1;
if((left_eye(1,1) - prev_left(1,1))>50)
flag = 0;
display('moved left');
elseif ((left_eye(1,1) - prev_left(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_left = left_eye;
elseif((right_flag==1)&&(prev_right_flag==1))
prev_right_flag = 1;
if((right_eye(1,1) - prev_right(1,1))>50)
flag = 0;
display('moved left');
elseif ((right_eye(1,1) - prev_right(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_right = right_eye;
elseif(left_flag==1)
prev_left_flag = 1;
if((left_eye(1,1) - prev_left(1,1))>50)
flag = 0;
display('moved left');
elseif ((left_eye(1,1) - prev_left(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_left = left_eye;
elseif(right_flag==1)
prev_right_flag = 1;
if((right_eye(1,1) - prev_right(1,1))>50)
flag = 0;
display('moved left');
elseif ((right_eye(1,1) - prev_right(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_right = right_eye;
end
if (left_flag == 0)
prev_left_flag=0;
elseif (right_flag == 0)
prev_right_flag=0;
end
%%Motor Control Signals
if ((prev_flag == 0) && (flag == 1)) % straight movement
display('motor moved straight');
move = 2;
check_straight_right = 1;
check_left_right = 1;
elseif ((prev_flag ==1) && (flag == 0))
move = 2;
display('motor moved straight');
check_straight_left = 1;
check_right_left = 1;
elseif ((prev_flag == 0) && (flag == 2)) % left movement
if ((check_right == 1) || (check_straight_right == 1)||(check_right_left))
move = 3;
display('motor stays');
check_right = 0;
check_straight_right = 0;
check_right_left = 0;
else
move = 0;
display('motor moved left');
check_left = 1;
end
elseif ((prev_flag == 1) && (flag == 2)) % right movement
if ((check_left == 1) || (check_straight_left == 1) || (check_left_right))
move = 3;
display('motor stays');
check_left = 0;
check_straight_left = 0;
check_left_right = 0;
else
move = 1;
display('motor moved right');
check_right = 1;
end
else % no movement
move = 3;
display('motor stays');
end
prev_flag = flag;
%%Serial Transmission
fprintf(s,'%1d\n',move);
disp('done');
end
hold on;
end
stop(vid);
flushdata(vid);
pause(0.04);
fclose(s);
delete(s);
clear s;
clear all;
NURUL NABILAH
2017 年 10 月 3 日
sir this is my full coding. i want change this coding by using arduino because mt project using arduino uno. but when i change serial to arduino error fopen. and motor driver cannot move.
Walter Roberson
2017 年 10 月 3 日
Arduino can be used in two ways.
1) You could have a program ("sketch") running on the arduino that is actively reading the serial port, examining the data received, and dealing with external hardware all on its own, in a way that is hidden from the host program. That is the style that the current program is written for, where the program just sends out an encoded value of '1' (move right) or '2' (move left) or '3' (stay), and the device receiving the information is responsible for figuring out what to do what that data. If you have such a program running on the arduino, then all you should need to do is change the port number to the one that the arudino is on: you would continue to use serial() and so on. There would be almost no change to the program you show.
2) You can instead have the host program set up connections to particular hardware devices like digital pins or i2c sensors, and the host would use calls such as writeDigitalPin or writeRegister to send more direct hardware instructions as to exactly what to do at the hardware level. The program you show would have to change to set up the hardware and would have to change in how it sent hardware instructions. It is only this form of using the arduino that would use the arduino() call.
NURUL NABILAH
2017 年 10 月 3 日
sir for the first way. i cant understand about program ("sketch"). can you explain or give me link about this.
Walter Roberson
2017 年 10 月 3 日
Arduino "sketches" (programs) are written in C or C++.
You can find examples about controlling bidirectional motors. See for example http://www.instructables.com/id/Control-DC-and-stepper-motors-with-L298N-Dual-Moto/
NURUL NABILAH
2017 年 10 月 6 日
編集済み: Walter Roberson
2017 年 10 月 6 日
//test for Serial COM with matlab
#define F_CPU 16000000UL
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <math.h>
#include <util/delay.h>
// serial communication library
#include "uart.h"
#include "uart.c"
double counter;
// UART file descriptor
// putchar and getchar are in uart.c
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
void delay (void);
void initialize(void);
int main(void)
{
int move;
// 0 - left, 1- right, 2- straight, 3 - stop
initialize();
while(1){
fscanf(stdin,"%d",&move); // receive data
switch (move)
{
case 0:
PORTA = 0x01; // move left
_delay_ms (1000);
break;
case 1:
PORTA = 0x04; // move right
_delay_ms (1000);
break;
case 2:
PORTA = 0x05; // move straight
_delay_ms (1000);
break;
case 3:
PORTA = 0x00; // halt
_delay_ms (1000);
break;
default:
PORTA = 0x00; // halt
_delay_ms (1000);
break;
}
}
}
void initialize(void)
{
DDRA =0xFF;
//init the UART -- uart_init() is in uart.c
uart_init();
stdout = stdin = stderr = &uart_str;
fprintf(stdout,"Starting...\n\r");
}
NURUL NABILAH
2017 年 10 月 6 日
sir coding above is coding c. This coding can not be run. is this coding error?
Walter Roberson
2017 年 10 月 6 日
The use of // as a comment delimiter requires C99 or later.
Writing to a port using something like PORTA = 0x04; would require..... ummm, I would have to research to see if it is possible at all. More typical would be *PORTA = 0x04; which is valid for memory mapped ports.
It is uncommon for motors to handle bytes of commands like that. Much more common would be pulse width modulation schemes, or rotary pulse schemes.
NURUL NABILAH
2017 年 10 月 8 日
thank you sir. sir did you know how to compile matlab software and code block?
Walter Roberson
2017 年 10 月 8 日
I have gone back and checked, and I find that on some systems, it is possible to write to a port using code such as PORTA = 0x04; The conditions for doing this is that the linker needs to be instructed to place the variable at a particular memory location (on a device that supports that.) See the description of scatter.txt in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3750.html
"did you know how to compile matlab software and code block?"
I know some things; I would prefer if you asked specific questions. Are you talking about MATLAB Compiler product (create .exe), or MATLAB Coder product (create C/C++ code), or MATLAB Compiler SDK (create .dll); or are you talking about Simulink Coder (generate C/C++ code from Simulink models); or are you talking about code generation from Simulink models for specific hardware targets such as Arduino ?
If you are trying to use MATLAB Coder to generate C/C++ code for Arduino, the task is a bit harder, as MATLAB Coder does not know anything about Arduino as a target and so has no libraries to talk to Arduino operating system or hardware -- Simulink does know about Arduino as a target.
If you want to move your entire MATLAB program onto Arduino, then you have the problem that MATLAB Coder, and Simulink, and the Support Package for Arduino, do not know how to create graphics on Arduino using imshow() or any other direct graphics. Simulink does have an Arduino video display block, which has to be sent complete images -- requiring that you do all of the drawing into an array and then send the array to be displayed as an image. The Computer Vision routines such as shape inserter and text inserter are very useful for those purposes.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Arduino Hardware についてさらに検索
タグ
タグが未入力です。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
