0% found this document useful (0 votes)
219 views

PID Motor Position Control

This document describes a PID motor position control system using an Arduino. It includes code to initialize a PID controller library and set PID parameters to control a motor's position based on encoder feedback. The PID output drives an H-bridge motor controller. An interrupt routine reads the encoder pulses to continuously update the motor's position.

Uploaded by

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

PID Motor Position Control

This document describes a PID motor position control system using an Arduino. It includes code to initialize a PID controller library and set PID parameters to control a motor's position based on encoder feedback. The PID output drives an H-bridge motor controller. An interrupt routine reads the encoder pulses to continuously update the motor's position.

Uploaded by

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

/ PID

motor
position
control.
// Thanks to Brett Beauregard for his nice PID library
http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-
introduction/

#include <PinChangeInt.h>
#include <PID_v1.h>
#define encodPinA1 2 // Quadrature encoder A
pin
#define encodPinB1 8 // Quadrature encoder B
pin
#define M1 9 // PWM outputs to L298N
H-Bridge motor driver module
#define M2 10

double kp = 5 , ki = 1 , kd = 0.01; // modify for optimal


performance
double input = 0, output = 0, setpoint = 0;
long temp;
volatile long encoderPos = 0;
PID myPID(&input, &output, &setpoint, kp, ki, kd, DIRECT); // if motor
will only run at full speed try 'REVERSE' instead of 'DIRECT'

void setup() {
pinMode(encodPinA1, INPUT_PULLUP); // quadrature
encoder input A
pinMode(encodPinB1, INPUT_PULLUP); // quadrature
encoder input B
attachInterrupt(0, encoder, FALLING); // update encoder
position
TCCR1B = TCCR1B & 0b11111000 | 1; // set 31KHz PWM
to prevent motor noise
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(1);
myPID.SetOutputLimits(-255, 255);
Serial.begin (115200); // for debugging
}
void loop() {
setpoint = analogRead(0) * 5; // modify to fit
motor and encoder characteristics, potmeter connected to A0
input = encoderPos ; // data from
encoder
// Serial.println(encoderPos); // monitor motor
position
myPID.Compute(); // calculate new
output
pwmOut(output); // drive L298N H-
Bridge module
}

void pwmOut(int out) { // to H-Bridge


board
if (out > 0) {
analogWrite(M1, out); // drive motor CW
analogWrite(M2, 0);
}
else {
analogWrite(M1, 0);
analogWrite(M2, abs(out)); // drive motor
CCW
}
}

void encoder() { // pulse and


direction, direct port reading to save cycles
if (PINB & 0b00000001) encoderPos++; //
if(digitalRead(encodPinB1)==HIGH) count ++;
else encoderPos--; //
if(digitalRead(encodPinB1)==LOW) count --;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy