Skip to content

Sherevv/robot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Robot

Robot on the plaid field

Описание на русском

Table of Content

Requirements

  • Python 3.7 and above (3.7)
  • tkinter (8.6.9, include in the standard Python distribution, in Linux should be install separately)
  • numpy (1.17.4)
  • matplotlib (3.1.1)

Installation

You should use command line terminal.

First variant. Clone repo and go to the package dir

git clone https://github.com/Sherevv/robot.git robot

cd robot

and execute command:

python setup.py install

or

pip install .

Second variant. Remote using pip:

pip install git+https://github.com/Sherevv/robot.git

Class Robot

r = Robot()
r = Robot( mapfile )
r = Robot( delay=0.5 ) or r = Robot(None, 0.5 )
params = {'delay': 0.2, 'size': [10, 5], 'frame': True}
r = Robot(**params) # 4

In the 1st case, the standard dialog box opens for in case of failure, the file selection dialog opens for definition of field options

In the 2nd case:

  • mapfile - the name of the file where the field map is stored

In the 3rd case:

  • delay - the time delay during animation (defaults to 0.5 seconds)

In the 4th case a dictionary of parameters is passes:

  • size - field sizes in list [height, width] (defaults [5, 3])
  • frame - whether the field frame is enabled (defaults True)

Robot methods

step(side) - moves robot on one step to the set side

mark() - puts a marker in the current cell

is_mark() - check if marker exists in the current cell

is_bord(side) - check if border exists in the set side

get_tmpr() - returns value of temperature in the current cell

side can use values 'n', 's', 'w', 'o' respectively North, South, West, East

Additional methods for working with the field, similar to the functionality of controls:

_is_frame () - check if there is an outer frame (wall) of the field

_frame_on(), _frame_off() - adds/removes the outer frame of the field

_set_field_size(size) - creates a new value for the specified size size = [coln, rown]

_set_field_coln(coln) - sets the new height of the field with coln

_set_field_rown(rown) - sets the new width of the field with rown

_restore_field() - restores the state of a field from a file

_save_field() - saves the state of the field in the file

_delay_on(), _delay_off() - enables / disables time delay

_tmpr_on(), _tmpr_off() - enables / disables cell temperature display

_remove_borders() - removes all partitions from the field (except the outer frame)

_remove_markers() - removes all markers from the field

_effects_on(), _effects_off() - enables/disables visualization of the field checks by robot

RobotOrt

The RobotOrt class, each object of this class represents an executor of a "oriented robot on a cellular field" The direction of travel, turns and check for obstacles are set on:

  • step forward;
  • turn left;
  • turn right;
  • check if there is an wall right on the course.

RobotOrt methods

forward() - move the robot forward to the next cell

right() - turn the robot to the right

left() - turn the robot to the left

is_bord() - check if there is an wall right on the course.

get_side() - get the current direction of the robot

Methods mark(), is_mark(), get_tmpr() - correspond to the Robot class methods

RobotRot

It differs from the RobotOrt class in that instead of two methods left and right the RobotOrt class has only one method rot, but with a parameter, which can have 3 values: 'left', 'right', 'back' (or 'l', 'r','b') and also because the is_bord method has a parameter that can accept also 3 values: 'forward', 'left', 'right'

RobotRot methods

rot(side) - turn the robot left, right or back

is_bord(side) - check if border exists in the side

The other methods are the same as in the RobotOrt class.

Basic usage

Create script, example start.py

from robot import Robot, RobotOrt, RobotRot

r = Robot()             # Robot create
if not r.is_mark():     # Check if current cell is marked
    r.mark()            # Mark cell
if not r.is_bord('w'):  # Check if border exists in West side
    r.step('w')         # Move to West
print(r.get_tmpr())     # Print temperature in current cell


ro = RobotOrt()         # Create orient robot
if not ro.is_bord():    # Check if border exists in front of robot
    ro.forward()        # Move forward
ro.right()              # Turn robot right
if not ro.is_bord():    # Check if border exists in front of robot
    ro.forward()        # Move forward
print(ro.get_side())    # Print robot orientation


rt = RobotRot()                # Create orient robot
if not rt.is_bord('forward'):  # Check if border exists in front of robot
    rt.forward()               # Move forward
if not rt.is_bord('right'):    # Check if border exists to the right of robot
    rt.rot('r')                # Turn robot right
    rt.forward()               # Move forward
print(rt.get_side())           # Print robot orientation


input()  # to prevent close plot window

then execute script

python start.py

Code examples

Usage with python command line

Prepare script with function (myfunc.py):

def walk_to_bord(r, side):
    """
    The robot goes until it meets the wall in the specified direction

    :param r:    robot object
    :param side: move side
    """
    while not r.is_bord(side):
        r.step(side)

then run python command line

python
>>> from robot import Robot
>>> from myfunc import walk_to_bord
>>> r = Robot()
>>> walk_to_bord(r, 'o')

Using with Jupyter

To run the robot in Jupyter Notebook, you must use the magic commands for matplotlib. To run the robot in a separate window, you can use the %matplotlib command with the tk parameter:

%matplotlib tk
from robot import Robot
r = Robot()
...

To display the robot directly in Notebook, you must use the widgetparameter

%matplotlib widget

For using widget parameter, you need to install this extension: jupyter-matplotlib.

Working with the interface

When creating a robot instance, a window with a field and a robot appears: Example of a field with robot walls and marker

Change ( install or edit ) the initial situation on the field, you can use the mouse:

  • click on a cell to set a marker
  • click on the marker to delete the marker
  • click on the dotted grid line of the checkered field sets partition
  • clicking on the partition removes the partition
  • click on the robot and move the mouse when the key is left moves the robot after the cursor to the desired cell (a" faulty "robot can also be "repaired" this way )

After any number of such actions you should save field state, after that the robot is ready for the command management

Keyboard control

The window has buttons to control the field and its state, they can be controlled from the keyboard.

Add column - add one column to the field (key w)

Remove column - remove one column from the field (key e)

Add row - add one row to the field (key h)

Remove row - remove one row from the field (key j)

Remove all borders - removes all borders (ctrl + b)

Remove all markers - remove all markers (ctrl + m)

Add/delete frame - add/delete the outer frame of the field (key b)

Show/hide temperature - show/hide the temperature of the cell (key t)

Enable / disable delay - enable/disable time delay and animation effects (key d)

Restore state - restore state of the field from file (key r)

Save state - save state of the field to file (without save dialog)(key s)

Save state as - save state of the field to file (ctrl + s)

Open file - load state of the field from file (ctrl + l)

Control buttons

The main keys to control the field are listed above.

In addition, the following combinations work:

ctrl + '+' - increases the time delay by 2 times

ctrl + '-' - reduces the time delay by half

ctrl + r - moves the robot to the lower left corner

Bug

There is a class for the "bug in the maze" problem (original problem here). Initially, the bug is in the upper left corner, the output is in the lower right cell. The bug moves independently in search of an exit by a certain algorithm. It is necessary to build a maze in which the bug will wander as long as possible. There must be a way to the exit, and the field must have an outer frame.

Usage:

from robot.bug import Bug

b = Bug('bug.map', delay=0.0001)  # or just Bug()
b.go()  # start bug walking


input() # to prevent close plot window

About

Robot on a grid field

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages