pythonpygamelocalizationparticle-filter

Ball keeps vibrating when no key is pressed


i am trying yo make a ball move when the keys are pressed a certain distance +- a given uncertainty. The code seems to be working quite well when the keys are pressed. However when no keys are pressed the ball continues to move about the location with gaussing noise. I would like it to NOT move when no keys are pressed. I am using pygame and I have tried to use

if event.type ==  pygame.KEYDOWN is False:          
        rx +=  0
        ry +=  0        
    return rx[0] , ry[0]

but it didn't work. Any suggestions on how to remove the movement?

import pygame 
import random 
import numpy as np

pygame.init()
map = pygame.image.load('occupancy1.png')
dimx , dimy = 800 , 600
display_surface = pygame.display.set_mode((dimx, dimy))
red = (255, 0, 0)
black = (0 ,0 ,0)
white =(255,255,255)
green = (0 , 255, 0)

#generate initial position on a valid coordinate
rx , ry = (random.randint(0, map.get_width()), random.randint(0, map.get_height()))
while pygame.Surface.get_at(map , (rx, ry)) == black:
    rx = random.randint(0, map.get_width())
    ry = random.randint(0, map.get_height())
    if rx and ry == white:
        break 
    
rtheta = 0 
step = 2

t = np.radians(25)
event = None
sigma_step = 0.5 
def get_input():   
        fwd = 0 
        turn = 0 
        side = 0 
        if event is not None:            
            if event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_UP:
                   fwd = -step 
                elif event.key == pygame.K_DOWN:
                    fwd = step 
                elif event.key == pygame.K_LEFT:
                    side = -step            
                elif event.key == pygame.K_RIGHT:
                    side = step     
              
        return fwd , side 
    

#sigma_turn = np.radians(2)


def move_robot(rx , ry , fwd , side):
    # if nothing is pressed there shouldnt be any noise
    
    
    fwd_noisy =  np.random.normal(fwd , sigma_step , 1)
    side_noisy =  np.random.normal(side , sigma_step , 1)
    rx +=  side_noisy #* np.cos(rtheta)
    ry +=  fwd_noisy #* np.sin(rtheta)    
    print('fwd noisy' , fwd_noisy)   
    if event.type ==  pygame.KEYDOWN is False:          
        rx +=  0
        ry +=  0        
    return rx[0] , ry[0]

    
while True : 
    for event in pygame.event.get() : 
        if event.type == pygame.QUIT :    
            # deactivates the pygame library
            pygame.quit()
            # quit the program.
            quit()
    
  
    
    
   
    pygame.time.delay(10)
      
    display_surface.fill(black)
   
    display_surface.blit(map, (0, 0)) 
    fwd , side  = get_input()
    rx, ry= move_robot(rx , ry , fwd , side) 
    avatar = pygame.draw.circle(display_surface, red, (rx , ry), 3)
   
     #surface , color , ceter , radius
    pygame.display.update()     
    pygame.time.delay(10)
     ```



Solution

  • Read How can I make a sprite move when key is held down and change the get_input and move_robot methods accordingly:

    def get_input():   
        keys = pygame.key.get_pressed()
        fwd = (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * step 
        side = (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * step    
        return fwd, side 
       
    def move_robot(rx , ry , fwd , side):
        if fwd != 0 or side != 0:
            fwd_noisy =  np.random.normal(fwd , sigma_step , 1)
            side_noisy =  np.random.normal(side , sigma_step , 1)
            rx +=  side_noisy[0]
            ry +=  fwd_noisy[0]
        return rx, ry