fix random moving tractor

This commit is contained in:
Dominik Cupał 2021-06-02 14:06:02 +02:00
parent 4c3f216c03
commit 8872992b6b
3 changed files with 28 additions and 13 deletions

View File

@ -2,7 +2,7 @@
### 1. Requirements
python version 3.9 or higher
```bash
python3 -v
python3 --version
```
### 2. Create virtual environments and install libs
```bash

View File

@ -30,7 +30,7 @@ class NeuralNetwork:
# labels
self.labels = ["cabbage", "carrot", "corn", "lettuce", "paprika", "potato", "sunflower", "tomato"]
def init_model(self):
def init_model(self) -> None:
if not self.model_dir_is_empty():
# Load the model
self.model = load_model(
@ -62,7 +62,7 @@ class NeuralNetwork:
# Display a model summary
# self.model.summary()
def load_images(self):
def load_images(self) -> None:
# Create a generator
self.train_datagen = ImageDataGenerator(
rescale=1. / 255

View File

@ -210,7 +210,7 @@ class Tractor(BaseField):
time.sleep(TIME_OF_MOVING)
is_running.clear()
def move_or_rotate(self, movement: str):
def move_or_rotate(self, movement: str) -> None:
print(f"Move {movement}")
if movement == M_GO_FORWARD:
self.move()
@ -267,14 +267,25 @@ class Tractor(BaseField):
board.get_fields()[x][y] = obj()
return obj()
def harvest_checked_fields_handler(self, is_running: threading.Event):
def harvest_checked_fields_handler(self, is_running: threading.Event) -> None:
thread = threading.Thread(target=self.harvest_checked_fields, args=(is_running,), daemon=True)
thread.start()
def harvest_checked_fields(self, is_running: threading.Event):
def go_forward_is_legal_move(self) -> bool:
flag = False
if (self.__direction == D_EAST and self.__pos_y - self.__move >= 0) or \
(self.__direction == D_NORTH and self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT) or \
(self.__direction == D_WEST and self.__pos_x - self.__move >= 0) or \
(self.__direction == D_SOUTH and self.__pos_x + self.__move + FIELD_SIZE <= WIDTH):
flag = True
return flag
def harvest_checked_fields(self, is_running: threading.Event) -> None:
while True:
moves = [M_GO_FORWARD, M_ROTATE_LEFT, M_ROTATE_RIGHT]
distribution = [0.6, 0.2, 0.2]
while True:
field = self.get_field_from_board()
self.__neural_network = NeuralNetwork()
@ -283,8 +294,12 @@ class Tractor(BaseField):
if prediction.capitalize() in CROPS:
self.harvest()
break
chosen_move = random.choices(moves,distribution)
if not self.go_forward_is_legal_move():
moves = moves[1:]
distribution = distribution[1:]
chosen_move = random.choices(moves, distribution)
self.move_or_rotate(chosen_move[0])
time.sleep(1)