Hello and thank you for taking your time to read this.
I have built an inverted pendulum robot. It is composed of three platforms made of plexiglass spaced 50mm apart, using 4 8mm threads.
I have confirmed that the motors behave as they should and that the accelerometer works fine through the use of the serial monitor.
The electronics are as follows: Arduino Nano ESP32, the drivers for the motors are a4998 and the motors are NEMA17, powered by a 2Ah 12.4V battery pack. The motor drivers have had their current limit set properly, the wheels spin fine.
The issue is the following: I believe that the robot is too heavy because when i begin tuning the PID regulator, no matter how high I set the P term the robot never self balances itself. It just jitters. It "almost" balances itself, making me to believe it is too heavy.
#include <Adafruit_MPU6050.h>
#include <Wire.h>
#include <AccelStepper.h>
#include <PID_v1.h>
Adafruit_MPU6050 mpu;
int MPU_addr = 0x68;
float const_calib_X = 0 ;
float const_calib_Y = 0 ;
float const_calib_Z = 0.2;
#define dirPin_X 2
#define stepPin_X 5
#define dirPin_Y 3 //3 Y //4 Z
#define stepPin_Y 6 //6 Y //7 Z
#define enablePin 8
//PID
double Kp = 100;
double Ki = 0;
double Kd = 0;
double input=0;
double output=0;
double setpoint=0;
PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);
float unghi; //trimite valoarea unghiului de la accelerometru
const int stepsPerRevolution = 200;
const int pulsesPerRevolution = 200;
int motorSpeed1;
int motorSpeed2;
unsigned long prevMillis = 0;
int revolutions = 0;
int led_R = 14;
int led_G = 15;
int led_B = 16;
float RateRoll, RatePitch, RateYaw;
float RateCalibrationRoll, RateCalibrationPitch, RateCalibrationYaw;
int RateCalibrationNumber;
float AccX, AccY, AccZ;
float AngleRoll, AnglePitch;
uint32_t LoopTimer;
float KalmanAngleRoll=0, KalmanUncertaintyAngleRoll=2*2;
float KalmanAnglePitch=0, KalmanUncertaintyAnglePitch=2*2;
float Kalman1DOutput[]={0,0};
int raspuns_PID = 0;
// AccelStepper instante
AccelStepper stepper1(1, stepPin_X, dirPin_X);
AccelStepper stepper2(1, stepPin_Y, dirPin_Y);
// FIlter smoothing angle
const float alpha_angle_filter = 1;
// MPU
float filteredAngleRoll = 0.0;
float filteredAnglePitch = 0.0;
//Deadzone for moverobot()
double deadzone = 1;
void gyro_signals() {
Wire.beginTransmission(0x68);
Wire.write(0x1A);
Wire.write(0x05);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(0x10);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0x68, 6);
int16_t AccXLSB = Wire.read() << 8 | Wire.read();
int16_t AccYLSB = Wire.read() << 8 | Wire.read();
int16_t AccZLSB = Wire.read() << 8 | Wire.read();
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x8);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(0x68, 6);
int16_t GyroX = Wire.read() << 8 | Wire.read();
int16_t GyroY = Wire.read() << 8 | Wire.read();
int16_t GyroZ = Wire.read() << 8 | Wire.read();
RateRoll = (float)GyroX / 65.5;
RatePitch = (float)GyroY / 65.5;
RateYaw = (float)GyroZ / 65.5;
AccX = (float)AccXLSB / 4096;
AccY = (float)AccYLSB / 4096;
AccZ = (float)AccZLSB / 4096 - const_calib_Z;
AngleRoll = atan(AccY / sqrt(AccX * AccX + AccZ * AccZ)) * 1 / (3.142 / 180);
AnglePitch = -atan(AccX / sqrt(AccY * AccY + AccZ * AccZ)) * 1 / (3.142 / 180);
filteredAngleRoll = alpha_angle_filter * AngleRoll + (1 - alpha_angle_filter) * filteredAngleRoll;
filteredAnglePitch = alpha_angle_filter * AnglePitch + (1 - alpha_angle_filter) * filteredAnglePitch;
}
void moveRobot()
{
if (filteredAngleRoll > deadzone) {
// Move forward
motorSpeed1 = raspuns_PID ;
motorSpeed2 = -raspuns_PID ;
stepper1.setSpeed(motorSpeed1);
stepper2.setSpeed(motorSpeed2);
stepper1.runSpeed();
stepper2.runSpeed();
} else if (filteredAngleRoll < -deadzone) {
// Move backward
motorSpeed1 = -raspuns_PID ;
motorSpeed2 = raspuns_PID ;
stepper1.setSpeed(motorSpeed1);
stepper2.setSpeed(motorSpeed2);
stepper1.runSpeed();
stepper2.runSpeed();
} else {
// Stop if input (AngleRoll) is within the deadzone
stepper1.setSpeed(0);
stepper2.setSpeed(0);
motorSpeed1 = 0 ;
motorSpeed2 = 0 ;
stepper1.runSpeed();
stepper2.runSpeed();
}
}
void setup() {
pinMode(led_R,OUTPUT);
pinMode(led_G,OUTPUT);
pinMode(led_B,OUTPUT);
//KALMAN+MPU START
Serial.begin(115200);
// while (!Serial);
if (!mpu.begin()) {
Serial.println("Sensor init failed");
while (1)
yield();
digitalWrite(led_R,HIGH);
}
Serial.println("Found a MPU6050 sensor");
for (RateCalibrationNumber=0; RateCalibrationNumber<2000; RateCalibrationNumber ++) {
gyro_signals();
RateCalibrationRoll+=RateRoll;
RateCalibrationPitch+=RatePitch;
RateCalibrationYaw+=RateYaw;
delay(1);
}
RateCalibrationRoll/=2000;
RateCalibrationPitch/=2000;
RateCalibrationYaw/=2000;
LoopTimer=micros();
// KALMAN+MPU FINISH
//CNC SHIELD
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW); //LOW=PORNIT
//MOTORS
pinMode(stepPin_X, OUTPUT);
pinMode(dirPin_X, OUTPUT);
pinMode(stepPin_Y, OUTPUT);
pinMode(dirPin_Y, OUTPUT);
stepper1.setMaxSpeed(1000);
stepper2.setMaxSpeed(1000);
pinMode(led_R,OUTPUT);
pinMode(led_G,OUTPUT);
pinMode(led_B,OUTPUT);
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(-1000, 1000);
}
void loop() {
gyro_signals();
double setpoint = 0;
input = abs(filteredAngleRoll);
myPID.Compute();
raspuns_PID = static_cast<int>(output);
moveRobot();
}
