spikedev.tank

class spikedev.tank.MoveTank(left_motor_port, right_motor_port, motor_class=<class 'spikedev.motor.SpikeMediumMotor'>, left_motor_polarity=1, right_motor_polarity=0, desc=None)

Bases: object

_images/tank.png

A class for supporting tank style robots that are driven by two motors

Parameters
  • left_motor_port (str) – port letter of the left motor

  • right_motor_port (str) – port letter of the right motor

  • motor_class (Motor) – the type of motor that is used, defaults to SpikeMediumMotor

  • left_motor_polarity (MotorPolarity) – the polarity for the left motor, defaults to MotorPolarity.REVERSED

  • right_motor_polarity (MotorPolarity) – the polarity for the right motor, defaults to MotorPolarity.NORMAL

  • desc (str) – defaults to None

Example:

import hub
from spikedev.motor import MotorSpeedDPS
from spikedev.tank import MoveTank

tank = MoveTank(hub.port.E, hub.port.F)
tank.run_for_time(3000, MotorSpeedDPS(180), MotorSpeedDPS(360))
stop()

Stop both motors

run_at_speed(left_speed, right_speed, **kwargs)

Run the motors at the specified speeds. The motors will run until you call stop()

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

  • right_speed (MotorSpeed) – the speed of the right motor

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

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

Run the motors at the specified speeds, the two motors combined will move an average of degrees

Parameters
  • degrees (int) – the average number of degrees for the two motors to move

  • left_speed (MotorSpeed) – the speed of the left motor

  • right_speed (MotorSpeed) – the speed of the right 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(left_position, right_position, speed, stop=1, block=True, **kwargs)

Run both motors at speed to the desired positions

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

  • right_position (int) – the target position for the right motor

  • speed (MotorSpeed) – the speed of both motors

  • 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, left_speed, right_speed, stop=1, block=True, **kwargs)

Run the motors at the specified speeds for msec

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

  • left_speed (MotorSpeed) – the speed of the left motor

  • right_speed (MotorSpeed) – the speed of the right 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.tank.MoveSteering(left_motor_port, right_motor_port, motor_class=<class 'spikedev.motor.SpikeMediumMotor'>, left_motor_polarity=1, right_motor_polarity=0, desc=None)

Bases: spikedev.tank.MoveTank

_images/steering.jpg

MoveSteering is a child of MoveTank that adds the ability to control the tank via a steering value and a speed value.

Example:

import hub
from spikedev.motor import MotorSpeedDPS
from spikedev.tank import MoveSteering

ms = MoveSteering(hub.port.E, hub.port.F)

# Move forward and to the right for 500 degrees
ms.run_for_degrees(500, 60, MotorSpeedDPS(180))
run_at_speed(steering, speed, **kwargs)

Run the motors according to the provided steering at speed The motors will run until you call stop()

steering details:
  • -100 means turn left on the spot (right motor at 100% forward, left motor at 100% backward),

  • 0 means drive in a straight line, and

  • 100 means turn right on the spot (left motor at 100% forward, right motor at 100% backward).

Parameters
  • steering (int) – a number from -100 to 100

  • speed (SpeedValue) – The speed that should be applied to the outmost motor (the one rotating faster). The speed of the other motor will be computed automatically.

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

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

Rotate the motors according to the provided steering at speed where the outmost motor (the one rotating faster) will move for degrees.

steering details:
  • -100 means turn left on the spot (right motor at 100% forward, left motor at 100% backward),

  • 0 means drive in a straight line, and

  • 100 means turn right on the spot (left motor at 100% forward, right motor at 100% backward).

Parameters
  • steering (int) – a number from -100 to 100

  • speed (SpeedValue) – The speed that should be applied to the outmost motor (the one rotating faster). The speed of the other motor will be computed automatically.

  • degrees (int) – the number of degrees for the outmost motor (the one rotating faster) to move

  • 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, steering, speed, stop=1, block=True, **kwargs)

Rotate the motors according to the provided steering for seconds.

