Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
hotfix and improved autonomy bit
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Apr 9, 2022
1 parent b428b4d commit 06012d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
20 changes: 17 additions & 3 deletions scenes/bind_addr.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func check_inputs():
if err != OK:
push_error(\"Error code: \" + str(err) + \" while trying to set up broadcaster\")
return
print(\"Success!\")
print(\"Socket setup successful!\")

yield(BotServer, \"packet_received\")
get_tree().change_scene(\"res://scenes/main.tscn\")
Expand Down Expand Up @@ -102,9 +102,9 @@ script = SubResource( 5 )
[node name="VBoxContainer" type="VBoxContainer" parent="."]
margin_left = 437.0
margin_top = 189.0
margin_top = 175.0
margin_right = 587.0
margin_bottom = 411.0
margin_bottom = 425.0
alignment = 1
[node name="Label" type="Label" parent="VBoxContainer"]
Expand Down Expand Up @@ -187,10 +187,24 @@ text = "42420"
align = 1
script = SubResource( 4 )
[node name="Control4" type="Control" parent="VBoxContainer"]
margin_top = 226.0
margin_right = 150.0
margin_bottom = 226.0
[node name="Button" type="Button" parent="VBoxContainer"]
margin_left = 23.0
margin_top = 230.0
margin_right = 126.0
margin_bottom = 250.0
size_flags_horizontal = 4
text = "Start Listening"
[connection signal="text_entered" from="VBoxContainer/LineEdit" to="VBoxContainer/LineEdit" method="_on_LineEdit_text_entered"]
[connection signal="valid_input" from="VBoxContainer/LineEdit" to="." method="check_inputs"]
[connection signal="text_entered" from="VBoxContainer/LineEdit2" to="VBoxContainer/LineEdit2" method="_on_LineEdit_text_entered"]
[connection signal="valid_input" from="VBoxContainer/LineEdit2" to="." method="check_inputs"]
[connection signal="text_entered" from="VBoxContainer/LineEdit3" to="VBoxContainer/LineEdit3" method="_on_LineEdit_text_entered"]
[connection signal="valid_input" from="VBoxContainer/LineEdit3" to="." method="check_inputs"]
[connection signal="text_entered" from="VBoxContainer/LineEdit4" to="VBoxContainer/LineEdit4" method="_on_LineEdit_text_entered"]
[connection signal="pressed" from="VBoxContainer/Button" to="." method="check_inputs"]
62 changes: 32 additions & 30 deletions scripts/bot_server.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ enum {
PATHING,
COST_MAP,
REQUEST_COST_MAP,
AUTONOMY_BIT,
MAKE_AUTONOMOUS,
MAKE_MANUAL,
ECHO,
TARGET_ARM_ANGLE
}

Expand All @@ -27,7 +29,6 @@ var bot_tcp: StreamPeerTCP
var bot_udp := PacketPeerUDP.new()

var broadcaster := PacketPeerUDP.new()
#var autonomy := true setget set_autonomy

var _last_packet_time := OS.get_system_time_msecs()
var start_time := OS.get_system_time_secs()
Expand All @@ -38,27 +39,11 @@ var bind_port: int
var broadcast_timer := Timer.new()


#func set_autonomy(bit: bool) -> void:
# if bot_udp == null:
# print("bot_udp IS NOT CONNECTED, CANNOT CHANGE AUTONOMY STATE")
# return
# # warning-ignore:return_value_discarded
## bot_udp.put_packet(to_json({AUTONOMY_BIT: bit}).to_utf8())
# autonomy = bit
# set_process_input(not bit)
## logs[AUTONOMY_BIT].append([get_runtime(), bit])
#
# if bit:
# print("bot_udp IS FULLY AUTONOMOUS")
# return
# print("bot_udp IS AWAITING INPUT")


func _ready():
set_process_input(false)
set_process(false)
add_child(broadcast_timer)
broadcast_timer.connect("timeout", self, "broadcast")
broadcast_timer.start(BROADCAST_DELAY)


func start_brodcasting(addr: String, port: int) -> int:
Expand All @@ -76,16 +61,15 @@ func start_brodcasting(addr: String, port: int) -> int:
return ERR_CANT_RESOLVE

var err := broadcaster.join_multicast_group(addr, interface_name)

if err != OK:
return err

err = broadcaster.set_dest_address(addr, port)

if err != OK:
return err

broadcasting = true
broadcast_timer.start(BROADCAST_DELAY)
return OK


Expand All @@ -103,6 +87,7 @@ func start_listening(addr: String, port: int) -> int:
bind_addr = addr
bind_port = port
set_process_input(true)
set_process(true)
listening = true
return OK

Expand All @@ -111,6 +96,24 @@ func broadcast():
broadcaster.put_packet((bind_addr + ":" + str(bind_port)).to_utf8())


func make_autonomous():
bot_tcp.put_data(PoolByteArray([MAKE_AUTONOMOUS]))
push_warning("Sent MAKE_AUTONOMOUS to bot")


func make_manual():
bot_tcp.put_data(PoolByteArray([MAKE_MANUAL]))
push_warning("Sent MAKE_MANUAL to bot")


func _input(event):
if (event is InputEventJoypadMotion and abs(event.axis_value) >= DEADZONE) or \
event is InputEventJoypadButton:
var err := bot_udp.put_packet(_get_controller_state())
if err != OK:
push_error("Faced error code " + str(err) + "while sending input data!")


func _process(delta):
match bot_udp.get_available_packet_count():
0: pass
Expand All @@ -121,6 +124,7 @@ func _process(delta):
if bot_tcp == null:
if bot_tcp_server.is_connection_available():
bot_tcp = bot_tcp_server.take_connection()
bot_tcp.set_no_delay(true)
if broadcasting:
broadcasting = false
broadcast_timer.stop()
Expand Down Expand Up @@ -190,18 +194,16 @@ func _handle_message(msg: PoolByteArray):
))
ARM_ANGLE:
emit_signal("arm_angle_received", Serde.deserialize_f32(msg))
ECHO:
match msg[0]:
MAKE_AUTONOMOUS:
push_warning("Bot is autonomous")
MAKE_MANUAL:
push_warning("Bot is manual")
_:
push_error("Received unrecognized header: " + str(msg[0]))


func _input(event):
if (event is InputEventJoypadMotion and abs(event.axis_value) >= DEADZONE) or \
event is InputEventJoypadButton:
var err := bot_udp.put_packet(_get_controller_state())
if err != OK:
push_error("Faced error code " + str(err) + "while sending input data!")


func _get_joy_axis(device: int, axis: int) -> float:
var value := Input.get_joy_axis(device, axis)
if abs(value) < DEADZONE:
Expand Down Expand Up @@ -243,5 +245,5 @@ static func _concat_bytes(bytes_arr: Array) -> PoolByteArray:
return bytes


func get_runtime() -> int:
func _get_runtime() -> int:
return OS.get_system_time_secs() - start_time

0 comments on commit 06012d1

Please sign in to comment.