Neurotic Robot

by intivdamme in Craft > Art

251 Views, 1 Favorites, 0 Comments

Neurotic Robot

f378f984-3c0b-4d8a-9948-f984573d1500.jpg

It's a small project I made for school.
The idea is that its a small little robot that suffers from paranoia, feels sad, disturbed and has mood swings.
to give off a comedic effect at first but also makes you feel at least a bit bad for him.
Can we sympathize with a robot?
So in short, it's meant to entertain and a sort small scale experiment if you can sympathize with a robot.

The idea at first was to make him drive around as well, but that was a optional function I did not have time for in the end.
I'l mark the parts as *optional*
And here is a more in-depth guide i was planning to look into for using those motors:
https://www.electronicshub.org/raspberry-pi-l298n-...

Supplies

*Prices below are all in euro's and not precise, but likely a bit higher*

Raspberry pi -

Small speaker with aux input -

Grove sound sensor - 6,- (can be way lower)

US-015 Ultrasoon sensor - 4,-

Battery pack/power bank - 4,- / 30,-

What ever you want to use as "hull" - (I bought a teddy bear!)

2 Resistors: R1 330 ohm - R2 470 ohm

*Optional*

DC motor's - 5,-

L298N Driver - 3,-

Audio!

Let's start lightly. We'll be working with audio later, and before we start coding it.
We can better start actually getting those files, either record or download them and convert them to WAV files.
You can doe this with online converters or the one I used: Audacity
https://www.audacityteam.org

For recording I suggest using Audacity as well.
Or if you are a windows user, there is a Recorder built in.

Setting Up

Raspberry-Pi-GPIO-Header-with-Photo.png
Ultrasonicsensor.png

Okay you got the parts and audio files, now lets setup the electronics.
I added to images.
1 has all the pins on the pi named, to help you find which pin you need.
2 is a blueprint of how you should connect the Ultrasonic sensor.
A reminder for the Ultrasonic sensors resistors: R1 330 ohm - R2 470 ohm


For the sound sensor the pins are to be directly connected.
Check the underside of the sound sensor to see which pin is what and match the, with the raspberry pi.

Testing

Before it does things we need some code of course, What I like to do is test all things separate.

This way it's easier to understand the code and you'll be able to find issues way quicker and without messing with all the code at once!

TIP: use whatsapp web to easily send files to the pi.

I Advice to create a script in a custom "scripts" folder, it's easy to edit and in the console you just use: "cd scripts"

And then type: python3 myscript.py

Replacing "myscript" with your actual script name of course.

Now that thats out of the way...

For the Ultrasonic and sound sensors do the following:

I uploaded the test scripts I used. just place them in your scripts folder and start it!

(ctr+c stops the scripts)

For the audio test we first need to do something different,

and that's placing your audio files in the scripts folder.

Then make sure the names of the files are the same as used in the script:

Scream.wav - Treathen.wav - Cry.wav - ngry.wav - What.wav

You can also just change the names in the script it self.

Now you can do the same you did for the sensors, add the script and start it!


Audio test code

import sys
import time
import pygame
import random




Paranoid_list = ['Scream.wav', 'Treathen.wav', 'Cry.wav', 'Angry.wav', 'What.wav']
SecR = random.SystemRandom()
Pick = SecR.choice(Paranoid_list)
print (Pick)
pygame.mixer.init()
pygame.mixer.music.load(Pick)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == False:
    continue
	print ("Done")




time.sleep(50)

Sound sensor test code

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)

def callback(channel):
    if GPIO.input(channel):
        print "Sound Detected!"
    else:
        print "Sound Detected!"

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change

# infinite loop
while True:
    time.sleep(1)

Ultrasonic test code

#Libraries
import RPi.GPIO as GPIO
import time
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
 
def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2
 
    return distance
 
if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
 
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

Creating the Main Script

You can try it your self using the test scripts or just download my main script and edit it to your liking!

Then all that is needed to do is to add it to your scripts folder and once again start it up!

Check if everything works and you got a reactive robot!

The final and only script you need

import sys
import RPi.GPIO as GPIO  
import time    
import pygame    
import random


#GPIO Setup Ultrasoon
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)


#GPIO SETUP Audio
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)


#--------------------------------------------------------------
#Retrieve distance info
def distance():
	time.sleep(0.5)
	GPIO.output(GPIO_TRIGGER, True)
	time.sleep(0.00001)
	GPIO.output(GPIO_TRIGGER, False)
	StartTime = time.time()
	StopTime = time.time()
	while GPIO.input(GPIO_ECHO) == 0:
		StartTime = time.time()


	while GPIO.input(GPIO_ECHO) == 1:
		StopTime = time.time()
 
	TimeElapsed = StopTime - StartTime
	
	distance = (TimeElapsed * 34300) / 2


	return distance
if __name__ == '__main__':
	try:
		#Checks if distance is less or equal to 65, then picks and plays random audio file from the list.
		while True:
			dist = distance()
			print (dist)
			if dist<=65:
				Paranoid_list = ['hey.wav', 'who.wav', 'why.wav', 'miserable.wav', 'rage.wav', 'crazy.wav', 'look.wav', 'hello.wav', 'see.wav', 'burnt.wav', 'wheels.wav', 'scared.wav', 'heat.wav', 'throw.wav']
				SecR = random.SystemRandom() #randomizer thingy
				Pick = SecR.choice(Paranoid_list) #picks file name based on randomizer above
				print (Pick)
				#uses pygame to initialize, load and play audio file. then checks how long it plays for.
				pygame.mixer.init()
				pygame.mixer.music.load(Pick)
				pygame.mixer.music.play()
				while pygame.mixer.music.get_busy() == True:
					continue


			else:
				print ("can't see anything")
				#pre-selection of audio file, works like before. But this time its when the sound sensor triggers.
				Paranoid_list = ['music.wav', 'paranoia.wav', 'talk.wav', 'noise.wav', 'song.wav', 'on.wav', 'neurotic.wav', 'silent.wav']
				SecR = random.SystemRandom()
				Pick = SecR.choice(Paranoid_list)
				print (Pick)


				if GPIO.input(channel):
					print ("Sound Detected!")
					pygame.mixer.init()
					pygame.mixer.music.load(Pick)
					pygame.mixer.music.play()
					while pygame.mixer.music.get_busy() == True:
						continue


				else:
					print ("Sound NotDetected!")
				#feedback to be able to follow the code.
				print ("sound gets tested")
				#End of the code, clean up and 
				def callback(channel):
					GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=100)
					GPIO.add_event_callback(channel, callback)
					while True:
						time.sleep(1)


 
 
		# Reset by pressing CTRL + C
	except KeyboardInterrupt:
		print("Measurement stopped by User")
		GPIO.cleanup()

Step 5: Finalizing

f378f984-3c0b-4d8a-9948-f984573d1500.jpg
e4ef6b07-d982-48ae-bea6-8d6ce4e69811.jpg

At this point it basically works.
All you need now is something to make it look like a robot or animatronic.

My first plan was a 3D print or laser cut robot model.
But due to time constraints and a way more fun idea I bought a bunny instead!
A PLUSHY NOT A REAL BUNNY!