Skip to content

Commit

Permalink
Issue #1656: improve camera configuration, add and allow multiple det…
Browse files Browse the repository at this point in the history
…ectors
  • Loading branch information
heini208 authored and bjost2s committed Aug 14, 2024
1 parent 54c5f5a commit 29a953d
Show file tree
Hide file tree
Showing 85 changed files with 2,289 additions and 1,058 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class RobotCommunicationData {
private static final Logger LOG = LoggerFactory.getLogger(RobotCommunicationData.class);
private static final int TIMEOUT_UNTIL_TOKEN_EXPIRES_WHEN_USER_DOESNT_APPROVE = 300000;
private static final int TIMEOUT_UNTIL_ASSUME_DISCONNECTED_IF_ROBOT_DOESNT_PUSH = 10000;
private static final int TIMEOUT_UNTIL_ASSUME_DISCONNECTED_IF_ROBOT_DOESNT_PUSH = 11000;
private static final int PUSH_TIMEOUT_INTERVALL = 10000;

private final String token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ private static ConfigurationComponent block2NewConfigComp(Block block, BlocklyDr
for ( int i = 1; i < block.getField().size(); i++ ) {
map.put(block.getField().get(i).getName(), block.getField().get(i).getValue());
}
for ( Value value : block.getValue() ) {
String name = value.getName();
String val = value.getBlock().getField().get(0).getValue().toString();
map.put(name, val);
}
LinkedHashMap<String, List<ConfigurationComponent>> subcomponents = new LinkedHashMap<>();
List<Statement> statements = block.getStatement();
if ( statements.size() == 0 ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,4 @@
SerialWriteAction[value: StringConst[value: Camera Tests]]
SerialWriteAction[value: StringConst[value: Press right display button to switch to next Detector]]
SerialWriteAction[value: StringConst[value: Config Settings are 30, Orange, 2]]
SerialWriteAction[value: StringConst[value: Camera Color]]

(repeat [FOREVER, BoolConst[value: true]]
AktionStmt [SerialWriteAction[value: SensorExpr [ColorSensor [C, COLOUR, - EMPTY_SLOT -]]]]WaitTimeStmt[time: NumConst[value: 200]]
if SensorExpr [TouchKeySensor [RIGHT, PRESSED, - EMPTY_SLOT -]]
,thenStmtFlowCon[flow: BREAK]

)
SerialWriteAction[value: StringConst[value: Camera Color compared to red]]
WaitTimeStmt[time: NumConst[value: 300]]

(repeat [FOREVER, BoolConst[value: true]]
AktionStmt [SerialWriteAction[value: ActionExpr [ColourCompare[op: EQ, colour1: SensorExpr [ColorSensor [C, COLOUR, - EMPTY_SLOT -]], colour2: ColorConst[hexValue: #ff0000], tolerance: NumConst[value: 50]]]]]WaitTimeStmt[time: NumConst[value: 200]]
if SensorExpr [TouchKeySensor [RIGHT, PRESSED, - EMPTY_SLOT -]]
,thenStmtFlowCon[flow: BREAK]

)
SerialWriteAction[value: StringConst[value: Line Information]]
WaitTimeStmt[time: NumConst[value: 300]]

(repeat [FOREVER, BoolConst[value: true]]
AktionStmt [SerialWriteAction[value: FunctionExpr [TextJoinFunct [StringConst[value: Number of lines: ], SensorExpr [CameraLineSensor [C, NUMBERLINES, - EMPTY_SLOT -]]]]]]
AktionStmt [SerialWriteAction[value: FunctionExpr [TextJoinFunct [StringConst[value: Color of first line: ], SensorExpr [CameraLineColourSensor[mode: COLOUR, port: - EMPTY_PORT -, lineId: NumConst[value: 0]]]]]]]
AktionStmt [SerialWriteAction[value: StringConst[value: Position and Width of line 0]]]
(repeat [FOR_EACH, Binary [IN, VarDeclaration [NUMBER, item, EmptyExpr [defVal=NUMBER], false, false], SensorExpr [CameraLineInformationSensor[mode: INFORMATION, port: - EMPTY_PORT -, lineId: NumConst[value: 0]]]]]
AktionStmt [SerialWriteAction[value: Var [item]]]
)WaitTimeStmt[time: NumConst[value: 200]]
if SensorExpr [TouchKeySensor [RIGHT, PRESSED, - EMPTY_SLOT -]]
,thenStmtFlowCon[flow: BREAK]

)
SerialWriteAction[value: StringConst[value: Ball Information]]
WaitTimeStmt[time: NumConst[value: 300]]

(repeat [FOREVER, BoolConst[value: true]]
AktionStmt [SerialWriteAction[value: FunctionExpr [TextJoinFunct [StringConst[value: X: ], FunctionExpr [ListGetIndex [GET, FROM_START, [SensorExpr [CameraBallSensor [C, BALL, - EMPTY_SLOT -]], NumConst[value: 0]]]]]]]]
AktionStmt [SerialWriteAction[value: FunctionExpr [TextJoinFunct [StringConst[value: Y: ], FunctionExpr [ListGetIndex [GET, FROM_START, [SensorExpr [CameraBallSensor [C, BALL, - EMPTY_SLOT -]], NumConst[value: 1]]]]]]]]
AktionStmt [SerialWriteAction[value: FunctionExpr [TextJoinFunct [StringConst[value: Diameter: ], FunctionExpr [ListGetIndex [GET, FROM_START, [SensorExpr [CameraBallSensor [C, BALL, - EMPTY_SLOT -]], NumConst[value: 2]]]]]]]]WaitTimeStmt[time: NumConst[value: 200]]
if SensorExpr [TouchKeySensor [RIGHT, PRESSED, - EMPTY_SLOT -]]
,thenStmtFlowCon[flow: BREAK]

)
SerialWriteAction[value: StringConst[value: Motion]]
WaitTimeStmt[time: NumConst[value: 300]]

(repeat [FOREVER, BoolConst[value: true]]
AktionStmt [SerialWriteAction[value: SensorExpr [MotionSensor [C, MOTION, - EMPTY_SLOT -]]]]WaitTimeStmt[time: NumConst[value: 200]]
if SensorExpr [TouchKeySensor [RIGHT, PRESSED, - EMPTY_SLOT -]]
,thenStmtFlowCon[flow: BREAK]

)
SerialWriteAction[value: StringConst[value: DONE]]
WaitTimeStmt[time: NumConst[value: 500]]
MethodCall [ball, , , VOID]
MethodCall [color, , , VOID]
MethodCall [motion, , , VOID]
MethodCall [line, , , VOID]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Sensors:
[UsedSensor [C, CAMERA, COLOUR], UsedSensor [C, COLOUR, COLOUR], UsedSensor [DISPLAY, BUTTON, PRESSED], UsedSensor [, COLOURCOMPARE, COLOURCOMPARE], UsedSensor [C, CAMERA, LINE], UsedSensor [C, LINE, LINE], UsedSensor [C, CAMERA, BALL], UsedSensor [C, BALL, BALL], UsedSensor [C, CAMERA, MOTION], UsedSensor [C, MOTION, MOTION]]
[UsedSensor [M, CAMERA, MOTION], UsedSensor [M, MOTION, MOTION], UsedSensor [M2, CAMERA, MOTION], UsedSensor [M2, MOTION, MOTION], UsedSensor [DISPLAY, BUTTON, PRESSED], UsedSensor [B, CAMERA, BALL], UsedSensor [B, BALL, BALL], UsedSensor [B2, CAMERA, BALL], UsedSensor [B2, BALL, BALL], UsedSensor [L, CAMERA, LINE], UsedSensor [L, LINE, LINE], UsedSensor [L2, CAMERA, LINE], UsedSensor [L2, LINE, LINE], UsedSensor [C2, CAMERA, COLOUR], UsedSensor [C2, COLOUR, COLOUR], UsedSensor [C3, CAMERA, COLOUR], UsedSensor [C3, COLOUR, COLOUR], UsedSensor [, COLOURCOMPARE, COLOURCOMPARE]]
Actors:
[UsedActor [DISPLAY, DISPLAY]]
Methods:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,102 @@
TXT_M_USB1_1_camera.set_width(320)
TXT_M_USB1_1_camera.set_fps(15)
TXT_M_USB1_1_camera.start()
CAMERA_HEIGHT = 240
CAMERA_WIDTH = 320

motion_detector = txt_factory.camera_factory.create_motion_detector(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, CAMERA_WIDTH * CAMERA_HEIGHT * (2 / 100) / 500)
TXT_M_USB1_1_camera.add_detector(motion_detector)
color_detector = txt_factory.camera_factory.create_color_detector(112, 84, 96, 72, 1)
TXT_M_USB1_1_camera.add_detector(color_detector)
line_detector = txt_factory.camera_factory.create_line_detector(60, 45, 200, 150, 5, 100, -100, 100, 6)
TXT_M_USB1_1_camera.add_detector(line_detector)
ball_detector = txt_factory.camera_factory.create_ball_detector(0, 0, 320, 240, 10, 100, -100, 100, [255, 165, 0], 20)
TXT_M_USB1_1_camera.add_detector(ball_detector)

motion_detector_M = txt_factory.camera_factory.create_motion_detector(0, 100, 320, 20, 100)
TXT_M_USB1_1_camera.add_detector(motion_detector_M)
motion_detector_M2 = txt_factory.camera_factory.create_motion_detector(0, 100, 320, 20, 49)
TXT_M_USB1_1_camera.add_detector(motion_detector_M2)
color_detector_C2 = txt_factory.camera_factory.create_color_detector(0, 100, 320, 20, 1)
TXT_M_USB1_1_camera.add_detector(color_detector_C2)
color_detector_C3 = txt_factory.camera_factory.create_color_detector(0, 100, 320, 20, 1)
TXT_M_USB1_1_camera.add_detector(color_detector_C3)
line_detector_L = txt_factory.camera_factory.create_line_detector(0, 100, 100, 100, 12, 50, -100, 100, 6)
TXT_M_USB1_1_camera.add_detector(line_detector_L)
line_detector_L2 = txt_factory.camera_factory.create_line_detector(20, 10, 479, 290, 20, 60, -100, 100, 4)
TXT_M_USB1_1_camera.add_detector(line_detector_L2)
ball_detector_B = txt_factory.camera_factory.create_ball_detector(0, 100, 320, 20, 10,100, -100, 100, [255, 165, 0], 20)
TXT_M_USB1_1_camera.add_detector(ball_detector_B)
ball_detector_B2 = txt_factory.camera_factory.create_ball_detector(0, 100, 320, 20, 10,100, -100, 100, [102, 102, 0], 20)
TXT_M_USB1_1_camera.add_detector(ball_detector_B2)
txt_factory.initialized()
time.sleep(0.1)


def get_ball_information():
if ball_detector.detected():
return [ball_detector.get_center_x(),
ball_detector.get_center_y(),
ball_detector.get_diameter()]
def ____motion():
print("Motiondetectors")
while True:
print(motion_detector_M.detected())
print(motion_detector_M2.detected())
print("#######")
time.sleep(500/1000)
if display.get_attr("buttonRight.pressed"):
break
time.sleep(500/1000)

def ____ball():
print("Ball Detectors")
while True:
print(get_ball_information(ball_detector_B)[0])
print(get_ball_information(ball_detector_B)[1])
print(get_ball_information(ball_detector_B)[2])
print("---------")
print(get_ball_information(ball_detector_B2)[0])
print(get_ball_information(ball_detector_B2)[1])
print(get_ball_information(ball_detector_B2)[2])
print("#######")
time.sleep(500/1000)
if display.get_attr("buttonRight.pressed"):
break
time.sleep(500/1000)

def ____line():
print("Line Detector 1")
while True:
print(line_detector_L.get_line_count())
for ___i in range(int(0), int(line_detector_L.get_line_count()), int(1)):
print("".join(str(arg) for arg in ["Line:", ___i]))
print(get_line_information(line_detector_L, ___i)[0])
print(get_line_information(line_detector_L, ___i)[1])
print(get_line_colour(line_detector_L, ___i))
print("#######")
time.sleep(500/1000)
if display.get_attr("buttonRight.pressed"):
break
time.sleep(500/1000)
print("Line Detector 2")
while True:
print(line_detector_L2.get_line_count())
for ___j in range(int(0), int(line_detector_L2.get_line_count()), int(1)):
print("".join(str(arg) for arg in ["Line:", ___j]))
for ___item in get_line_information(line_detector_L2, ___j):
print(___item)
print("#######")
time.sleep(500/1000)
if display.get_attr("buttonRight.pressed"):
break

def ____color():
print("Color Detectors")
while True:
print(get_camera_colour(color_detector_C2))
print(get_camera_colour(color_detector_C3))
print(compare_colour(get_camera_colour(color_detector_C2), 0x000000, 20))
print("#######")
time.sleep(500/1000)
if display.get_attr("buttonRight.pressed"):
break
time.sleep(500/1000)

def get_ball_information(detector):
if detector.detected():
return [detector.get_center_x(),
detector.get_center_y(),
detector.get_diameter()]
return [-1, -1, -1]

def get_camera_colour():
if color_detector.detected():
result = color_detector.get_result().value.get_hex()
def get_camera_colour(detector):
if detector.detected():
result = detector.get_result().value.get_hex()
return int(result[1:], 16)
else:
return -1
Expand All @@ -60,16 +131,16 @@ def compare_colour(hex1, hex2, tolerance):
value_diff = abs(hsv1[2] - hsv2[2])
return (hue_diff <= (tolerance / 360) and saturation_diff <= 0.5 and value_diff <= 0.5)

def get_line_colour(index):
line = line_detector.get_line_by_index(index)
def get_line_colour(detector, index):
line = detector.get_line_by_index(index)
if line:
colour = line.color.get_hex()
return int(colour[1:], 16)
else:
return -1

def get_line_information(index):
line = line_detector.get_line_by_index(index)
def get_line_information(detector, index):
line = detector.get_line_by_index(index)
if line is not None:
return [line.position, line.width]
else:
Expand All @@ -78,61 +149,24 @@ def get_line_information(index):
def camera_initialized():
while True:
try:
ball_detector.detected()
line_detector.detected()
color_detector.detected()
motion_detector.detected()
ball_detector_B.detected()
ball_detector_B2.detected()
line_detector_L.detected()
line_detector_L2.detected()
color_detector_C2.detected()
color_detector_C3.detected()
motion_detector_M.detected()
motion_detector_M2.detected()
break
except Exception:
pass

def run():
camera_initialized()
print("Camera Tests")
print("Press right display button to switch to next Detector")
print("Config Settings are 30, Orange, 2")
print("Camera Color")
while True:
print(get_camera_colour())
time.sleep(200/1000)
if display.get_attr("buttonRight.pressed"):
break
print("Camera Color compared to red")
time.sleep(300/1000)
while True:
print(compare_colour(get_camera_colour(), 0xff0000, 50))
time.sleep(200/1000)
if display.get_attr("buttonRight.pressed"):
break
print("Line Information")
time.sleep(300/1000)
while True:
print("".join(str(arg) for arg in ["Number of lines: ", line_detector.get_line_count()]))
print("".join(str(arg) for arg in ["Color of first line: ", get_line_colour(0)]))
print("Position and Width of line 0")
for ___item in get_line_information(0):
print(___item)
time.sleep(200/1000)
if display.get_attr("buttonRight.pressed"):
break
print("Ball Information")
time.sleep(300/1000)
while True:
print("".join(str(arg) for arg in ["X: ", get_ball_information()[0]]))
print("".join(str(arg) for arg in ["Y: ", get_ball_information()[1]]))
print("".join(str(arg) for arg in ["Diameter: ", get_ball_information()[2]]))
time.sleep(200/1000)
if display.get_attr("buttonRight.pressed"):
break
print("Motion")
time.sleep(300/1000)
while True:
print(motion_detector.detected())
time.sleep(200/1000)
if display.get_attr("buttonRight.pressed"):
break
print("DONE")
time.sleep(500/1000)
____ball()
____color()
____motion()
____line()

def main():
try:
Expand Down
Loading

0 comments on commit 29a953d

Please sign in to comment.