spikedev.motor

class spikedev.motor.MotorSpeed

Bases: object

A base class for MotorSpeed classes. Do not use this directly. Use one of:

class spikedev.motor.MotorSpeedPercent(percent)

Bases: spikedev.motor.MotorSpeed

Motor speed as a percentage of the motor’s maximum rated speed

Parameters

percent (int) – the speed percentage to store

Example:

import hub
from spikedev.motor import MotorSpeedPercent, SpikeMediumMotor

# run for 720 degrees at 40% of motor's maximum speed
mtr = SpikeMediumMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedPercent(40))
to_native_units(motor)

The native unit for a Spike motor is speed percentage so there is nothing to adjust here

Parameters

motor (Motor) – the motor to use for calculating the speed percentage

Returns

the speed percentage

Return type

int

class spikedev.motor.MotorSpeedRPS(rotations_per_second)

Bases: spikedev.motor.MotorSpeed

Motor speed in rotations-per-second

Parameters

rotations_per_second (int) – the rotations-per-second to store

Example:

import hub
from spikedev.motor import MotorSpeedRPS, SpikeMediumMotor

# run for 720 degrees at 1.5 rotations-per-second
mtr = SpikeMediumMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedRPS(1.5))
to_native_units(motor)

Return the speed percentage required to achieve the desired rotations-per-second

Parameters

motor (Motor) – the motor to use for calculating the speed percentage

Returns

the speed percentage required to achieve the desired rotations-per-second

Return type

int

class spikedev.motor.MotorSpeedRPM(rotations_per_minute)

Bases: spikedev.motor.MotorSpeed

Motor speed in rotations-per-minute

Parameters

rotations_per_minute (int) – the rotations-per-minute to store

Example:

import hub
from spikedev.motor import MotorSpeedRPM, SpikeMediumMotor

# run for 720 degrees at 20 rotations-per-minute
mtr = SpikeMediumMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedRPM(20))
to_native_units(motor)

Return the speed percentage required to achieve the desired rotations-per-minute

Parameters

motor (Motor) – the motor to use for calculating the speed percentage

Returns

the speed percentage required to achieve the desired rotations-per-minute

Return type

int

class spikedev.motor.MotorSpeedDPS(degrees_per_second)

Bases: spikedev.motor.MotorSpeed

Motor speed in degrees-per-second

Parameters

degrees_per_second (int) – the degrees-per-second to store

Example:

import hub
from spikedev.motor import MotorSpeedDPS, SpikeMediumMotor

# run for 720 degrees at 180 degrees-per-second
mtr = SpikeMediumMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedDPS(180))
to_native_units(motor)

Return the speed percentage required to achieve the desired degrees-per-second

Parameters

motor (Motor) – the motor to use for calculating the speed percentage

Returns

the speed percentage required to achieve the desired degrees-per-second

Return type

int

class spikedev.motor.MotorSpeedDPM(degrees_per_minute)

Bases: spikedev.motor.MotorSpeed

Motor speed in degrees-per-minute

Parameters

degrees_per_minute (int) – the degrees-per-minute to store

Example:

import hub
from spikedev.motor import MotorSpeedDPM, SpikeMediumMotor

# run for 720 degrees at 10000 degrees-per-minute
mtr = SpikeMediumMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedDPM(10000))
to_native_units(motor)

Return the speed percentage required to achieve the desired degrees-per-minute

Parameters

motor (Motor) – the motor to use for calculating the speed percentage

Returns

the speed percentage required to achieve the desired degrees-per-minute

Return type

int

class spikedev.motor.MotorStop

Bases: object

FLOAT = 0
BRAKE = 1
HOLD = 2
class spikedev.motor.MotorCallbackEvent

Bases: object

COMPLETED = 0
INTERRUPTED = 1
STALL = 2
class spikedev.motor.MotorPolarity

Bases: object

NORMAL = 0
REVERSED = 1
class spikedev.motor.MotorMode

Bases: object

POWER = 0
SPEED = 1
POS = 2
APOS = 3
LOAD = 4
CALIB = 5
exception spikedev.motor.InvalidMotorMode

Bases: ValueError

class spikedev.motor.Motor(port, polarity=0, desc=None)

Bases: object

A base class for SPIKE motors

Parameters
property position

Returns: int: the motor’s position encoder value

property is_stalled

Returns: bool: True if the motor has stalled

property is_running

Returns: bool: True if the motor is running

init_position()

Initialize the POS value from the APOS value

stop(stop_action=1)

stops the motor

Parameters

stop_action (MotorStop) – defaults to MotorStop.BRAKE

run_at_speed(speed, **kwargs)

Run the motor at speed. The motor will run until you call stop()

Parameters
  • speed (MotorSpeed) – the speed of the motor

  • **kwargs – optional kwargs that will pass all the way down to the LEGO hub.port.X.motor API call

run_for_degrees(degrees, speed, stop=1, block=True, **kwargs)

Run the motor at speed for degrees

Parameters
  • degrees (int) – the number of degrees to move the motor

  • speed (MotorSpeed) – the speed of the motor

  • stop (MotorStop) – how to stop the motors, defaults to MotorStop.BRAKE

  • block (bool) – if True this function will not return until the motors have finished moving

  • **kwargs – optional kwargs that will pass all the way down to the LEGO hub.port.X.motor API call

run_to_position(position, speed, direction='shortest', stop=1, block=True, **kwargs)

Run the motor at speed to the desired position

Parameters
  • position (int) – the target position for the left motor

  • speed (MotorSpeed) – the speed of the motor

  • direction (str) – one of clockwise, counterclockwise or shortest

  • stop (MotorStop) – how to stop the motors, defaults to MotorStop.BRAKE

  • block (bool) – if True this function will not return until the motors have finished moving

  • **kwargs – optional kwargs that will pass all the way down to the LEGO hub.port.X.motor API call

run_for_time(msec, speed, stop=1, block=True, **kwargs)

Run the motor at speed for msec

Parameters
  • msec (int) – the number of milliseconds to run the motor

  • speed (MotorSpeed) – the speed of the motor

  • stop (MotorStop) – how to stop the motors, defaults to MotorStop.BRAKE

  • block (bool) – if True this function will not return until the motors have finished moving

  • **kwargs – optional kwargs that will pass all the way down to the LEGO hub.port.X.motor API call

class spikedev.motor.SpikeMediumMotor(port, polarity=0, desc=None)

Bases: spikedev.motor.Motor

_images/spike-medium-motor.jpg

Example:

import hub
from spikedev.motor import MotorSpeedPercent, SpikeMediumMotor

# run for 720 degrees at 40% of motor's maximum speed
mtr = SpikeMediumMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedPercent(40))
MAX_RPM = 135
MAX_RPS = 2.25
MAX_DPM = 48600
MAX_DPS = 810
class spikedev.motor.SpikeLargeMotor(port, polarity=0, desc=None)

Bases: spikedev.motor.Motor

_images/spike-large-motor.jpg

Example:

import hub
from spikedev.motor import MotorSpeedPercent, SpikeLargeMotor

# run for 720 degrees at 40% of motor's maximum speed
mtr = SpikeLargeMotor(hub.port.E)
mtr.run_for_degrees(720, MotorSpeedPercent(40))
MAX_RPM = 175
MAX_RPS = 2.916666
MAX_DPM = 63000
MAX_DPS = 1050