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.
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.
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.
Here is an example solution:
- Keep turning the robot and using the Front Distance Sensor to check if there is a building in front of it.
- 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)
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.
Please let us know if you have any questions, comments, or feedback. Thank you!