Motors module¶
- class motors.Motor(pin: int, addr: int, slot: int)[source]¶
Bases:
object
A class to represent a motor and offers an API to control it.
- Additionnal documentation can be found here:
- CMD_MOVE_AGL = 17¶
- CMD_MOVE_SPD = 5¶
- CMD_MOVE_SPD_TIME = 8¶
- CMD_RESET = 7¶
- END = 90¶
- HEADER = [165, 1]¶
- __init__(pin: int, addr: int, slot: int)[source]¶
Initialize I2C communication to motor.
- Parameters
pin – I2C bus’ pin (2 or 4)
addr – slave’s address
slot – motor’s slot (1 or 2)
- __recv_data(length: int) bytearray ¶
Receives data from I2C slave’s address
- Parameters
receive (length number of bytes to) –
- Returns
data received in bytes
- Return type
buffer
- async __send_data(data: list)¶
Creates a trame from the data and send it to motor via I2C.
- Parameters
data – [slot, CMD, args]: data to send
- static _lrc_calc(data) int [source]¶
Calculate the Longitudinal Redondancy Check (LRC)
- Returns
the value of LRC
- Return type
lrc
- static _to_bytes(fmt: str, data) list [source]¶
Convert and pack data with a given format
The list of available formats can be found here: https://docs.python.org/3/library/struct.html
- Parameters
fmt – string used to pack the data from a given format.
data – data to be converted to bytes.
- Returns
a list of each element of data converted to bytes.
- Return type
data_bytes
- async move(angle: float, speed: float) None [source]¶
Move motor of angle degrees at a speed given.
- Parameters
speed – rotation speed (RPM) in [-200, +200]
angle – angle in degrees to rotate.
- async run(speed: float, time=None) None [source]¶
Controls motor rotation with speed given for an optional time.
- Parameters
speed – rotation speed (RPM) in [-200, +200]
time – in seconds, runs for a specified time
- scan() list [source]¶
Scan slaves connected to the current I2C pin.
- Returns
addresses of slaves that respond
- Return type
list_of_slaves
- property speed¶