Leow Cheah Wei
Created January 4, 2019

Pet Nanny Garden Edition

A bot to take photo of your pet in the garden

48
Pet Nanny Garden Edition

Things used in this project

Hardware components

Spresense boards (main & extension)
Sony Spresense boards (main & extension)
×1
Spresense camera board
Sony Spresense camera board
×1
Cytron MD13S
×2
lobot tank track chasis
×1

Story

Read more

Schematics

simple schematics

connection from spreesense board to MD13s

Code

maincode

Arduino
this main code with only logic to move front and back
#include "CytronMotorDriver.h"

// Configure the motor driver.
CytronMD motor1(PWM_DIR, 3, 4);   // PWM 1A = Pin 3, DIR=4 
CytronMD motor2(PWM_DIR, 5, 7); // PWM 2A = Pin 5, DIR=pin 7 


// The setup routine runs once when you press reset.
void setup() {
  
}


// The loop routine runs over and over again forever.
void loop() {

movefront();
delay(2000);
movefront();
delay(2000);
moveback();
delay(2000);
moveback();

}

void movefront()
{
  motor1.setSpeed(128);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(128);  // Motor 2 runs forward at 50% speed.
  delay(1000);
  
  motor1.setSpeed(255);   // Motor 1 runs forward at full speed.
  motor2.setSpeed(255);  // Motor 2 runs forward at full speed.
  delay(10000);

  motor1.setSpeed(0);     // Motor 1 stops.
  motor2.setSpeed(0);     // Motor 2 stops.
  delay(5000);
  
}

void moveback()
{
    motor1.setSpeed(-128);  // Motor 1 runs backward at 50% speed.
  motor2.setSpeed(-128);   // Motor 2 runs backward at 50% speed.
  delay(1000);
  
  motor1.setSpeed(-255);  // Motor 1 runs backward at full speed.
  motor2.setSpeed(-255);   // Motor 2 runs backward at full speed.
  delay(10000);

  motor1.setSpeed(0);     // Motor 1 stops.
  motor2.setSpeed(0);     // Motor 2 stops.
  delay(5000);
}

maincode2

Arduino
combine with gps and camera with moving
/*
 *  camera.ino - One minute interval time-lapse Camera
 *  Copyright 2018 Sony Semiconductor Solutions Corporation
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  This is a test app for the camera library.
 *  This library can only be used on the Spresense with the FCBGA chip package.
 */

#include <SDHCI.h>
#include <stdio.h>  /* for sprintf */

#include <Camera.h>

#include "CytronMotorDriver.h"

#include <GNSS.h>
#include <SDHCI.h>

SDClass SD;
File myFile;
SpGnss gps;

// Configure the motor driver.
CytronMD motor1(PWM_DIR, 3, 4);   // PWM 1A = Pin 3, DIR=4 
CytronMD motor2(PWM_DIR, 5, 7); // PWM 2A = Pin 5, DIR=pin 7 


#define BAUDRATE                (115200)

SDClass  theSD;
int take_picture_count = 0;


/**
 * Callback from Camera library when video frame is captured.
 */

void CamCB(CamImage img)
{

  /* Check the img instance is available or not. */

  if (img.isAvailable())
    {

      /* If you want RGB565 data, convert image data format to RGB565 */

      img.convertPixFormat(CAM_IMAGE_PIX_FMT_RGB565);

      /* You can use image data directly by using getImgSize() and getImgBuff().
       * for displaying image to a display, etc. */

      Serial.print("Image data size = ");
      Serial.print(img.getImgSize(), DEC);
      Serial.print(" , ");

      Serial.print("buff addr = ");
      Serial.print((unsigned long)img.getImgBuff(), HEX);
      Serial.println("");
    }
  else
    {
      Serial.print("Failed to get video stream image\n");
    }
}

/**
 * @brief Initialize camera
 */
