module motor

class Motors

Example

Initilize

# Import Motor class
from robot_hat import Motors

# Create Motor object
motors = Motors()

Directly control a motor. Motor 1/2 is according to PCB mark

# Motor 1 clockwise at 100% speed
motors[1].speed(100)
# Motor 2 counter-clockwise at 100% speed
motors[2].speed(-100)
# Stop all motors
motors.stop()

Setup for high level control, high level control provides functions from simple forword, backward, left, right, stop to more complex like joystick control, motor directions calibration, etc.

Note

All these setup only need to run once, and will save in a config file. Next time you load Motors class, it will load from config file.

# Setup left and right motors
motors.set_left_id(1)
motors.set_right_id(2)
# Go forward and see if both motor directions are correct
motors.forward(100)
# if you found a motor is running in the wrong direction
# Use these function to correct it
motors.set_left_reverse()
motors.set_right_reverse()
# Run forward again and see if both motor directions are correct
motors.forward(100)

Now control the robot

import time

motors.forward(100)
time.sleep(1)
motors.backward(100)
time.sleep(1)
motors.turn_left(100)
time.sleep(1)
motors.turn_right(100)
time.sleep(1)
motors.stop()

API

class robot_hat.Motors(db='/root/.config/robot-hat/robot-hat.conf', *args, **kwargs)

Bases: _Basic_class

__init__(db='/root/.config/robot-hat/robot-hat.conf', *args, **kwargs)

Initialize motors with robot_hat.motor.Motor

Parameters

db (str) – config file path

__getitem__(key)

Get specific motor

stop()

Stop all motors

property left

left motor

property right

right motor

set_left_id(id)

Set left motor id, this function only need to run once It will save the motor id to config file, and load the motor id when the class is initialized

Parameters

id (int) – motor id (1 or 2)

set_right_id(id)

Set right motor id, this function only need to run once It will save the motor id to config file, and load the motor id when the class is initialized

Parameters

id (int) – motor id (1 or 2)

set_left_reverse()

Set left motor reverse, this function only need to run once It will save the reversed status to config file, and load the reversed status when the class is initialized

Returns

if currently is reversed

Return type

bool

set_right_reverse()

Set right motor reverse, this function only need to run once It will save the reversed status to config file, and load the reversed status when the class is initialized

Returns

if currently is reversed

Return type

bool

speed(left_speed, right_speed)

Set motor speed

Parameters
  • left_speed (float) – left motor speed(-100.0~100.0)

  • right_speed (float) – right motor speed(-100.0~100.0)

forward(speed)

Forward

Parameters

speed (float) – Motor speed(-100.0~100.0)

backward(speed)

Backward

Parameters

speed (float) – Motor speed(-100.0~100.0)

turn_left(speed)

Left turn

Parameters

speed (float) – Motor speed(-100.0~100.0)

turn_right(speed)

Right turn

Parameters

speed (float) – Motor speed(-100.0~100.0)

class Motor

Example

# Import Motor class
from robot_hat import Motor, PWM, Pin

# Create Motor object
motor = Motor(PWM("P13"), Pin("D4"))

# Motor clockwise at 100% speed
motor.speed(100)
# Motor counter-clockwise at 100% speed
motor.speed(-100)

# If you like to reverse the motor direction
motor.set_is_reverse(True)

API

class robot_hat.Motor(pwm, dir, is_reversed=False)

Bases: object

__init__(pwm, dir, is_reversed=False)

Initialize a motor

Parameters
speed(speed=None)

Get or set motor speed

Parameters

speed (float) – Motor speed(-100.0~100.0)

set_is_reverse(is_reverse)

Set motor is reversed or not

Parameters

is_reverse (bool) – True or False