UserPreferences

JoystickMapProgram


You are not allowed to edit this page. Are you logged in? Click on UserPreferences in upper-righthand corner.

Clear message

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 

from pyrobot.brain import *
from pyrobot.tools.joystick import Joystick
import pyrobot.system.share as share
from pyrobot.map.lps import LPS
from pyrobot.map.gps import GPS
import thread

class Map(Brain):
   def setup(self):
      # We want our map to measure in MM, so we first store our current unit of measure
      units = self.robot.range.units
      # We then reset our measurements to MMs
      self.robot.range.units = 'MM'
      # Calculate the maximum range of our sensors
      rangeMaxMM = self.robot.range.getMaxvalue()
      sizeMM = rangeMaxMM * 3 + self.robot.radius
      # Reset our unit of measure
      self.robot.range.units = units
      # Now, we create our Local Perceptual Space window - this will hold our local map
      # Map will be 20px by 20px and will represent a height and width of sizeMM (total sensor range)
      self.lps = LPS( 40, 40,
                      widthMM = sizeMM,
                      heightMM = sizeMM)
      # Then create our Global Perceptual Space window - this will hold our global map
      # This map will be 500px by 500px and will represent an area ten times the size of our maximum range
      self.gps = GPS( cols=300, rows=300,
                      heightMM = sizeMM * 5, widthMM = sizeMM * 5)
      self.joystick = Joystick(share.gui)
      self.need_redraw = False
      self.lock = thread.allocate_lock()

   def step(self):
      if not self.lock.acquire(False):
         return
      #print "Stepping...",
      # First we clear out all our old LPS data
      self.lps.reset()
      # Next we update our LPS with current 'range' sensor readings
      self.lps.sensorHits(self.robot, 'range')
      # Now redraw our LPS window - the LPS redraw can be improve performance

      # Then update our GPS with the new information in the LPS
      self.gps.updateFromLPS(self.lps, self.robot)
      # Finally, we redraw the GPS window

      self.need_redraw = True
      self.move(self.joystick.translate, self.joystick.rotate)
      self.lock.release()
      #print "done stepping!"

   def redraw(self):
      if (not self.lock.acquire(False)):
         return
      #print "Redrawing...",
      if self.need_redraw:
         self.lps.redraw(drawLabels=False)
         self.gps.update()
         self.gps.redraw()
         self.need_redraw = False
      self.lock.release()
      #print "done redrawing!"

   def destroy(self):
      # Make sure we close down cleanly
      self.lps.destroy()
      self.gps.destroy()
      self.joystick.destroy()

def INIT(engine):
   return Map("Mapping Brain", engine)