Hello,
So I was able to code it up in MATLAB such that the actuator moves to the desired position and returns to the start position (this is what I want). However, I think there is a little bit of overshoot and I wanted to make sure that I am programming it correctly. I am fairly new to this, so please let me know if there is a better way to do this. This is the code that I am using.
clear; clc;
targetpos = [1, 3, 5]; % in mm
for i=1:length(targetpos)
execute(targetpos(i))
pause(5)
end
function execute(targetpos)
a = arduino('COM10', 'Uno');
Ain1 = 'D9';
Ain2 = 'D8';
stby = 'D10';
pwma = 'D3';
spd = 5; % in volts
configurePin(a, Ain1,'DigitalOutput');
configurePin(a, Ain2,'DigitalOutput');
configurePin(a, stby,'DigitalOutput');
configurePin(a, pwma,'DigitalOutput');
configurePin(a, Bin1,'DigitalOutput');
configurePin(a, Bin2,'DigitalOutput');
configurePin(a, pwmb,'DigitalOutput');
% Function to power on actuator
start(a, stby)
% Function to move actuator to target position and move back
squeeze(a, targetpos, Ain1, Ain2, pwma, spd, Bin1, Bin2, pwmb)
% Function to power off actuator
stop(a, stby)
end
function squeeze(a, targetpos, Ain1, Ain2, pwma, spd)
sl = 10; % total stroke length in mm
max_vol = 4.6; %voltage at max stroke length
min_vol = 0.4; %voltage at min stroke length
vd = max_vol-min_vol; % voltage difference between max and min stroke
targetvol = min_vol+(vd/sl)*targetpos; % voltage at target position
pres_vol1 = min_vol; % intial voltage
% Move forward
tic
while pres_vol1 <= targetvol
writeDigitalPin(a, Ain1, 1);
writeDigitalPin(a, Ain2, 0);
writePWMVoltage(a, pwma, spd);
pres_vol1 = readVoltage(a, 'A0'); % update voltage values
end
time = toc;
% Move backward
writeDigitalPin(a, Ain1, 0);
writeDigitalPin(a, Ain2, 1);
writePWMVoltage(a, pwma, spd);
pause(time)
end
function stop(a, stby)
writeDigitalPin(a, stby, 0);
end
function start(a, stby)
writeDigitalPin(a, stby, 1);
end