0% found this document useful (0 votes)
13 views2 pages

Controls Code

Uploaded by

calebhagen2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Controls Code

Uploaded by

calebhagen2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <FastPID.

h>

#include "ADS1115-Driver.h"
ADS1115 ads1115 = ADS1115(ADS1115_I2C_ADDR_GND);
uint16_t adcValue = 0;

const int stepX = 2; //stepper pin


const int dirX = 5; //direction switch pin
const int enPin = 8; //motor enable pin
const int analogInPin = A0; // Analog input pin that the potentiometer is attached
to
volatile int currentPosition = 0; //rapidly changing stepper position indicator
(goes positive and negative)
int16_t setpointShift=0;
volatile int previousPosition = 0;

int staticSetpoint = 12200;


int activeSetpoint = 0;
int stepTarget = 0;

volatile int loopCounter = 0; //loop loopCounter

float Kp=1.0, Ki=0.0, Kd=1.0, Hz=10.0;


int output_bits = 16;
bool output_signed = true;
FastPID myPID(Kp, Ki, Kd, Hz, output_bits, output_signed);

// FUNCTIONS ====================================================================

void setupStepper() {
pinMode(stepX,OUTPUT);
pinMode(dirX,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
digitalWrite(dirX,HIGH);
}

static int sign(long val) {


if (val < 0) return -1;
if (val==0) return 0;
return 1;
}

void makeStep(int steps) {


if(steps < 0){ digitalWrite(dirX, LOW); } else {digitalWrite(dirX, HIGH);} //set
DIRECTION of stepper driver
if(abs(steps) > 8){ //make step! and then change the current position of the
carriage
digitalWrite(stepX,HIGH);
delayMicroseconds(5);
digitalWrite(stepX,LOW);
if(steps > 0){ currentPosition = (currentPosition + 1); } else
{ currentPosition = (currentPosition - 1); }

}
delayMicroseconds(700);
}
void setupADC() {
ads1115.reset();
ads1115.setDeviceMode(ADS1115_MODE_SINGLE);
ads1115.setDataRate(ADS1115_DR_250_SPS);
ads1115.setPga(ADS1115_PGA_6_144);
ads1115.setMultiplexer(ADS1115_MUX_AIN0_GND);

adcValue = readADC();
}

uint16_t readADC() {
ads1115.startSingleConvertion();
delayMicroseconds(25); // The ADS1115 needs to wake up from sleep mode and
usually it takes 25 uS to do that
while (ads1115.getOperationalStatus() == 0) { makeStep(stepTarget); }
return ads1115.readRawValue();
}

// RUN MAIN =====================================================

void setup() {
setupStepper();
pinMode(analogInPin, INPUT);
Serial.begin(115200);

if (myPID.err()) {
Serial.println("There is a configuration error!");
for (;;) {}
}
}

void loop() {
//setpointShift = (previousPosition - currentPosition);
Serial.println(String(setpointShift));
activeSetpoint = staticSetpoint + setpointShift;
stepTarget = readADC() - activeSetpoint;
if(loopCounter % 10 == 0) {
//loopCounter = 0;
setpointShift = 0;//(previousPosition - currentPosition);
previousPosition = currentPosition;
//setpointShift = myPID.step(0, currentPosition);

}
makeStep(int(stepTarget));
loopCounter++;
}

You might also like