hello guys, Iam asked to build a self balancing robot. my problem is that using gyroscpe i can know the angle, but so far i used PID to help it balance when tilted to 1 angle, i dont know how to use it to the opposite angle. i have no idea how to use two PIDs in same code, my first time.
here is my code:
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <PID_v1.h>
#define PIN_OUTPUT 3
MPU6050 mpu6050(Wire);
long timer = 0;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=15, Ki=0, Kd=2;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
//initialize the variables we're linked to
Setpoint = -90;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop() {
mpu6050.update();
Input = mpu6050.getAngleY();
analogWrite(PIN_OUTPUT, Output);
myPID.Compute();
Serial.print(" anglex : ");Serial.print(mpu6050.getAngleY());
Serial.println("!!");
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.