1 Star 0 Fork 8

zhaozhenyu2020/XY串联型机械臂算法实现

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
plot.cpp 8.35 KB
一键复制 编辑 原始数据 按行查看 历史
harryzhang 提交于 2020-12-16 18:16 +08:00 . v5.0.0
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
static TCHAR szAppName[] = TEXT("GDI Test");
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox (NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hWnd = CreateWindow(szAppName, // window class name
szAppName, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
1000, // initial x size
500, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//绘制指定属性的直线
static void DrawLine(HDC hDC, int x0, int y0, int x1, int y1, int style, int width, COLORREF color)
{
HPEN hPen = CreatePen(style, width, color);
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
MoveToEx(hDC, x0, y0, NULL);
LineTo(hDC, x1, y1);
SelectObject(hDC, hOldPen);
DeleteObject(hPen);
}
//绘制实心圆
static void DrawCircle(HDC hDC, int x, int y, int len, COLORREF color)
{
HBRUSH hBrush = CreateSolidBrush(color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
HPEN hPen = CreatePen(PS_SOLID, 1, color);
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
Ellipse(hDC, x-len/2, y-len/2, x+len/2, y+len/2);
SelectObject(hDC, hOldBrush);
DeleteObject(hPen);
SelectObject(hDC, hOldPen);
DeleteObject(hOldBrush);
}
//绘制填充矩形
static void DrawRect(HDC hDC, int left, int top, int width, int height, int style, COLORREF color)
{
HBRUSH hBrush = CreateHatchBrush(style, color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
Rectangle(hDC, left, top, left+width, top+height);
SelectObject(hDC, hOldBrush);
DeleteObject(hOldBrush);
}
//绘制位图填充矩形
static void DrawBmpRect(HDC hDC, int left, int top, int width, int height, LPCTSTR file)
{
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
HBRUSH hBrush = CreatePatternBrush(hBitmap);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
Rectangle(hDC, left, top, left+width, top+height);
SelectObject(hDC, hOldBrush);
DeleteObject(hOldBrush);
DeleteObject(hBitmap);
}
const float L1 = 110.0;
const float L2 = 110.0;
const float H = 110.0;
static char buf[200];
typedef struct point{
float x;
float y;
};
typedef struct solution{
point inv;
point solved;
float ORG_X;
float ORG_Y;
float CACL_A;
float CACL_B;
float DeltaX;
bool success;
};
static int getrealx(int x)
{
return x+10;
}
static int getrealy(int y)
{
if(y<0) y=0;
if(y>400-20) y = 400-20;
return 400 - 20 - y;
}
//ax^2+bx+c=0,返回较小方程的根
static solution caclduo(float a,float b,float c)
{
solution solu;
float root = pow(b,2)-4*a*c;
solu.success = true;
if(root>=0 || a!=0)
{
if(root == 0)
{
solu.DeltaX = -b/2*a;
solu.success = true;
goto end;
}
float x1 = (-b+sqrt(root))/(2*a);
float x2 = (-b-sqrt(root))/(2*a);
// cout << "a:" << a << " b:" << b << " c:" << c << " root:" << root << " Dx1:" << x1 << " Dx2:" << x2 << endl;
if(x1>x2)
solu.DeltaX = x2;
else
solu.DeltaX = x1;
}
else
solu.success = false;
end:return solu;
}
static solution invx(solution solu2)
{
solution solu = solu2;
float L1_U,L2_U;
if(solu.CACL_A > solu.CACL_B){
L1_U = L1;
L2_U = L2;
}else{
L1_U = L2;
L2_U = L1;
}
float ax = solu.CACL_A-solu.CACL_B;
float A = pow(H,2)+pow(L1_U,2)-pow(ax,2)-pow(L2_U,2);
float B = 2*H*L1_U;
float C = 2*ax*L2_U;
float a = pow(B,2)*pow(L2_U,2)+pow(C,2)*pow(L1_U,2);
float b = -2*L1_U*A*B*pow(L2_U,2)-2*H*pow(L1_U,2)*pow(C,2);
float c = pow(A,2)*pow(L1_U,2)*pow(L2_U,2)-pow(C,2)*pow(L1_U,2)*pow(L2_U,2)+pow(C,2)*pow(L1_U,2)*pow(H,2);
solution solu3 = caclduo(a,b,c);
if(solu.CACL_A > solu.CACL_B){
solu.solved.x = solu.CACL_B + sqrt(pow(L1,2)-pow(solu3.DeltaX,2));
solu.solved.y = solu3.DeltaX;
}else{
solu.solved.x = solu.CACL_A + sqrt(pow(L2,2)-pow(solu3.DeltaX,2));
solu.solved.y = H-solu3.DeltaX;
}
// cout << sqrt(pow(L2,2)-pow(solu.DeltaX,2)) << "," << sqrt(pow(L1,2)-pow(solu.DeltaX,2)) << "," << org_a << "," << org_b << "," << ax << endl;
return solu;
}
static solution getx(float x,float y)
{
solution solu = caclduo(1,-2*x,pow(x,2)+pow(y,2)-pow(L1,2));
solu.inv.x = x;
solu.inv.y = y;
if(solu.success)
{
x -= solu.DeltaX;
float a1 = atan(y/x);
float sina2 = (H-L1*sin(a1))/L2;
float cosa2 = sqrt(1-pow(sina2,2));
float ax = L2*cosa2;
// cout << "a1:" << a1 << " sina1:" << sin(a1) << " sina2:" << sina2 << " cosa2:" << cosa2 << " ax:" << ax <<" DeltaX:" << solu.DeltaX << endl;
solu.CACL_A = solu.DeltaX + (x-ax);
solu.CACL_B = solu.DeltaX;
}
return solu;
}
static float getdist(float x0,float y0,float x1,float y1)
{
return sqrt(pow(x1-x0,2)+pow(y1-y0,2));
}
#define Ax 250
#define Ay 20
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
solution solu,solu2;
solu = getx(Ax,Ay);
solu2 = invx(solu);
switch (message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
{
hDC = BeginPaint(hWnd, &ps);
DrawLine(hDC, getrealx(0), getrealy(0), getrealx(0), getrealy((int)H), PS_SOLID, 2, RGB(0,255,0));
DrawLine(hDC, getrealx(0), getrealy((int)H), getrealx(800), getrealy((int)H), PS_SOLID, 2, RGB(0,255,0));
DrawLine(hDC, getrealx(800), getrealy((int)H), getrealx(800), getrealy(0), PS_SOLID, 2, RGB(0,255,0));
DrawLine(hDC, getrealx(0), getrealy(0), getrealx(800), getrealy(0), PS_SOLID, 2, RGB(0,255,0));
if(!solu.success) TextOut(hDC, 0, 0, TEXT("坐标错误"), 10);
else{
sprintf(buf,"通过H、L点正解A点:%.01lf,%.01lf",solu2.solved.x,solu2.solved.y);
TextOut(hDC, 0, 80, TEXT(buf), strlen(buf));
sprintf(buf,"H(%.01lf,%.01lf)",solu.CACL_A,H);
TextOut(hDC, getrealx((int)solu.CACL_A), getrealy((int)H+20), TEXT(buf), strlen(buf));
sprintf(buf,"L(%.01lf,%.01lf)",solu.CACL_B,0.0);
TextOut(hDC, getrealx((int)solu.CACL_B), getrealy(0), TEXT(buf), strlen(buf));
sprintf(buf,"A(%d,%d)",Ax,Ay);
TextOut(hDC, getrealx(Ax+1), getrealy(Ay), TEXT(buf), strlen(buf));
sprintf(buf,"DeltaX:%.01lf",solu.DeltaX);
TextOut(hDC, 0, 60, TEXT(buf), strlen(buf));
sprintf(buf,"L2:%.01lf",getdist(Ax,Ay,solu.CACL_A,H));
TextOut(hDC, getrealx((int)(Ax+solu.CACL_A)/2), getrealy((H+Ay)/2+10), TEXT(buf), strlen(buf));
sprintf(buf,"L1:%.01lf",getdist(Ax,Ay,solu.CACL_B,0));
TextOut(hDC, getrealx((int)(Ax+solu.CACL_B)/2), getrealy((Ay)/2), TEXT(buf), strlen(buf));
DrawCircle(hDC, getrealx((int)solu.CACL_A), getrealy((int)H), 10, RGB(0, 0, 255));
DrawCircle(hDC, getrealx((int)solu.CACL_B), getrealy(0), 10, RGB(0, 0, 255));
DrawLine(hDC, getrealx((int)solu.CACL_A), getrealy((int)H),getrealx(Ax), getrealy(Ay), PS_SOLID, 2, RGB(0,0,255));
DrawLine(hDC, getrealx((int)solu.CACL_B), getrealy(0), getrealx(Ax), getrealy(Ay),PS_SOLID, 2, RGB(0,0,255));
DrawCircle(hDC, getrealx(Ax), getrealy(Ay), 10, RGB(255, 0, 0));
}
}
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0 ;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zhaozhenyu2020/harry-xy-robot.git
git@gitee.com:zhaozhenyu2020/harry-xy-robot.git
zhaozhenyu2020
harry-xy-robot
XY串联型机械臂算法实现
master

搜索帮助