Wixel communication between computer and Pololu 3P+ 2040OLED

I follow the manual of the Wixel and configure two of those by using the newest wireless serial app. One connects to the computer and here is the computer python code.

import serial
import time
port = 'COM5' 
baud_rate = 9600

ser = serial.Serial(port, baud_rate)
def send_command(command):
    ser.write(command.encode('utf-8'))
    print(f'Sent: {command}')
try:
    while True:
        command = input("Enter command (F: Forward, B: Backward, L: Left, R: Right, S: Stop): ")
        if command in ['F', 'B', 'L', 'R', 'S']:
            send_command(command)
        else:
            print("Invalid command. Please enter F, B, L, R, or S.")
except KeyboardInterrupt:
    print("Exiting...")
finally:
    ser.close()

And the other one is connect to 3P+ 2040 OLED by connect rx tx on pin 29,28 and 3.3V and GND.
The code running on the 3P+ is

import time
from machine import Pin, UART
from pololu_3pi_2040_robot import robot
from pololu_3pi_2040_robot.extras import editions

bump_sensors = robot.BumpSensors()
motors = robot.Motors()
display = robot.Display()
edition = editions.select()
yellow_led = robot.YellowLED()
if edition == "Standard":
    max_speed = 1500
    turn_time = 250
elif edition == "Turtle":
    max_speed = 3000
    turn_time = 500
elif edition == "Hyper":
    max_speed = 1125
    turn_time = 150
    motors.flip_left(True)
    motors.flip_right(True)
uart = UART(0, baudrate=9600, tx=Pin(28), rx=Pin(29))

display.fill(0)
display.text("Ready", 80, 0)
display.show()
try:
    while True:
        if uart.any():
            command = uart.read(1).decode('utf-8')
            yellow_led.on()
            time.sleeps(0.5)
            yellow_led.off()
except UnicodeError:
    display.fill(0)
    display.text("ERROR", 80, 0)
    display.show()
except KeyboardInterrupt:
    print("Exiting...")

However, it always shows an error when I use the keyboard input which means always getting unicodeError. is there any solution?

Hello.

Nothing immediately stands out to me as problematic from glancing through your code, but you might try debugging it using something like c = uart.read(1) and then printing repr(c) and/or displaying it on the 3pi+ robot’s OLED so you can see what data it is receiving.

Brandon

Hello BrandonM
I tried to print on the screen about the msg received, but it directly showed UnicodeError and sound beeee. I tried ecnoder() and encoder(utf-8) both way.

Thank you
Zheiwei

It sounds like you are still trying to process the data before printing it to the display. For debugging purposes, you shouldn’t need to call encode or decode. You can just call repr to convert the bytes read from the UART into a string.

Brandon

Thank you. I will try it.

Hi, thank you for your help. When I send b’\x05\x06\x07\x08’ from computer. The 3pi+ would receive b’\xfc’ printed. Is that what you mean for debugging?

Zhewei

It sounds like it could be a baud rate mismatch issue (since \xFC would be 0001111111 on the UART line). Are you sure both Wixels and the 3pi+ are set to the same baud rate?

You might try sending just \x00 in one experiment, then \x01 in another one and comparing the results.

Additionally, it sounds like your program might only be printing the first byte it receives; you should probably make it print all the bytes received, just in case the there are a few bytes of noise or something that are causing problems.

Brandon

Thank you so much. I get it working now. I can receive the full byte, even the other standard byte I send from the computer. I can receive x01 and x00 when I send x01 and x00. Also, when I use an encoder and decoder, so the command would pass through. I just unplug and plug the tx rx line again into the robot. Maybe it would be the connecting problem.

Zhewei

1 Like