[Quick Coding Tips] Using Sensor Data Instead of Hard Coding

The ancient Greek philosopher Heraclitus said, “Change is the only constant.” This wisdom extends to the world of programming as well.

When programming a robot, we want to enable it to react in real-time to dynamic environments and accomplish its tasks successfully. At the same time, we also want our programs to be more readable, easily modifiable and offer greater flexibility. To achieve these goals, we can use sensor data and variables in our code instead of relying on hard coding.

dimage1

Using Sensor Data vs Hardcoding in VEXcode VR

Hard coding is using specific values in a program that leads to fixed functionalities.

Let’s take a look at an example of hard coding in VEXcode VR: to navigate the robot to knock over one building on the Dynamic Castle Crasher Playground (Image 1), we use the drive for block to code the robot to drive forward for 1000 mm (Image 2) to complete this task.

However, the challenge arises when we reset the playground, as the Dynamic Castle Crasher Playground may be set to a different configuration, such as Image 3. If we hard code the robot to drive forward for 1000 mm, it won’t be able to knock over any building.

himage2

example1

On the other hand, by using Distance Sensor data in programming, we can develop an adaptable solution that guides the robot to knock down one building on the playground, regardless of the playground setting.

example2

Here is an example solution:

  1. Keep turning the robot and using the Front Distance Sensor to check if there is a building in front of it.
  2. If a building is detected, drive the robot forward for a distance exceeding 200 meters more than the distance between the robot and the building. (Use FrontDistance Reporter block and add block to find the distance for the robot to drive)

himage3

Note: To find the distance for the robot to drive, use Reporter block and add block to add 200 with the value from Front Distance Sensor and report the sum. Use the sum as the parameter for the drive for block to drive the robot forward for a certain distance to knock over the building (Image 4).

By creating an algorithm using Distance Sensor data, we can effectively clear every building from the Dynamic Castle Crasher Playground, regardless of the playground setting. We will explore these algorithms in more detail in upcoming tips.

example3

Please let us know if you have any questions, comments, or feedback. Thank you!

3 Likes