Skip to content

Commit 6d3114b

Browse files
committed
Merge pull request RTCSD15#12 from penghuichen/master
Create myprogram.cpp
2 parents 625694b + def7829 commit 6d3114b

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

U201210544/myprogram.cpp

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
 
32+
class Frame{
33+
    private:
34+
        Point origin;
35+
        double degree;
36+
    public:
37+
        Frame(){}
38+
        Frame(Point orig,double deg){
39+
            origin=orig;
40+
            degree=deg;
41+
         }
42+
        Point getPoint(){
43+
            return origin;
44+
        }
45+
        double getDegree(){
46+
            return degree;
47+
        }
48+
          
49+
};
50+
class Joint{
51+
    private:
52+
        double deg1;
53+
        double deg2;
54+
    public:
55+
        Joint(){}
56+
        Joint(int a,int b){
57+
            deg1=a;
58+
            deg2=b;
59+
        }
60+
        void setDeg1(double rad){
61+
            deg1=rad*180/PI;
62+
        }
63+
        void setDeg2(double rad){
64+
            deg2=rad*180/PI;
65+
        }
66+
        double getDeg1(){
67+
            return deg1;
68+
        }
69+
        double getDeg2(){
70+
            return deg2;
71+
        }
72+
          
73+
};
74+
  
75+
  
76+
class Solver{
77+
    private:
78+
        Joint joint;
79+
        Frame frame;
80+
    public:
81+
        Point move(Point p1,Point p2){
82+
            double tempx=p1.getX()+p2.getX();
83+
            double tempy=p1.getY()+p2.getY();
84+
            Point point(tempx,tempy);
85+
            return point;
86+
        }
87+
        Point rotate(Point p,double deg){
88+
            double tempx;
89+
            double tempy;
90+
            tempx=p.getX()*cos(PI*deg/180)-p.getY()*sin(PI*deg/180);
91+
            tempy=p.getX()*sin(PI*deg/180)+p.getY()*cos(PI*deg/180);
92+
            Point point(tempx,tempy);
93+
            return point;
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+
        FrameToJoint(Point po,double arm1,double arm2){
102+
            double len= sqrt(po.getX()*po.getX()+po.getY()*po.getY());
103+
              
104+
            if(len>=(arm1+arm2)||len<=abs(arm1-arm2)){
105+
                cout<<"坐标超范围,机器人无法达到"<<endl;
106+
            }else{
107+
                double rad1=acos((arm1*arm1+len*len-arm2*arm2)/(2*arm1*len));
108+
                double rad2=acos((arm1*arm1+arm2*arm2-len*len)/(2*arm1*arm2));
109+
                double rad11=atan(po.getY()/po.getX());
110+
                double rad22=PI;
111+
                joint.setDeg1(rad1+rad11);
112+
                joint.setDeg2(rad2+rad22);
113+
                cout<<"关节1应转动角度为:"<<joint.getDeg1()<<" 关节2应转动角度为:"<<joint.getDeg2()<<endl;
114+
            }
115+
                      
116+
        }
117+
        FrameReturn(Frame fr){
118+
        }
119+
        JointToFrame(Point po){
120+
              
121+
        }
122+
          
123+
};
124+
class Robot{
125+
    private:
126+
        double arm1;
127+
        double arm2;
128+
        double arm1Range;
129+
        double arm2Range;
130+
    public:
131+
        Robot(){}
132+
        Robot(double a,double b){
133+
            arm1=a;
134+
            arm2=b;
135+
        }
136+
        void PTPmove(Frame fr,Point po){
137+
            Solver solver;
138+
            Point point;
139+
              
140+
            point=solver.FrameStandardize(fr,po);
141+
            solver.FrameToJoint(point,arm1,arm2);
142+
                  
143+
        }
144+
};
145+
   
146+
int main(){
147+
    Robot myRobot(4,3);  
148+
      
149+
    Point origin(0,0);
150+
    Point origin1(2,3);
151+
    Point origin2(6,1);
152+
    Point origin3(5,9);  
153+
      
154+
    Point point1(-5,1);
155+
    Point point2(-3,-1);
156+
    Point point3(4,2);
157+
    Point point4(3,7);  
158+
      
159+
    Frame WF(origin,0);
160+
    Frame TF1(origin1,30);
161+
    Frame TF2(origin2,60);
162+
    Frame TF3(origin3,90);
163+
      
164+
    myRobot.PTPmove(WF,point1);
165+
    myRobot.PTPmove(TF1,point2);
166+
    myRobot.PTPmove(TF2,point3);
167+
    myRobot.PTPmove(TF3,point4);
168+
      
169+
      
170+
    return 0;
171+
}

0 commit comments

Comments
 (0)