I had lost all interest in things of late but after sharing my last part on the Battery Powered Brick Discord and generating others, I now have more interest and Hackster user Copernicon has joined me allowing me to bounce ideas.
While I'm doing this on an iMac, Copernicon is running his on linux so we both can share and compare results.
Stripping back the test code due to bugs.import serial
import time
ser = serial.Serial(
port='/dev/cu.PL2303G-USBtoUART2430',
baudrate=9600,
#parity=serial.PARITY_NONE,
#stopbits=serial.STOPBITS_ONE,
#bytesize=serial.SEVENBITS,
)
ser.write(b'p\0###Do you byte, when I knock?$$$')
#ser.read(16)
print(ser.read(31))
#ser.flushInput()
#ser.flushOutput()
while True:
ser.write(b'2')
#time.sleep(1)
#ser.read(16)
print(ser.read(19)) # Must always be 19 bytes
#print(ser.read(2))
#print(ser.readline())
#ser.flushInput()
#ser.close()
The code shown above proved to be very buggy hence the need to "Comment out" most of the lines. After stripping out the buggy bits, the code ended up like the following working on both our systems.
import serial
import time
ser = serial.Serial(
port='/dev/cu.PL2303G-USBtoUART2430',
baudrate=9600,
)
ser.write(b'p\0###Do you byte, when I knock?$$$')
print(ser.read(31))
while True:
ser.write(b'2')
print(ser.read(19)) # Must always be 19 bytes
One thing I did find was that even though I plugged the USB adapter into the same port and last time, the com port changed number.
Input PortsThere are 4 passive input ports and 4 active input ports on the left of the control centre. The yellow ports are Passive sensor ports and the easiest to use as they just read across the two pins where as the blue Active ports supply power and read from the same two pins.
The 4 yellow ports are passive ports and return the two byte value of \xff\xc8 which has a decimal value of 114080 and the active ports have the 2 byte value of \xff\xcc which gives a decimal value of -13057.
A point of note about these values. These values are dependent upon the input supply voltage. I didn't have a mains 9>12V adapter and was using a 12V lipo RC battery pack. As the voltage of this pack dropped so did the voltage in the interface resulting in changes in the readings being received.
The port order in the data stream is a bit mixed. The stream doesn't got Port 1 to Port 8 or port 8 to port 1 instead the ports are mixed as follows.
Port4,Port8,Port3,Port7,Port2,port6,port1,Port5,
This "mix" is why we see xff\xcc and xff\xc8 mixed together.
\x00\x00\xff\xc8\xff\xcc\xff\xc8\xff\xcc\xff\xc8\xff\xcc\xff\xc8\xff\xcc\xb7
After a lot of Help from Darius of the Micropython Discord group I ended up with the following working code:
import serial
import time
from struct import *
import functools
ser = serial.Serial(
port='/dev/cu.PL2303G-USBtoUART2430',
baudrate=9600,
)
ser.write(b'p\0###Do you byte, when I knock?$$$')
print(ser.read(31))
print("Lets looks at the input ports")
while True:
ser.write(b'2')
dat = ser.read(19)
# cksum = functools.reduce(lambda x, y: x + y, dat)
# if cksum != 0xff:
# print('Checksum error got', cksum, 'expected 0xff')
_, p4, p8, p3, p7, p2, p6, p1, p5 = unpack('<hhhhhhhhh', dat[:-1])
# print(p1, p2, p3, p4, p5, p6, p7, p8)
print(f'P1: {p1}, P2: {p2}, P3: {p3}, P4: {p4}, P5: {p5}, P6: {p6}, P7: {p7}, P8: {p8}')
The code produces the following output in a terminal.
The Commented code is for the checksum value which will come into use later.
No we have are value connected to ports we can see they are all in the negative values. As there is noting connected to each port we need to use an offset value to make the results zero. We get the offset values from the results of the last code and just use the 14081 and 13057 values for the offsets.
To set the offset for each port use the following code:
pt1 = p1 + 14081
print(f'Port1: {pt1}')
I manually set the off set for the four passive ports with the following code:
import serial
import time
from struct import *
import functools
ser = serial.Serial(
port='/dev/cu.PL2303G-USBtoUART2430',
baudrate=9600,
)
ser.write(b'p\0###Do you byte, when I knock?$$$') #initiates communication with the Control center.
print(ser.read(31)) # Check to see if the Interface has responded. Stop LED should go off.
print("Lets looks at the input ports")
# the First two bytes should always be x00\x00. If the first two bytes are b'\x10\x00 then the stop button has been hit.
#Port 1 > 4 ofset value = 14081 when output voltage = 9vish
#Port 5 > 8 ofset value = 13057 when output voltage = 9vish
time.sleep(2)
while True:
ser.write(b'2')
dat = ser.read(19)
# cksum = functools.reduce(lambda x, y: x + y, dat)
# if cksum != 0xff:
# print('Checksum error got', cksum, 'expected 0xff')
_, p4, p8, p3, p7, p2, p6, p1, p5 = unpack('<hhhhhhhhh', dat[:-1])
# print(p1, p2, p3, p4, p5, p6, p7, p8)
# print(f'Port1: {p1}, Port2: {p2}, Port3: {p3}, Port4: {p4}, Port5: {p5}, Port6: {p6}, Port7: {p7}, Port8: {p8}')
# print(f'Port5: {p5}')
pt1 = p1 + 14081
pt2 = p2 + 14081
pt3 = p3 + 14081
pt4 = p4 + 14081
print(f'Port1: {pt1}')
print(f'Port2: {pt2}')
print(f'Port3: {pt3}')
print(f'Port4: {pt4}')
And now we get a readable output!
Well that's all for now as my brain is brained out. please bare with me until part 3 commit soon.
References.I wouldn't be here if it wasn't for the people who went through all this effort before me.
Special thanks go out to the Eurobrick community found here:
https://www.eurobricks.com/forum/index.php?/forums/topic/67665-dacta-control-lab-software/&page=12
The messages found here: http://www.blockcad.net/dacta/
Battery Powered brick Youtube channel here: https://www.youtube.com/@BatteryPoweredBricks
Battery Powered Bricks Discord.https://discord.gg/CGZRwhTj
And the LGauge site found here: https://lgauge.com/article.php?article=technic/articles/LEGOInterfaceB
The Unofficial Guide to Mindstorms robotics by Jonathan B. Knudsen.https://amzn.to/4mtWFai
Big thanks to Darius from the Micropython Discord channel for your patient help.
Contact Me.Due to Issues with code and researching functions, this part has taken a lot longer than the other parts. If you find this useful then there is more crossover guides coming. You can drop a Message in Hackster.io message box below of find me via the following links:
https://twitter.com/Cpt_J_Purcell
https://bsky.app/profile/jamespurcell.bsky.social
On Discord (if I ever work out how to share the profile!)https://mastodonapp.uk/@AdamBryant
And on the M5Stack Facebook group and community forum.
If you have some spare change you can now buy me a tea @ https://bmc.link/ajb2k35
Comments