Evan Rust
Published © GPL3+

Making a Robot Play Fetch

Use the new Pixy2 and a DFRobot ESP32 FireBeetle to create a robot that can find and retrieve items!

IntermediateFull instructions provided4 hours1,452

Things used in this project

Hardware components

Pixy2 Camera
×1
DFRobot ESP32 FireBeetle
×1
DFRobot Servo Robot Gripper
×1
DFRobot 2WD MiniQ Chassis
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
1800mAh 11.1V LiPo Battery
×1

Software apps and online services

PixyMon
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Custom parts and enclosures

Battery Platform

Schematics

Board Schematic

Code

ESP32 Code

C/C++
Upload to ESP32
#include <ESP32_Servo.h>
#include <Pixy2I2C.h>
#include "esp32-hal-ledc.h"
Pixy2I2C pixy;

#define CENTER 100
#define DEADZONE 10

int motor_pins[] = {26,27,9,10};

Servo gripper;
int servo_pin = 25;
int pos;
int state = 1;

const int freq = 490, resolution = 8;

void setup()
{
  Serial.begin(115200);
  Serial.print("Starting...\n");
  
  pixy.init();
  gripper.attach(servo_pin);
  for(int i=0;i<4;i++){
    ledcAttachPin(motor_pins[i],i+2);
    ledcSetup(i+2,freq,resolution);
    
  }
  set_gripper(0);
  delay(2000);
  set_gripper(1);
}

void loop()
{ 
  int i; 
  // grab blocks!
  pixy.ccc.getBlocks();
  
  // If there are detect blocks, print them!
  if (pixy.ccc.numBlocks)
  {
    //Serial.print("Detected ");
    //Serial.println(pixy.ccc.numBlocks);
    for (i=0; i<pixy.ccc.numBlocks; i++)
    {
      /*Serial.print(pixy.ccc.blocks[i].m_width);
      Serial.print(" ");
      Serial.println(pixy.ccc.blocks[i].m_height);
      /*Serial.print("  block ");*/
      //Serial.print(i);
      //Serial.print(": ");
      //pixy.ccc.blocks[i].print();
      if(pixy.ccc.blocks[i].m_width > 220){
        set_gripper(2);
        move(0);
        state = 0;
        move(4);
        delay(3000);
        move(0);
        set_gripper(1);
        while(1);
      }
    Serial.print(pixy.ccc.blocks[i].m_x);
    Serial.print(",");
    Serial.println(pixy.ccc.blocks[i].m_width);
    }
    if(state==1){
    if(pixy.ccc.blocks[i].m_x >= CENTER-DEADZONE || pixy.ccc.blocks[i].m_x <= CENTER+DEADZONE){
      move(3);
      delay(2);
      move(0);
    }
    else if(pixy.ccc.blocks[i].m_x > CENTER){
      //while(pixy.ccc.blocks[i].m_x > CENTER){
        move(1);
        delay(2);
        move(0);
      //}
    }
    
    else if(pixy.ccc.blocks[i].m_x < CENTER){
      //while(pixy.ccc.blocks[i].m_x < CENTER){
        move(2);
        delay(2);
        move(0);
      //}
    }
  }  
  }
  if(Serial.available()>0){
    pos = Serial.parseInt();
    Serial.println(pos);
    if(pos != 0){
      gripper.write(pos);
    }
  }
  
}

void move(int dir){
  switch(dir){
    case 0: //stop
      ledcWrite(2, 0);
      ledcWrite(3, 0);
      ledcWrite(4, 0);
      ledcWrite(5, 0);
      break;
    case 1: //left
      ledcWrite(2, 120);
      ledcWrite(3, 0);
      ledcWrite(4, 120);
      ledcWrite(5, 0);
      break;
    case 2: //right
      ledcWrite(2, 0);
      ledcWrite(3, 120);
      ledcWrite(4, 0);
      ledcWrite(5, 120);
      break;
    case 3: //forward
      ledcWrite(2, 0);
      ledcWrite(3, 120);
      ledcWrite(4, 120);
      ledcWrite(5, 0);
      break;
    case 4: //back
      ledcWrite(2, 120);
      ledcWrite(3, 0);
      ledcWrite(4, 0);
      ledcWrite(5, 120);
      break;
  }
}

void set_gripper(int state){
  switch(state){
    case 0: 
      gripper.write(180);//closed
      break;
    case 1:
      gripper.write(0);//open
      break;
    case 2:
      gripper.write(60);//grip
      break;
  }
}

Credits

Evan Rust

Evan Rust

122 projects • 1087 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments