Undefined function 'writePosition' for input arguments of type 'double'.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Nick
am 13 Apr. 2017
Bearbeitet: Nick
am 13 Apr. 2017
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
Antworten (1)
Guillaume
am 13 Apr. 2017
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 Kommentare
Nick
am 21 Apr. 2017
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);
Siehe auch
Kategorien
Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!