curiores / arduinotutorials Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
When I write your code, the motor goes to the desired position, but the Arduino triggers the motor twice, that is, if it is supposed to go 180 degrees at the beginning, it goes to 180 degrees and stops, and then it turns another 180 degrees and then stops. How can I solve this? ?
#include <util/atomic.h> // For the ATOMIC_BLOCK macro
#define ENCA 2 // YELLOW
#define ENCB 3 // WHITE
#define PWM 5
#define IN2 6
#define IN1 7
volatile int posi = 0; // specify posi as volatile: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
long prevT = 0;
float eprev = 0;
float eintegral = 0;
void setup() {
Serial.begin(9600);
pinMode(ENCA, INPUT);
pinMode(ENCB, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
pinMode(PWM, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
Serial.println("target pos");
}
void loop() {
// set target position
int target = 112;
// PID constants
float kp = 5;
float kd = 0.5;
float ki = 1;
// time difference
long currT = micros();
float deltaT = ((float)(currT - prevT)) / (1.0e6);
prevT = currT;
// Read the position in an atomic block to avoid a potential
// misread if the interrupt coincides with this code running
// see: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
int pos = 0;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
pos = posi;
}
// error
int e = target - pos;
// derivative
float dedt = (e - eprev) / (deltaT);
// integral
eintegral = eintegral + e * deltaT;
// control signal
float u = kp * e + kd * dedt + ki * eintegral;
// motor power
float pwr = fabs(u);
if (pwr > 255)
{
pwr = 255;
}
// motor direction
int dir = 1;
if (u < 0)
{
dir = -1;
}
// signal the motor
setMotor(dir, pwr, PWM, IN1, IN2);
// store previous error
eprev = e;
// Print target and position
Serial.print(target);
Serial.print(" ");
Serial.println(pos);
}
void setMotor(int dir, int pwmVal, int pwm, int in1, int in2)
{
analogWrite(pwm, pwmVal);
if (dir == 1)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
else if (dir == -1)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
else
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
void readEncoder()
{
int b = digitalRead(ENCB);
if (b > 0)
{
posi++;
}
else
{
posi--;
}
} my code
Your explanations are very clear and impressive.
I am trying to implement high pass filter using the same method you mentioned. But I am not familier with the structure can you please guide on what value should i put here?``
# Low-pass filter w0 = 2*np.pi*5; # pole frequency (rad/s) num = w0 # transfer function numerator coefficients den = [1,w0] # transfer function denominator coefficients lowPass = signal.TransferFunction(num,den) # Transfer function
I got that, I must have to made changes here but dont know what changes should i make to get it work for high pass filter.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.