Driving a specific distance
For driving a specific distance, we use the PI controller again and incorporate the distance measurements into our encoder object. We calculate how many ticks we want the left wheel to have turned for a given distance, and then use this instead of a timeout component.
Refactoring unit conversions into the EncoderCounter class
We want the conversions for our encoders in the EncoderCounter
class to use them in these behaviors. Refactoring is the process of moving code or improving code while retaining its functionality. In this case, converting distances is one of the purposes of using encoders, so it makes sense to move this code in there:
- Open up your
encoder_counter.py
class. First, we need themath
import:from gpiozero import DigitalInputDevice import math ...
- At the top of the class, add
ticks_to_mm_const
as a class variable (not an instance variable) to use it without any instances of the class. Set this to none initially so that we...