Hello! Up until now I have been using numerical approximations to do some motor speed PID control. I came across this library: GitHub - natnqweb/Motor_PID: //author:: Natan Lisowski // this library let you control motor with pwm signal
The sample code are controlling position not speed. I don't have enough knowledge/time to figure out how to modify this code to control speed instead. Could someone please help me?
Here's the code I would like to modify:
// remember that l298n need 3 volt over DC motor nominal voltage to run full speed
//you can supply even more voltage but then limit it with pwm like i did here
#include <Motor_PID.h>
#define ENCA 2 // YELLOW from polulu encoder
#define ENCB 3 // WHITE from polulu encoder
#define IN2 10 //in2 from driver // pins to control direction
#define IN1 11 //in1 from driver // pins to control direction
#define PWM_PIN 5 //pwm to control speed
int target = 900;
// PID constants
float kp = 1;
float kd = 0.1;
float ki = 0.02;
int pwm_lower_limit = 0;//full range
int pwm_upper_limit = 255;//full range
Motor motor1(ENCA, ENCB, IN2, IN1, PWM_PIN, pwm_lower_limit, pwm_upper_limit);
void setup()
{
motor1.init(kp, kd, ki);
motor1.set_target(target);
}
void loop()
{
motor1.start();
if (motor1.get_position() == target) motor1.turn_off();
}
I'll attach a schematic just in case:
Thanks in advance