Steven Reubenstone
· New YorkU.S.
|  \$25.00
Completed

# Completed Signal Filtering and PID Tuning

Completed Signal Filtering and PID Tuning

Team – I want to speed up this process, as this Project is important to keep moving. So I am going to use a super power here, and invest \$25 into this task for who ever can complete it.

Control Systems
13 likes
Like
Harsha Alva
Mechanical Engineer + Software Dev
· MangaluruIndia

Try taking average of past measurements, like say 10. This method was successfully used by our seniors in college for an arduino controlled quadcopter.

1 like
Faiz Ur Rehman
Lets make this world a better place
· LahorePakistan

Steven Reubenstone and @team I have implemented PID on filtered accelerometer sensor data in make car move in straight direction only. PID is auto-tuned . And great thing is everything is done on standalone arduino. Simulation results are looking good. I have ready sketch for test run.Lets decide who is going to prototype this car so that we can test code.

1 like
Faiz Ur Rehman
Lets make this world a better place
· LahorePakistan

Now we can say that this task is completed successfully 😃

1 like
Steven Reubenstone
· New YorkU.S.

Can you upload all the code/work? I am going to mark this as complete. Payment will follow.

1 like
Steven Reubenstone
· New YorkU.S.

😃

1 like
Faiz Ur Rehman
Lets make this world a better place
· LahorePakistan

Steven Reubenstone here is the code for this task.

``````
#include <PID_v1.h>

int pwm=127;
double input, output, setpoint=150;
double kp=2,ki=0.5,kd=2;        //tuned values for PID controller

// These constants won't change:
#define leftmotor A5   //contro; pin for left motor
#define rightmotor A6   //control pin for right motor
#define sensorPin A1 //Sensor pin
#define filterSamples   9  // filterSamples should  be an odd number, no smaller than 3
int smoothData,sensSmoothArray [filterSamples]; // arra for holding raw sensor values for sensor1

PID accel_PID(&input, &output, &setpoint, kp, ki, kd, DIRECT);
// variables:
int sensorValue = 0;         // the sensor value
int sensorMin =690;        // minimum sensor value
int sensorMax = 3;           // maximum sensor value

void setup() {

Serial.begin(9600);
pinMode(sensorPin, INPUT);
pinMode(leftmotor, OUTPUT);
pinMode(rightmotor, OUTPUT);
//turn the PID on
accel_PID.SetMode(AUTOMATIC);
}

void loop() {

//*******Accelerometer data Input

sensorValue = map(sensorValue, sensorMin, sensorMax,0,300);

//*******Accelerometer data smoothing

smoothData = digitalSmooth(sensorValue, sensSmoothArray);
input=smoothData;

//*******PID contoller

accel_PID.Compute();

int pwm_out=output;

Serial.print(input);Serial.print("\t");Serial.println(pwm_out);
//output=map(output,0,255,-127,126);
//int pwm_out=pwm+output;
//Serial.print(input);Serial.print("\t");Serial.println(output);

//******Writing PWM on motor's control Pin
analogWrite(leftmotor, pwm_out);
analogWrite(rightmotor, pwm);
delay(5);
}

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){        // simple swap sort, sorts numbers from lowest to highest
done = 1;
for (j = 0; j < (filterSamples - 1); j++){
if (sorted[j] > sorted[j + 1]){     // numbers are out of order - 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));// +1 is to make up for asymetry caused by int rounding
k = 0;
total = 0;
for ( j = bottom; j< top; j++){
total += sorted[j];  // total remaining indices
k++;
// Serial.print(sorted[j]);
// Serial.print("   ");
}