kramann.info
© Guido Kramann

Login: Passwort:










kramann.info
© Guido Kramann

Login: Passwort:




Simulation of LUAbot

(EN google-translate)

(PL google-translate)

  • Um LUA-Skripte innerhalb von Java ausführen zu können, wird eine Implementierung von LUA für Java benötigt.
  • Diese ist in Form der Bibliothek luaj verfügbar:
  • Konkret muss die Datei luaj-jse-3.0.2.jar nach /home/fhbstud/processing-3.5.4/core/library/ und /home/fhbstud/processing-4.2/core/library/ kopiert werden.
LUAJ.zip -- enthält luaj-jse-3.0.2.jar
Quelle: https://github.com/luaj/luaj
LUAbot005_LUA.zip -- Processing Project.
function behavior( col, colwall , countdown)
    v=0
    w=0
    if ( (col==1 or colwall==1) and countdown==0) then
        countdown=20        
    end

    if ( countdown>0 ) then
        w=5
        v=0
        countdown = countdown - 1
    else
        w=0
        v=10
    end

    return countdown*10000+v*100+20+w
end

Code 0-1: File behavior.lua

/**
LUA Referenz:
https://www.lua.org/manual/5.1/de/manual.html

*/

LUAbot[] luabot = new LUAbot[8];
LUAbot[] luabotB = new LUAbot[8];

LUA lua = new LUA();

public void setup()
{
    size(640,640);
    frameRate(20);
    for(int i=0;i<luabot.length;i++)
    {
       luabot[i] = new LUAbot(i,80+i*60,80,30.0*i,(i%2)*127+128,((i/2)%2)*127+128,((i/4)%2)*127+128, 
       luabot, lua);
       luabotB[i] = new LUAbot(i,80+i*60,80,30.0*i,(i%2)*127+128,((i/2)%2)*127+128,((i/4)%2)*127+128, 
       luabotB, lua);
    }   
}
public void draw()
{
    scale(1,-1); //coordinate transformation
    translate(0,-640);
    background(255);
    strokeWeight(height/200);
    
    for(int i=0;i<luabot.length;i++)
    {
       luabot[i].draw(0,0);
       luabotB[i].draw(10,0);
       for(int k=0;k<10;k++) //Animation ten times faster
       {
           //luabot[i].behavior();
           luabot[i].behaviorLUA();
           luabot[i].timestep();
           luabotB[i].behavior();
           luabotB[i].timestep();
       }    
    }   
}

Code 0-2: LUAbot005_LUA

public class LUAbot
{
    LUAbot[] bot;
    LUA lua;   
    int index;
  
    float R=20.0;
    float x,y,phi; //position orientation
    float v,w;     //speed pixel per second, angular speed degree per second
    int r,g,b;     //color RED GREEN BLUE
    public LUAbot(int index, float x, float y, float phi, int r, int g, int b, LUAbot[] bot,LUA lua)
    {
        this.index = index;
      
        this.x=x;
        this.y=y;
        this.phi=phi;
        this.r=r;
        this.g=g;
        this.b=b;
        
        this.bot =bot;
        this.lua = lua;
        
        v=0.0;
        w=0.0;
    }

    int COUNTDOWN = 0;
    public void behavior()
    {        
      
        if(   (checkCollision() || checkCollisionWall()) && COUNTDOWN==0)
        {
             COUNTDOWN = 20;
        }
        
        if(COUNTDOWN>0)
        {
             setW(5);
             setV(0);
             COUNTDOWN--;
        }
        else
        {
             setW(0);
             setV(10);
        }
    }

    public void behaviorLUA()
    {
        int col = 0;
        int colWall = 0;
        if(checkCollision())
           col = 1;
        if(checkCollisionWall())
           colWall = 1;
        int[] result = lua.behavior(col,colWall,COUNTDOWN);    
        setV(result[0]);
        setW(result[1]);
        COUNTDOWN = result[2];
        //println("v="+result[0]+" w="+result[1]+" countdown="+result[2]);        
    }
    
    public void setV(int v) //Pixel pro Sekunde
    {
        if(v>20) v=20;
        if(v<-20) v=-20;
        this.v = v*0.05; //Pixel per timestep
    }
    
    public void setW(int w) //Degree per second
    {
        if(w>20) w=20;
        if(w<-20) w=-20;
        this.w = w*0.05; //degree per timestep of 0,05s (1/20s)
    }
    
    public void timestep()
    {
        //Integration angular speed:
        phi+=w;
        //  0<=phi<360:
        if(phi<0.0)
            phi+=(1.0-floor(phi/360.0))*360.0;
        phi=phi - floor(phi/360.0)*360.0;
        float p = phi*PI/180.0;
        //Integration translational speed:
        if(!checkCollision() && !checkCollisionWall()) //if collision no translational movement 
        {
            x+=v*cos(p);
            y+=v*sin(p);
        }    
    }
    
    public boolean checkCollisionWall()
    {
         float p = phi*PI/180.0;
         float vx = cos(p);
         float vy = sin(p);
         if(vx<0.0 && x<=R) return true;         
         if(vx>0.0 && x>=height-R) return true;
         if(vy<0.0 && y<=R) return true;         
         if(vy>0.0 && y>=height-R) return true;
         
         return false;
    }
    
    public boolean checkCollision()
    {
         float p = phi*PI/180.0;
         float vx = cos(p);
         float vy = sin(p);
         for(int i=0;i<bot.length;i++)
         {
             if(i!=index)
             {
                  float dist = sqrt((x-bot[i].x)*(x-bot[i].x)+(y-bot[i].y)*(y-bot[i].y));
                  if(dist<=2.0*R)
                  {
                       float ux = bot[i].x - x;
                       float uy = bot[i].y - y;
                       float uu = sqrt(ux*ux+uy*uy); //normation
                       ux/=uu;
                       uy/=uu;
                       if(vx*ux+vy*uy>=0.0)
                          return true;
                  }
             }
         }
      
         return false;
    }
    
    public void draw(int xoff, int yoff)
    {
        stroke(0);
        fill(r,g,b);
        ellipse(x+xoff,y+yoff,R*2,R*2);
        float p = phi*PI/180.0;
        line(x+xoff,y+yoff,xoff+x+R*cos(p),yoff+y+R*sin(p));
    }
}

Code 0-3: Tab LUAbot

import org.luaj.vm2.*;
import org.luaj.vm2.lib.jse.*;

public class LUA
{
    LuaValue luafunction;
    public LUA()
    {
        Globals globals = JsePlatform.standardGlobals();
        globals.get("dofile").call( LuaValue.valueOf("/home/fhbstud/sketchbook/LUAbot005_LUA/behavior.lua"));
        luafunction = globals.get("behavior");
    }
        
    public int[] behavior(int col, int colWall, int COUNTDOWN)
    {
        int V=0;
        int W=0;
        LuaValue retvals = luafunction.call(LuaValue.valueOf(col), LuaValue.valueOf(colWall),
                           LuaValue.valueOf(COUNTDOWN)); 
        int value = retvals.toint();
        COUNTDOWN = value/10000;
        value-=COUNTDOWN*10000;
        V=value/100;
        value-=V*100;
        W=value-20;
        
        return new int[] {V,W,COUNTDOWN};
    }
    
}

Code 0-4: Tab LUA