5. Lesson 5 - Personal Yacht

Introduction

Gyrocompass

Did you know that most of the modern ships and bigger boats have a gyroscope sensor on board? How do you think it looks like? How big is it?

content picture

These ship devices are called gyrocompasses. Their purpose is to show where is North - just like a compass - but they are more accurate and are not influenced by metal or magnet objects. The gyrocompasses are not huge devices, but they are much larger than our sensors. They are about 50 centimeters wide and about 1,2 meters high, though the purpose of the size is to make it easier to work with. Today gyrocompasses are used along with different systems, such as GPS, to locate the exact position of a ship.

Construction and Theory

After building the robot, close the box to keep the desk clear.

  1. Gather all the parts from the desk and put them back in the box.
  2. Close the box and move it aside so it's out of your way.

Programming and Theory

Gyro sensor - Switch

We already know how to use the gyro sensor with wait block, now its time to learn how to use it with Switch.

The Gyro sensor, like every other sensor, has its own setting in the Switch.

Unlike the wait block, where there are two options, here we have only a compare mode. If you place the mouse over it, you will see two sub-modes. 

  •  Angle - the robot compares with a certain value, by default 90, the number of degrees the sensor has rotated. The block checks if the comparison is true and executes the relevant commands according to the result.content picture

  • Rate -  the robot compares with a certain value, by default 90, the rate of change in the rotations measured by the sensor - i.e. how many degrees per second the sensor rotates. The block checks if the comparison is true and executes the relevant commands according to the result. content picture

 

The last setting we need to pay attention to is the port to which the sensor is connected. By default it's 2, but if we have to, we can change it to any port from 1 to 4.

content picture

Important part of the programming of the gyro sensor is that when we run a program or start port view, it starts measuring degrees from 0. Clockwise rotation is measured as positive and counterclockwise as negative degrees. Just like with the rotation sensor that follows the same logic.

Unlike the degrees, the rate is always 0, until it turns. When it stops turning, it goes to 0 again.

Keeping a straight line using a gyro sensor.

The gyro sensor indicates the angle of the turn the robot made. We certainly can use this to create a robot that could perfectly follow a direction, right? Now we'll show you the easiest way to do it.

This program is exactly the same as following a line with one color sensor. The idea is to move straight, when we're in the right direction (the gyro sensor shows 0 degrees), and to turn, when the gyro is not showing 0 in order to get back to the right direction.

content picture

However, this would be complicated. In general the program represents a series of constant turns, also known as "duck walking". The back line from the line following task will be replaced by the imaginary line that we want to follow. The target value will be 0, but we can modify it for the angle needed. The algorithm is as follows:

  • If the gyro sensor shows less than 0 degrees, we turn towards 0 degrees - in this case we turn right.

content picture

  • If the gyro sensor shows more than 0 degrees, we turn towards 0 degrees again. In this case we turn left.

content picture

And we're ready! We have a program with which our robot will always follow the same direction, even if we try to push it away from its path.

Like in line following, this program could be done with both switch and wait blocks.

This is the simplest algorithm for following a direction, but it's not the smoothest. Fortunately, line following is exactly the same task as direction following. Therefore, you could check the tutorials for line following with 3 or more states or proportional line following. The logic is exactly the same.

Three or More State Programs

In more advanced robotics programming, your robot may need to respond in three or more different ways depending on sensor input. Up to this point, we've created programs with only two possible outcomes using a switch block: a condition is checked, and the robot performs one action or the other based on the result.

But what if the robot needs more than just two responses? To handle this, we can use multiple conditions—often implemented with nested switch blocks or structured decision chains. In this section, we’ll explore how to build programs that go beyond binary choices and enable your robot to react intelligently to more complex environments.

Let’s revisit the 3-state line-following example using the following setup:

Threshold Diagram

Suppose we use a single switch block that checks whether the value read by the color sensor is less than 27:

Initial Switch Block

In this case, the robot knows to turn right when the value is less than 27. However, it has no instructions for what to do when the value is greater than or equal to 27. At that point, the robot needs to choose between going straight or turning left. If the value is less than 37, it should go straight. If it's greater, it should turn left. So how can we add this decision-making?

The same way we handled the first condition—by using another switch block. If we place a second switch inside the "false" branch of the first one, comparing the value to 37, we can handle all three cases. Since the second switch is only evaluated if the first condition is false, we already know the value is at least 27 when it runs.

Nested Switch Diagram

This approach—placing a switch block inside another—is known as a nested switch.

Tasks to finish a class

  1. Take pictures and make videos of your robots
  2. Disassemble and arrange the robot
  3. Arrange your workplace
    • It is important to arrange the electronics of the robots on the cover of the box.
    • Wind up the programming cable so that it is assembled.
    • Put the robot cables next to the electronics.
    • Wind up the mouse cable around the mouse.
    • Wind up the cable of the laptop charger in a way that your teacher will show you.
    • When you turn off the computer, leave it next to the robot box.
  4. Turn off all programs on your computer
  5. Exit FLLCasts  
  6. Shut down your computer
  7. Put your chair under the desk.