Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

robot control via keyboard

Sun Jul 24, 2016 11:40 am

I am trying to control a simple two motor robo using keyboard .

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
 
GPIO.setmode(GPIO.BOARD)
 
Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 11
Motor2B = 13
Motor2E = 15
 
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
GPIO.setup(Motor2E,GPIO.OUT)

def forward(x): 
   print "Going forwards"
   GPIO.output(Motor1A,GPIO.HIGH)
   GPIO.output(Motor1B,GPIO.LOW)
   GPIO.output(Motor1E,GPIO.HIGH)
   GPIO.output(Motor2A,GPIO.HIGH)
   GPIO.output(Motor2B,GPIO.LOW)
   GPIO.output(Motor2E,GPIO.HIGH)
 
   sleep(x)
   
def backward(x): 
   print "Going backwards"
   GPIO.output(Motor1A,GPIO.LOW)
   GPIO.output(Motor1B,GPIO.HIGH)
   GPIO.output(Motor1E,GPIO.HIGH)
   GPIO.output(Motor2A,GPIO.LOW)
   GPIO.output(Motor2B,GPIO.HIGH)
   GPIO.output(Motor2E,GPIO.HIGH)
 
   sleep(x)

def stop(): 
   print "Now stop"
   GPIO.output(Motor1E,GPIO.LOW)
   GPIO.output(Motor2E,GPIO.LOW)

def clockwise(x):
   print "rotate clockwise"
   GPIO.output(Motor1A,GPIO.LOW)
   GPIO.output(Motor1B,GPIO.HIGH)
   GPIO.output(Motor1E,GPIO.HIGH)
   GPIO.output(Motor2A,GPIO.HIGH)
   GPIO.output(Motor2B,GPIO.LOW)
   GPIO.output(Motor2E,GPIO.HIGH)
 
   sleep(x)

def anticlockwise(x):
   print "rotate anticlockwise"
   GPIO.output(Motor1A,GPIO.HIGH)
   GPIO.output(Motor1B,GPIO.LOW)
   GPIO.output(Motor1E,GPIO.HIGH)
   GPIO.output(Motor2A,GPIO.LOW)
   GPIO.output(Motor2B,GPIO.HIGH)
   GPIO.output(Motor2E,GPIO.HIGH)
   sleep(x)

def turnleft(x):
   print "left turn"
   GPIO.output(Motor1A,GPIO.HIGH)
   GPIO.output(Motor1B,GPIO.LOW)
   GPIO.output(Motor1E,GPIO.HIGH)
   sleep(x)

def turnright(x):
   print "left right"
   GPIO.output(Motor2A,GPIO.HIGH)
   GPIO.output(Motor2B,GPIO.LOW)
   GPIO.output(Motor2E,GPIO.HIGH)
   sleep(x)

try:
    
   while True:
       var=raw_input()
       if(var=="w"):
           forward(1)
           stop()
       elif(var=='s'):
            backward(1)
            stop()
       elif(var=='q'):
            clockwise(1)
            stop()
       elif(var=='e'):
            anticlockwise(1)
            stop()
       elif(var=='a'):
            turnleft(1)
            stop()
       elif(var=='d'):
            turnright(1)
            stop()
except KeyboardInterrupt:
       pass
finally:
    GPIO.cleanup()
without using the keyboard input , i just tried to run all the functions in sequence and it is working properly .
But after modifying the program with rawinput() , The program is compiled with no errors but the motors doesnt respond after pressing any buttons.
Please help me out!!
Thank you

User avatar
B.Goode
Posts: 8224
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: robot control via keyboard

Sun Jul 24, 2016 3:16 pm


Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: robot control via keyboard

Sun Jul 24, 2016 5:50 pm

Please can you tell me the modifications of my code??
Because the in the link you have shared , The code is completely different.

User avatar
B.Goode
Posts: 8224
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: robot control via keyboard

Sun Jul 24, 2016 8:17 pm

Both the examples link keystrokes to functions for forward, back, left, right.

You already have functions in your own code that perform those actions.

The changes look as though they should be simple, but I have no intention of writing code for you to solve your problem.

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: robot control via keyboard

Mon Jul 25, 2016 3:10 pm

As I'm new to python , maybe the variable is unable to compare the keyboard pressed value and the assigned value.
The functions work well as Ive tested it by running it for a specific amount of time in sequence.
The main problem is in main() part of the program.
Please help me out!!

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: robot control via keyboard

Mon Jul 25, 2016 5:43 pm

Never mind , I got the solution!! :)
I never pressed enter after pressing w,a,s,d,q,e .
raw_input() takes value only after enter is pressed
This program is a perfect example for making wireless robots. You can modify and tune the above code according to your requirements.
Instead of going for download of pre coded libraries for robot control which are much complex , you can follow my code for better understanding.

Thank you again for your response :)

Return to “Automation, sensing and robotics”