I'm having trouble using the ENABLE pin on my microstepper driver, I'm not sure of the exact model but it looks exactly like the one pictured here
Here's my code:
#define MAX_LEN 8
#define MIN_INCREMENT 0.5
#define LIMIT_SWITCH 3
#define ENABLE 10
float buoyancymass = 4401.425;
float centrebuoyancy = 14.164;
float mainarm = 14.3;
float nosearm = 1.8;
float adc = 11.4;
float meanadc = 22.65;
volatile byte enable_state = LOW;
#include <Stepper.h>
const int stepsPerRevolution = 1600; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
float stepinch = 0.00524; // 1 step = x"
void setup() {
// initialize the serial port:
Serial.begin(9600);
pinMode(LIMIT_SWITCH, INPUT_PULLUP);
pinMode(ENABLE, OUTPUT);
//enable motor
digitalWrite(ENABLE, enable_state);
//interrupt
attachInterrupt(digitalPinToInterrupt(LIMIT_SWITCH), limitbrake, FALLING);
// set the speed
myStepper.setSpeed(30);
}
void limitbrake() {
digitalWrite(ENABLE, !enable_state);
}
The problem I'm noticing is that when I set the pinMode(ENABLE,OUTPUT);
my stepper doesn't draw current and therefor, doesn't drive. This occurs regardless of whether I set my enable_state
to HIGH or LOW.
Funny enough, when I set it to pinMode(ENABLE,INPUT);
it does draw current and drive the motor, but unfortunately my limit switch which I've attached an interrupt to doesn't disable the motor when its pressed.
Before you ask, I've tested the enable pin on my driver and my limit switch circuitry in other programs and they both work. I'm just having trouble with putting it together.
Here is the rest of my code if you were wondering.
float calc_margin(float pos, float mass) {
// calculate margin
Serial.print("Distance: ");Serial.println(pos);
float batterymoment = mass*pos;
Serial.println(batterymoment);
float totalmass = mass + 12404;
Serial.println(totalmass);
float totalmoment = 149650+batterymoment;
Serial.println(totalmoment);
float buoyancymoment = buoyancymass*centrebuoyancy;
Serial.println(buoyancymoment);
float wheelmoment = totalmoment - buoyancymoment;
Serial.println(wheelmoment);
float wheelweight = totalmass - buoyancymass;
Serial.println(wheelweight);
float cgwheels = wheelmoment/wheelweight;
Serial.println(cgwheels);
float maindistance = mainarm - cgwheels;
Serial.println(maindistance);
float nosedistance = cgwheels - nosearm;
Serial.println(nosedistance);
float mainmass = (((wheelweight)*(1-((maindistance)/(maindistance+nosedistance))))/2);
Serial.println(mainmass);
float nosemass = (((wheelweight)*(1-((nosedistance)/(nosedistance+maindistance))))/2);
Serial.println(nosemass);
float mainmoment = mainmass*mainarm;
Serial.println(mainmoment);
float nosemoment = nosemass*nosearm;
Serial.println(nosemoment);
float netmoment = buoyancymoment+(mainmoment*2)+(nosemoment*2);
Serial.println(netmoment);
float netcg = netmoment/totalmass;
Serial.println(netcg);
float staticmargin = adc - netcg;
Serial.println(staticmargin);
float answer = staticmargin/meanadc;
Serial.println(answer);
float percentage = answer*100;
Serial.print(percentage); Serial.println("%");
float result = answer;
return result;
}
boolean check_margin(float test_margin) {
if (test_margin > -0.04 && test_margin < -0.02)
return true;
return false;
}
float armcalc(float mass) {
for(float x=0;x<MAX_LEN;x+=MIN_INCREMENT) {
// calculate margin
float margin = calc_margin(x,mass);
if (check_margin(margin)) {
return x;
}
}
return -1;
}
float stepcalc(float arm){
int arminch = arm*12;
float numberofsteps = arminch/stepinch;
int result = numberofsteps;
Serial.print("Calculated steps: "); Serial.println(result);
return result;
}
void loop() {
// read input
Serial.println("Enter mass: ");
while (!Serial.available());
float mass = Serial.parseFloat();
float arm = armcalc(mass);
if (arm < 0) {
// Error message
Serial.println("Error!");
return;
}
Serial.print("Arm distance is "); Serial.println(arm);
// step
const int steplength = (int) stepcalc(arm);
Serial.print("Number of steps is "); Serial.println(steplength);
myStepper.step(-steplength);
while(1);
}
-
An interrupt service routine which toggles state sounds like you are just asking for trouble. Even if it is not the cause of your problem, you would likely be better off with logic to assertively set the correct level. Are you aware of the issue of "contact bounce"? That could see you toggle the motor state a somewhat random number of times, with the result depending if that is even or odd...Chris Stratton– Chris Stratton2016年04月14日 19:48:08 +00:00Commented Apr 14, 2016 at 19:48
-
I drive those with 12 Volts from a printer port. I remember mine should be also 5V-compatible. Plus, I have the feeling, that you don't exactly know, how these drivers work...moestly– moestly2016年04月14日 19:54:44 +00:00Commented Apr 14, 2016 at 19:54
1 Answer 1
These drivers differ from the H-Bridge-design described here. You don't need to generate pulses for the coils, as you would do with a classic H-Bridge.
Connect your motor to
A+
,A-
,B+
,B-
(a is one coil, b the other)Connect Power (guess 12+ volts, mine take up to 48V) to
GND
andV+
Either connect 5V to
ENA+(+5V)
or a free arduino Pin andENA-(ENA)
to GND. This pin enables/disables the driver. If no input is present at this pin, the driver will not work.Connect
DIR+(+5V)
to a free arduino pin andDIR-(DIR)
toGND
. This pin controls the direction.HIGH
makes the motor turn right, whileLOW
on this pin makes the motor turn left.Connect
PUL+(+5V)
to a free arduino pin andPUL-(PUL)
toGND
. This pin controls the speed. The more pulses/second one applies, the faster the motor turns. (Up to a limit, of course).
Sketch should be easy.
Plus: You could turn around the control connections, by hardwiring all +5V
pins to 5V and use INPUT_PULLUP
to switch the current flow on the negative lines. I know that these drivers are optical isolated and draw as little as 10 mA from a pin, if at all. So you should be safe.
-
I fixed the enable function and I'm able to control the motor, but I'm still unable to use my limit switch to set the enable pin on or off. Would you have any idea as to why that is?Jertise– Jertise2016年04月14日 21:36:11 +00:00Commented Apr 14, 2016 at 21:36