Micro:Bit Car Kinematics
Lesson Objectives:
Materials:
- Students will learn about numerical integration
- Students will learn about relationship between position, velocity, and acceleration
- Students will learn to write data to files
Materials:
- BBC micro:bits
- Laptop with Mu Software
- Assorted LEGO pieces to construct a cart
Let's Get Started!
As you may know if you've taken physics, velocity is the derivative of position and acceleration is the derivative of velocity. Conversely, velocity is the integral of acceleration and position is the integral of velocity. So, if you know one of the three, you can calculate the other two. Micro:bits have an accelerometer built-in, so you can easily measure the acceleration a micro:bit is experiencing. If you apply numerical integration to the accelerometer data, you can calculate the velocity and position of the micro:bit.
Goal:
Create a micro:bit program that measures the micro:bit's acceleration and integrates it to determine velocity and position. All of this data should be repeatedly recalculated and logged to a text file.
As you may know if you've taken physics, velocity is the derivative of position and acceleration is the derivative of velocity. Conversely, velocity is the integral of acceleration and position is the integral of velocity. So, if you know one of the three, you can calculate the other two. Micro:bits have an accelerometer built-in, so you can easily measure the acceleration a micro:bit is experiencing. If you apply numerical integration to the accelerometer data, you can calculate the velocity and position of the micro:bit.
Goal:
Create a micro:bit program that measures the micro:bit's acceleration and integrates it to determine velocity and position. All of this data should be repeatedly recalculated and logged to a text file.
Construction:
Build a small cart out of LEGO pieces (or anything else you can find). The micro:bit and its battery should securely mount to the car with one axis of the micro:bit's accelerometer aligned with the direction of motion. If you want to use the cart with photogates, make sure at least one side of the it has an opaque wall. See the picture at the top of the page for an example.
Also consider how you want to test the car. You may want to create a ramp or race track of some sort.
Build a small cart out of LEGO pieces (or anything else you can find). The micro:bit and its battery should securely mount to the car with one axis of the micro:bit's accelerometer aligned with the direction of motion. If you want to use the cart with photogates, make sure at least one side of the it has an opaque wall. See the picture at the top of the page for an example.
Also consider how you want to test the car. You may want to create a ramp or race track of some sort.
Numerical Integration:
You may have heard integrals referred to as the 'area under the curve'. Numerical Integration tries to approximate this area. The simplest way is to add together the areas of a lot of rectangles of height equal to the value of the function at that point and a width of the time step, dt. In other words, it is the measurement value * time since last measurement + previous integral value.
More Information
So,
velocity = velocity + acceleration * dt
position = position + velocity * dt
You will want to create a function that that takes acceleration, dt, previous velocity, and previous position as inputs. It should calculate and return new velocity and position values.
Code Structure:
The structure for this code is similar to the Micro:bit Pendulum. Take a look at the instructions and code from it. The basic structure is:
You may have heard integrals referred to as the 'area under the curve'. Numerical Integration tries to approximate this area. The simplest way is to add together the areas of a lot of rectangles of height equal to the value of the function at that point and a width of the time step, dt. In other words, it is the measurement value * time since last measurement + previous integral value.
More Information
So,
velocity = velocity + acceleration * dt
position = position + velocity * dt
You will want to create a function that that takes acceleration, dt, previous velocity, and previous position as inputs. It should calculate and return new velocity and position values.
Code Structure:
The structure for this code is similar to the Micro:bit Pendulum. Take a look at the instructions and code from it. The basic structure is:
- Collect measurement
- Perform calculation on measured data (In this case, Numerical Integration)
- Write data to file
- Repeat
Car Motion Integration Code
Improving Integration:
If you look at your results for this project, you'll likely see that they are not very accurate. There are two major contributing factors:
Combining with Photogates:
You can combine this project with the Micro:bit Photogate project. You can either just test your car on a track with photogates to compare your results or use the photogates to send a start and stop signal to the car to control the time period that the numerical integration code is run for. Code for this is included below.
If you look at your results for this project, you'll likely see that they are not very accurate. There are two major contributing factors:
- The micro:bit accelerometer - the micro:bit's accelerometer is fairly inaccurate and noisy. While you cannot entirely solve this problem, you can reduce its effect by filtering the data. One filter you can use is called a median filter.
- Numerical integration - Numerical integration is an inherently error-prone process. However, rectangular numerical integration is the least accurate method. A more accurate method of integration can be used, such as trapezoidal integration.
Combining with Photogates:
You can combine this project with the Micro:bit Photogate project. You can either just test your car on a track with photogates to compare your results or use the photogates to send a start and stop signal to the car to control the time period that the numerical integration code is run for. Code for this is included below.
Car with Photogates code
Start Photogate code
Stop Photogate code
References
The following functions and documentation may be helpful.
The full documentation may be found here:
The following functions and documentation may be helpful.
The full documentation may be found here:
Useful Functions
microbit.running_time()
Return the number of milliseconds since the board was switched on or restarted.
microbit.accelerometer.get_y()
Get the acceleration measurement in the y axis, as a positive or negative integer, depending on the direction. The measurement is given in milli-g. By default the accelerometer is configured with a range of +/- 2g, and so this method will return within the range of +/- 2000mg.
Return the number of milliseconds since the board was switched on or restarted.
microbit.accelerometer.get_y()
Get the acceleration measurement in the y axis, as a positive or negative integer, depending on the direction. The measurement is given in milli-g. By default the accelerometer is configured with a range of +/- 2g, and so this method will return within the range of +/- 2000mg.