Sensors - jetstreamc/PySimbot GitHub Wiki

The purpose of this example is for understanding how sensor works, and how environment is described by sensor values.

By running this example, PySimbot will create the predefined simulation with WASD control (WASD Robot). Moreover, the sensor values are recorded by the Logging and printed out to the command windows.

Two kinds of sensor are defined.

  • 8 distance sensors

    The sensors are located around the robots at the edges in 8 directions (+45 degrees for every sensor starting from the robot front edge). These sensors has operational distance of 100 pixels. If there is no obstacle and wall in the operation range, the sensors will return 100.

    To get the distance sensor values of the robot, the robot instance can call the instance method

    self.distance()
    
  • 1 smell sensor

    The sensor measures the angle in degree (-180 to 180) from the robot center position to the specified food. If the food is on the left of robot the angle will be negative. If the food is on the right of the robot the angle will be positive.

    To get the smell sensor values of the robot, the robot instance can call the instance method

    self.smell()
    

Creating your own Robot class and call the functions to get the sensor values in the update(self) method. Then, pass the class object as the argument of the PySimbotApp.

from pysimbotlib.core import PySimbotApp, Robot
from kivy.logger import Logger

class ExplorerRobot(Robot):
    def update(self):
        Logger.info("Smell Angle: {0}".format(self.smell()))
        Logger.info("Distance: {0}".format(self.distance()))

if __name__ == '__main__':
    app = PySimbotApp(robot_cls=ExplorerRobot, enable_wasd_control=True)
    app.run()

To run this example, enter this command

python example2_sensors.py

In the command window, you should see the result like this.

Logging

The key point of this example is to call the self.distance() and self.smell() in the update(self) method.

Full code of this example is available at example2_sensors.py