Senin, 08 Juli 2019

Robot balance dengan Arduino dan Nrf24L01

Kali ini kita akan membahas tentang robot balance, yaitu robot keseimbangan.
Robot yang mampu berdiri hanya dengan menggunakan 2 buah roda

Langsung aja kita ke topik bagaiman cara membuatnya dan apa" saja bahannya

Bahan -  bahan :
1.Receiver
  • Arduino mega
  • Motor Dc
  • Driver motor L298
  • Mpu 6050
  • Nrf 24L01
  • Batery 7.4 Volt bisa juga 12 volt
2. Remote
  • Arduino nano
  • Nrf 24L01
  • Button 4 buah
  • Batery 7.4 Volt bisa juga 12 volt  
setelah bahan" nya cukup silahkan rekan" buat rangkaiannya seperti di bawah ini:

 



Setelah di bikin Rangkaian nya seperti yang diatas
persiapkan dulu library yang akan digunakan silahkan download dulu
Nrf 24L01 = https://github.com/nRF24/RF24
PID =  https://goo.gl/gDHGoW
LMotorController = https://goo.gl/4UwCGq
I2Cdev =  https://goo.gl/jJMYL3
MPU6050 = https://goo.gl/ZGVs71

silahkan copy program yang dibawah ini

Remote :
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

int but1 = 2;
int but2 = 3;
int but3 = 4;
int but4 = 5;

int stick[6];


const uint64_t pipe = 0xE8E8F0F0E1LL; 
RF24 radio(9,10);

void setup()
{
Serial.begin(9600);
Serial.println("Initialize MPU");
radio.begin();
radio.openWritingPipe(pipe);

}

void loop()
{
 stick[0] = digitalRead(but1);
 stick[1] = digitalRead(but2);
 stick[2] = digitalRead(but3);
 stick[3] = digitalRead(but4);

radio.write( stick, sizeof(stick) );
}


Receiver:

#include <PID_v1.h>
#include <LMotorController.h>
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
SoftwareSerial mySerial(12, 13);

RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL; 
int stick[6];

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
 #include "Wire.h"
#endif

#define MIN_ABS_SPEED 100

MPU6050 mpu;

// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently 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 and gravity vector

//PID
double originalSetpoint = 181.50;  // silahkan diganti sesuai dengan tegak lurusnya robot balance
double setpoint = originalSetpoint;
double movingAngleOffset = 0.9;
double input, output;

// silahkan setting posisi sendiri sampai robot balnce anda tenang
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 = 6;
int IN1 = 8;
int IN2 = 3;
int IN3 = 4;
int IN4 = 5;
int ENB = 7;
LMotorController motorController(ENA, IN1, IN2, ENB, IN3, IN4, motorSpeedFactorLeft, motorSpeedFactorRight);

char val;
int stat = 0;


volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady()
{
 mpuInterrupt = true;
}


void setup()
{
 Serial.begin(9600);
 mySerial.begin(9600);
 #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

pinMode(out, OUTPUT);
pinMode(input1, INPUT);

 mpu.initialize();

delay(250);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();;

 devStatus = mpu.dmpInitialize();

 // supply your own gyro offsets here, scaled for min sensitivity
 mpu.setXGyroOffset(220);
 mpu.setYGyroOffset(76);
 mpu.setZGyroOffset(-85);
 mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
 if (devStatus == 0)
 {
  mpu.setDMPEnabled(true);
 attachInterrupt(0, dmpDataReady, RISING);
 mpuIntStatus = mpu.getIntStatus();
 dmpReady = true;
 packetSize = mpu.dmpGetFIFOPacketSize();

 //setup PID
 pid.SetMode(AUTOMATIC);
 pid.SetSampleTime(10);
 pid.SetOutputLimits(-255, 255);
 }
 else
 {
 Serial.print(F("DMP Initialization failed (code "));
 Serial.print(devStatus);
 Serial.println(F(")"));
 }
}

void loop(){
  a = digitalRead(input1);
  if (!dmpReady) return;
 while (!mpuInterrupt && fifoCount < packetSize)
 {

 if ( radio.available() )
{
bool done = false;
radio.read( stick, sizeof(stick) );

if (stick[0] == 0){
stat = 1;
Serial.println("MAJU ");
}

if (stick[1] == 0){
stat = 2;
Serial.println("MUNDUR");
}

if (stick[2] == 0){
stat = 3;
Serial.println("KIRI");
}

if (stick[3] == 0){
stat = 4;
Serial.println("KANAN");
}

if (stick[0] == 1 && stick[1] == 1 && stick[2] == 1 && stick[3] == 1)
{
  stat = 0;
  Serial.println("STOP ");
}



}

 if (stat == 0){
 //buzz();
 //digitalWrite(out, HIGH);
 setpoint = originalSetpoint;
 pid.Compute();
 motorController.move(output, MIN_ABS_SPEED);
 }

 if (stat == 1){
 setpoint = originalSetpoint + 4;
 pid.Compute();
 motorController.move(output, 100);
 //digitalWrite(out, HIGH);
 }

 if (stat == 2){
 setpoint = originalSetpoint - 3;
 pid.Compute();
 motorController.move(output, 100);
 //digitalWrite(out, HIGH);
 }

 if (stat == 3){
setpoint = originalSetpoint + 0.5;
pid.Compute();
motorController.turnRight(output, 100);

 }

 if (stat == 4){
setpoint = originalSetpoint + 0.5;
pid.Compute();
motorController.turnLeft(output, 100);

 }

 }
 mpuInterrupt = false;
 mpuIntStatus = mpu.getIntStatus();
 fifoCount = mpu.getFIFOCount();
 if ((mpuIntStatus & 0x10) || fifoCount == 1024)
 {
 mpu.resetFIFO();
 Serial.println(F("FIFO overflow!"));
 }
 else if (mpuIntStatus & 0x02)
 {
  while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
 mpu.getFIFOBytes(fifoBuffer, packetSize);
 fifoCount -= packetSize;

 mpu.dmpGetQuaternion(&q, fifoBuffer);
 mpu.dmpGetGravity(&gravity, &q);
 mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
 input = ypr[1] * 180/M_PI + 180;
 }
}

Silahkan downloadkan program ke Arduino sesuaikan yang untuk remote dan receiver
untuk library Nrf24L01 cari yg pas, apabila terjadi eror saat di compile pada librar RF24 berarti library nya tidak cocok ya gan.
selanjutnya apabila robot balance nya tidak mau seimbang pada saat dihidupkan coba di balik pin IN1 & IN2 atau balik pin IN3 & IN4

Selamat Mencoba semoga sukses Ya!!!!
Bagi teman" ada yg ragu jangan malu untuk bertanya, silahkan coment dibawah insyallah kami bantu ya!

Share:

0 komentar:

BTemplates.com


zulpandru hendra. Diberdayakan oleh Blogger.