I tried to recreate the sun tracking hat done by a youtuber (william osman) and I ran into a problem in my code, more precisely I don't understand the code. It uses PID to control a servo which is 360 modded (so with a fixed resistor to replace potentiometer so servo will never reach where it wants to go and continues spinning) Can someone help explain the following lines of code? Thanks!
Part of code that needs explaining:
float error =sensLeft - sensRight;
float pid_p, pid_i, pid_d;
pid_p = error * 20;
pid_i = 0.0;
pid_d = 2.0;
byte motor_pwm = SERVO_NEUTRAL + pid_p + pid_i + pid_d;
hatservo.write(motor_pwm);
For this line, will the servo never stop spinning even when the error is 0, as it never reaches the neutral position? (modded)
byte motor_pwm = SERVO_NEUTRAL + pid_p + pid_i + pid_d;
Full code:
#include <Servo.h>
#define LIGHTSENS_RIGHT A1
#define LIGHTSENS_LEFT A0
#define HATSERVO_PIN 11 //D11pin
#define SERVO_NEUTRAL 93 //
#define PID_P 20
#define PID_I 0.0
#define PID_D 2.0 //
Servo hatservo;
void setup() {
Serial.begin(9600);
hatservo.attach(HATSERVO_PIN);
}
void loop() {
trackSun();
}
int8_t trackSun(void){
int sensRight = analogRead(LIGHTSENS_RIGHT);
int sensLeft = analogRead(LIGHTSENS_LEFT);
float error =sensLeft - sensRight;
float pid_p, pid_i, pid_d;
pid_p = error * 20;
pid_i = 0.0;
pid_d = 2.0;
byte motor_pwm = SERVO_NEUTRAL + pid_p + pid_i + pid_d;
hatservo.write(motor_pwm);
delay (3);
}
Thanks guys!