Encoders

The Zumo32U4 includes on-board encoders for closed-loop motor control. In our application we need to read the angular position and angular speed of the motors because to the Segway model we used defines these quantities as state variables. For more information review the State Variable Model.

The optical encoder [7] available in the Zumo32u4 uses the Sharp GP2S60 [12]. In the case of the encoders the Zumo Library [8] abstracts all the needed configuration and read/write operations. The only thing needed for implementation is the interpretation of the count provided by the encounters.

According to [7] the optical encoder provides 12 CPR. Therefore its count can be interpreted as shown in (1).

(1)\[cycles = \frac{encodersCount}{12 \times gearRatio}\]

According to [5], [6] and [4] the gear ratio for the different Zumo modules are shown in Table 2.

Table 2 Gear Ratio and Count to Degree per Zumo32u4 model
Model Gear Ratio Count to Degree (\(^\circ\))
50:1 51.45 \(0.58309^\circ\)
75:1 75.81 \(0.39573^\circ\)
100:1 100.37 \(0.29889^\circ\)

Furthermore we can convert number of cycles to degree with the conversion ratio \(\frac{360^\circ}{1 \times cycle}\). Multiplying (1) by this ratio;

\[motorAngularPosition = encodersCount \times countToDegrees\]

With;

\[countToDegrees = \frac{360}{12 \times gearRatio}\]

Note

Since the Zumo32u4 has one encoder per motor we decided to estimate the actual motor:motorAngularPosition as the average of both motors’ angular position.

Listing 5 shows the implementation of the explained above. Note that the \(motorAngularSpeed\) is obtained by derivating \(motorAngularPosition\) both are needed by our state variable model. For more information review the State Variable Model.

Listing 5 Encoders Code
/** Zumo 100:1 motor gear ratio */
const float gearRatio = 100.37;
/** Encoder count to cycle convertion constant */
const float countToDegrees = 360 / (float)(12.0 * gearRatio);

/** Zumo encoders */
Zumo32U4Encoders encoders;

/**
* Clear the counters of the encoder
*/
void clearEncoders() {
  encoders.getCountsAndResetLeft();
  encoders.getCountsAndResetRight();
}

/**
* Sample the encoders
*/
void sampleEncoders() {
  static float prevPosition = 0;
  static uint16_t lastUpdate = 0;
  static float leftPosition = 0;
  static float rightPosition = 0;
  uint16_t m = micros();
  uint16_t dt = m - lastUpdate;
  lastUpdate = m;

  leftPosition += (float)encoders.getCountsAndResetLeft() * countToDegrees;
  rightPosition += (float)encoders.getCountsAndResetRight() * countToDegrees;
  float motorAngularPosition = -(leftPosition + rightPosition) / 2.0;

  motorAngularSpeed = (motorAngularPosition - prevPosition) * 1000000.0 / dt;
  prevPosition = motorAngularPosition;
}

Note

  • encoders.getCountsAndResetLeft() and encoders.getCountsAndResetRight() get the actual count of the respective motor and clear its counter.
  • \(motorAngularPosition\) is the average of both speeds multiplied by \(-1\) to match our reference frame.
  • The source code of the Encoders can be reviewed at src/SegwayLQR/ZumoEncoders.ino

ZumoEncoders API

class ZumoEncoders
const float gearRatio = 100.37

Zumo 100:1 motor gear ratio

const float countToDegrees = 360 / (float)(12.0 * gearRatio);

Encoder count to cycle convertion constant

Zumo32U4Encoders encoders

Zumo encoders

void clearEncoders()

Clear the counters of the encoder

void sampleEncoders()

Sample the encoders

[4]Pololu. 100:1 micro metal gearmotor hp 6v with extended motor shaft. URL: https://www.pololu.com/product/2214.
[5]Pololu. 50:1 micro metal gearmotor hp 6v with extended motor shaft. URL: https://www.pololu.com/product/2213.
[6]Pololu. 75:1 micro metal gearmotor hp 6v with extended motor shaft. URL: https://www.pololu.com/product/2215.
[7](1, 2) Pololu. Optical encoder pair kit for micro metal gearmotors. URL: https://www.pololu.com/product/2590.
[8]Pololu. Zumo library. URL: https://github.com/pololu/zumo-32u4-arduino-library.
[12]Sharp. GP2S60, SMT, Detecting Distance : 0.5mm, Phototransistor Output, Compact Reflective Photointerrupter. October 2005. URL: https://www.pololu.com/file/0J683/GP2S60_DS.pdf.