Error opening a text file using fopen
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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일
0 개 추천
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.
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.
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.
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?
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?
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
