Update 'Object_Sorting/Program/Object_sorter.py'

This commit is contained in:
Tanja Sukal 2024-09-10 12:43:29 +00:00
parent cdcfe9ba74
commit 7361f1b093

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3
# Version 2.1 from 12/07/24
from time import sleep
from ev3dev2.motor import Motor, MoveTank, SpeedPercent, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D
from ev3dev2.sensor import INPUT_2,INPUT_3,INPUT_4
@ -70,10 +70,10 @@ class Object_Sorter():
# Define method/state search
def search (self):
# turn slowly aroud on place and search for blocks,
# if nothing is found change searchplace
# if nothing is found change search place
self.leds.set_color("LEFT", "BLACK")
self.leds.set_color("RIGHT", "BLACK")
# turn on place to the right
# turn on spot to the right
self.m_right.on(SpeedPercent(-1*self.params["slow"]))
self.m_left.on(SpeedPercent(self.params["slow"]))
# search for blocks
@ -94,7 +94,7 @@ class Object_Sorter():
self.last_state = self.state
self.state = "avoid"
elif self.gyro.angle > 720: # when the robot does not find a block drive a little bit forward
rounds=0 # rounds of driving forward
rounds = 0 # control variable of rounds driving forward
while self.cs.reflected_light_intensity > self.params["line"] and rounds <= 5:
self.tank.on(self.params["fast"],self.params["fast"])
sleep(0.2)
@ -104,7 +104,6 @@ class Object_Sorter():
self.last_state = self.state
self.state = "edge"
self.gyro.reset()
rounds=0 # Reset control variable
# Define method/state get
def get (self):
@ -118,12 +117,12 @@ class Object_Sorter():
if nr_sort > 0: # if block is found again get position
x = sort_block[0].x_center #get where in the field of vision the block lays
self.cycles = 0
else: # when block is not found, virtually place the robot in the middle of the vision
else: # when block is not found, virtually place the cube in the middle of the vision
x = 130
self.cycles +=1
if 4 <= self.cycles < 6: # camera has not found block for 4 cycles
self.tank.on_for_seconds(-1*self.params["fast"],-1*self.params["fast"],0.4) # drive backwards max. 2 times
elif 6 == self.cycles: # if block is not found drive a little bit to left
elif 6 == self.cycles: # if block is not found turn a little bit to left
self.m_right.on(SpeedPercent(self.params["slow"]))
self.m_left.on(SpeedPercent(-1*self.params["slow"]))
sleep(0.2)
@ -135,7 +134,7 @@ class Object_Sorter():
if self.cs.reflected_light_intensity < self.params["line"]: # robot detected line change to edge
self.tank.stop()
self.last_state = self.state
self.state = "edge" # When colorsensor detects line then switch state
self.state = "edge" # When color sensor detects line then switch state
elif self.us.distance_centimeters <= self.params["dist_us"]: # block is in the arms of robot change to sort
self.last_state = self.state
self.state = "sort" # when block distance is small switch state
@ -201,9 +200,11 @@ class Object_Sorter():
self.state = "search"
self.gyro.reset() # reset gyro sensor again
# Method for getting parameters of the object
def get_params(self):
return self.params
# Method that is used for executing
def run(self):
print('start')
while True: # endless-loop for switching states