Archive | November 2013

Update 11/24/13

I have been able to get the motor working with the valve, as well as mechanically stabilize the motor so it does not turn itself rather than turning the valve.

I have run into several issues with the code/hardware. The main issue is the tradeoff between resolution and speed. In order to have a resolution conducive to application, the speed drops to below 20 rpm, this however is more than likely too slow for any application. The second issue is the initial force required to open the valve causes calculation issues in the code. The code does not have the ability to determine the actual position of the motor, it can only tell the motor to move a set number of ticks and can record that, if there is too much resistance and the motor cannot turn, the recorded number is off and the motor will over or under close. I attempted to solve this by introducing a stopping mechanism at the beginning that would complete a circuit and make the program break if the motor returned to the “almost close” position (did not want full closure as it required too much force to open). However this exposed a new problem, the amount of time it took to read the data from a second (or the same) arduino into the system greatly dropped the resolution again.

This program that runs the interface between MATLAB and the arduino seems to have some faults along communication. It seems to work well for simple tasks (inputs and outputs) but sending a waveform through a motor that is so time sensitive seems to cause problems.

The code I have now that works to some degree is below. It introduces a break in the motor code if the position becomes less than 0 ( to prevent over closure) It also has the speed greatly lowered (to help with resolution). This speed as well with the constants set at the beginning will change calibration in the future with the flow meter. (For some reason if I change the speed it changes the maximum position)

Note: I introduced a second arduino into the system prior using “b = arduino”COM 4”; and using the b variable to control it. This is not included in this code as it caused issues

\code
%% Inhalation Profile to Motor Movement
% Last Updated 11/22/2013
%% code overview
%input from excel spreadsheet
%-> profile (flow rate) as a matrix
%-> time (corrosponding to profile) as a matrix
% the input will be translated to motor position vs. time
% which will involve changing the scale to fit the motor callibrations
% the motor will follow the calibrated matrix and perform the profile

%% Inhalation Profile Data Bank
%
% Format for importation:
% matrix = xlsread(‘File name’, ‘spreadsheet name’);
% note: Filename must include extension (.xlsx) and make sure to include
% the single quotes
%
% File name: ‘InhalationProfileExcel.xlsx’ MUST BE IN MATLAB FOLDER
%
% Spreadsheet names (First is for time data, second is for flow rate)
% ‘Profile1Time’; ‘Profile1Flow’; This is test data made using a sigmoid derivitive with max flow rate of around 3 L/s
clc;clear all; close all;

%% Connect to Arduino and set motor speed

a=arduino(‘COM3’);

rpmmax = 15; % 150-160 is the max rpm we can get without the motor malfunctioning
mot = 2; % motor is connected to the stepper 2 spot
posmax = 300; % (probably bad) estimation will be callibrated later
flowmax = 1.5; % L/s
convert = posmax/flowmax; % conversion factor to convert flow rate to position

direction = 1; %direction of movement (for position tracker)
a.stepperSpeed(mot,rpmmax) % We want to set the stepper speed to max
pt = 0;%position tracker to make sure that we get back to full close

%% Import Inhalation Profile via Excel Spreadsheet

%max inhalation ~ 280 L/min ~ 5 L/s

%% This Portion will be the only part that will need to be changed to test various profiles (the rest will auto-correct accordingly)
%See Inhalation Profile Data Bank section at begining of code for
% instructions, file names, and descriptions

%time data for inhalation profile
time = xlsread(‘InhalationProfileExcel.xlsx’,’Profile1Time’);% test data = linspace(0,5);
%Flow rate for inhalation profile
profile = xlsread(‘InhalationProfileExcel.xlsx’,’Profile1Flow’);% test data = 200.*(exp(time))./(exp(time) +10).^2 -2 -> sigmoid derivitive with max 3 L/s

%% Make sure that no negatives are present in the flowrate (make sure program doesn’t crash)

for i=1:length(profile)
if profile(i) <0
profile(i) = 0;
end
end

%% plot inhalation profile for referance
figure()
plot(time,profile)
title('Inhalation Profie')
xlabel('Time (s)')
ylabel('Flow Rate (L/s)')

%% Translate to motor position vs. time

motorpos = profile * convert;
movearray = zeros(size(profile)); %create a blank array to store the movements of the motor
movetime = zeros(size(profile)); %create a blank array to store the time relative to motor
track = zeros(size(profile));
moveindex = 1; %array index for the motor movement arrays;

%% translate to num steps vs time.

stepcount = diff(motorpos);

%% Perform Motor Function
tic %tic/toc will be used as it will provide the quickest response possible from the system

while toc=0)); %want the non-zero minimum (so we move foward in the matrix)
%cont. otherwise the minimum would always be the first index
if isempty(minCheck) == true % failsafe since sometimes a very small neg is formed instead of a 0 term for the final index
minCheck = max(check);
end

timeindex = find(check == minCheck); %finds time index that corrolates with the time closest to clock
move = round(abs(stepcount(timeindex-1))); % uses time index to find motor position needed at this time

movearray(moveindex) = move; % store motor position for comparison

%determine direction of motor movement from slope

if stepcount(timeindex-1)> 0
dir = ‘backward’;
d = 1;
else
dir = ‘forward’;
d = -1;
end

%% move stepper motor to correct position
if move > 0

a.stepperStep(2,dir,’interleave’,move);
pt = pt + d*move
if pt <= 0
break
end

track(moveindex) = pt;
moveindex = moveindex + 1;
%pause(.1);
end

end

pt

track(moveindex) = pt;
while pt < 0

a.stepperStep(2,'backward','interleave',1)

pt = pt + 1;
end

a.stepperStep(2,'release');
%plot motor movement for comparison to inhalation profile
figure()

plot(movetime,track)
title('Motor Position vs. Time')
xlabel('Time (s)')
ylabel('Motor Position (relative)')

a.delete
\end code