WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Schrittmotoransteuerung mit Matlab GUI und Arduino

 

Verena...

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 10.10.2019, 03:06     Titel: Schrittmotoransteuerung mit Matlab GUI und Arduino
  Antworten mit Zitat      
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:
Code:


        % Value changed function: StartSerialButton
        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:
Code:

function CWRotationButtonValueChanged(app, event)
            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

        end

 


Arduino Code:

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(1000);
        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 …
Mein Problem: Wenn ich den Steppermotor nur mit Arduino ansteuere und ihn nach vorne und hinten fahren lasse, klappt alles ohne Problem.

Verwende ich zusaetzlich MATLAB, pfeift der Motor und bewegt sich leider nicht:(
Vielleicht koennt ihr mir sagen, wo mein Problem liegt…

Bin leider neu in diesem Gebiet und freu mich ueber alle Ratschlaege, Hinweise und Hilfe.

Vielen Dank im Voraus
Verena


Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.