Undefined function 'writePosition' for input arguments of type 'double'.
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,guys. I used arduino support from mathworks and I face some problems when I tried to apply codes to instruct matlab to rotate angle. Below is part my code:
%convert the delay s into degrees ang
ang=round((s+128).*179/256);
%rotate angle ang
writePosition(s, ang);pause(0.01);c;
Once I run this code, I keep got error stated:
Undefined function 'writePosition' for input arguments of type 'double'.
Do you have any idea how to solve this error?Thanks for your response:)
댓글 수: 2
Nick
2017년 4월 13일
편집: Nick
2017년 4월 13일
In your code what is s that you are using for ang?
Does this code work for you? It should continuously rotate the servo between 0 and 180 degrees. If the servo shakes its just trying to go too far past 0 or 180 degrees. This number can be between 0 and 1 and is slightly different for different motors. For my servo 0 degrees is about 0.06 and 180 degrees is about 0.98 and the servo will shake if I go pas that number
%Create an Arduino object
a = arduino('com3','uno','libraries','Servo');
%Attach a servo motor to pin 9
s = servo(a,9)
while(2>1)
%Move servo to 0 degrees
writePosition(s, 0.06)
pause(1)
%Move servo to 180 degrees
writePosition(s, 0.98)
pause(1)
end
답변 (1개)
Guillaume
2017년 4월 13일
Clearly, s is supposed to be a variable referencing the object doing the rotation, in Nick's example a servomotor. It's certainly not supposed to be a numeric value.
A piece of advice: rather than using only one letter to name your variable, which don't mean anything to anybody, use full words that actually indicates what's in the variable. Rather than naming your delay s (or delay2, and why give it two names?), why not name it simply delay? (and rather than ang use angle or even better angledegree). It makes the code immediately clearer and it makes it less likely that you'd reuse variable names inadvertently.
arduinoref = arduino('com3','uno','libraries','Servo');
servoref = servo(a,9)
delay = samplingfrequency / 2 - SomethingBadlyNamedSoWeDontKnowWhatItIs
delay = max(min(delay, 127), -127); %restrict delay to [-127, 127]
angledegree = round((delay + 128) * 179/256);
writeposition(servoref, angledegree);
댓글 수: 6
Nick
2017년 4월 21일
can you just map the angle you are getting to a value between 0 and 1? I'm not sure if matlab has a built-in function to do this but it would be easy enough to write. This is from Arduino here at the bottom of the page
in_min = 0
in_max = 180
out_min = 0
out_max = 1
new_angle = (angle-in_min) * (out_max - out_min)/(in_max-in_min) + out_min
writePosition(servo_motor, new_angle);
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!