Hello,
The following code in OpenMV will send the outputs to the Raspberry Pi over Bluetooth but the Raspberry Pi (Central Device) is only reading the last output. Is this a timing issue or an issue in the code? Any thing will help. The code used for the transmission is below.
def _encode_dist(dist):
return struct.pack("<h", int(dist))
def _decode_data(data):
try:
print(f"Raw data: {data}, Type: {type(data)}")
if data is None or not isinstance(data, bytes):
print("No valid data received or data is not bytes")
return None
decoded_data = struct.unpack("<h", data)[0]
print(f"Decoded data: {decoded_data}")
return decoded_data
except Exception as e:
print(f"Error decoding data: {e}")
return None
async def send_ack():
print("Sending acknowledgement")
ack_value = 1
ACK_characteristic.write(_encode_dist(ack_value))
await asyncio.sleep(0.1)
print("ACK sent")
elif case_select == 2:
print('Executing case 2: Scan for elevator buttons')
#measure distance away, send (x,y,z)')
detected_buttons = ScanForButtons()
z_coord = MeasureDistance()
print(f"z coord data:{z_coord}")
dist_characteristic.write(_encode_dist(z_coord))
if detected_buttons:
number_labels = len(detected_buttons)
print(f"number: {number_labels}")
numlabels_characteristic.write(_encode_dist(number_labels))
await asyncio.sleep(0.2)
await wait_for_ack()
await wait_for_ack()
for buttons in detected_buttons:
center_x, center_y, label, score = buttons
print(f"Center X: {center_x}, Center Y: {center_y}, Label: {label}, Score: {score}")
if isinstance(label, str):
if label == "down":
label = 100
elif label == "up":
label = 101
encoded_label = _encode_dist(label)
print(f"Sending Label: {label}, Encoded: {encoded_label.hex()}")
labels_characteristic.write(_encode_dist(label))
await wait_for_ack()
delta_x, delta_y = check_tight_centering(center_x, center_y)
print(f"delta_x: {delta_x}, delta_y: {delta_y}")
delta_x_characteristic.write(_encode_dist(delta_x))
await asyncio.sleep(0.2)
await wait_for_ack()
delta_y_characteristic.write(_encode_dist(delta_y))
await asyncio.sleep(0.2)
await wait_for_ack()
else:
print("No buttons detected.")