steering details:
  • -100 means turn left on the spot (right motor at 100% forward, left motor at 100% backward),

  • 0 means drive in a straight line, and

  • 100 means turn right on the spot (left motor at 100% forward, right motor at 100% backward).

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

  • steering (int) – a number from -100 to 100

  • speed (SpeedValue) – The speed that should be applied to the outmost motor (the one rotating faster). The speed of the other motor will be computed automatically.

  • 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.tank.MoveDifferential(left_motor_port, right_motor_port, wheel_class, wheel_distance, motor_class=<class 'spikedev.motor.SpikeMediumMotor'>, left_motor_polarity=1, right_motor_polarity=0)

Bases: spikedev.tank.MoveTank

_images/differential.jpg
MoveDifferential is a child of MoveTank that adds the following capabilities:
  • drive in a straight line for a specified distance

  • rotate in place in a circle (clockwise or counter clockwise) for a specified number of degrees

  • drive in an arc (clockwise or counter clockwise) of a specified radius for a specified distance

All of the args from MoveTank apply plus two additional args:

Parameters
  • wheel_class (Wheel) – used to get the circumference of the wheels of the robot. The circumference is needed for several calculations in this class.

  • wheel_distance_mm (DistanceValue) – The distance between the mid point of the two wheels of the robot. You may need to do some test drives to find the correct value for your robot. It is not as simple as measuring the distance between the midpoints of the two wheels. The weight of the robot, center of gravity, etc come into play.

Example:

import hub
from spikedev.motor import MotorSpeedDPS
from spikedev.tank import MoveDifferential
from spikedev.unit import DistanceInches, DistanceStuds
from spikedev.wheel import SpikeWheel

md = MoveDifferential(hub.port.E, hub.port.F, SpikeWheel, DistanceStuds(11))

# rotate 90 degrees clockwise
md.turn_right(90, MotorSpeedDPS(100))

# rotate 90 degrees counter-clockwise
md.turn_left(90, MotorSpeedDPS(100))

# drive forward 6 inches
md.run_for_distance(DistanceInches(6), MotorSpeedDPS(100))

# Drive in arc to the right along an imaginary circle of radius 12 inches.
# Drive for 6 inches around this imaginary circle.
md.run_arc_right(DistanceInches(12), DistanceInches(6), MotorSpeedDPS(100))
run_for_distance(distance, speed, stop=1, block=True, **kwargs)

Drive in a straight line for distance

Parameters
  • distance (DistanceValue) – how far the midpoint between the wheels should travel

  • speed (MotorSpeed) – how fast the midpoint between the wheels should travel

  • 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_arc_right(radius, distance, speed, stop=1, block=True, **kwargs)

Drive clockwise in a circle with radius for distance

Parameters
  • radius (DistanceValue) – the radius of the arc to drive in, think of this as the size of the imaginary circle the robot will follow

  • distance (DistanceValue) – how far the midpoint between the wheels should travel along the imaginary circle

  • speed (MotorSpeed) – how fast the midpoint between the wheels should travel

  • 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_arc_left(radius, distance, speed, stop=1, block=True, **kwargs)

Drive counter-clockwise in a circle with radius for distance

Parameters
  • radius (DistanceValue) – the radius of the arc to drive in, think of this as the size of the imaginary circle the robot will follow

  • distance (DistanceValue) – how far the midpoint between the wheels should travel along the imaginary circle

  • speed (MotorSpeed) – how fast the midpoint between the wheels should travel

  • 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

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

Rotate in place degrees. Both wheels will turn at the same speed for us to rotate in place.

Parameters
  • degrees (int) – the number of degrees to rotate in place

  • speed (MotorSpeed) – how fast each wheel should move

  • 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

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

Rotate clockwise degrees in place

Parameters
  • degrees (int) – the number of degrees to rotate clockwise in place

  • speed (MotorSpeed) – how fast each wheel should move

  • 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

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

Rotate counter-clockwise degrees in place

Parameters
  • degrees (int) – the number of degrees to rotate counter-clockwise in place

  • speed (MotorSpeed) – how fast each wheel should move

  • 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