Mechanics - Motion and Speed Image by Freepik

Mechanics - Motion and Speed

In this lesson, we will study the equations of motion in a straight line. We will then apply these equations of motion to determine how fast an object, such as our Elecfreaks Smart CuteBot, is moving,.

Requirements

In order to apply the equations of motion to the robotics experiment described in this lesson (optional), please ensure that you have read and followed the instructions described in our CuteBot - Getting Started tutorial in order to prepare a Micro:bit and Smart CuteBot for programming.

Motion

Motion is the change in an object's position over time, relative to a frame of reference. We describe motion by concepts including distance, speed, velocity and acceleration.

Distance

Distance is how far an object moves. We can measure distance in units of length such as meters (m). Distance is an example of a scalar quantity. Scalar quantities only have magnitude with no associated direction. For example, we may say that a car has travelled a distance of 100m.

Speed

Speed is a measure of how fast an object is moving. In mathematical terms, it is the rate of change of distance - in other words, it is the distance travelled per unit of time, where time is normally measured in seconds (s). Speed is another example of a scalar quantity - it only has magnitude with no associated direction. For example, we may say that a car is moving at a speed of 20 metres per second (20 m/s), a plane is moving at a speed of 250 metres per second (250 m/s), or that sound is moving in air at a speed of around 330 meters per second (330 m/s).

The speed of objects can vary and is dependent on numerous factors. For example, a person on a leisurely walk may slow down when climbing up a hill. And the speed of sound (specifically the speed at which sounds waves propagate through a material) will increase when travelling through liquid water.

Velocity and Acceleration

The velocity of an object is its speed in a particular direction. Velocity is an example of a vector quantity. Vector quantities have both magnitude and an associated direction. And acceleration is the rate of change of velocity - in other words, it is the amount that velocity changes per unit of time.

Velocity, acceleration (and displacement) are beyond the scope of this lesson. However, if you would like to learn more about the physics of motion, please visit BBC Bitesize - Motion and Forces.

Motion in a Straight Line

In this section, we will introduce the equations of motion in a straight line, described by the concepts of speed, distance and time.

Speed, Distance and Time

To calculate the speed of an object, we need to know the distance that it has travelled and the time taken to travel that distance. Therefore, there are three variables involved in this calculation - speed, distance and time. If we know the value of two of these variables, we can calculate the value of the remaining third variable using the following formula triangle.

Speed, Distance and Time formula triangle
Speed, Distance and Time formula triangle

Units of distance include metric units such as kilometres (km), metres (m) and centimetres (cm), and imperial units such as miles and feet. Units of time include seconds (s), minutes or hours. Speed is therefore stated as the distance per unit of time, for example metres per second (m/s) or miles per hour (mph).

Speed Equation

To calculate the speed of an object, we divide the distance travelled by the time taken to travel that distance.

Speed equation
Speed equation

For example, Usain Bolt ran 100 metres in a world record time of 9.58 seconds at the 2009 World Athletics Championships in Berlin, Germany. His average speed can therefore be calculated as follows:

$\text{speed} = \frac{\text{distance}}{\text{time}} = \frac{100\text{m}}{9.58\text{s}} = 10.44\text{ m/s}$

We can therefore state that Usain Bolt ran at an astonishing average speed of 10.44 metres per second!

Distance Equation

To calculate the distance that an object has travelled, we multiply the speed of the object by the time taken to travel that distance.

Distance equation
Distance equation

For example, a Formula 1 car can reach a top speed of approximately 100 metres per second (m/s). If it travels at this speed in a straight line for 8 seconds, the distance it has travelled can be calculated as follows:

$\text{distance} = \text{speed} \times \text{time} = 100\text{ m/s} \times 8\text{s} = 800\text{m} $

We can therefore state that a Formula 1 car moving in a straight line at a constant speed of 100 m/s for 8 seconds travels a distance of 800m!

Time Equation

To calculate the time taken for an object to travel a distance, we divide the distance travelled by the speed of the object.

Time equation
Time equation

For example, if a car travelled 18 miles in a straight line at an average speed of 60 miles per hour (mph), the time it took to travel this distance can be calculated as follows:

$\text{time} = \frac{\text{distance}}{\text{speed}} = \frac{18\text{ miles}}{60\text{mph}} = 0.3\text{ hours}$

We can therefore state that a car that has travelled 18 miles in a straight line at an average speed of 60mph took 0.3 hours (or 18 minutes) to travel that distance.

Laboratory

Experiment Design

As described in our CuteBot - Object Detection tutorial, the Smart CuteBot is equipped with an ultrasonic sensor that enables it to calculate the distance to objects. The ultrasonic sensor works by emitting high-frequency sound waves and measuring the time it takes for the sound to travel to an object, reflect off its surface, and return as an echo to the sensor.

In our CuteBot - Basic Movements tutorial, we introduced the motors(lspeed: number, rspeed: number) method of the cuteBot object to control the speed of the Smart CuteBot. However, this method does NOT control linear speed, but rather controls the rotational speed of the Smart CuteBot's motors as a percentage of their maximum revolutions per minute (RPM). The Smart CuteBot motors are capable of rotating at a maximum of 300 RPM. Therefore, instructing the Smart CuteBot's wheels to spin at a rotational speed of 20 using the motors(lspeed: number, rspeed: number) method results in the motors rotating at 60 RPM (i.e. 20% of 300 RPM). And instructing the Smart CuteBot's wheels to spin at a rotational speed of 100 results in the motors rotating at their maximum rotational speed i.e. 300 RPM.

In this laboratory work, we will construct an experiment designed to measure the approximate linear speed, in centimetres per second, of the Smart CuteBot as it travels in a straight line using its ultrasonic sensor. The experiment will be designed as follows:

  1. Place the Smart CuteBot on one end of a flat table with an object, such as an empty box, directly in front of it on the opposite end of the same table.
Start of the robotics experiment
Initial state of the experiment
  1. Program the Smart CuteBot to move forwards in a straight line at a constant speed using the cuteBot.motors(20, 20) method of the cuteBot object. Please refer to our CuteBot - Basic Movements tutorial for further information on programming the Smart CuteBot to perform basic movements including moving forwards.
  2. As the Smart CuteBot moves forwards in a straight line, program the Smart CuteBot to continuously measure the distance, in centimetres, to the approaching object directly in front of it, until the object is within 15cm at which time the Smart CuteBot should stop moving completely. Please refer to our CuteBot - Object Detection tutorial for further information on programming the Smart CuteBot to measure the distance to objects using its ultrasonic sensor.
End of the robotics experiment
End state of the experiment
  1. Program the Smart CuteBot to measure the time taken, in seconds, from when it first detected the object until it came to a complete stop.
  2. By measuring the initial distance to the object at the point the Smart CuteBot first detected it, and by measuring the time it took the Smart CuteBot to travel to within 15cm of the object, program the Smart CuteBot to calculate the approximate linear speed, in centimetres per second, at which it was travelling, and finally display this speed on its Micro:bit LED screen.
Source Code

The following Python program instructs the Smart CuteBot to automatically calculate and display its linear speed, in centimeters per second, with the help of its ultrasonic sensor as it travels in a straight line.

# Global speed, distance and time tracking variables.
forever_loop_active = False
start_time = input.running_time()
initial_distance_to_object = -1
closest_distance_to_object = 15
speed = -1


def on_button_pressed_a():
    """
    This function will reset the global speed, distance and time tracking 
    variables, clear the Micro:bit LED screen, and 'activate' the forever loop.
    """

    # Reset the speed, distance and time tracking variables.
    global forever_loop_active, start_time
    global initial_distance_to_object, speed
    start_time = input.running_time()
    initial_distance_to_object = -1
    speed = -1

    # Clear the Micro:bit LED screen.
    basic.clear_screen()

    # 'Activate' the forever loop.
    forever_loop_active = True


