Skip to content

Commit

Permalink
Moved Python example
Browse files Browse the repository at this point in the history
  • Loading branch information
gabryelreyes committed Nov 19, 2023
1 parent 289bdf5 commit 6b279bf
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
17 changes: 17 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example

The following example is written for testing the SerialMuxProt both with Python and CPP on a microcontroller.

## Installation

### CPP

- Using PlatformIO and the Arduino Framework, compile the files in the `examples\cpp` folder.
- Flash them onto an ESP32 board of your choice and leave it connected to your computer.
- Note the COM port the board is connected to, as you will need this in the next step.

### Python

- Install the requirements of the Python Script using the `pip install -r requirements.txt` command while inside the `examples\python` folder.
- Write the correct COM port number on the constructor of the SerialClient in `__main__.py`.
- Run `__main__.py` and follow the instructions on the terminal.
File renamed without changes.
66 changes: 43 additions & 23 deletions python/__main__.py → examples/python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
import sys
import time
from struct import Struct
import keyboard # pylint: disable=import-error
import keyboard # pylint: disable=import-error
import numpy as np
from SerialMuxProt import SerialMuxProt
from socket_client import SocketClient
# from socket_client import SocketClient
from serial_client import SerialClient

################################################################################
# Variables
################################################################################

g_socket = SocketClient("localhost", 65432)
g_socket = SerialClient("COM3", 115200)
smp_server = SerialMuxProt(10, g_socket)
START_TIME = round(time.time()*1000)

Expand Down Expand Up @@ -70,7 +71,18 @@ def callback_line_sensors(payload: bytearray) -> None:
data = unpacker.unpack_from(payload)
print(np.array(data, dtype=np.int16))

def callback_remote_response(payload:bytearray) -> None:

def callback_timestamp(payload: bytearray) -> None:
""" Callback of TIMESTAMP Channel """

print(payload.hex())

# unpacker = Struct(">HH")
# data = unpacker.unpack_from(payload)
# print(np.array(data, dtype=np.int16))


def callback_remote_response(payload: bytearray) -> None:
""" Callback of REMOTE_CMD Channel """
if payload == b'\x00':
print("Command OK")
Expand All @@ -79,18 +91,20 @@ def callback_remote_response(payload:bytearray) -> None:
elif payload == b'\x02':
print("Command Error")


def send_motor_setpoints(set_point_left: int, set_point_right: int):
"""
Send Motor Setpoints
"""

packer = Struct(">H")
payload = bytearray()
payload.extend(packer.pack(set_point_left))
payload.extend(packer.pack(set_point_right))
payload.append(0x01)
payload.append(0x00)
payload.append(0x00)
payload.append(0x00)

if len(payload) == 4:
smp_server.send_data("MOT_SPEEDS", payload)
smp_server.send_data("COUNTER", payload)


def send_command(command: str) -> None:
"""Send command to RadonUlzer"""
Expand Down Expand Up @@ -121,22 +135,28 @@ def main():
print(err)
return

smp_server.create_channel("MOT_SPEEDS", 4)
smp_server.create_channel("REMOTE_CMD", 1)
smp_server.subscribe_to_channel("REMOTE_RSP", callback_remote_response)
smp_server.subscribe_to_channel("LINE_SENS", callback_line_sensors)
# smp_server.create_channel("MOT_SPEEDS", 4)
# smp_server.create_channel("REMOTE_CMD", 1)
# smp_server.subscribe_to_channel("REMOTE_RSP", callback_remote_response)
# smp_server.subscribe_to_channel("LINE_SENS", callback_line_sensors)
smp_server.subscribe_to_channel("TIMESTAMP", callback_timestamp)
smp_server.create_channel("COUNTER", 4)

keyboard.on_press_key("w", lambda e: send_motor_setpoints(0x8000, 0x8000))
keyboard.on_press_key("s", lambda e: send_motor_setpoints(0x7FFF, 0x7FFF))
keyboard.on_press_key("a", lambda e: send_motor_setpoints(0x7FFF, 0x8000))
keyboard.on_press_key("d", lambda e: send_motor_setpoints(0x8000, 0x7FFF))
keyboard.on_release_key("w", lambda e: send_motor_setpoints(0x0000, 0x0000))
keyboard.on_release_key("a", lambda e: send_motor_setpoints(0x0000, 0x0000))
keyboard.on_release_key("s", lambda e: send_motor_setpoints(0x0000, 0x0000))
keyboard.on_release_key("d", lambda e: send_motor_setpoints(0x0000, 0x0000))
keyboard.on_press_key("l", lambda e: send_command("line_calib"))
keyboard.on_press_key("m", lambda e: send_command("motor_calib"))
keyboard.on_press_key("e", lambda e: send_command("enable_drive"))
# keyboard.on_press_key("s", lambda e: send_motor_setpoints(0x7FFF, 0x7FFF))
# keyboard.on_press_key("a", lambda e: send_motor_setpoints(0x7FFF, 0x8000))
# keyboard.on_press_key("d", lambda e: send_motor_setpoints(0x8000, 0x7FFF))
# keyboard.on_release_key(
# "w", lambda e: send_motor_setpoints(0x0000, 0x0000))
# keyboard.on_release_key(
# "a", lambda e: send_motor_setpoints(0x0000, 0x0000))
# keyboard.on_release_key(
# "s", lambda e: send_motor_setpoints(0x0000, 0x0000))
# keyboard.on_release_key(
# "d", lambda e: send_motor_setpoints(0x0000, 0x0000))
# keyboard.on_press_key("l", lambda e: send_command("line_calib"))
# keyboard.on_press_key("m", lambda e: send_command("motor_calib"))
# keyboard.on_press_key("e", lambda e: send_command("enable_drive"))

while True:
if (millis() - last_time) >= 5:
Expand Down
1 change: 1 addition & 0 deletions examples/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyserial == 3.5
File renamed without changes.
2 changes: 0 additions & 2 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
keyboard==0.13.5
numpy==1.26.0

0 comments on commit 6b279bf

Please sign in to comment.