Skip to content

Commit 1d09ff4

Browse files
committed
Create myprogram.cpp
第二次编程作业 学号:U201210712
1 parent a3a7655 commit 1d09ff4

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

U201210712/myprogram.cpp

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#include <iostream>
2+
#include <math.h>
3+
#define PI 3.1415926
4+
using namespace std;
5+
6+
class Point{
7+
private:
8+
double x;
9+
double y;
10+
public:
11+
Point(){}
12+
Point(double a,double b){
13+
x=a;
14+
y=b;
15+
}
16+
double getX(){
17+
return x;
18+
}
19+
double getY(){
20+
return y;
21+
}
22+
void setX(double a){
23+
x=a;
24+
}
25+
void setY(double b){
26+
y=b;
27+
}
28+
29+
};
30+
31+
class Joint{
32+
private:
33+
double deg1;
34+
double deg2;
35+
public:
36+
Joint(){}
37+
Joint(int a,int b){
38+
deg1=a;
39+
deg2=b;
40+
}
41+
void setDeg1(double rad){
42+
deg1=rad*180/PI;
43+
}
44+
void setDeg2(double rad){
45+
deg2=rad*180/PI;
46+
}
47+
double getDeg1(){
48+
return deg1;
49+
}
50+
double getDeg2(){
51+
return deg2;
52+
}
53+
54+
};
55+
56+
class Frame{
57+
private:
58+
Point origin;
59+
double degree;
60+
public:
61+
Frame(){}
62+
Frame(Point orig,double deg){
63+
origin=orig;
64+
degree=deg;
65+
}
66+
Point getPoint(){
67+
return origin;
68+
}
69+
double getDegree(){
70+
return degree;
71+
}
72+
73+
};
74+
75+
class Solver{
76+
private:
77+
Joint joint;
78+
Frame frame;
79+
public:
80+
Point move(Point p1,Point p2){
81+
double tempx=p1.getX()+p2.getX();
82+
double tempy=p1.getY()+p2.getY();
83+
Point point(tempx,tempy);
84+
return point;
85+
}
86+
Point rotate(Point p,double deg){
87+
double tempx;
88+
double tempy;
89+
tempx=p.getX()*cos(PI*deg/180)-p.getY()*sin(PI*deg/180);
90+
tempy=p.getX()*sin(PI*deg/180)+p.getY()*cos(PI*deg/180);
91+
Point point(tempx,tempy);
92+
return point;
93+
}
94+
95+
//将用户坐标系转化为世界坐标系
96+
Point FrameStandardize(Frame fr,Point po){
97+
Point point1=rotate(po,fr.getDegree());
98+
Point point2=move(point1,fr.getPoint());
99+
return point1;
100+
}
101+
//在世界坐标系下反算关节坐标
102+
FrameToJoint(Point po,double arm1,double arm2){
103+
double len= sqrt(po.getX()*po.getX()+po.getY()*po.getY());
104+
105+
if(len>=(arm1+arm2)||len<=abs(arm1-arm2)){
106+
cout<<"坐标超出范围,机器人无法达到"<<endl;
107+
}else{
108+
double rad1=acos((arm1*arm1+len*len-arm2*arm2)/(2*arm1*len));
109+
double rad2=acos((arm1*arm1+arm2*arm2-len*len)/(2*arm1*arm2));
110+
double rad11=atan(po.getY()/po.getX());
111+
double rad22=PI;
112+
joint.setDeg1(rad1+rad11);
113+
joint.setDeg2(rad2+rad22);
114+
cout<<"关节1应转动角度为:"<<joint.getDeg1()<<" 关节2应转动角度为:"<<joint.getDeg2()<<endl;
115+
}
116+
117+
}
118+
//将世界坐标系下的关节坐标还原为用户坐标系
119+
FrameReturn(Frame fr){
120+
}
121+
JointToFrame(Point po){
122+
123+
}
124+
125+
};
126+
class Robot{
127+
private:
128+
double arm1;
129+
double arm2;
130+
double arm1Range;
131+
double arm2Range;
132+
public:
133+
Robot(){}
134+
Robot(double a,double b){
135+
arm1=a;
136+
arm2=b;
137+
}
138+
void PTPmove(Frame fr,Point po){
139+
Solver solver;
140+
Point point;
141+
142+
point=solver.FrameStandardize(fr,po);
143+
solver.FrameToJoint(point,arm1,arm2);
144+
145+
}
146+
};
147+
148+
int main(){
149+
Robot myRobot(4,3); //定义机器人的关节长度
150+
151+
Point origin(0,0);
152+
Point origin1(2,3);
153+
Point origin2(6,1);
154+
Point origin3(5,9); //设置一组坐标系原点在世界坐标系中的坐标值
155+
156+
Point point1(-5,1);
157+
Point point2(-3,-1);
158+
Point point3(4,2);
159+
Point point4(3,7); // 设置每个坐标系中机器人运动目标坐标点
160+
161+
Frame WF(origin,0);
162+
Frame TF1(origin1,30);
163+
Frame TF2(origin2,60);
164+
Frame TF3(origin3,90); //建立世界坐标系与用户坐标系
165+
166+
167+
myRobot.PTPmove(WF,point1);
168+
myRobot.PTPmove(TF1,point2);
169+
myRobot.PTPmove(TF2,point3);
170+
myRobot.PTPmove(TF3,point4);
171+
172+
173+
return 0;
174+
}

0 commit comments

Comments
 (0)