Undefined function 'writePosition' for input arguments of type 'double'.

2 Ansichten (letzte 30 Tage)
sulo
sulo am 12 Apr. 2017
Kommentiert: Nick am 21 Apr. 2017
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
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
sulo
sulo am 13 Apr. 2017
Bearbeitet: sulo am 13 Apr. 2017
Hi,Nick.Thanks for your response. Im really appreciate it. Actually, my project is an automatic rotating camera in conference room. I used 2 microphones to generate the sound source. Then, I applied delay to determine which mic produced sound first. So, the camera will rotate to the incoming sound source. Below is some codes :
delay2=fs/2-b3; %delay is half of sampling frequency minus b3(maximum value for c), in samples
s=delay2;
if s<-127 %round values to outside degree parameters to furthest degree value left or right (to maintain 180 degree range)
s=-127;
elseif s>127
s=127;
end
%convert the delay s into degrees ang
ang=round((s+128).*179/256);
writePosition(s, ang);
pause(0.01);c;
But, the problem is I keep getting error for writePosition(s,ang). Thanks for your response.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
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
sulo
sulo am 21 Apr. 2017
Bearbeitet: sulo am 21 Apr. 2017
Hi,Nick. Thanks a lot for the answer. You gave a good explanation that i can easily understand. Once I edited my code, I get result
a =
arduino with properties:
Port: 'COM3'
Board: 'Uno'
AvailableAnalogPins: [0, 1, 2, 3, 4, 5]
AvailableDigitalPins: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Libraries: {'I2C', 'SPI', 'Servo'}
SampleRate: 48000
BitsPerSample: 16
NumberOfChannels: 2
DeviceID: -1
CurrentSample: 1
TotalSamples: 0
Running: 'off'
StartFcn: []
StopFcn: []
TimerFcn: []
TimerPeriod: 0.0500
Tag: ''
UserData: []
Type: 'audiorecorder'
*BEGINNING ACQUISITION***
ans =
1 17885 17886
*DATA COLLECTED***
delay =
0
angle =
90
Error using SS (line 113) Invalid position value. The value must be in the range 0 and 1.
As what I can understand,this system cannot calculate the delay, therefore there's error at line 113 for writeposition(servo_motor,angledegree). As stated
ans =
1 17885 17886
It only detect 'a' part of my microphone, in this case I stated a1,b1 & a2,b2 for my both microphone. Did you have any idea on how can i resolve this problem? Just for additional information, I applied cross correlation algorithm.
P/s: As you stated, I need to gave argument 0,0.5,1 . But I cannot since the value is calculated by delay and angledegree.
Nick
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);

Melden Sie sich an, um zu kommentieren.

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!

Translated by