Skip to menu

Robotics with Object Pascal

MAR (Machine of Attack & Return). V.01

( THIS IS TOY R/C LEVEL DEVICE, NOT A REAL WEAPON )

Inverse Kinematic with AI

2024.01.19 16:31

me Views:81

 

Learn from sample code : https://github.com/OmarJItani/Deep-Neural-Network-for-Solving-the-Inverse-Kinematics-Problem?fbclid=IwAR0PaL39gFw6mYR3mWtUCDjwF0gD_0O8j6lX3U6aG-guFp1IYVbBnZ7txMk

 

* for practive project

ex_01.png

 

 

* for real targeting from drone.

target_01.png

 

####################################################################

Object Pascal Example : from https://youtu.be/mMhF3_8uiv0?si=0nyJwsuO7ZmXyK2M

 

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
   ExtCtrls, ComCtrls, Math;

type
   TArm = record
      r : Integer;
      color : TColor;
      x2,y2, x1, y1, angle_rad  : Single;
   end;


   { TForm1 }

   TForm1 = class(TForm)
      StatusBar1: TStatusBar;
      procedure FormCreate(Sender: TObject);
      procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
   private
      procedure Transform(i : Integer; x,y : Single);

   public
   end;

const maxParts = 10;

var
  Form1: TForm1;
  arm : array[0..maxParts+1] of TArm;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
   arm[0].r:=20;

   arm[0].color:=clRed;
   arm[1].r:=20;
   arm[1].color:=clBlue;
   arm[2].r:=20;
   arm[2].color:=clGreen;
   arm[3].r:=20;
   arm[3].color:=clYellow;
   arm[4].r:=20;
   arm[4].color:=clMaroon;
   arm[5].r:=20;
   arm[5].color:=$FFFF00;
   arm[6].r:=20;
 
  arm[6].color:=$808000;
 
  arm[7].r:=20;
 
  arm[7].color:=$FF00FF;
 
  arm[8].r:=20;
 
  arm[9].r:=20;
 
  arm[10].r:=20;

   canvas.Brush.Color:=clWhite;
   canvas.pen.Width:=5;
   canvas.pen.Color:=clRed;
end;

 

procedure TForm1.Transform(i : Integer; x, y : Single);
begin
   arm[i].angle_rad:=ArcTan2((y-arm[i].y1),(x-arm[i].x1));
   if arm[i].x1 = x then begin
      if y > arm[i].y1 then arm[i].angle_rad := 0.5*pi else arm[i].angle_rad := 1.5*pi;
   end;
   if arm[i].angle_rad<0 then arm[i].angle_rad:=2*pi+arm[i].angle_rad;
   arm[i].x1:=(arm[i].x2)-arm[i].r*cos(arm[i].angle_rad);
   arm[i].y1:=(arm[i].y2)-arm[i].r*sin(arm[i].angle_rad);
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
   var k, t : Integer;
begin
   if ssleft in shift then
   begin

//  canvas.Clear;
   Canvas.FillRect(ClientRect);

   arm[maxParts+1].x1:=x;
   arm[maxParts+1].y1:=y;
   for k:=maxParts downto 0 do begin
      arm[k].x2:=arm[k+1].x1;
      arm[k].y2:=arm[k+1].y1;
      Transform(k,arm[k].x2,arm[k].y2);
      Canvas.Pen.Color:=arm[k].color;
      Canvas.Line(Round(arm[k].x1),Round(arm[k].y1),Round(arm[k].x2),Round(arm[k].y2));
   end;
   {
    arm[1].x2:=x;
    arm[1].y2:=y;
    Transform(1,x,y);

    arm[0].x2:=arm[1].x1;
    arm[0].y2:=arm[1].y1;
    Transform(0,arm[0].x2,arm[0].y2);

    Canvas.FillRect(ClientRect);
    Canvas.Line(Round(arm[1].x1),Round(arm[1].y1),Round(arm[1].x2),Round(arm[1].y2));
    Canvas.Line(Round(arm[0].x1),Round(arm[0].y1),Round(arm[0].x2),Round(arm[0].y2));
   }

   end;
end;

end.