-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo.m
36 lines (31 loc) · 850 Bytes
/
servo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
clear a;
try
a = arduino('COM4', 'Mega2560', 'Libraries', 'Servo');
catch err
clear a;
error('Check port configurations. Remember to clear when done.');
end
%%
% from datasheet:
% min pulse: 500 microseconds
% max pulse: 2500 microseconds
% https://github.com/microrobotics/DS3235-270/blob/master/DS3235-270_datasheet.pdf
clear s;
try
s = servo(a, 'D2', 'MinPulseDuration', 500*10^-6, 'MaxPulseDuration', 2500*10^-6);
catch err
clear s;
error('Check servo configurations.');
end
%%
% read and write servo angle
% servo goes from 0 to 1 duty cycle -> 0 to 270 degrees
for angle = 0:0.2:1
writePosition(s, angle);
current_pos = readPosition(s);
current_pos = current_pos*270;
fprintf('Current motor position is %d degrees\n', current_pos);
pause(2);
end
%%
clear s a