Playing with mechanics action control

Everything else that doesn't fall into one of the other PB categories.
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Playing with mechanics action control

Post by Psychophanta »

I wrote some algorithms for the so called P.I.D. to control any mechanical device, like heating resistences, motors, or valves, etc.

To ilustrate it i displayed the speed and the acceleration for each time instant. And at the time, the more efficient algo is the shown in the tip.
However, I am not satisfied because i guess there can be done much better, I mean, the acceleracion must be more soft, and the time must be less , in order to reach the target. So any better formula would be welcome.

The tip is ilustrative to be valid , as theory, for ANY application for real. It shows a body in its position in 2D plane, and with any speed preset, and also, the target position, with any speed preset (normally 0); the body must be positionated into the target position; in the less time, using the less max. acceleration (less motor resource), and showing the softer possible movement.

Code: Select all

Structure D3DXVECTOR3
  x.f
  y.f
  z.f
EndStructure
Structure Vector3D Extends D3DXVECTOR3
  m.f;<-length(modulo)
EndStructure
Macro ProductoEscalar(a,b,ax=x,ay=y,az=z,bx=x,by=y,bz=z)
  (a#\ax#*b#\bx#+a#\ay#*b#\by#+a#\az#*b#\bz#)
EndMacro
Macro getmodulo(v,vx=x,vy=y,vz=z)
  (Sqr#ProductoEscalar(v#,v#,vx#,vy#,vz#,vx#,vy#,vz#))
EndMacro
Procedure DrawVector2D(CX.f,CY.f,X.f,Y.f,color.l=$CCCCCC,long.w=13,transv.w=5)
  Protected modc.f=Sqr(X*X+Y*Y),BX.f=CX+X,BY.f=CY+Y
  Circle(CX,CY,4,$3399AA)
  If X Or Y
    LineXY(CX,CY,BX,BY,color):X/modc:Y/modc
    LineXY(BX,BY,BX-X*long+Y*transv,BY-Y*long-X*transv,color):LineXY(BX,BY,BX-X*long-Y*transv,BY-Y*long+X*transv,color)
  EndIf
EndProcedure
RX.u=800:RY.u=600
InitSprite():InitMouse():InitKeyboard()
OpenWindow(0,0,0,RX,RY,"gestion motriz",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,1,0,0,#PB_Screen_WaitSynchronization)

CreateSprite(0,9,9)
StartDrawing(SpriteOutput(0))
Circle(4,4,4,$3399AA)
StopDrawing()

Macro algo
  DisplayTransparentSprite(0,p\x-4,p\y-4,200,$DE44CF); <- se muestra al objeto en su posición instantánea (en el presente bucle 'tick temporal')
  DisplayTransparentSprite(0,pf\x-4,pf\y-4,200,$DEEACF); <- se muestra al objeto destino en su posición instantánea (en el presente bucle 'tick temporal')
  d\x=pf\x-p\x:d\y=pf\y-p\y:d\m=getmodulo(d); <- distancia entre la posicion instantanea del objeto y la del objeto destino
  d_v\x=d\x-v\x:d_v\y=d\y-v\y:d_v\m=getmodulo(d_v); <- diferencia entre esa distancia y la velocidad anterior
  d_v\x+pfv\x:d_v\y+pfv\y; <- se ajusta a la velocidad que lleva el objeto destino
  ;ALGORITHM:
  if ticks.l=0
    ddv.Vector3D=d_v
  endif
  var.f=Exp(1)-d\m/(ddv\m/6.8541+d\m)
  F\x=d_v\x*var:F\y=d_v\y*var
  ;
  p\x+v\x/divisor:p\y+v\y/divisor; <- implementación del flujo inercial para el objeto
  pf\x+pfv\x/divisor:pf\y+pfv\y/divisor; <- implementación del flujo inercial para el objeto destino
  If StrF(pf\x,precision.a)=StrF(p\x,precision.a) And StrF(pf\y,precision.a)=StrF(p\y,precision.a); <- termina todo cuando se alcanza el objeto destino
    lanza.b=0
  EndIf
  ;
  ac\x=F\x/masa:ac\y=F\y/masa; <- nuevo flujo de aceleración; obtenido a partir de la fuerza aplicada por el algoritmo
  v\x+ac\x:v\y+ac\y; <- nueva velocidad, obtenida al sumar el nuevo flujo de aceleración
  ;
  v\m=getmodulo(v):ac\m=getmodulo(ac); <- las amplitudes de ambos flujos, el de velocidad y el de aceleración
  AddElement(grafica()):grafica()\v=v\m:grafica()\a=ac\m
  ticks.l+1
EndMacro
Structure velocidadesyaceleraciones
  v.f:a.f
EndStructure
NewList grafica.velocidadesyaceleraciones()
Macro init
  p.Vector3D:p\x=30:p\y=185; <- la posicion instantanea del cuerpo
  F.Vector3D:F\x=0:F\y=0; <- vector fuerza aplicada
  ac.Vector3D:ac\x=0:ac\y=0:masa.f=10; <- vector aceleracion y masa del cuerpo
  v.Vector3D:v\x=20:v\y=220:divisor.f=15; <- la velocidad instantanea del cuerpo
  pf.Vector3D:pf\x=730:pf\y=356; <- punto instantaneo de destino
  pfv.Vector3D:pfv\x=0:pfv\y=0; <- velocidad del punto de destino
  d.Vector3D    ; <- vector instantaneo de la distancia entre el punto de destino y la posicion instantanea del cuerpo
  d_v.Vector3D  ; <- d-> - v->
  lanza.b=0:ticks.l=0:ClearList(grafica())
  precision.a=3
EndMacro
init
Repeat
  WaitWindowEvent()
  ExamineMouse():ExamineKeyboard()
  CursorX.f=MouseX():CursorY.f=MouseY():lmb.b=MouseButton(#PB_MouseButton_Left):rmb.b=MouseButton(#PB_MouseButton_Right):mmb.b=MouseButton(#PB_MouseButton_Middle)
  mdx.f=MouseDeltaX():mdy.f=MouseDeltaY():mdz.f=MouseWheel()
  ClearScreen(0)
  StartDrawing(ScreenOutput())
  DrawVector2D(p\x,p\y,v\x,v\y)
  DrawVector2D(p\x,p\y,F\x,F\y,$8899DD)
  DrawVector2D(pf\x,pf\y,pfv\x,pfv\y,$BBBBDD)
  DrawText(0,0,"Location: ("+Str(p\x)+","+Str(p\y)+")",$bbbbbb)
  DrawText(0,20,"Velocity: ("+Str(v\x)+","+Str(v\y)+")",$bbbbbb)
  DrawText(0,40,"ticks: "+Str(ticks.l),$EEE999,$AA7799)
  ;dibujar grafica:
  ForEach grafica()
    Line(RX/3+ListIndex(grafica())*3,RY/3,1,-grafica()\v/4,$bbbbbb)
    Line(RX*2/3+ListIndex(grafica())*3,RY/3,1,-grafica()\a,$33bbbb)
  Next
  ;
  StopDrawing()
  If lmb
    If KeyboardPushed(#PB_Key_LeftShift):pf\x+MouseDeltaX():pf\y+MouseDeltaY()
    Else:p\x+MouseDeltaX():p\y+MouseDeltaY()
    EndIf
  ElseIf rmb
    If KeyboardPushed(#PB_Key_LeftShift):pfv\x+MouseDeltaX():pfv\y+MouseDeltaY()
    Else:v\x+MouseDeltaX():v\y+MouseDeltaY()
    EndIf
  EndIf
  If KeyboardPushed(#PB_Key_LeftControl)
    DisplayTransparentSprite(0,p\x-4,p\y-4,200,$DE44CF)
    p\x+v\x/divisor:p\y+v\y/divisor
  ElseIf KeyboardReleased(#PB_Key_F5):init
  ElseIf KeyboardReleased(#PB_Key_Space):lanza.b=1:ticks.l=0:ClearList(grafica())
  ElseIf lanza.b
    algo
  EndIf
  FlipBuffers():Delay(16)
Until KeyboardPushed(#PB_Key_Escape)
CloseScreen():End
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Playing with mechanics action control

Post by Psychophanta »

Looking for any LQG code, but no success until now.
https://www.youtube.com/watch?v=8jk17dXETHs

Any link would be welcome.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply