Autonomous Robot Part 2 – Watchdog

Group members: Daniel Hua, Jesse Klein

Introduction

Watchdog is an autonomous robot that keeps an eye on its surroundings and shines a light to investigate anything of interest. It uses two ultrasonic rangefinders to monitor nearby objects, scanning back and forth continuously and tracking nearby objects. When it identifies something of interest, it swivels towards its target, locks on, and shines a bright light to illuminate its target.

Video

 

Technical Notes

The top platform of the robot rotates on a slip ring driven by a geared stepper motor, which allows it to rotate without twisting wires. On the top platform, there are two ultrasonic rangefinders that sense distance. These distance values are collected by a Teensy 2.0, then sent to a laptop, which runs a Python program that converts it to behavior. The Python program then sends a signal back to the Teensy, and that signal contains information about the behavior of the  motor and the lamp.

The motor is powered by a 12v stepper motor driver, and the Arduino is powered by the USB port from the laptop. The lamp is powered by 120v AC from the wall, which is switched on and off using a relay. Because the Arduino’s output pins cannot provide enough current current to the relay to switch it on, we add a transistor in between the Arduino and the relay to switch the relay on and off.

circuit diagram

Circuit Diagram

 

IMG_7046IMG_7021IMG_7041IMG_5316

 

 

Code

 Arduino code:

#include <NewPing.h>
#include <AccelStepper.h>

#define TRIGGER_PIN_B 0
#define TRIGGER_PIN_A 2
#define ECHO_PIN_B 1
#define ECHO_PIN_A 3
#define RELAY_PIN 11

#define MAX_DISTANCE 200

NewPing sonarA(TRIGGER_PIN_A, ECHO_PIN_A, MAX_DISTANCE);
NewPing sonarB(TRIGGER_PIN_B, ECHO_PIN_B, MAX_DISTANCE);

AccelStepper stepper(1, 9, 10);

int counter = 0;
int A, B;
int lampTime = 0;

int lastSpeed = 0;

boolean lampOn = false;

int serialSpeed = 400;

void setup() {
stepper.setMaxSpeed(10000);
stepper.setAcceleration(10000);
// stepper.setSpeed(5000);

pinMode(RELAY_PIN, OUTPUT);

Serial.begin(115200);
}

void loop() {
if (counter % 100 == 0) {
A = sonarA.ping() / US_ROUNDTRIP_CM;
B = sonarB.ping() / US_ROUNDTRIP_CM;
int pos = stepper.currentPosition() % 3840;
// if(pos <= 30)
// delay(1000);
// Serial.print(pos);
// Serial.print(“\t”);
Serial.print(A);
Serial.print(“\t”);
Serial.println(B);
}

if (Serial.available()) {
int num = 0;
int sign = 1;
byte digit;
byte incomingByte = Serial.read();
if (incomingByte == ‘-‘) {
sign = -1;
}
else {
digit = incomingByte – ‘0’;

if (digit >= 0 && digit <= 9) {
num = digit;
}
}

while (Serial.available()) {
incomingByte = Serial.read();
digit = incomingByte – ‘0’;
if (digit >= 0 && digit <= 9) {
num *= 10;
num += digit;
}
}

serialSpeed = sign*num;

if(serialSpeed == 0) {
lampOn = true;
lampTime = 0;
}
lastSpeed = serialSpeed;
}

if(lampOn) {
digitalWrite(RELAY_PIN, HIGH);
lampTime++;

if (lampTime > 2000) {
lampOn = false;
}
}
else {
digitalWrite(RELAY_PIN, LOW);
}

stepper.setSpeed(serialSpeed);
stepper.runSpeed();

counter++;
}

Python code:

import serial
import random
ser = serial.Serial(‘/dev/tty.usbmodem12341’, 9600)

while 1 :
input = ser.readline()
values = input.split(“\t”, 2)
s1 = long(values[0])
s2 = long(values[1][:len(values[1])])
print s1, s2
if (s1 – s2 > 0) :
#move clockwise
ser.write(“-750”)
# print “clockwise”
elif (s1 – s2 < 0) :
#move counter clockwise
ser.write(“750”)
elif (s1 < 30):
ser.write(“0”)
else :
ser.write(“100”)