Yanxue Huang
Published

VisionAI Automatic Dog Treat Dispenser /wRoboflow and YOLOv5

Dispense dog treats automatically when the dog's food bowl is empty.

BeginnerWork in progress5 hours780
VisionAI Automatic Dog Treat Dispenser /wRoboflow and YOLOv5

Things used in this project

Story

Read more

Schematics

Hardware Layout

Code

Judys_Bowl_Vision_AI_Servo

Arduino
#include "Seeed_Arduino_GroveAI.h"
#include <Wire.h>
#include <Servo.h>

GroveAI ai(Wire);
uint8_t state = 0;

// Servo parameters
Servo MyServo;
#define servoPin 5
#define pushButtonPin 7
const int minAngle = 0;
const int maxAngle = 180;
int thrangle = 45;
int buttonPushed = 0;
int angle = 0;

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  
   Serial.println("begin");
  if (ai.begin(ALGO_OBJECT_DETECTION, MODEL_EXT_INDEX_1)) // Object detection and externel model 1
  {
    Serial.print("Version: ");
    Serial.println(ai.version());
    Serial.print("ID: ");
    Serial.println( ai.id());
    Serial.print("Algo: ");
    Serial.println( ai.algo());
    Serial.print("Model: ");
    Serial.println(ai.model());
    Serial.print("Confidence: ");
    Serial.println(ai.confidence());
    state = 1;
  }
  else
  {
    Serial.println("Algo begin failed.");
  }
  { 
    MyServo.attach(servoPin);
    pinMode(pushButtonPin,INPUT_PULLUP);
    Serial.begin(9600);
    MyServo.write(angle);
  }
}

void loop()
{
  if (state == 1)
  {
    uint32_t tick = millis();
    if (ai.invoke()) // begin invoke
    {
      while (1) // wait for invoking finished
      {
        CMD_STATE_T ret = ai.state(); 
        if (ret == CMD_STATE_IDLE)
        {
          break;
        }
        delay(20);
      }

     uint8_t len = ai.get_result_len(); // receive how many people detect
     if(len)
     {
       int time1 = millis() - tick; 
       Serial.print("Time consuming: ");
       Serial.println(time1);
       Serial.print("Empty Bowl: ");
       Serial.println(len);
       object_detection_t data;       //get data

       for (int i = 0; i < len; i++)
       {
          Serial.println("result: detected");
          Serial.print("Detecting and calculating: ");
          Serial.println(i+1);
          ai.get_result(i, (uint8_t*)&data, sizeof(object_detection_t)); //get result
  
          Serial.print("confidence:");
          Serial.print(data.confidence);
          Serial.println();
          if (data.confidence >= 50)
           buttonPushed = 1;
        }

     }
     else
     {
       Serial.println("No identification");
     }
    }
    else
    {
      delay(1000);
      Serial.println("Invoke Failed.");
    }
  }
  else
  {
    state == 0;
  }
  if(digitalRead(pushButtonPin) == HIGH) {
   buttonPushed = 1;
   }

  if(buttonPushed > 0) {
   MyServo.write(thrangle - 10);
   thrangle = thrangle + 45;
   buttonPushed = 0;
   Serial.print("Angle ");
   Serial.print(thrangle);
   Serial.println(" degrees");
   }
   delay(200);

  if(thrangle > 180){
    thrangle = -45;
      }
}

Credits

Yanxue Huang

Yanxue Huang

1 project • 1 follower

Comments