void setup()
{

  /* Open serial communications and wait for port to open */

  Serial.begin(BAUDRATE);
  while (!Serial)
    {
      ; /* wait for serial port to connect. Needed for native USB port only */
    }
 if (gps.begin() != 0)  {
    Serial.println("GPS failed!");
  } else {
    gps.select(GPS);
    gps.select(SBAS);
    if (gps.start(COLD_START) != 0)    {
      Serial.println("GPS error!!");
    } else {
      Serial.println("GPS OK");
    }
  }

  /* begin() without parameters means that
   * number of buffers = 1, 30FPS, QVGA, YUV 4:2:2 format */

  Serial.println("Prepare camera");
  theCamera.begin();


  /* Start video stream.
   * If received video stream data from camera device,
   *  camera library call CamCB.
   */

  Serial.println("Start streaming");
  theCamera.startStreaming(true, CamCB);

  /* Auto white balance configuration */

  Serial.println("Set Auto white balance parameter");
  theCamera.setAutoWhiteBalanceMode(CAM_WHITE_BALANCE_DAYLIGHT);
 
  /* Set parameters about still picture.
   * In the following case, QUADVGA and JPEG.
   */

  Serial.println("Start streaming");
  theCamera.setStillPictureImageFormat(
     CAM_IMGSIZE_QUADVGA_H,
     CAM_IMGSIZE_QUADVGA_V,
     CAM_IMAGE_PIX_FMT_JPG);
}

/**
 * @brief Take picture with format JPEG per second
 */

void loop()
{

  movefront();
delay(2000);
movefront();
delay(2000);
moveback();
delay(2000);
moveback();
  
  sleep(1); /* wait for one second to take still picture. */

//if (gps.waitUpdate(-1)){
//SpNavData NavData;
    gps.getNavData(&NavData);

    Serial.print(NavData.latitude);
    Serial.print(",");
    Serial.println(NavData.longitude);

    // Write the latitude and longitude to a file that can be imported to GPS Visulator
    myFile = SD.open("gps.txt", FILE_WRITE);
    if (myFile) {
      myFile.print(NavData.latitude);
      myFile.print(",");
      myFile.println(NavData.longitude);
      myFile.close();
  //  }
  /* You can change the format of still picture at here also, if you want. */

  /* theCamera.setStillPictureImageFormat(
   *   CAM_IMGSIZE_HD_H,
   *   CAM_IMGSIZE_HD_V,
   *   CAM_IMAGE_PIX_FMT_JPG);
   */

  /* This sample code can take 100 pictures in every one second from starting. */

  if (take_picture_count < 10)
   {

      /* Take still picture.
      * Unlike video stream(startStreaming) , this API wait to receive image data
      *  from camera device.
      */
  
      Serial.println("call takePicture()");
      CamImage img = theCamera.takePicture();

      /* Check availability of the img instance. */
      /* If any error was occured, the img is not available. */

      if (img.isAvailable())
        {
          /* Create file name */
    
          char filename[16] = {0};
          sprintf(filename, "PICT%03d.JPG", take_picture_count);
    
          Serial.print("Save taken picture as ");
          Serial.print(filename);
          Serial.println("");

          /* Save to SD card as the finename */
    
          File myFile = theSD.open(filename, FILE_WRITE);
          myFile.write(img.getImgBuff(), img.getImgSize());
          myFile.close();
        }

      take_picture_count++;
    }
}

void movefront()
{
  motor1.setSpeed(128);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(128);  // Motor 2 runs forward at 50% speed.
  delay(1000);
  
  motor1.setSpeed(255);   // Motor 1 runs forward at full speed.
  motor2.setSpeed(255);  // Motor 2 runs forward at full speed.
  delay(10000);

  motor1.setSpeed(0);     // Motor 1 stops.
  motor2.setSpeed(0);     // Motor 2 stops.
  delay(5000);
  
}

void moveback()
{
    motor1.setSpeed(-128);  // Motor 1 runs backward at 50% speed.
  motor2.setSpeed(-128);   // Motor 2 runs backward at 50% speed.
  delay(1000);
  
  motor1.setSpeed(-255);  // Motor 1 runs backward at full speed.
  motor2.setSpeed(-255);   // Motor 2 runs backward at full speed.
  delay(10000);

  motor1.setSpeed(0);     // Motor 1 stops.
  motor2.setSpeed(0);     // Motor 2 stops.
  delay(5000);
}

Credits

Leow Cheah Wei

Leow Cheah Wei

9 projects • 7 followers
I am as happy as I want to be

Comments