Skip to content

Commit

Permalink
Created man function
Browse files Browse the repository at this point in the history
  • Loading branch information
NestorDP committed Dec 4, 2024
1 parent b1a7272 commit 0572c9c
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions littlebot_base/scripts/littlebot_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,67 @@
@author: nestor
"""

import base64
import serial
import sys
import threading

import littlebot_protocol_pb2 as LittlebotProtocol

serial_port = sys.argv[1]
# Define global start and end characters
START_CHARACTER = b'<'
END_CHARACTER = b'>'

ser = serial.Serial(serial_port, baudrate=115200) # open serial port
def serial_configuration(serial_port):
serial_port = sys.argv[1]

start_character = b'<'
end_character = b'>'
# Open serial port
return serial.Serial(serial_port, baudrate=115200) # open serial port

# Create a new TodoList
littlebot_msg_protocol = LittlebotProtocol.LittlebotProtocol()
def send_message(ser, msg):
left_motor = msg.motor_interface.add(side=LittlebotProtocol.MotorSide.Value("LEFT"))
left_motor.status_velocity = 10
left_motor.status_position = 20

left_motor = littlebot_msg_protocol.motor_interface.add()
left_motor.side = LittlebotProtocol.MotorSide.Value("LEFT")
left_motor.status_velocity = 10
left_motor.status_position = 20
left_motor.command_velocity = 30
encoded_data = START_CHARACTER + msg.SerializeToString() + END_CHARACTER
ser.write(encoded_data)

right_motor = littlebot_msg_protocol.motor_interface.add()
right_motor.side = LittlebotProtocol.MotorSide.Value("RIGHT")
right_motor.status_velocity = 40
right_motor.status_position = 50
right_motor.command_velocity = 60
threading.Timer(1, send_message, args=(ser, msg)).start()

print(littlebot_msg_protocol)
def receive_message_callback(encoded_data_received):
print(f"Encoded data received: {encoded_data_received}")

# Serialize the TodoList and send it over the serial port
encoded_data = start_character + littlebot_msg_protocol.SerializeToString() + end_character
ser.write(encoded_data)
print(f"Encoded data sent: {encoded_data}\n")

ser.flush()
def read_from_serial(ser, callback):
while True:
data = ser.read()
if data == START_CHARACTER:
encoded_data_received = ser.read_until(END_CHARACTER)
callback(encoded_data_received)

littlebot_msg_protocol2 = LittlebotProtocol.LittlebotProtocol()
def main():
if len(sys.argv) < 2:
print("Usage: python littlebot_fake.py <serial_port>")
sys.exit(1)

# Read the encoded data from the serial port
data = ser.read()
if data == start_character:
encoded_data_received = ser.read_until(end_character)
serial_port = sys.argv[1]
ser = serial_configuration(serial_port)

# Debugging print
print(f"Encoded data received: {encoded_data_received}")
littlebot_msg_protocol = LittlebotProtocol.LittlebotProtocol()

# Parse the decoded data into a TodoList
try:
littlebot_msg_protocol2.ParseFromString(encoded_data_received[:-1])
print(littlebot_msg_protocol2)
except Exception as e:
print(f"Failed to parse the message: {e}")
# Send a message
send_message(ser, littlebot_msg_protocol)

# Start a thread to read from the serial port and call the callback function
read_thread = threading.Thread(target=read_from_serial, args=(ser, receive_message_callback))
read_thread.daemon = True
read_thread.start()

if left_motor.side == False:
print("Right motor side is LEFT")
# Keep the main thread alive
try:
while True:
pass
except KeyboardInterrupt:
ser.close()

ser.close()
if __name__ == "__main__":
main()

0 comments on commit 0572c9c

Please sign in to comment.