This is a code snippet from lib_robotis
and the value n
is the angle that we are inputting(to rotate the dimexel motor).
According to the architecture of the chip within the motor, we require to give the lower bit (identified by lo
) of the goal position into the address 0x1e and the higher bit (identified by hi
) of the goal position into the address 0x1f.
def move_to_encoder(self,n);
n=min(max(n,0),self.settings['max_encoder'])
hi,lo=n/256, n%256
return self.write_address(0x1e, [hi,lo])
n is a number in the range 0 <= n < 256*256. It takes two bytes to express that number since one byte can encode a value from 0 to 255.
hi,lo=n/256, n%256
is the same as
hi = n / 256
lo = n % 256
/
is the division operator and %
is the remainder operator, so the above code says to divide n
by 256 and store the quotient in hi
and the remainder in lo
. Each of hi
and lo
will be in the range 0 to 255, so they will each fit into one byte. And n == (256 * hi) + lo, so no information has been lost & the motor controller has all the information it needs to perform the desired operation.
I assume that the code in your post was written for Python 2; it won't work properly on Python 3. You should replace the n / 256
by n // 256
to make the code work on all versions of Python.
FWIW, there are various better ways to break a number up into high and low bytes. One uses the built-in divmod function:
hi, lo = divmod(n, 256)