def on_forever():
    """
    This function will move the car forwards at a constant speed, 
    measure the distance to the approaching object, 
    stop the car when it is within the specified distance to the object, 
    and calculate & display the linear speed at which it was travelling.
    """

    global forever_loop_active, start_time
    global initial_distance_to_object, closest_distance_to_object, speed

    # Determine whether the forever loop is active.
    if forever_loop_active:

        # Move forwards at a constant speed.
        cuteBot.motors(20, 20)

        # Measure the distance to the approaching object in cm 
        # using the ultrasonic sensor.
        distance_to_object = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)

        # Determine the initial distance to the object when it was first detected.
        if initial_distance_to_object == -1 and distance_to_object > 1:
            initial_distance_to_object = distance_to_object

        # Determine whether the distance to the object is 
        # less than the specified threshold e.g. 15cm.
        if distance_to_object <= closest_distance_to_object and distance_to_object > 1:

            # Stop the car.
            cuteBot.stopcar()

            # Calculate the speed (speed = distance / time).
            distance_cm = initial_distance_to_object - distance_to_object
            time_seconds = (input.running_time() - start_time) / 1000
            speed = distance_cm / time_seconds

            # Display the speed on the Micro:bit LED screen.
            basic.show_number(speed)

            # 'Deactivate' the forever loop until the A button is pressed again.
            forever_loop_active = False

    else:

        # Stop the car.
        cuteBot.stopcar()

        # Continue to display the last recorded speed  
        # on the Micro:bit LED screen.
        if speed > 0:
            basic.show_number(speed)


# Specify the event handler for button A.
input.on_button_pressed(Button.A, on_button_pressed_a)

# Run the 'on_forever' function in the background in a forever loop.
basic.forever(on_forever)
Source Code Explained

We begin the Python code above by defining variables to reference certain speed, distance and time measurements that will be used later in our code to calculate the linear speed of the Smart CuteBot. The exact purpose of these variables will be explained below when they are referenced by the relevant functions in our code.

We then define a custom function called on_button_pressed_a. This custom Python function instructs the Micro:bit to do the following:

  • Lines 16 - 17: The statements starting with global instruct the function to reference the relevant variables that have been initialised outside of the function body. In other words, the forever_loop_active, start_time, initial_distance_to_object and speed variables defined on lines 2 to 6 have global scope, meaning that they can be referenced by any function.
  • Line 18: The function resets the value of start_time to the current time. Specifically, the input.running_time() method returns the number of milliseconds that have elapsed since the Micro:bit was powered on. This will be used later in our code to calculate the duration, in seconds, of travel.
  • Lines 19 - 20: The function resets the values of initial_distance_to_object and speed respectively. initial_distance_to_object will be used later in our code to reference the distance, in centimeters, to the object when it was first detected by the Smart CuteBot's ultrasonic sensor. And speed will also be used later in our code to reference and display the eventual result of the speed equation calculation.
  • Line 23: The function then clears the Micro:bit LED screen by calling the basic.clear_screen() method.
  • Line 26: Finally, the function sets the forever_loop_active variable to the boolean value of True. The forever_loop_active boolean variable will be used later in our code to determine whether the Smart CuteBot has already successfully determined its linear speed, at which time it should stop moving and display its linear speed on the Micro:bit LED screen, or whether it should continue to move and continuously determine the distance to the approaching object until it is within the specified distance threshold, at which time it should calculate and display its linear speed.

