Arduino-Matlab Serial Communicatoin

1 visualizzazione (ultimi 30 giorni)
David Zamora
David Zamora il 14 Lug 2018
Commentato: David Zamora il 19 Lug 2018
I have a stepper motor connected to an Arduino Uno MC. Arduino and Matlab are communicating through the serial port, and I want to send to the Arduino the number of steps it needs to rotate. When I do so, the motor doesn't move. From the print statements that I placed in my code, I know Matlab is executing that line, but the stepper motor doesn't do anything. Why is the stepper motor not moving? What is the best way to send an integer number of steps to the Arduino? What's the best way to have Arduino read that value from Arduino? Below is my matlab code and my arduino code, respectively.
% July 2018
% Run and test program block by block
fclose(instrfind);
a = serial('com3', 'BaudRate', 115200); % establish communication with Arduino
fopen(a); % same as above
global trgt;
trgt = -0.14;
readasync(a);
while 1
temp = fscanf(a);
magY = str2double(temp);
s1 = "Magnetometer value: " + magY;
disp(s1)
steps = pidCtrl(magY);
s3 = "Control Signal: " + steps;
disp(s3)
fprintf(a, num2str(steps)); % send to Arduino
end
/* July 2018
* northTracker.ino
* This program runs parallel to 'northTracker.m'.
* Needs to be programmed and tested block by block with its parallel program
* to ensure that everything works as expected.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM9DS0.h>
#include <Stepper.h>
#include <math.h>
#define LSM9DS0_XM_CS 10
#define LSM9DS0_GYRO_CS 9
#define LSM9DS0_SCLK 13
#define LSM9DS0_MISO 12
#define LSM9DS0_MOSI 11
const int stepsPerRev = 64;
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1000);
Stepper myStep(stepsPerRev, 8, 10, 9, 11);
sensors_event_t accel, mag, gyro, temp;
String matlabData;
void configureSensor(void) {
lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if(!lsm.begin()) {
// runs if there was a problem detecting the IMU
//Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
while(1);
}
//Serial.println(F("Found LSM9DS0 9DOF"));
configureSensor();
//Serial.println("");
myStep.setSpeed(200);
}
void loop() {
// put your main code here, to run repeatedly:
lsm.getEvent(&accel, &mag, &gyro, &temp);
Serial.println(mag.magnetic.y); // sends magnetometer data to Matlab
Serial.flush();
if (Serial.available() > 0) {
matlabData = Serial.readString();
int steps = matlabData.toInt();
myStep.step(steps);
}
}
  2 Commenti
Walter Roberson
Walter Roberson il 14 Lug 2018
Is the arduino code getting past the setup routine?
David Zamora
David Zamora il 16 Lug 2018
Yes the arduino code is getting past the setup routine.

Accedi per commentare.

Risposte (1)

Brian Hu
Brian Hu il 17 Lug 2018
Hi David,
From the information and code you provided, there are a few areas which could be causing issues for the motor functionality. Here are several methods you can try which may help you resolve your issue.
1. Make sure you are receiving the expected signal at the serial port on the Arduino. Certain factors, such as baud rate mismatch or library discrepancies between MATLAB and the Arduino can cause transmitted signals to differ from their expected values along the serial transmission.
2. It seems like you may be missing a call to set the “ReadAsyncMode” property of your serial port to ‘manual’ in your MATLAB code. From the documentation, the “readasync” function is ignored if “ReadAsyncMode” is ‘continuous’ for serial port applications. The documentation page for “readasync” is shown below.
3. If you can verify that the Arduino is transmitting and receiving expected values, it might be possible that your stepper motor is not receiving enough power to drive the steps. Depending on the power rating of the stepper motor, it is possible that the microcontroller alone is not able to supply enough power to drive the motor. If this is the case, you can resolve the power issue by either using a motor drive or implementing one through your own circuit design.
4. If you are interested in alternatives for facilitating stepper motor functionality, MATLAB offers a support package for Arduino hardware interfacing which may be able to provide further assistance for your issue. Here is a helpful link for more information.

Categorie

Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by