Skip to content

Commit

Permalink
Creat class fake_littlebot
Browse files Browse the repository at this point in the history
  • Loading branch information
NestorDP committed Dec 4, 2024
1 parent 0572c9c commit c2e25d6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 66 deletions.
96 changes: 51 additions & 45 deletions littlebot_base/scripts/littlebot_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,70 @@
import serial
import sys
import threading

import littlebot_protocol_pb2 as LittlebotProtocol

# Define global start and end characters
START_CHARACTER = b'<'
END_CHARACTER = b'>'
class LittlebotFake:
START_CHARACTER = b'<'
END_CHARACTER = b'>'

def serial_configuration(serial_port):
serial_port = sys.argv[1]
def __init__(self, serial_port):
self.ser = self.serial_configuration(serial_port)
self.littlebot_msg = LittlebotProtocol.LittlebotProtocol()

# Open serial port
return serial.Serial(serial_port, baudrate=115200) # open serial port
def serial_configuration(self, serial_port):
# Open serial port
return serial.Serial(serial_port, baudrate=115200) # open serial port

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
def send_status(self):
self.littlebot_msg.left_status_vel = self.left_command_vel
self.littlebot_msg.left_status_pos = 10

encoded_data = START_CHARACTER + msg.SerializeToString() + END_CHARACTER
ser.write(encoded_data)
self.littlebot_msg.right_status_vel = self.right_command_vel
self.littlebot_msg.right_status_pos = 10

threading.Timer(1, send_message, args=(ser, msg)).start()
encoded_data = self.START_CHARACTER + self.self.littlebot_msg.SerializeToString() + self.END_CHARACTER
self.ser.write(encoded_data)

def receive_message_callback(encoded_data_received):
print(f"Encoded data received: {encoded_data_received}")
threading.Timer(1, self.send_status).start()

def receive_command_callback(self, encoded_data_received):
try:
self.littlebot_msg.ParseFromString(encoded_data_received[:-1])
except Exception as e:
print(f"Failed to parse the message: {e}")

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)
self.left_command_vel = self.littlebot_msg.left_command_vel
self.right_command_vel = self.littlebot_msg.right_command_vel

def main():

def read_from_serial(self):
while True:
data = self.ser.read()
if data == self.START_CHARACTER:
encoded_data_received = self.ser.read_until(self.END_CHARACTER)
self.receive_command_callback(encoded_data_received)

def start(self):
# Send a message periodicaly
self.send_status()

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

# Keep the main thread alive
try:
while True:
pass
except KeyboardInterrupt:
self.ser.close()

if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python littlebot_fake.py <serial_port>")
sys.exit(1)

serial_port = sys.argv[1]
ser = serial_configuration(serial_port)

littlebot_msg_protocol = LittlebotProtocol.LittlebotProtocol()

# 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()

# Keep the main thread alive
try:
while True:
pass
except KeyboardInterrupt:
ser.close()

if __name__ == "__main__":
main()
littlebot_fake = LittlebotFake(serial_port)
littlebot_fake.start()
8 changes: 2 additions & 6 deletions littlebot_base/scripts/littlebot_protocol_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 7 additions & 15 deletions littlebot_base/src/littlebot_protocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ syntax = "proto3";
// in the Protocol Buffers namespace and non-Python languages
package protoblog;

// Style guide prefers prefixing enum values instead of surrounding
// with an enclosing message
enum MotorSide {
LEFT = 0;
RIGHT = 1;
}

// Message definition for the LittlebotProtocol
message LittlebotProtocol {
message MotorInterface {
bool side = 1;
float status_velocity = 2;
float status_position = 3;
float command_velocity = 4;
float left_status_vel = 1;
float left_status_pos = 2;
float left_command_vel = 3;
float right_status_vel = 4;
float right_status_pos = 5;
float right_command_vel = 6;
}

repeated MotorInterface motor_interface = 3;
}

0 comments on commit c2e25d6

Please sign in to comment.