5.f. 0x88 - 0x8F: Set Motor Forward/Reverse

Motor Control Overview

The qik can independently drive up to two bidirectional brushed DC motors, referred to as M0 and M1, using two VNH2SP30 motor drivers. Variable speed is achieved with 7-bit or 8-bit pulse width modulated (PWM) outputs at one of several selectable frequencies. 7-bit control allows for PWM frequencies of 19.7 kHz, 2.5 kHz, and 310 Hz; 8-bit control allows for PWM frequencies of 9.8 kHz, 1.2 kHz, and 150 Hz. The highest achievable frequency of 19.7 kHz is ultrasonic, which can result in quieter motor control. Lower frequencies might produce louder motor noise, but they can help decrease power losses due to switching. The resolution and frequency can be set via the qik’s PWM configuration parameter (see Section 5.a).

The motor direction convention used in this document is that “forward” corresponds to holding the + output at VIN while PWMing the - output between ground and high impedance. “Reverse” is the same as forward but with the outputs flipped: - is held at VIN while + is PWMed between ground and high impedance. As a result, the motor is rapidly alternating between drive and coast when the direction is “forward” or “reverse”. Variable speed control is achieved by varying the fraction of the cycle that the motor outputs are driving. Full speed arises when the motor outputs are driving 100% of the time (one motor output is held at steady at VIN and the other steady at ground). In 7-bit mode, motor speed ranges from stopped (high-impedance outputs, or “coast”) to full speed as the speed parameter ranges from 0 to 127. In 8-bit mode, motor speed ranges from stopped (high-impedance outputs, or “coast”) to full speed as the speed parameter ranges from 0 – 255.

The eight set-motor commands are enumerated below, but some users might prefer to think of the set-motor command byte in terms of the meanings of various bits in the byte (bit 0 is the least-significant bit):

  • bit 7 (sync bit): Set this bit when using the Compact Protocol; clear this bit when using the Pololu Protocol.
  • bits 6:3 (set motor command bits): for this command, set bit 3 and clear bits 4 – 6.
  • bit 2 (motor bit): Set this bit for motor M1; clear this bit for motor M0.
  • bit 1 (direction bit): Set this bit for “reverse”; clear this bit for “forward”.
  • bit 0 (speed mode bit): This is the eighth motor speed bit when the qik is in 8-bit PWM mode (i.e. set this bit to add 128 to the speed data byte). This bit has no effect when the qik is in 7-bit PWM mode.

Motor M0 Commands

Commands 0x88 – 0x8B apply to motor M0. In 8-bit mode, commands 0x89 and 0x8B set the motor speed to 128+motor speed; in 7-bit mode, command 0x89 is identical to command 0x88 and command 0x8B is identical to command 0x8A.

For example, in 8-bit mode, the command packet 0x88, 0x7F sets motor M0 speed to 127 out of a maximum of 255, which turns the motor at half speed. The command packet 0x89, 0x7F sets motor M0 speed to 127+128 = 255, which turns the motor at full speed. In 7-bit mode, both commands set motor M0 speed to 127 out of a maximum of 127, which turns the motor at full speed.

    Command 0x88 (136): Motor M0 Forward

    Compact protocol: 0x88, motor speed
    Pololu protocol: 0xAA, device ID, 0x08, motor speed

    Command 0x89 (137): Motor M0 Forward (speed + 128; used in 8-bit mode)

    Compact protocol: 0x89, motor speed
    Pololu protocol: 0xAA, device ID, 0x09, motor speed

    Command 0x8A (138): Motor M0 Reverse

    Compact protocol: 0x8A, motor speed
    Pololu protocol: 0xAA, device ID, 0x0A, motor speed

    Command 0x8B (139): Motor M0 Reverse (speed + 128; used in 8-bit mode)

    Compact protocol: 0x8B, motor speed
    Pololu protocol: 0xAA, device ID, 0x0B, motor speed

Motor M1 Commands

Commands 0x8C – 0x8F apply to motor M1. In 8-bit mode, commands 0x8D and 0x8F set the motor speed to 128+motor speed; in 7-bit mode, command 0x8D is identical to command 0x8C and command 0x8F is identical to command 0x8E.

For example, in 8-bit mode, the command packet 0x8C, 0x7F sets motor M1 speed to 127 out of a maximum of 255, which turns the motor at half speed. The command packet 0x8D, 0x7F sets motor M1 speed to 127+128 = 255, which turns the motor at full speed. In 7-bit mode, both commands set motor M1 speed to 127 out of a maximum of 127, which turns the motor at full speed.

    Command 0x8C (140): Motor M1 Forward

    Compact protocol: 0x8C, motor speed
    Pololu protocol: 0xAA, device ID, 0x0C, motor speed

    Command 0x8D (141): Motor M1 Forward (speed + 128; used in 8-bit mode)

    Compact protocol: 0x8D, motor speed
    Pololu protocol: 0xAA, device ID, 0x0D, motor speed

    Command 0x8E (142): Motor M1 Reverse

    Compact protocol: 0x8E, motor speed
    Pololu protocol: 0xAA, device ID, 0x0E, motor speed

    Command 0x8F (143): Motor M1 Reverse (speed + 128; used in 8-bit mode)

    Compact protocol: 0x8F, motor speed
    Pololu protocol: 0xAA, device ID, 0x0F, motor speed