Alex Glow
Published © GPL3+

F3NR1R: Fennec Companion AI

A friendly, wearable robot based on a fennec fox. Work in progress for Virtual Maker Faire :)

AdvancedWork in progress6 hours3,238
F3NR1R: Fennec Companion AI

Things used in this project

Story

Read more

Code

F3N motor code (Teensy 3.1)

C/C++
/*
  Code for F3NR1R fennec fox companion bot
  By Alex Glow
  Project notes: bit.ly/f3nr1r

  Servos for neck (continuous), left ear & right ear (absolute)
  Touch-response lines on left & right paw: adjust head tilt
  
  Pins on Teensy 3.1:
  15-19 = A1-5, touch
  20-21 = A6-7, PWM
  22-23 = A8-9, PWM, touch
    Reserve 15-19 for touch
    Reserve 20-23 for 4 servos
  Last 3 pins: 3.3V, AGND, Vin (3.7-5.5V)
  
*/

#include <Servo.h>

Servo servoN; // neck
Servo servoL; // left ear
Servo servoR; // right ear

int tilt = 0;
int ears = 0;

int touchL = 0;
int touchR = 0;
int threshL = 3000;  // calibrate touch responsiveness threshold
int threshR = 3000;

void setup()
{
  Serial.begin(9600);

  servoN.attach(20);
  servoL.attach(21);
  servoR.attach(22);

  earsUp();
  delay(500);
}

void loop() {

  // read capacitive touch values
  touchL = touchRead(A4);
  Serial.println(touchL);
  touchR = touchRead(A5);
  Serial.println(touchR);
  
  if (touchL >= threshL) {
    Serial.println("touch adjust left");
    headLeft();
  } else if (touchR >= threshR) {
    Serial.println("touch adjust right");
    headRight();
  } else {
    Serial.println("no touch");
    headRandom();
  }

  // move ears at random
  earsRandom();

  delay(random(1000,2000));
  
}

void headLeft() {   // adjust head left
  Serial.println("tilt left");
  servoN.write(60);
  delay(60);
  servoN.write(90);  // stop
}

void headRight() {  // adjust head right
  Serial.println("tilt right");
  servoN.write(120);
  delay(60);
  servoN.write(90);  // stop
}

void headRandom() {
  tilt = random(2);
  switch(tilt) {
    case 0:
      Serial.println("no movement");
      break;
    case 1:
      Serial.println("tilt left");
      servoN.write(120);
      delay(60);
      servoN.write(90);
      delay(1000);
      servoN.write(60);
      delay(60);
      servoN.write(90);
      break;
    case 2:
      Serial.println("tilt right");
      servoN.write(60);
      delay(60);
      servoN.write(90);
      delay(1000);
      servoN.write(120);
      delay(60);
      servoN.write(90);
      break;
  }
}

void earsUp() {
  servoL.write(90);
  servoR.write(90);
}

void earsRandom() {
  ears = random(4);               // random int, 0-4
  switch(ears) {
    case 0:
      Serial.println("no movement");
      break;
    case 1:
      Serial.println("ears center");
      servoL.write(90);
      servoR.write(90);
      break;
    case 2:
      Serial.println("both ears down");
      servoL.write(150);
      servoR.write(30);
      delay(500);
      break;
    case 3:
      Serial.println("left ear down");
      servoL.write(140);
      break;
    case 4:
      Serial.println("right ear down");
      servoR.write(40);
      break;
    case 5:
      Serial.println("left ear up");
      servoL.write(90);
      break;
    case 6:
      Serial.println("right ear up");
      servoR.write(90);
      break;
    case 7:
      Serial.println("left ear twitch");
      servoL.write(90);
      servoR.write(90);
      delay(500);
      servoL.write(100);
      delay(10);
      servoL.write(90);
      break;
    case 8:
      Serial.println("right ear twitch");
      servoL.write(90);
      servoR.write(90);
      delay(500);
      servoR.write(100);
      delay(10);
      servoR.write(90);
      break;
  }
}

F3nTouchTest.ino

C/C++
Use this to find the typical touch sensor readings for your robot. This will enable you to set the threshold in the main sketch, to detect when it's actually being touched.
void setup() {
  Serial.begin(9600);
}

void loop()
{
  Serial.println("left: " + touchRead(A4));
  Serial.println("right: " + touchRead(A5));
  delay(100);
}

voicetest.py

Python
Text-to-speech with Festival, and recording and playing back audio over the ReSpeaker module.
#!/usr/bin/env python3
import serial
import time
import os

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()

    ser.write(b"sEarsUp\n")
    os.system("echo 'Hey! How are you?' | festival --tts")

    ser.write(b"sEarsDown\n")
    os.system("arecord -d 3 testrec.wav")
    time.sleep(2)

    ser.write(b"sEarsUp\n")
    os.system("echo 'You said' | festival --tts")
    os.system("aplay testrec.wav")
    time.sleep(1)

Credits

Alex Glow

Alex Glow

108 projects • 1119 followers
The Hackster team's resident Hardware Nerd. I like robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments