MATLAB Answers

Using ArduinoIO Package and seeedstudio motor shield to control a stepper

2 views (last 30 days)
Bella
Bella on 1 Jul 2013
Hi, I am using the MATLAB Support Package for Arduino (aka ArduinoIO Package)to run a stepper motor and read voltages. I am trying to run a stepper motor using a seeedstudio motor shield v1.0 paired with an arduino uno, and it is totally fine running the stepper in the arduino IDE but I haven't managed to budge it using MATLAB. I have been successful moving servos and reading analog voltages through MATLAB, so maybe the problem is the motor shield?
My code is below, adapted from one found here: http://www.seeedstudio.com/wiki/Motor_Shield_V1.0
function[]=stepperattempt()
%stepperattempt
%A program to sweep a stepper back and forth using Arduino I/O package
clear all;
a=arduino('/dev/tty.usbmodem3d11'); %creates arduino object
tlen=30;
spr=200;
a.stepperSpeed(1,60);
a.pinMode(9,'OUTPUT');
a.pinMode(10,'OUTPUT');
a.digitalWrite(9,1);
a.digitalWrite(10,1);
for i=1:tlen
a.stepperStep(1,'forward','double',spr);
pause(.5);
a.stepperStep(1,'backward','double',spr);
pause(.5);
end
a.delete(); %ALWAYS DO THIS
end
The problem with this code is that the stepper did not move, and entering command line, um, commands doesn't move it either.
Any help would be greatly appreciated!

Answers (1)

Bella
Bella on 3 Jul 2013
Edited: Bella on 3 Jul 2013
Hi, if anybody ever checks this again, the key is rewriting the srv or motorsrv code that comes in the package. You need to change it from the adafruit library, probably to the built-in stepper library. Below, I'll post every part that I made changes to in order to use a different stepper motor.
I made these changes:
#include <AFMotor.h>
#include <Servo.h>
#include <Stepper.h> //I added this so I could run a non-adafruit stepper
/* create and initialize motors */
const int stepsPerRevolution=200;
Stepper myStepper(stepsPerRevolution,8,11,12,13); /* I replaced the AFMotor
initializer with the Stepper library equivalent */
AF_DCMotor dcm1(1, MOTOR12_64KHZ); /* dc motor #1, 64KHz pwm */
AF_DCMotor dcm2(2, MOTOR12_64KHZ); /* dc motor #2, 64KHz pwm */
AF_DCMotor dcm3(3, MOTOR12_64KHZ); /* dc motor #3, 64KHz pwm */
AF_DCMotor dcm4(4, MOTOR12_64KHZ); /* dc motor #4, 64KHz pwm */
/* s=190 or 191 means STEPPER MOTOR SET SPEED ******* */
case 190:
/* the second received value indicates the motor number
from abs('1')=49, motor1, to abs('2')=50, motor4 */
if (val>48 && val<51) {
stm=val-48; /* calculate motor number */
s=191; /* next we will need to get value from serial */
}
else {
s=-1; /* if value is not a stepper then return to -1 */
}
break; /* s=190 taken care of */
case 191:
/* the third received value indicates the speed in rpm */
/* Bellacode */
myStepper.setSpeed(val); /* I replaced the AFMotor call
with the Stepper library equivalent */
s=-1; /* we are done with set speed so go to -1 next */
break; /* s=191 taken care of */
/* s=200 or 201 means STEPPER MOTOR STEP/RELEASE **** */
case 200:
/* the second received value indicates the motor number
from abs('1')=49, motor1, to abs('2')=50, motor4 */
if (val>48 && val<51) {
stm=val-48; /* calculate motor number */
s=201; /* we still need stuff from serial */
}
else {
s=-1; /* if value is not a motor then return to -1 */
}
break; /* s=200 taken care of */
case 201:
/* the third received value indicates forward, backward,
release, with characters 'f', 'b', 'r', respectively,
that have ascii codes 102, 98 and 114 */
switch (val) {
/* Bellacode */
case 102:
dir=1; //I changed this from FORWARD
s=202;
break;
case 98:
dir=-1; //I changed this from BACKWARD
s=202;
break;
case 114: /* release and return to -1 here */
//here i removed everything because the stepper library has no release
s=-1;
break;
default:
s=-1; /* unrecognized character, go to -1 */
break;
}
break; /* s=201 taken care of */
case 202:
/* the third received value indicates the style, single,
double, interleave, microstep, 's', 'd', 'i', 'm'
that have ascii codes 115,100,105 and 109 */
switch (val) {
/* Bellacode */
case 115:
sty=1; //used to be SINGLE
s=203;
break;
case 100:
sty=2; //used to be DOUBLE
s=203;
break;
case 105:
sty=0; //INTERLEAVE
s=203;
break;
case 109:
sty=0; //MICROSTEP
s=203;
break;
default:
s=-1; /* unrecognized character, go to -1 */
break;
}
break; /* s=201 taken care of */
case 203:
/* Bellacode */
/* the last received value indicates the number of
steps, */
val=sty*dir*val;
myStepper.step(val); /* essentially, I'm a genius */
s=-1; /* we are done with step so go to -1 next */
break; /* s=203 taken care of */
And now this works! This is just a MATLAB .m function:
function[]=stepperattempt()
%stepperattempt - Written on 7/1/13 by Bellaella
%A program to sweep a stepper back and forth 360 degrees
clear all;
a=arduino('/dev/tty.usbmodem3d11'); %creates arduino object
tlen=25; %just an arbitrary time length
spr=200; %the steps per revolution of the stepper, aka, 200*1.8 degrees=360
%P.S. 1.8 degrees is the step angle of our machine
a.stepperSpeed(1,60); %the stepper will now move at 60 rpm
a.pinMode(9,'OUTPUT'); %This is necessary because the seeedstudio motor is
a.pinMode(10,'OUTPUT'); %attached to these pins (9 and 10)
a.digitalWrite(9,1);
a.digitalWrite(10,1);
for i=1:tlen
a.stepperStep(1,'forward','single',spr); %this just moves it forward
pause(.5); %1 revolution
a.stepperStep(1,'backward','single',spr); %and this moves it backward
pause(.5); %1 revolution
end
a.delete(); %ALWAYS DO THIS
end
Huzzah!

Communities

More Answers in the  Power Electronics Control

Community Treasure Hunt

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

Start Hunting!