Hi everyone
I couldn’t decide whether this should go in the hardware, sensors or programming section since it involves all(more of a software solution I think I need though), so I hope this is the right place.
So I’m doing a little test with trying to set up proportional control with a potentiometer and a 3000 step stepper motor(it’s highly geared, which is why it has 3000 steps), so if the pot moves 50 degrees, to stepper will as well.
All the hardware is working and I’ve done some basic smoothing for the pot data, however my stepper motor is still jittering around quite a bit. So when the pot is still and the filtering kicks in everything is fine, but fine movements of a difference is very small it causes the jittering.
So my question is, does anyone have any information on programming techniques that may help combat this jittering. I could add a threshold so that if the movement different between the stepper and the pot is less than something like 1 degree (remember there are 3000 steps so resolution is ~0.12 degrees), then it won’t move, but in my eyes that just decreases the resolution and perhaps even increases jittering, so I was hoping anyone had some ideas for a more advanced approach than that.
It’s fine when there is a lot of reasonable difference in movement, but it’s when things are at a stand still and just slightly touched/moved that the jittering arises. I was going to also experiment using a UM7 sensor for proportional input, however there will be significantly more variation from the output of that sensor, which is why I was starting with just a pot.
For reference here is my code for you to view, if interested. I was originally stepping the stepper manually but then decided to use accelstepper library due to it’s wide variety of functions that may be useful in the future and makes the code more compact.
#include <AccelStepper.h>
#define AIN A0
#define filterSamples 13
int smoothArray[filterSamples];
int rawData, smoothData;
AccelStepper stepper(1, 12, 13); //first motor, clock pin 12, direction pin 13
void setup() {
Serial.begin(9600);
stepper.setMaxSpeed(1000);
}
void loop() {
rawData = analogRead(AIN);
smoothData = digitalSmooth(rawData, smoothArray);
rawData = map(rawData, 0, 1023, 0, 3000);
smoothData = map(smoothData, 0, 1023, 0, 3000);
Serial.print(rawData);
Serial.print(" ");
Serial.println(smoothData);
stepper.moveTo(smoothData);
stepper.setSpeed(1000);
stepper.runSpeedToPosition();
}
int digitalSmooth(int rawIn, int *sensSmoothArray) { // "int *sensSmoothArray" passes an array to the function - the asterisk indicates the array name is a pointer
int j, k, temp, top, bottom;
long total;
static int i;
// static int raw[filterSamples];
static int sorted[filterSamples];
boolean done;
i = (i + 1) % filterSamples; // increment counter and roll over if necc. - % (modulo operator) rolls over variable
sensSmoothArray[i] = rawIn; // input new data into the oldest slot
for (j=0; j<filterSamples; j++){ // transfer data array into anther array for sorting and averaging
sorted[j] = sensSmoothArray[j];
}
done = 0; // flag to know when we're done sorting
while(done != 1){ // sort lowest to highest
done = 1;
for (j = 0; j < (filterSamples - 1); j++){
if (sorted[j] > sorted[j + 1]){ // swap
temp = sorted[j + 1];
sorted [j+1] = sorted[j] ;
sorted [j] = temp;
done = 0;
}
}
}
// throw out top and bottom 15% of samples - limit to throw out at least one from top and bottom
bottom = max(((filterSamples * 15) / 100), 1);
top = min((((filterSamples * 85) / 100) + 1 ), (filterSamples - 1));
k = 0;
total = 0;
for ( j = bottom; j< top; j++){
total += sorted[j]; // total remaining indices
k++;
}
return total / k; // divide by number of samples
}
Thanks, any help or advice appreciated