Change how different sensor types ar handled
This commit is contained in:
@@ -5,7 +5,7 @@ extends Node
|
||||
var udp = PacketPeerUDP.new()
|
||||
var connected = false
|
||||
|
||||
var fast_broadcast_rate = 1.0
|
||||
var fast_broadcast_rate = 0.5
|
||||
var slow_broadcast_rate = 10.0
|
||||
var current_broadcast_rate = fast_broadcast_rate
|
||||
var last_broadcast = 0
|
||||
@@ -16,6 +16,7 @@ var thread: Thread
|
||||
var mutex: Mutex
|
||||
var signal_queue = []
|
||||
var exit_thread = false
|
||||
var use_thread = true
|
||||
|
||||
func _ready():
|
||||
_try_set_ip()
|
||||
@@ -23,44 +24,54 @@ func _ready():
|
||||
udp.set_dest_address("192.168.0.255", 5000)
|
||||
SensorSignals.new_sensor_connected.connect(_on_sensor_connected)
|
||||
|
||||
thread = Thread.new()
|
||||
use_thread = OS.get_processor_count() > 1
|
||||
mutex = Mutex.new()
|
||||
|
||||
thread.start(_thread_function)
|
||||
if use_thread:
|
||||
thread = Thread.new()
|
||||
print('Starting UDP Broadcast thread')
|
||||
thread.start(_thread_function)
|
||||
else:
|
||||
print('UDP server running in single threaded mode')
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
mutex.lock()
|
||||
last_broadcast += delta
|
||||
mutex.unlock()
|
||||
if not use_thread:
|
||||
_process_broadcast()
|
||||
|
||||
func _exit_tree() -> void:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
if use_thread:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
|
||||
func _thread_function():
|
||||
print('UDP Broadcast thread started')
|
||||
while true:
|
||||
mutex.lock()
|
||||
var should_exit = exit_thread
|
||||
mutex.unlock()
|
||||
if should_exit:
|
||||
break
|
||||
|
||||
if ip_address.length() == 0:
|
||||
_try_set_ip()
|
||||
return
|
||||
mutex.lock()
|
||||
var do_broadcast = last_broadcast >= current_broadcast_rate
|
||||
mutex.unlock()
|
||||
if do_broadcast:
|
||||
# Try to contact server
|
||||
mutex.lock()
|
||||
last_broadcast = 0
|
||||
mutex.unlock()
|
||||
var broadcast_str = ip_address + "|%s" % Time.get_datetime_string_from_system(true)
|
||||
print('Broadcasting to UDP: ', broadcast_str)
|
||||
udp.put_packet(broadcast_str.to_utf8_buffer())
|
||||
_process_broadcast()
|
||||
|
||||
func _process_broadcast():
|
||||
if ip_address.length() == 0:
|
||||
_try_set_ip()
|
||||
return
|
||||
# Try to contact server
|
||||
var broadcast_str = ip_address + "|%s" % Time.get_datetime_string_from_system(true)
|
||||
print('Broadcasting to UDP: ', broadcast_str)
|
||||
udp.put_packet(broadcast_str.to_utf8_buffer())
|
||||
|
||||
func _try_set_ip():
|
||||
for address in IP.get_local_addresses():
|
||||
|
||||
@@ -12,9 +12,10 @@ var awaiting_sensors = {}
|
||||
var invalid_sensors = {}
|
||||
var valid_sensors = {}
|
||||
|
||||
var use_thread: bool = false
|
||||
var thread: Thread
|
||||
var mutex: Mutex
|
||||
var exit_thread = false
|
||||
var exit_thread = true
|
||||
|
||||
var signal_queue = []
|
||||
|
||||
@@ -27,10 +28,16 @@ func _ready():
|
||||
print('Listening on port: ', port)
|
||||
|
||||
mutex = Mutex.new()
|
||||
thread = Thread.new()
|
||||
thread.start(_thread_function)
|
||||
if use_thread:
|
||||
thread = Thread.new()
|
||||
print('Starting UDP Server thread')
|
||||
thread.start(_thread_function)
|
||||
else:
|
||||
print('UDP server running in single threaded mode')
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if not use_thread:
|
||||
_process_connections()
|
||||
while signal_queue.size() > 0:
|
||||
mutex.lock()
|
||||
var signal_data = signal_queue.pop_front()
|
||||
@@ -51,10 +58,11 @@ func _process(delta: float) -> void:
|
||||
DataSignals.magnetometer_data_received.emit(signal_data[1], signal_data[2], signal_data[3])
|
||||
|
||||
func _exit_tree() -> void:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
if use_thread:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
|
||||
func _on_sensor_approved(sensor_ip: String):
|
||||
mutex.lock()
|
||||
@@ -72,6 +80,7 @@ func _on_sensor_declined(sensor_ip: String):
|
||||
mutex.unlock()
|
||||
|
||||
func _thread_function():
|
||||
print('UDP Server thread started')
|
||||
while true:
|
||||
mutex.lock()
|
||||
var should_exit = exit_thread
|
||||
@@ -177,15 +186,13 @@ func _handle_new_peer_request(peer_ip: String, packet: PackedByteArray):
|
||||
10 | 1 byte | bool | peer provides quaternion orientation
|
||||
"""
|
||||
mutex.lock()
|
||||
awaiting_sensors[peer_ip] = [
|
||||
#packet.decode_u8(4),
|
||||
packet.decode_u8(5),
|
||||
packet.decode_u8(6),
|
||||
packet.decode_u8(7),
|
||||
packet.decode_u8(8),
|
||||
#packet.decode_u8(9),
|
||||
#packet.decode_u8(10),
|
||||
]
|
||||
print(packet)
|
||||
print(packet.get(4))
|
||||
print(0xf1)
|
||||
if packet.get(4) == 0xf1:
|
||||
awaiting_sensors[peer_ip] = "VEHICLE"
|
||||
elif packet.get(4) == 0x23:
|
||||
awaiting_sensors[peer_ip] = "LIDAR_2D"
|
||||
signal_queue.append(["sensor_request_received", peer_ip, awaiting_sensors[peer_ip]])
|
||||
mutex.unlock()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user