roboticlemon / cmd-robot

Automatically exported from code.google.com/p/cmd-robot
1 stars 1 forks source link

keyboard control with WASD #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I will kick-start this one and see where I can get...I have to do some research 
and any help would be nice.

Tom, could you please send me the sample one you made on 9/11/12 in Religion.

Thank You.

Original issue reported on code.google.com by cian.by...@sydstu.catholic.edu.au on 10 Nov 2012 at 11:09

GoogleCodeExporter commented 9 years ago
Here is progress so far:
[ code ]

#!/usr/bin/env python 

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((64, 64))
pygame.display.set_caption('Pygame Caption')
pygame.mouse.set_visible(0)
done = False
while not done:
   for event in pygame.event.get():
        if (event.type == KEYUP): #or (event.type == KEYDOWN):
            if (event.key == K_a):
                print "Moving Left!"
            elif (event.key == K_s):
                print "Moving Backwards!"
            elif (event.key == K_d):
                print "Moving Right!"
            elif (event.key == K_w):
                print "Moving Forwards!"

    ##      if (event.type == KEYUP) or (event.type == KEYDOWN):
    ##         print event
            elif (event.key == K_ESCAPE):
                done = True

[ /code ]

I am not sure If I will be able to add this to the 1.5.3 tonight but it is work 
a try later tomorrow.

Original comment by cian.by...@sydstu.catholic.edu.au on 10 Nov 2012 at 11:45

Attachments:

GoogleCodeExporter commented 9 years ago
yeah I've got some, i'll post below:

{{{

import time
import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((128, 128))
pygame.display.set_caption('Pygame Input Windows')
pygame.mouse.set_visible(1)

##done = False
while True:
   for event in pygame.event.get() :
     if event.type == pygame.KEYDOWN :
       if event.key == pygame.K_UP :
         print "Up key pressed."
         start = time.time
       elif event.key == pygame.K_DOWN :
         print "Down key pressed down."
     elif event.type == pygame.KEYUP :
       if event.key == pygame.K_UP :
         print "Up key released."
         finish = time.time
         print finish - start
       elif event.key == pygame.K_DOWN :
         print "Down key released."
}}}

I'm working on finding out how long the key has been pressed for!

Original comment by thomas.i...@sydstu.catholic.edu.au on 10 Nov 2012 at 11:30

GoogleCodeExporter commented 9 years ago
You do that!

Original comment by cian.by...@sydstu.catholic.edu.au on 11 Nov 2012 at 1:55

GoogleCodeExporter commented 9 years ago
I think a better way to do that would be to wait or sleep() for a small amount 
of time and increase by 1 each time round.  I have not tried this myself but it 
could be a viable option:

[ pseudo code ]

while <key being pressed>:
  motor_a = motor_a + 1
  sleep(0.125)

[ /pseudo code ]

Original comment by cian.by...@sydstu.catholic.edu.au on 12 Nov 2012 at 10:30

GoogleCodeExporter commented 9 years ago
I gave that pseudo code a go and came across an unexpected output, it appears 
that the key detection method does not continually check if you hold the key 
down but rather only when if comes up then down.

Code below:

{{{
import time
sleep = time.sleep
import pygame
from pygame.locals import *
print " Loading WASD Full Speed Control..."
pygame.init()
screen = pygame.display.set_mode((64, 64))
pygame.display.set_caption('WASD Control Central')
pygame.mouse.set_visible(0)
done = False
motor_a = 0
motor_b = 0
dir_y = 1
print "Controls loaded!"
while not done:
   for event in pygame.event.get():
        if event.type == KEYDOWN: #or (event.type == KEYDOWN):
            if event.key == K_a:
                pygame.event.pump()
                start = time.clock()
                print "Moving Left!"
                motor_a = 0
                dir_y = 1
                motor_b = motor_b + 1
##                sleep(0.25)
                print motor_a, motor_b, dir_y
                while event.type == KEYDOWN:
                    motor_b = motor_b + 1
                    print motor_a, motor_b, dir_y
                    pygame.event.pump()

            elif (event.key == K_s):
                print "Moving Backwards!"
                dir_y = 2
                motor_a = 255
                motor_b = 255                    
            elif (event.key == K_d):
                print "Moving Right!"
                dir_y = 1
                motor_b = 0
                motor_a = 255
            elif (event.key == K_w):
                print "Moving Forwards!"
                dir_y = 1
                motor_a = 255
                motor_b = 255
            elif (event.key == K_ESCAPE):
                done = True
            else:
                motor_a = 0
                motor_b = 0                
            print (str(motor_a),str(motor_b) + " Direction: " + str(dir_y))

}}}          

Original comment by hacker16...@gmail.com on 13 Nov 2012 at 10:30

GoogleCodeExporter commented 9 years ago
WASD code is currently wrong in 1.6.2 I have the correct code for 1.6.3 and 
will merge with any new code you have!

Original comment by thomas.i...@sydstu.catholic.edu.au on 13 Dec 2012 at 4:13

GoogleCodeExporter commented 9 years ago
You don't have 1.6.3.  Only I have it.  Send the code to me and I will merge.  
I think I know the problem anyway.

Original comment by cian.by...@sydstu.catholic.edu.au on 14 Dec 2012 at 10:35

GoogleCodeExporter commented 9 years ago
Have we given up on this???

Original comment by cian.by...@sydstu.catholic.edu.au on 27 Mar 2013 at 10:43

GoogleCodeExporter commented 9 years ago
Issue deprecated in terms of python but perfectly doable for VB.net may look 
into this a bit later but to be honest doesn't really seem worth it.

Deprecated.

Original comment by cmdenterpriseshq on 4 Apr 2013 at 2:43

GoogleCodeExporter commented 9 years ago
Sounds good but should get CBFD

Original comment by cian.by...@sydstu.catholic.edu.au on 4 Apr 2013 at 2:46