Hi,
It’s been a while since I got the shield, trying to make it work. I got hold of a arduino library called vma02Stepper, which I am putting the source code at the end of this post.
I can’t get it to work properly. Sometimes the motor reacts properly, most of the times doesn’t.
It would be nice to get this working.
Thanks,
Regards
VMA03Stepper.h
#include <Arduino.h>
class vma03Stepper
{
public:
vma03Stepper( byte pwmaPin, byte diraPin,
byte pwmbPin, byte dirbPin,
int stepsPerRevolution,
int speed,
int speedOffset);
void setSpeed(int speed);
int speed();
void begin();
void end();
void subStep(long steps);
void setDegree(int degree);
double degree();
int currentStep();
private:
byte _pwmaPin;
byte _diraPin;
byte _pwmbPin;
byte _dirbPin;
int _stepsPerRevolution;
int _speed;
int _speedOffset;
int _currStep = 0; //current step, corresponds to angular position, limits: 0…STEPS
int _sub = 0; //current halfstep within repeating sequence (8 halfsteps), limits: 0…7
//total count of halfsteps per full rotation (*2 for half steps!)
// e.g. 1.8deg stepper => 200 steps => 400
const int STEPS = 2 * _stepsPerRevolution;
double _degree;
};
VMA03Stepper.cpp
#ifndef vma03Stepper_h
#define vma03Stepper_h
#include “vma03Stepper.h”
vma03Stepper::vma03Stepper( byte pwmaPin, byte diraPin,
byte pwmbPin, byte dirbPin,
int stepsPerRevolution,
int speed,
int speedOffset)
{
_pwmaPin = pwmaPin;
_diraPin = diraPin;
_pwmbPin = pwmbPin;
_dirbPin = dirbPin;
_stepsPerRevolution = stepsPerRevolution;
_speed = speed;
_speedOffset = speedOffset;
_degree = 0;
// ensure to have the delay between the steps at least at speedOffest
if (_speed < _speedOffset)
{
_speed = _speedOffset;
}
pinMode(_pwmaPin, OUTPUT);
pinMode(_diraPin, OUTPUT);
pinMode(_pwmbPin, OUTPUT);
pinMode(_dirbPin, OUTPUT);
}
int vma03Stepper::speed()
{
return _speed;
}
void vma03Stepper::setSpeed(int newSpeed)
{
if (newSpeed < _speedOffset)
{
_speed = _speedOffset;
}
else
{
_speed = newSpeed;
}
}
void vma03Stepper::begin(){
digitalWrite(_pwmaPin, HIGH);
digitalWrite(_diraPin, HIGH);
digitalWrite(_pwmbPin, HIGH);
digitalWrite(_dirbPin, HIGH);
}
void vma03Stepper::end()
{
digitalWrite(_pwmaPin, LOW);
digitalWrite(_diraPin, LOW);
digitalWrite(_pwmbPin, LOW);
digitalWrite(_dirbPin, LOW);
}
int vma03Stepper::currentStep()
{
return _currStep;
}
double vma03Stepper::degree()
{
double r = _currStep * (360.00 / _stepsPerRevolution / 2);
return r;
}
void vma03Stepper::setDegree(int degree)
{
double futureStep = 0.00;
futureStep = degree / 360.00 * _stepsPerRevolution * 2;
int stepsToGo = futureStep - _currStep;
subStep(stepsToGo);
}
// This method is called in order to make the stepper motor make a number of sub steps (depending on your wiring).
// Variable steps is for number of steps (forwards = positive, backwards = negative)
// stepDelay is for waiting time between steps in microseconds => bigger means lower speed, smaller means higher speed
void vma03Stepper::subStep(long steps){
// The function will run for the amount of times called in the method.
// This is accomplished by a while loop, where it will subtract 1 from the amount after every run (forwards).
// In case of backward rotation it will add 1 to the negative number of steps until 0 is reached.
while(steps!=0)
{
if(steps>0){_currStep++;} //increment current halfstep (forward)
if(steps<0){_currStep--;} //decrement current halfstep (backward)
if(_currStep>STEPS){_currStep= _currStep-STEPS;} //position >360deg is reached => set position one turn back
if(_currStep<0){_currStep= _currStep+STEPS;} //position <0deg is reached => set position one turn forward
_sub = _currStep%8; //determine the next halfstep
switch(_sub)
{
case 0:
// Starting position (if repeated, full step (4))
// in this case, both our power are high.
// Therefore both coils are activated, with their standard polarities for their magnetic fields.
digitalWrite(_pwmaPin,HIGH);
digitalWrite(_pwmbPin,HIGH);
digitalWrite(_diraPin,HIGH);
digitalWrite(_dirbPin,HIGH);
break;
case 1:
//Half step (½)
//In this case, only out b-coil is active, still with it's stand polarity.
digitalWrite(_pwmaPin,HIGH);
digitalWrite(_pwmbPin,LOW);
digitalWrite(_diraPin,HIGH);
digitalWrite(_dirbPin,LOW);
break;
case 2:
//Full step (1)
// In this case, the b-coil is activated as in previous cases.
// But the a-coil now has it's direction turned on. So now it's active, but with the reversered polarity.
// By continuing this pattern (for reference: http://www.8051projects.net/stepper-motor-interfacing/full-step.gif) , you'll get the axis to turn.
digitalWrite(_pwmaPin,HIGH);
digitalWrite(_pwmbPin,HIGH);
digitalWrite(_diraPin,HIGH);
digitalWrite(_dirbPin,LOW);
break;
case 3:
// Half step (1½)
digitalWrite(_pwmaPin,LOW);
digitalWrite(_pwmbPin,HIGH);
digitalWrite(_diraPin,LOW);
digitalWrite(_dirbPin,LOW);
break;
case 4:
// Full step (2)
digitalWrite(_pwmaPin,HIGH);
digitalWrite(_pwmbPin,HIGH);
digitalWrite(_diraPin,LOW);
digitalWrite(_dirbPin,LOW);
break;
case 5:
// Half step (2½)
digitalWrite(_pwmaPin,HIGH);
digitalWrite(_pwmbPin,LOW);
digitalWrite(_diraPin,LOW);
digitalWrite(_dirbPin,LOW);
break;
case 6:
// Full step (3)
digitalWrite(_pwmaPin,HIGH);
digitalWrite(_pwmbPin,HIGH);
digitalWrite(_diraPin,LOW);
digitalWrite(_dirbPin,HIGH);
break;
case 7:
// Half step (3½)
digitalWrite(_pwmaPin,LOW);
digitalWrite(_pwmbPin,HIGH);
digitalWrite(_diraPin,LOW);
digitalWrite(_dirbPin,HIGH);
break;
}
delayMicroseconds(_speed); //Waiting time to next halfstep
if(steps>0){steps--;} //decrement of remaining halfsteps of forward rotation
if(steps<0){steps++;} //increment of remaining halfsteps of backward rotation
}
}
#endif