Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help
#1
Hi all, is there a way to make this code transmit 1 byte without affecting the output?
I am grateful and appreciative anyone could help out.

def encode(sign, x, y):
    assert 0 <= x <= 2
    assert 0 <= y <= 9
    assert sign in (0, 1)
    return 64 * sign + 16 * x + y
 
def decode(n):
    m, y = divmod(n, 16)
    sign, x = divmod(m, 4)
    return sign, x, y
 
if __name__ == '__main__':
    from itertools import product
    for s, x, y in product(range(2), range(3), range(10)):
        n = encode(s, x, y)
        print(s, x, y, bin(n), decode(n))
Output:
0 0 0 0b0 (0, 0, 0) 0 0 1 0b1 (0, 0, 1) 0 0 2 0b10 (0, 0, 2) 0 0 3 0b11 (0, 0, 3) 0 0 4 0b100 (0, 0, 4) 0 0 5 0b101 (0, 0, 5) 0 0 6 0b110 (0, 0, 6) 0 0 7 0b111 (0, 0, 7) 0 0 8 0b1000 (0, 0, 8) 0 0 9 0b1001 (0, 0, 9) 0 1 0 0b10000 (0, 1, 0) 0 1 1 0b10001 (0, 1, 1) 0 1 2 0b10010 (0, 1, 2) 0 1 3 0b10011 (0, 1, 3) 0 1 4 0b10100 (0, 1, 4) 0 1 5 0b10101 (0, 1, 5) 0 1 6 0b10110 (0, 1, 6) 0 1 7 0b10111 (0, 1, 7) 0 1 8 0b11000 (0, 1, 8) 0 1 9 0b11001 (0, 1, 9) 0 2 0 0b100000 (0, 2, 0) 0 2 1 0b100001 (0, 2, 1) 0 2 2 0b100010 (0, 2, 2) 0 2 3 0b100011 (0, 2, 3) 0 2 4 0b100100 (0, 2, 4) 0 2 5 0b100101 (0, 2, 5) 0 2 6 0b100110 (0, 2, 6) 0 2 7 0b100111 (0, 2, 7) 0 2 8 0b101000 (0, 2, 8) 0 2 9 0b101001 (0, 2, 9) 1 0 0 0b1000000 (1, 0, 0) 1 0 1 0b1000001 (1, 0, 1) 1 0 2 0b1000010 (1, 0, 2) 1 0 3 0b1000011 (1, 0, 3) 1 0 4 0b1000100 (1, 0, 4) 1 0 5 0b1000101 (1, 0, 5) 1 0 6 0b1000110 (1, 0, 6) 1 0 7 0b1000111 (1, 0, 7) 1 0 8 0b1001000 (1, 0, 8) 1 0 9 0b1001001 (1, 0, 9) 1 1 0 0b1010000 (1, 1, 0) 1 1 1 0b1010001 (1, 1, 1) 1 1 2 0b1010010 (1, 1, 2) 1 1 3 0b1010011 (1, 1, 3) 1 1 4 0b1010100 (1, 1, 4) 1 1 5 0b1010101 (1, 1, 5) 1 1 6 0b1010110 (1, 1, 6) 1 1 7 0b1010111 (1, 1, 7) 1 1 8 0b1011000 (1, 1, 8) 1 1 9 0b1011001 (1, 1, 9) 1 2 0 0b1100000 (1, 2, 0) 1 2 1 0b1100001 (1, 2, 1) 1 2 2 0b1100010 (1, 2, 2) 1 2 3 0b1100011 (1, 2, 3) 1 2 4 0b1100100 (1, 2, 4) 1 2 5 0b1100101 (1, 2, 5) 1 2 6 0b1100110 (1, 2, 6) 1 2 7 0b1100111 (1, 2, 7) 1 2 8 0b1101000 (1, 2, 8) 1 2 9 0b1101001 (1, 2, 9)
Reply
#2
Not sure what you're looking for. Where would the byte be transmitted? To stdout with the other print statements or elsewhere? What would the contents be?
Reply
#3
(Dec-15-2020, 02:25 AM)bowlofred Wrote: Not sure what you're looking for. Where would the byte be transmitted? To stdout with the other print statements or elsewhere? What would the contents be?
Hi, so I am sending cmd_vel which consists of linear and angular. It is from a base station to the robot. I have calculated the number of bits that I will be sending. They are both 7 bits. (X(3 bits). X (4bits)) (The first X values are from 0 to 2 (2 bits) and I used 1 bit as a sign bit to represent +/-)(For the second X value, it is from 0-9 (4 bits)). So in total, that would be 14 bits, so I have to use 1 byte for each. I have let my supervisor checked the code, he said that I am almost done with the code just that I need to make sure that I transmit this code as 1 byte in respect to line 5. So sorry that it was unclear.
Reply
#4
You still didn't say where the byte needs to be delivered. I'm assuming you have a device file that you can open() to deliver the data.

If you open a file in binary mode, you can write the byte directly.

f = open("/my/robot", "wb")
n = encode(s, x y)
f.write(n)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020