Hallo zusammen,
mit Hilfe eines GUI moechte ich in MATLAB die Geschwindigkeit und die Richtung meines Steppermotors einlesen - diese an Arduino uebergeben und damit meinen Steppermotor ansteuern.
Die Teile meines Matlab-Programms sehen wie folgt aus:
function StartSerialButtonValueChanged(app, event)
value = app.StartSerialButton.Value;
global temp
if ~isempty(instrfind) % use instrfind() to detect the possible open port
fclose(instrfind);
delete(instrfind); % and to delete the object if found
end
temp=serial('COM3'); % assign serial port object
set(temp, 'BaudRate', 9600); % set BaudRate to 9600
set(temp, 'Parity', 'none'); % set Parity Bit to None
set(temp, 'DataBits', 8); % set DataBits to 8
set(temp, 'StopBit', 1); % set StopBit to 1
%display the properties of serial port object in MATLAB Window
disp(get(temp,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits'}));
fopen(temp); % Open Serial Port Object
pause(2);
disp('Serial port is opened');
app.StatusTextArea.Value = 'Serial port is opened';
end
Beispiel, wenn der Button "CW Rotation" gedrueckt wird:
value = app.CWRotationButton.Value;
global temp
global dataOut
valueRpm = app.rpmEditField_3.Value;
valueRpm = ((valueRpm/60)*app.StepsperrevolutionEditField.Value)/100; % conversion rpm -> steps/sec
dataOut(2) = valueRpm;
dataOut(3) = 2;
fwrite(temp,dataOut); % print character to the serial port
disp(dataOut);
disp('Motor is rotating in clockwise direction');
app.StatusTextArea.Value = 'Motor is rotating in clockwise direction'; % updating GUI text
Arduino Code:
// Include the AccelStepper library:
#include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int matlab[4]; // received data matlab
int mchoice;
int mspeed,msteps;
void setup() {
//put your setup code here, to run once:
Serial.begin(9600);
stepper.disableOutputs();
pinMode(13, OUTPUT);
}
void loop() {
//put your main code here, to run repeatedly:
if(Serial.available()>=3)
{
for(int i=0; i<3;i++)
{
matlab[i]= Serial.read();
}
msteps = matlab[0];
mspeed = matlab[1]*100;
mchoice = matlab[2];
if (mchoice == 1)
{
}
else if (mchoice == 2)
{
stepper.enableOutputs();
stepper.setCurrentPosition(0);
while(stepper.currentPosition()!= 4000){ // returns the current motor position in steps
stepper.setSpeed(mspeed);
stepper.runSpeed();
}
stepper.disableOutputs();
}
else if (mchoice == 3)
{
stepper.setCurrentPosition(0);
while(stepper.currentPosition()!= (-4000)){ // returns the current motor position in steps
stepper.setSpeed((-1*mspeed));
stepper.runSpeed();
}
}
}
}
Mir geht es aktuell nur um mchoice = 2 - das heist der Motor soll mit der uebergebenen Geschwindigkeit CW rotieren.
Mein Problem: Wenn ich den Steppermotor nur mit Arduino ansteuere und ihn nach vorne und hinten fahren lasse, klappt alles ohne Probleme.
Verwende ich zusaetzlich MATLAB, pfeift der Motor und bewegt sich leider nicht:( (Allerdings versucht er es - schafft es aber nicht…)
Vielleicht koennt ihr mir sagen, wo mein Problem liegt…
Freu mich ueber alle Ratschlaege, Hinweise und Hilfe.
Vielen Dank im Voraus
Verena