> For the complete documentation index, see [llms.txt](https://docs.launchboard.app/cxd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.launchboard.app/cxd/code-robotics-1/references/navigation-modes/autonomous-navigation.md).

# Autonomous Navigation

When using autonomous navigation, the robot uses its sensors to detect features in environment (obstacles, etc.), and then decides what actions to take (stop, turn, drive, etc.). The robot may not necessarily follow a pre-determined path, but it will follow pre-determined decision-making rules.

* **ADVANTAGE:**  The robot can adapt to changes in its environment (e.g., obstacles in different positions, etc.). The robot can be programmed to perform more complex behaviors (e.g., [solving a maze](https://www.instructables.com/id/Robot-Maze-Solver/), etc.).
* **DISADVANTAGE:**  The robot's behavior is limited by which sensors it has. Depending on the behavior needed, it may be more challenging to program the decision-making rules.

## Example Task Scenario

In this task scenario, a security robot will patrol an area in a semi-random pattern. The robot will use its IR line sensors to avoid crossing the line around the area's perimeter. When the robot detects the line, the robot will turn back towards the interior of the area. In addition, the robot will use its ultrasonic sensor to avoid colliding with any obstacles within the area by changing direction when an obstacle is too close. (The red rectangles are cardboard boxes representing obstacles.)

Every time the robot makes a turn, the angle has been programmed to be slightly random (though within a certain range), which makes the robot's pattern different every time the demo is run. (Therefore, the diagram only shows one possible path.) The robot can be started from anywhere in the environment (pointing in any direction), and the robot will still perform its task of patrolling within the area while avoiding obstacles. You can also change the number of obstacles and their positions at any time.

For the purposes of the demonstration, the robot will only patrol for a limited amount of time (30 seconds). In addition, the demo environment is obviously much smaller than a real environment for a security guard patrol.

![](https://3916960112-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LRN96JmOE7RJXgSXrTZ%2F-LRmdAtju6u0v_Aluq92%2F-LRmdXah6ntbnYPmDXzp%2Frobot-demo4.jpg?alt=media\&token=15c4b17c-a65e-4fe4-9674-425e06910d8c)

## Example Code

Here is a possible way to code a custom function to perform this task scenario:

```cpp
void task1() {
  // add code to perform task scenario 1

  // drive autonomously by avoiding lines and obstacles

  // get current time (in milliseconds)
  unsigned long time = millis();
  
  // set end time (in milliseconds)
  unsigned long duration = 30000; // 30 seconds
  unsigned long endTime = time + duration;

  // while current time is less than end time, loop performs task
  while (time < endTime) {
    // add code to perform continuous task
    avoidLine();
    avoidCollision();
    time = millis(); // check current time again
  }

  // time's up
  motors.stop();
  doubleBeep();

  // at end of this task, reset for next task
  started = false;
  nextTask = 2;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.launchboard.app/cxd/code-robotics-1/references/navigation-modes/autonomous-navigation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
