Professional Documents
Culture Documents
Ecen90 Robot Barcas Mendoza
Ecen90 Robot Barcas Mendoza
Laboratory Project
In partial fulfillment
of the requirements for
ECEN 90
(Feedback and Control System)
Submitted to:
Submitted by:
Barcas, Maevie-lyn B.
Mendoza, Hillary G.
BS ECE 4-1
Materials
All the other miscellaneous materials used for the case like wood, fiber
glass, paints and wood glue were either obtained for free or available
at home.
Theory
Procedure
For the body, the main material used is a ply wood obtained. The ply wood is
cut into pieces will the total dimensions of (22 cm x 15 cm x 10 cm).
Barcas, Maevie-lyn B. 05/31/2019
Mendoza, Hillary G. BS ECE 4-1
The wood was cut again to a more suitable shape and design. Completing and
enclosing the body from the front and back.
Step 3: Programming
#include "PID_v1.h"
#include "LMotorController.h"
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#define MIN_ABS_SPEED 30
MPU6050 mpu;
// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
Barcas, Maevie-lyn B. 05/31/2019
Mendoza, Hillary G. BS ECE 4-1
//PID
double originalSetpoint = 180;
double setpoint = originalSetpoint;
double movingAngleOffset = 0.1;
double input, output;
int moveState=0; //0 = balance; 1 = back; 2 = forth
double Kp = 50; // First adjustment
double Kd = 0.2 ; // Second adjustment
double Ki = 0; // Third adjustment
PID pid(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT); // kp = 55,kd=0.9
//timers
long time1Hz = 0;
long time5Hz = 0;
void setup()
{
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
// 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(67);//(220);
mpu.setYGyroOffset(64);//(76);
mpu.setZGyroOffset(-23);//(-85);
mpu.setZAccelOffset(1817);
// make sure it worked (returns 0 if so)
if (devStatus == 0)
{
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
mpu.setDMPEnabled(true);
// 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;
//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 to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}
Barcas, Maevie-lyn B. 05/31/2019
Mendoza, Hillary G. BS ECE 4-1
void loop()
{
// if programming failed, don't try to do anything
if (!dmpReady) return;
pid.Compute();
motorController.move(output, MIN_ABS_SPEED);
// 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();
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
#if LOG_INPUT
Serial.print("ypr\t");
Serial.print(ypr[0] * 180/M_PI);
Barcas, Maevie-lyn B. 05/31/2019
Mendoza, Hillary G. BS ECE 4-1
Serial.print("\t");
Serial.print(ypr[1] * 180/M_PI);
Serial.print("\t");
Serial.println(ypr[2] * 180/M_PI);
#endif
input = ypr[1] * 180/M_PI + 180;
}
}
#include <Servo.h>
#include <NewPing.h>
Servo myservo1; //Left servo
Servo myservo2; // Right Servo
void setup()
{
myservo1.attach(12); //SERVO 1
myservo2.attach(13); // SERVO 2
myservo1.write(0);
myservo2.write(0);
delay(1000);//wait for a second
Serial.begin (9600);
}
void punch ()
{
myservo1.write(30);//goes to 30 degrees
delay(100);
myservo2.write(30);//goes to 30 degrees
delay(100);
myservo1.write(120);//goes to 30 degrees
delay(100);
myservo2.write(120);//goes to 30 degrees
delay(100);
}
void loop() {
distance1 = sonar1.ping_cm();
else {
Barcas, Maevie-lyn B. 05/31/2019
Mendoza, Hillary G. BS ECE 4-1
Serial.println("Out of Range");
delay(500);
}
delay(500);
Front
Barcas, Maevie-lyn B. 05/31/2019
Mendoza, Hillary G. BS ECE 4-1
Final Thoughts
For better balancing results, the different values for PID should be tried.
Also, keep in mind that the build, size and weight, of the robot matters when
it comes to the robot stability in balancing. Finding the PID and offset
values for the robot will take time, so make sure to finish all connections
and circuitry needed as soon as possible to be able to test your robot
earlier.