Coder Social home page Coder Social logo

javier747belbruno / car-control-simulation Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 530 KB

PID Controller Car Simulator (Demo).

Home Page: https://javier747belbruno.github.io/car-control-simulation

License: MIT License

HTML 13.26% TypeScript 78.30% CSS 8.44%
typescript nodejs cannonjs car simulation datgui

car-control-simulation's Introduction

PID Controller Car Simulator (Demo)

> Go to Page Link


Build The project

Make sure you installed Node.js and Git before start.
Clone the project

git clone https://github.com/Javier747Belbruno/car-control-simulation.git

Change path to Project

cd car-control-simulation

Install dependencies

npm i

Build (Files to /dist)

npm run build

Run in Developer Mode (Changes on the fly)

npm run dev

Run in Developer Mode with Productions Files (Changes on the fly)

npm run serve


Controller Code.
const point = new CANNON.Vec3(0,0,100 ); // Objetive.

const constantsControl = {
  //Kp = Proptional Constant.
  Kp: 480,
//Kd = Derivative Constant.
  Kd: 900,
  //Ki = Integral Constant.
  Ki: 0
};
if(stateProgram==2){
const constFolder = gui.addFolder("Control Constants");
constFolder.add(constantsControl, "Kp", -200, 2000, 10)
constFolder.add(constantsControl, "Ki", -200, 2000, 10)
constFolder.add(constantsControl, "Kd", -200, 2000, 10)
constFolder.open();};

var P , I , D = 0;

var ek_1 = 0; 
var dt = 1/60;
var int = 0;
var u = 0;

function PID_Controller(){
  var refZ = chassisBody.position.z;
  var pointZ = point.z;
  //err = Expected Output - Actual Output;
  var e = refZ - pointZ;
  
  //Proptional action
  P = constantsControl.Kp * e;
  //Differential action
  D = constantsControl.Kd * (e - ek_1) / dt; 
  //Integral action
   I = constantsControl.Ki * int * dt;
  
  // u = Kp * err + (Ki * int * dt) + (Kd * der /dt) //Action
   u = P + I + D; 
  
  //Apply to Car.
  EngineForceVal(u);
  
  //der  = err - err from previous loop; ( i.e. differential error)
  ek_1 = e;
  //int  = int from previous loop + err; ( i.e. integral error )
  int = int + e;
}

Button Configuration:

Key Action
w Accelerator
Space key Brake
s Reverse
a Turn Left
d Turn Right
Number 1 key Chase Camera
Number 2 key Chase Camera Side

How it works? Non-technical Explanation.

Cannon-es is a lightweight 3D physics engine written in JavaScript. All Objects, forces and interactions are simulated here (Cannon-es is Pure maths).

This world is invisible, so we need a library that can create a "skin" for those objects and makes them "visible".

That library is Three.js (JavaScript 3D Library).

Basically we need Lights, Camera and Meshes (Geometry+Material) for see an Scene on the Screen.

For bind that 3D object which have to be same shape and dimensions with the physic object.

We do something like this:

THREE.Scene.skinObject.position.set( CANNON.world.physicObject.position ); //Update all the time.

This way the "skin" follows the body and we can see on screen what is going on in the physics world.


FlowChart

Somehow, this Javascript code works like an Arduino code is works on a microcontroller:

First Setup, then Render (Loop Function).


task list

  • Start Menu.
  • Free Drive.
  • Simulation Changes on the fly (dat.gui).
  • Corrections to PID formula.
  • Create an HTML with all functionality inside (Typescript to Javascript legible).
  • Create a follow path Car Simulation (Maybe is another project).

Credits

Technologies
  • Typescript by Microsoft.

  • Node.js by OpenJS Foundation.

  • Three.js by Mr.doob.

  • Cannon.js (Cannon-es) by Stefan Hedman, Poimandres Community and Marco Fugaro .

  • Dat.gui by Google Data Arts Team.

  • Vite.js by Evan You & Vite Contributors.

    Thanks for provide free open source software to the community making this possible.
Blogs and people that make life easier.
  • Handling Multiple Key Presses at Once in Vanilla JavaScript (for Game Controllers) by Nicky Dover.
  • How to Build a Multiplayer (.io) Web Game by Victor Zhou.
  • Bruno Simon โ€” Portfolio (case study) by Bruno Simon.

car-control-simulation's People

Contributors

javier747belbruno avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.