motion_controller – enables more precise motor control

The motion_controller module contains the MotionController class.

CodeBot’s lowest level motor control routines require you to work in terms of power settings and time durations.

This makes it easy to get going, but hard to be precise.

The MotionController class gives your code the ability to work in terms of distances and speeds.

Both are measured in ticks, as reported by the wheel encoders.

The MotionController class makes use of two other classes:

  • MeteredMotors - provides the ability to measure the motors movement.

  • SpeedController - handles some of the power level calculations for us.

class motion_controller.MotionController(left_speed_controller, right_speed_controller, svc=None)[source]

Builds upon MeteredMotors to provide a higher-level API.

Even higher level APIs are possible (and planned).

request_motion(speed_left, ticks_left, speed_right, ticks_right, duration, complete_cb=None)[source]

Request a move for a given distance, or an amount of time

  • speed_left (int) – how fast to turn the left motor

  • ticks_left (int) – how far to turn the left motor

  • speed_right (int) – how fast to turn the right motor

  • ticks_right (int) – how far to turn the right motor

  • duration (int) – how long to move (specified in milliseconds)

  • complete_cb (func) – an optional function to call when the requested motion is completed.

All speeds are specified in ticks/second. Positive speeds are forward, negative speeds are backwards.

All distances are specified in ticks.

A distance of 0 means there is no restriction on how far that motor is allowed to move.

A duration of 0 means there is no time limit on the movement.

The move will be ended when either condition is met, and the specified callback will be invoked.

Call stop_motion() if you need to stop sooner.

From this single function, all sorts of motions are possible:

  • If both requested speeds are the same, CodeBot will go straight.

  • If the speeds are different, CodeBot will move in an arc.

  • If one speed is 0 but the other is not, CodeBot will pivot on the stationary wheel.

  • If one speed is positive and the other speed is negative, CodeBot will spin in place.


Stop any motion in-progress. For example, when your robot detects something interesting…

Download Source