Photogate review and bot programming

To the optimist, the glass is half full.
To the pessimist, the glass is half empty.
​To the engineer, the glass is twice as big as it needs to be.

Normal people believe that if it ain't broke, don't fix it.
Engineers believe that if it ain't broke, it doesn't have enough features yet.

Concept summary and connections

Lesson Content

The robot has several things it needs to keep track of at each update:

These things taken together represent the state of our robot, and they're going to be called state variables.

We will use the encapsulation property of object-oriented design to make the state variables easier to manage. That means we're going to make a single class called Robot that keeps track of all of those variables for itself, using code that's organized and easy to maintain.

Without encapsulation, we'd end up with a bunch of global variables and special-case functions to deal with the complexity of this robot. By putting them into a single Robot, we get the big benefit of treating the whole works as a single package. Just knowing where to put things is a big aid in programming - it gives you a chance to make good names for things that are automatically interpreted relative to the class instead of globally to the whole program. It also makes it easy to find the locations of any variables and functions, because it collects them all into a neat package.

Eventually, our Robot class will have some cool features:

For today, it's enough to wrap the data we're using up with a class and make it drive in a straight line.

Today's goals for our bot:

Bonus if we have time

We have a few of these IMU 6050 sensor boards. They make it "easy" to track our orientation and acceleration, which we know from physics means we can use them to estimate our location with some math. They're a little bit complex to use, but if we have time today we'll take a look at what they can do.

The basic idea of an IMU is that it contains a device that can sense accelerations in three directions, and rotations around three axes. Some of them also have a three-axis magnetic field sensor that you can use to get an idea of your absolute orientation (basically a compass to read Earth's magnetic field). These don't have that, but we do have a few different boards with that feature.

When you use an IMU to keep track of your bot's pose, you end up measuring acceleration and rotation over and over again as quickly as possible. You then use the acceleration data to estimate your velocity change over that time, and then you use the average velocity over that time to estimate your position change. It's an error-prone process, but with a fast enough time step and a good sensor you can do pretty well at keeping track of where you are and which direction you're facing.

In order to implement that, we'll need to add several more state variables to our Robot:

Coordinate systems

We're used to thinking of the world as having a set coordinate system that's kind-of attached to the ground, and our bot just moves around on it. However, for this it's probably going to be more convenient to imagine our bot is the origin of the coordinate system, and the world is moving around under it. The practical meaning of that is that the acceleration and velocity data are all going to be relative to our bot, not absolute in the world (driving forward will always give us an acceleration in the Y direction, for example, instead of being different depending on which direction the bot is facing).

Media resources

Practice