Help Understanding This Program
beware: if frustrated overambitious newbies not continue.
can please explain code top bottom? written this guy. includes these libraries:
pid
lmotorcontroller
i2cdev
mpu6050
please explain reason every line being there in simpler terms already.
here is:
(it self-balancing robot)
can please explain code top bottom? written this guy. includes these libraries:
pid
lmotorcontroller
i2cdev
mpu6050
please explain reason every line being there in simpler terms already.
here is:
(it self-balancing robot)
code: [select]
#include <pid_v1.h>
#include <lmotorcontroller.h>
#include "i2cdev.h"
#include "mpu6050_6axis_motionapps20.h"
#if i2cdev_implementation == i2cdev_arduino_wire
#include "wire.h"
#endif
#define min_abs_speed 30
mpu6050 mpu;
// mpu control/status vars
bool dmpready = false; // set true if dmp init successful
uint8_t mpuintstatus; // holds actual interrupt status byte mpu
uint8_t devstatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetsize; // expected dmp packet size (default 42 bytes)
uint16_t fifocount; // count of bytes in fifo
uint8_t fifobuffer[64]; // fifo storage buffer
// orientation/motion vars
quaternion q; // [w, x, y, z] quaternion container
vectorfloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container , gravity vector
//pid
double originalsetpoint = 172.50;
double setpoint = originalsetpoint;
double movingangleoffset = 0.1;
double input, output;
//adjust these values fit own design
double kp = 60;
double kd = 2.2;
double ki = 270;
pid pid(&input, &output, &setpoint, kp, ki, kd, direct);
double motorspeedfactorleft = 0.6;
double motorspeedfactorright = 0.5;
//motor controller
int ena = 5;
int in1 = 6;
int in2 = 7;
int in3 = 9;
int in4 = 8;
int enb = 10;
lmotorcontroller motorcontroller(ena, in1, in2, enb, in3, in4, motorspeedfactorleft, motorspeedfactorright);
volatile bool mpuinterrupt = false; // indicates whether mpu interrupt pin has gone high
void dmpdataready()
{
mpuinterrupt = true;
}
void setup()
{
// join i2c bus (i2cdev library doesn't automatically)
#if i2cdev_implementation == i2cdev_arduino_wire
wire.begin();
twbr = 24; // 400khz i2c clock (200khz if cpu 8mhz)
#elif i2cdev_implementation == i2cdev_builtin_fastwire
fastwire::setup(400, true);
#endif
mpu.initialize();
devstatus = mpu.dmpinitialize();
// supply own gyro offsets here, scaled min sensitivity
mpu.setxgyrooffset(220);
mpu.setygyrooffset(76);
mpu.setzgyrooffset(-85);
mpu.setzacceloffset(1788); // 1688 factory default test chip
// make sure worked (returns 0 if so)
if (devstatus == 0)
{
// turn on dmp, it's ready
mpu.setdmpenabled(true);
// enable arduino interrupt detection
attachinterrupt(0, dmpdataready, rising);
mpuintstatus = mpu.getintstatus();
// set our dmp ready flag main loop() function knows it's okay use it
dmpready = true;
// expected dmp packet size later comparison
packetsize = mpu.dmpgetfifopacketsize();
//setup pid
pid.setmode(automatic);
pid.setsampletime(10);
pid.setoutputlimits(-255, 255);
}
else
{
// error!
// 1 = initial memory load failed
// 2 = dmp configuration updates failed
// (if it's going break, code 1)
serial.print(f("dmp initialization failed (code "));
serial.print(devstatus);
serial.println(f(")"));
}
}
void loop()
{
// if programming failed, don't try anything
if (!dmpready) return;
// wait mpu interrupt or packet(s) available
while (!mpuinterrupt && fifocount < packetsize)
{
//no mpu data - performing pid calculations , output motors
pid.compute();
motorcontroller.move(output, min_abs_speed);
}
// reset interrupt flag , int_status byte
mpuinterrupt = false;
mpuintstatus = mpu.getintstatus();
// current fifo count
fifocount = mpu.getfifocount();
// check overflow (this should never happen unless our code inefficient)
if ((mpuintstatus & 0x10) || fifocount == 1024)
{
// reset can continue cleanly
mpu.resetfifo();
serial.println(f("fifo overflow!"));
// otherwise, check dmp data ready interrupt (this should happen frequently)
}
else if (mpuintstatus & 0x02)
{
// wait correct available data length, should short wait
while (fifocount < packetsize) fifocount = mpu.getfifocount();
// read packet fifo
mpu.getfifobytes(fifobuffer, packetsize);
// track fifo count here in case there > 1 packet available
// (this lets read more without waiting interrupt)
fifocount -= packetsize;
mpu.dmpgetquaternion(&q, fifobuffer);
mpu.dmpgetgravity(&gravity, &q);
mpu.dmpgetyawpitchroll(ypr, &q, &gravity);
input = ypr[1] * 180/m_pi + 180;
}
}
which lines don't understand?
Arduino Forum > Using Arduino > Programming Questions > Help Understanding This Program
arduino
Comments
Post a Comment