Update 'Object_Sorting/Program/Object_sorter.py'
This commit is contained in:
parent
cdcfe9ba74
commit
7361f1b093
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# Version 2.1 from 12/07/24
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from ev3dev2.motor import Motor, MoveTank, SpeedPercent, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D
|
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
|
from ev3dev2.sensor import INPUT_2,INPUT_3,INPUT_4
|
||||||
@ -73,7 +73,7 @@ class Object_Sorter():
|
|||||||
# if nothing is found change search place
|
# if nothing is found change search place
|
||||||
self.leds.set_color("LEFT", "BLACK")
|
self.leds.set_color("LEFT", "BLACK")
|
||||||
self.leds.set_color("RIGHT", "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_right.on(SpeedPercent(-1*self.params["slow"]))
|
||||||
self.m_left.on(SpeedPercent(self.params["slow"]))
|
self.m_left.on(SpeedPercent(self.params["slow"]))
|
||||||
# search for blocks
|
# search for blocks
|
||||||
@ -94,7 +94,7 @@ class Object_Sorter():
|
|||||||
self.last_state = self.state
|
self.last_state = self.state
|
||||||
self.state = "avoid"
|
self.state = "avoid"
|
||||||
elif self.gyro.angle > 720: # when the robot does not find a block drive a little bit forward
|
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:
|
while self.cs.reflected_light_intensity > self.params["line"] and rounds <= 5:
|
||||||
self.tank.on(self.params["fast"],self.params["fast"])
|
self.tank.on(self.params["fast"],self.params["fast"])
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
@ -104,7 +104,6 @@ class Object_Sorter():
|
|||||||
self.last_state = self.state
|
self.last_state = self.state
|
||||||
self.state = "edge"
|
self.state = "edge"
|
||||||
self.gyro.reset()
|
self.gyro.reset()
|
||||||
rounds=0 # Reset control variable
|
|
||||||
|
|
||||||
# Define method/state get
|
# Define method/state get
|
||||||
def get (self):
|
def get (self):
|
||||||
@ -118,12 +117,12 @@ class Object_Sorter():
|
|||||||
if nr_sort > 0: # if block is found again get position
|
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
|
x = sort_block[0].x_center #get where in the field of vision the block lays
|
||||||
self.cycles = 0
|
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
|
x = 130
|
||||||
self.cycles +=1
|
self.cycles +=1
|
||||||
if 4 <= self.cycles < 6: # camera has not found block for 4 cycles
|
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
|
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_right.on(SpeedPercent(self.params["slow"]))
|
||||||
self.m_left.on(SpeedPercent(-1*self.params["slow"]))
|
self.m_left.on(SpeedPercent(-1*self.params["slow"]))
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
@ -201,9 +200,11 @@ class Object_Sorter():
|
|||||||
self.state = "search"
|
self.state = "search"
|
||||||
self.gyro.reset() # reset gyro sensor again
|
self.gyro.reset() # reset gyro sensor again
|
||||||
|
|
||||||
|
# Method for getting parameters of the object
|
||||||
def get_params(self):
|
def get_params(self):
|
||||||
return self.params
|
return self.params
|
||||||
|
|
||||||
|
# Method that is used for executing
|
||||||
def run(self):
|
def run(self):
|
||||||
print('start')
|
print('start')
|
||||||
while True: # endless-loop for switching states
|
while True: # endless-loop for switching states
|
||||||
|
Loading…
Reference in New Issue
Block a user