Balancebot Code
Balancebot Code
#include <PID_v1.h>
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor
measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor
measurements
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and
gravity vector
//omat muuttujat
float kallistuskulma;
double motor_r, motor_l, pwm_r, pwm_l, last_motor_r, last_motor_l;
float Kp, Ki, Kd;
char * newvalue;
float newvalues[3];
char saatoarvochar[17];
String saatoarvostring, Kp_new, Ki_new, Kd_new;
//MAARITELLAAN ENKOODERIEN PINNIT HUOM HUOM HUOM HUOM !!!! ONE-TWO JAKO SAATTAA
MENNA PUOLITTAIN RISTIIN MOOTTORIEN KANSSA -> TARKISTA KUN OFFSETTAAT!
//encoder one
int encoder1Pos = 0;
int phaseA1 = 10;
int phaseB1 = 11;
//encoder two
int phaseA2 = 12;
int phaseB2 = 13;
// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has
gone high
void dmpDataReady() {
mpuInterrupt = true;
}
// ================================================================
// === INITIAL SETUP ===
// ================================================================
void setup() {
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);
// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") :
F("MPU6050 connection failed"));
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(109);
mpu.setYGyroOffset(1);
mpu.setZGyroOffset(135);
mpu.setXAccelOffset(317); // These offset-values are values for this very
robot.
mpu.setYAccelOffset(2120); // The values have been calculated with a specific
calibration
mpu.setZAccelOffset(1107); // software in the MPU6050 library package.
// set our DMP Ready flag so the main loop() function knows it's okay to
use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;
//PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID
Kp=20 ;
Ki=5 ; //TASSA SYOTETAAN SAATOARVOT JA PRINTATAAN NE SERIALIIN JOTTA PYSYTAAN
KARRYILLA
Kd=0.67 ;
last_motor_r=0;
last_motor_l=0;
}
// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================
void loop() {
// if programming failed, don't try to do anything
if (!dmpReady) return;
// check for overflow (this should never happen unless our code is too
inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));
// otherwise, check for DMP data ready interrupt (this should happen
frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
#ifdef OUTPUT_PITCH_PID
Serial.print("kallistuskulma: ");
Serial.print(kallistuskulma);
#endif
//MITATAAN JANNITE
//TAHAN TAYTYIS KOODATA MOVINGAVERAGE-FILTTERI JOTTA VIRHEMITTAUKSET EI
SEKOTTAIS SAATOA
voltage_in = analogRead(analogInPin);
voltage = map(voltage_in, 0, 1023, 0, 4760)* 200/119;
#ifdef OUTPUT_VOLTAGE
//Serial.print(" voltage (bitteina): ");
//Serial.print(voltage_in);
Serial.print(" voltage (mV): ");
Serial.print(voltage);
#endif
//PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID
PID
Serial.println("/");Serial.println("/");Serial.println("/");
Serial.println("////////////////////////////////////////////////");
Serial.println("SAATOARVOT PAIVITETTY: ");
Serial.print("Kp= "); Serial.println(balancePID.GetKp());
Serial.print("Ki= "); Serial.println(balancePID.GetKi());
Serial.print("Kd= "); Serial.println(balancePID.GetKd());
Serial.println("////////////////////////////////////////////////");
Serial.println("/");Serial.println("/");Serial.println("/");
#ifdef OUTPUT_PITCH_PID
Serial.print(" PID-saatimen arvo: ");
Serial.println(Output); //Output valilla [ -255, +255 ]
#endif
//PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID
PID
// //JANNITEKOMPENSAATIO
// if (voltage >= 7400) {
// Output = Output * ( 1-0.000125*(voltage-7400));
// }
//SUUNNANVAIHTOPULSSI
if (last_motor_l * motor_l < 0) {pwm_l = 255;}
if (last_motor_r * motor_r < 0) {pwm_r = 255;}
//SUUNNANVAIHTOPULSSISYSTEEMIN MUUTTUJASIJOITUS
last_motor_l = motor_l;
last_motor_r = motor_r;
Serial.print("/");
}
}