> For the complete documentation index, see [llms.txt](https://udan-khatola.gitbook.io/geminiflight/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://udan-khatola.gitbook.io/geminiflight/guides/simulation.md).

# Simulation

Should take in URDF, show the drone in a 3D Env, apply real physics to it, useful for PID tuning, should simply then just apply the tune to the airframe. Autotuner can be monetized.

About URDF\
<https://chat.openai.com/share/22cbef87-4c72-483c-bfd9-f5ed9caae2bd>

This is a link to a URDF loader for three.js\
<https://www.npmjs.com/package/urdf-loader>

```javascript
// PID Controller in pure Javascript. To be integrated with threlte
// I'm, sure it can be made to look a lot prettier. This is FUGLY.

function PID(p, i, d) {
  this.sampleTime = 100
  this.target = 0
  this.output = 0

  this.errorSum = 0
  this.lastInput = 0
  this.lastTime = Date.now() - this.sampleTime

  this.setTunings(p, i, d)  
}

PID.prototype.setTunings = function(p, i, d) {
  var ratio = this.sampleTime / 1000
  
  this.p = p
  this.i = i * ratio
  this.d = d / ratio
}

PID.prototype.setSampleTime = function(sampleTime) {
  var ratio = sampleTime / this.sampleTime
  
  this.i *= ratio
  this.d /= ratio
  this.sampleTime = sampleTime
}

PID.prototype.setOutputLimits = function(min, max) {
  this.min = min
  this.max = max
}

PID.prototype.setTarget = function(value) {
  this.target = value
}

PID.prototype.compute = function(input) {
  var now = Date.now(),
      timeDiff = now - this.lastTime
  
  if (timeDiff >= this.sampleTime) {
    var error = this.target - input,
        inputDiff = input - this.lastInput
    
    this.errorSum = Math.max(this.min, Math.min(this.max, this.errorSum + (this.i * error)))    
    this.output = Math.max(this.min, Math.min(this.max, (this.p * error) + this.errorSum - (this.d * inputDiff)))
    this.lastInput = input
    this.lastTime = now
  }
                                              
  return this.output
}
                                              
var pid = new PID(875, 0.5, 0.1)
pid.setSampleTime(1000)
pid.setOutputLimits(0, 10000)
pid.setTarget(60)

setInterval(function() {
  var output = pid.compute(50 + (Math.random() * 10))
  console.log(output)
}, 50)
```


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://udan-khatola.gitbook.io/geminiflight/guides/simulation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