Next, we define a custom function called on_forever. This custom Python function instructs the Smart CuteBot to do the following:

  • Line 41: The function begins by determining whether the forever_loop_active boolean variable is True. If so, the block of code between lines 43 - 70 will be executed. If not, the else branch from line 72 will be executed.
  • Lines 44 - 48: If forever_loop_active is True, the function will instruct the Smart CuteBot to move forwards in a straight line at a constant speed by setting the rotational speeds of both the left and right wheels to 20% of the Smart CuteBot's motor's maximum rotational speed. This is achieved on line 44 by calling the motors(lspeed: number, rspeed: number) method of the cuteBot object and setting both lspeed and rspeed to 20. Next, the function measures the distance, in centimeters, to the object in front of it, and references this distance in the distance_to_object variable. This is achieved on line 48 by calling the ultrasonic(unit: SonarUnit) method of the cuteBot object, and passing the unit of measurement as the first parameter. To return the distance in centimeters, we use the cuteBot.SonarUnit.CENTIMETERS sonar unit of measurement.
  • Lines 51 - 52: The function stores the distance to the object when it was first detected in the initial_distance_to_object variable. This is achieved on line 51 by first checking that the value referenced by initial_distance_to_object has not already been set and that it is equal to -1 (i.e. its value at initialisation or when the A button has been pressed to reset the global speed, distance and time tracking variables - see the on_button_pressed_a() function for further details). If initial_distance_to_object has not already been set, and a non-zero and greater than 1cm distance to the object has been measured, then the value of initial_distance_to_object is set to the current value of distance_to_object on line 52.
  • Line 56: The function then checks whether the current distance to the approaching object is less than or equal to the closest permissible distance to the object, as defined by the globally-scoped closest_distance_to_object variable (which is set to the default value of 15cm on line 5). If it is within this threshold, then the block of code between lines 58 - 70 will be executed.
  • Lines 59: If the Smart CuteBot is within the specified distance threshold, the function will instruct the Smart CuteBot to stop moving by calling the stopcar() method of the cuteBot object.
  • Line 62: The function will calculate the total distance, in centimetres, travelled by the Smart CuteBot when it stopped moving. This is calculated by subtracting the current distance to the object, referenced by the distance_to_object variable, from the initial distance to the object, referenced by the initial_distance_to_object variable.
  • Line 63: The function will calculate the total time, in seconds, taken to travel the distance calculated on line 62. This is calculated by calling the input.running_time() method to return the number of milliseconds that have elapsed since the Micro:bit was powered on. The number of milliseconds that elapsed since the Micro:bit was powered on at the moment that the A button was pressed on the Micro:bit board is referenced by the start_time variable - see the on_button_pressed_a() function for further details. Therefore, subtracting the value referenced by the start_time variable from the value returned by calling input.running_time() will return the total time of travel in milliseconds. Finally, we divide this result by 1000 to convert milliseconds to seconds.
  • Lines 64 - 67: The function then calculates the linear speed, in centimeters per second, at which the Smart CuteBot was travelling. To do this, it applies the speed equation i.e. speed = distance / time. Once calculated, it displays this speed on the Micro:bit's LED screen.
  • Line 70: The forever_loop_active variable is set to False so that the Smart CuteBot stops moving and stops measuring the distance to the object as we have already calculated its linear speed.
  • Lines 72 - 80: If forever_loop_active is False, the function instructs the Smart CuteBot to stop moving, and to continue to display the last recorded non-zero speed on the Micro:bit's LED screen. Our Smart CuteBot and Micro:bit will continue to remain in this state until the A button is pressed on the Micro:bit board to reset the experiment.

Our program continues by specifying the function to execute when the A button is pressed on the Micro:bit board. This is achieved on line 84 by calling the input.on_button_pressed(button: Button, body: ()) event handler, with the 1st parameter set as Button.A and the 2nd parameter set as the name of our custom Python function i.e. on_button_pressed_a that will be invoked when the specified button is pressed. And finally, we specify that our custom on_forever function should run continuously in the background. This is achieved by calling the basic.forever(body: ()) method, with the first and only parameter set as the name of our custom Python function i.e. on_forever that will be executed continuously in the background within a forever event-based loop.

Experiment Results

In our specific experiment environment, our Smart CuteBot travelled at an average linear speed of around 18 centimetres per second when setting the rotational speeds of both the left and right wheels to 20% of the Smart CuteBot's motor's maximum rotational speed. The linear speed may be slightly different for you, as it is dependent on factors including the material, smoothness and inclination of the table. However, given similar conditions and the same rotational speed, we would expect a linear speed of somewhere between 16 to 20 cm/s.

To learn more about the relationship between speed, distance and time, please visit BBC Bitesize.

Jillur Quddus
Written by

Jillur Quddus

Computational Mathematician @ HyperLearning AI