DIY Robotics: Building Your Own AI Robot

				
					python -m pip freeze

				
			

Python to play mp3

mpg123 is a command-line tool that can play mp3 files

				
					sudo apt-get install mpg123
				
			
				
					from sshkeyboard import listen_keyboard
from picamera import PiCamera
import motors
import time
import os
from gpiozero import Servo
from time import sleep




def press(key):
    print(f"'{key}' pressed")

    if(key=='a'):
        servo = Servo(27)
        servo.max()
        sleep(1)
    
    if(key=='s'):
        servo = Servo(27)
        servo.mid()
        sleep(1)

    if(key=='d'):
        servo = Servo(27)
        servo.min()
        sleep(1)

    if(key=='t'):
        file="speech/testing_driving_forward.mp3"
        os.system("mpg123 " +file)
        Motor = motors.MotorDriver()
        Motor.MotorRun(0, 'forward', 60)
        Motor.MotorRun(1, 'forward', 60)
        time.sleep(2) 
        Motor.MotorStop(0)
        Motor.MotorStop(1)
        file="speech/testing_turning_right.mp3"
        os.system("mpg123 " +file)
        Motor.MotorRun(0, 'backward', 30)
        Motor.MotorRun(1, 'forward', 30)
        time.sleep(3) 
        Motor.MotorStop(0)
        Motor.MotorStop(1)

        file="speech/testing_turning_left.mp3"
        os.system("mpg123 " +file)
        Motor.MotorRun(0, 'forward', 30)
        Motor.MotorRun(1, 'backward', 30)
        time.sleep(3) 
        Motor.MotorStop(0)
        Motor.MotorStop(1)

    if(key=='up'):
        Motor = motors.MotorDriver()
        Motor.MotorRun(0, 'forward', 60)
        Motor.MotorRun(1, 'forward', 60)
        time.sleep(7) 
        Motor.MotorStop(0)
        Motor.MotorStop(1)
    if(key=='right'):
        Motor = motors.MotorDriver()
        Motor.MotorRun(0, 'forward', 30)
        Motor.MotorRun(1, 'backward', 30)
        time.sleep(7) 
        Motor.MotorStop(0)
        Motor.MotorStop(1)
    if(key=='left'):
        Motor = motors.MotorDriver()
        Motor.MotorRun(0, 'backward', 30)
        Motor.MotorRun(1, 'forward', 30)
        time.sleep(7) 
        Motor.MotorStop(0)
        Motor.MotorStop(1)
    if(key=='space'):
        Motor = motors.MotorDriver()
    
        Motor.MotorStop(0)
        Motor.MotorStop(1)
    # if(key=='s'):   
    #     file="speech/power.mp3"
    #     os.system("mpg123 " +file)
    if(key=='1'):   
        file="speech/1.mp3"
        os.system("mpg123 " +file)
    if(key=='2'):   
        file="speech/2.mp3"
        os.system("mpg123 " +file)
    if(key=='3'):   
        file="speech/3.mp3"
        os.system("mpg123 " +file)
    if(key=='4'):   
        file="speech/4.mp3"
        os.system("mpg123 " +file)
    if(key=='5'):   
        file="speech/5.mp3"
        os.system("mpg123 " +file)
    if(key=='6'):   
        file="speech/6.mp3"
        os.system("mpg123 " +file)
    if(key=='7'):   
        file="speech/7.mp3"
        os.system("mpg123 " +file)
    if(key=='8'): 
        servo = Servo(27)
        servo.min() # right8
        sleep(1) 
        file="speech/power.mp3"
        os.system("mpg123 " +file) 
        # while True:
        #     servo.min() # right
        #     sleep(1) 88
    if(key=='9'):         
        file="speech/joke1.mp3"
        os.system("mpg123 " +file) 
        


    if(key=='p'): 

        camera = PiCamera()        
        camera.resolution = (1280, 720)
        # camera.vflip = True
        camera.contrast = 10        
        camera.capture("pic.jpg")
        camera.close()

    if(key=='v'): 
        camera = PiCamera()   
        camera.start_preview()
        camera.start_recording('video2.h264')
        time.sleep(7)
        camera.stop_recording()
        camera.stop_preview()
        camera.close()

def release(key):
    print(f"'{key}' released")

listen_keyboard(
    on_press=press,
    on_release=release,
)
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *