第二次编程作业

1.建立点类

 1   #ifndef _POINT_H_
 2   #define _POINT_H_
 3
 4  class Point{
 5   public:
 6       double x;
 7       double y;
 8   public:
 9      Point(double a=0,double b=0);
10      void updatePoint(double a,double b);
11  };
12
13  #endif

Point.h

 1 #include"Point.h"
 2   #include<iostream>
 3
 4   Point::Point(double a,double b){
 5      this->x=a;
 6      this->y=b;
 7  }
 8
 9  void Point::updatePoint(double a,double b){
10      this->x=a;
11      this->y=b;
12  }

Point.c

2.建立坐标系类

 1  #ifndef _FRAME_H_
 2  #define _FRAME_H_
 3
 4  #include<Eigen/Dense>
 5  #include<cmath>
 6  #include"Point.h"
 7  using namespace Eigen;
 8
 9  class Frame{
10      protected:
11          Point OrigialPoint,AimPoint;
12          double OrigialAngal;
13      public:
14          Frame(){
15              OrigialPoint.x=0;OrigialPoint.y=0;OrigialAngal=0.0;
16          }
17          Frame(Point OrigialPoint0,double OrigialAngal0=0){
18              this->OrigialPoint=OrigialPoint0;
19              this->OrigialAngal=OrigialAngal0;
20          }
21          void UpdateMatrix(Point Zero){
22              AimPoint=Zero;
23          }
24          Point To_WF(Point twpoint);
25  };
26
27
28  class WorldFrame:public Frame{
29      public:
30          WorldFrame(){
31              OrigialPoint.x=0;OrigialPoint.y=0;OrigialAngal=0.0;
32          }
33          WorldFrame(Point OrigialPoint0,double OrigialAngal0=0);
34          ~WorldFrame(){
35          }
36
37  };
38
39
40
41  class JointFrame:public Frame{
42          public:
43          JointFrame(){
44              OrigialPoint.x=0;OrigialPoint.y=0;OrigialAngal=0.0;
45          }
46          JointFrame(Point OrigialPoint0,double OrigialAngal0=0);
47          ~JointFrame(){
48          }
49
50
51  };
52
53
54
55  class TaskFrame:public Frame{
56          public:
57          TaskFrame(){
58              OrigialPoint.x=0;OrigialPoint.y=0;OrigialAngal=0.0;
59          }
60          TaskFrame(Point OrigialPoint0,double OrigialAngal0=0);
61          ~TaskFrame(){
62          }
63
64
65  };
66
67
68  #endif

Frame.h

 1  #include"Frame.h"
 2
 3  Point Frame::To_WF(Point twpoint){
 4       Matrix2d RotateMatrix;
 5       double OrigialAngal0=OrigialAngal/180*M_PI;
 6       Point wfpoint;
 7       RotateMatrix<< cos(OrigialAngal0),sin(OrigialAngal0),
 8             -sin(OrigialAngal0),cos(OrigialAngal0);
 9       Vector2d v1(twpoint.x,twpoint.y),v2(OrigialPoint.x,OrigialPoint.y),v3;
10       v3=RotateMatrix*v1+v2;
11       wfpoint.updatePoint(v3(0),v3(1));
12       return wfpoint;
13  }
14
15  WorldFrame::WorldFrame(Point OrigialPoint0,double OrigialAngal0):Frame(OrigialPoint0,OrigialAngal0){
16              this->OrigialPoint=OrigialPoint0;
17              this->OrigialAngal=OrigialAngal0;
18          }
19
20 JointFrame::JointFrame(Point OrigialPoint0,double OrigialAngal0):Frame(OrigialPoint0,OrigialAngal0){
21              this->OrigialPoint=OrigialPoint0;
22              this->OrigialAngal=OrigialAngal0;
23          }
24
25 TaskFrame::TaskFrame(Point OrigialPoint0,double OrigialAngal0):Frame(OrigialPoint0,OrigialAngal0){
26              this->OrigialPoint=OrigialPoint0;
27              this->OrigialAngal=OrigialAngal0;
28          }

Frame.c

3.Solver解释器

 1 #ifndef _SOLVER_H_
 2 #define _SOLVER_H_
 3 #include<math.h>
 4 #include"Point.h"
 5 #include"Frame.h"
 6
 7 class Solver{
 8     private:
 9         Point SolverResult;
10     public:
11         Solver();
12         Point PositiveKinematics(double L1,double L2,Point Angles);
13         Point NegativeKinematics(double L1,double L2,Point point);
14         Point FrameToWF(WorldFrame frame,Point AimPoint);
15         Point FrameToWF(JointFrame frame,Point AimPoint);
16         Point FrameToWF(TaskFrame frame,Point AimPoint);
17
18     };
19 #endif

Solver.h

 1 #include"Solver.h"
 2 #include"Point.h"
 3 #include<cmath>
 4
 5 Solver::Solver(){
 6     SolverResult.x=0;
 7     SolverResult.y=0;
 8 }
 9
10 Point Solver::NegativeKinematics(double L1,double L2,Point point){
11     SolverResult.x=0;
12     SolverResult.y=0;
13     SolverResult.updatePoint(SolverResult.x,SolverResult.y);
14     return SolverResult;
15 }
16
17 Point Solver::PositiveKinematics(double L1,double L2,Point Angles){
18     SolverResult.x=L1 *cos(Angles.x/180*M_PI)+L2 *cos(Angles.y/180*M_PI);
19     SolverResult.y=L1 *sin(Angles.x/180*M_PI)+L2 *sin(Angles.y/180*M_PI);
20     SolverResult.updatePoint(SolverResult.x,SolverResult.y);
21     return SolverResult;
22 }
23
24
25
26 Point Solver::FrameToWF(WorldFrame frame,Point AimPoint){
27     return frame.To_WF(AimPoint);}
28 Point Solver::FrameToWF(JointFrame frame,Point AimPoint){
29     return frame.To_WF(AimPoint);}
30 Point Solver::FrameToWF(TaskFrame frame,Point AimPoint){
31     return frame.To_WF(AimPoint);}

Solver.c

4,机器人类

 1 #ifndef _ROBOT_H_
 2 #define _ROBOT_H_
 3 #include"Solver.h"
 4 //#include"Frame.h"
 5 #include <vector>
 6
 7 class Robot{
 8     private:
 9         double JointAngle1,JointAngle2,L1,L2;
10         Point InsightPoint;
11         WorldFrame WF0;
12         JointFrame JF0;
13         TaskFrame  TF0;
14         Solver solver;
15         std::vector<TaskFrame> TaskFrameVec;
16     public:
17         Robot(double length1,double length2){
18             L1=length1;L2=length2;
19         }
20         void PTPmove(WorldFrame f,Point p);
21         void PTPmove(JointFrame f,Point p);
22         void PTPmove(TaskFrame f,Point p);
23         void addTFrame(TaskFrame frame);
24         void deleteFrame();
25     };
26 #endif

Robot.h

 1 #include<iostream>
 2 #include"Robot.h"
 3 using namespace std;
 4
 5 void Robot::PTPmove( WorldFrame f,Point p){
 6     Point WorldframePoint=solver.FrameToWF(f,p);
 7     Point AngleMatrix=solver.NegativeKinematics(L1,L2,WorldframePoint);
 8     Point PKResult=solver.PositiveKinematics(L1,L2,AngleMatrix);
 9     Point WorldframePoint1(0,30);
10     Point AngleMatrix1=solver.NegativeKinematics(L1,L2,WorldframePoint1);
11     InsightPoint.updatePoint(PKResult.x,PKResult.y);
12     cout<<"机械手两关节转角"<<"<"<<AngleMatrix1.x<<","<<AngleMatrix1.y<<">"<<endl;
13     cout<<"点的位置为"<<"<"<<InsightPoint.x<<"," <<InsightPoint.y<<">"<<endl;
14
15 }
16
17 void Robot::PTPmove(JointFrame f,Point p){
18     Point WorldframePoint=solver.FrameToWF(f,p);
19     Point AngleMatrix=solver.NegativeKinematics(L1,L2,WorldframePoint);
20     Point PKResult=solver.PositiveKinematics(L1,L2,AngleMatrix);
21     InsightPoint.updatePoint(PKResult.x,PKResult.y);
22     cout<<"机械手两关节转角"<<"<"<<AngleMatrix.x<<","<<AngleMatrix.y<<">"<<endl;
23     cout<<"点的位置为"<<"<"<<InsightPoint.x<<"," <<InsightPoint.y<<">"<<endl;
24 }
25
26 void Robot::PTPmove(TaskFrame f,Point p){
27     Point WorldframePoint=solver.FrameToWF(f,p);
28     Point AngleMatrix=solver.NegativeKinematics(L1,L2,WorldframePoint);
29     Point PKResult=solver.PositiveKinematics(L1,L2,AngleMatrix);
30     InsightPoint.updatePoint(PKResult.x,PKResult.y);
31     cout<<"机械手两关节转角"<<"<"<<AngleMatrix.x<<","<<AngleMatrix.y<<">"<<endl;
32     cout<<"点的位置为"<<"<"<<PKResult.x<<"," <<PKResult.y<<">"<<endl;
33
34 }
35
36 void Robot::addTFrame(TaskFrame frame){
37     TaskFrameVec.push_back(frame);
38 }
39
40 void Robot::deleteFrame(){
41     TaskFrameVec.pop_back();
42 }
43
44
45     

Robot.c

5 主函数

 1 #include <iostream>
 2 #include "Robot.h"
 3
 4 int main(){
 5     Point one(5,20),two(0,30),three(0,20),a(0,0),c(10,0),d(5,0),e(10,0);
 6     Robot MyRobot(10,20);
 7     WorldFrame wframe(a,0);
 8     JointFrame jframe1(a,0);
 9     JointFrame jframe2(c,0);
10     TaskFrame tframe1(d,0);
11     TaskFrame tframe2(e,90);
12     MyRobot.PTPmove(tframe1,one);
13     MyRobot.PTPmove(wframe,two);
14     MyRobot.PTPmove(jframe1,three);
15 }

main.cpp

6 结果显示

小结:

1 坐标系类还有可以简化的余地;

2 运动学反解没有算出公式,显示的角度值都是0度;

3 机器人的转动角度范围没有确定;

4.建立坐标系类时用了继承,但感觉用的不好,或许没必要用;

时间: 2024-10-28 18:53:20

第二次编程作业的相关文章

【助教】第二次编程作业[初稿]

童鞋们好,通过和老师商量,决定第二次的编程作业要求如下: 1) 从第一次作业出发,将程序改装成一个单机带用户界面(不是控制台)的程序.    2) 我们会列出 8 个可以扩展的方向,相互独立.     3) 老师会让同学两两结对, 根据学号做一个数学映射, 选取 8 个可扩展方向上面的 4个,每个结对的同学提交的一份代码里面必须独立实现这 4 个方向.    4) 两个童鞋只需要一个童鞋将代码提交到博客,代码提交要求参见:http://www.cnblogs.com/greyzeng/p/437

实时控制软件设计第二次编程作业

1 #include <iostream> 2 #include"robot.h" 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 5 int main(int argc, char** argv) { 6 Robot Robot(140,200,6,4); 7 jointframe JF; 8

第二次编程作业-咖啡角机器人

程序的结构模块有: 1.Point类. 1 #ifndef _POINT_H_ 2 #define _POINT_H_ 3 4 5 class Point{ 6 public: 7 double x; 8 double y; 9 public: 10 Point(); 11 Point(double a,const double b); 12 ~Point(); 13 void updatePoint(double a,double b); 14 }; 15 16 17 #endif Point

《实时控制软件设计》第二个编程作业

1 #include <iostream> 2 #include<Eigen/Dense> 3 #include<cmath> 4 #define PI 3.1415926 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 6 using namespace std; 7 using namesp

Coursera machine learning 第二周 编程作业 Linear Regression

必做: [*] warmUpExercise.m - Simple example function in Octave/MATLAB[*] plotData.m - Function to display the dataset[*] computeCost.m - Function to compute the cost of linear regression[*] gradientDescent.m - Function to run gradient descent 1.warmUpE

第二次编程作业(个人非成品,谢绝观赏)

#include<stdlib.h> #include<stdio.h> #include<time.h> int main() { int a,i,n,o,x,y; float z; printf("请输入题目数:"); scanf("%d",&n); while(n--) { srand((unsigned)time(NULL)); x=rand()%100+1; y=rand()%100+1; o=rand()%4;

2016福州大学软件工程第二次结对编程作业成绩

在这里跟大家道个歉,由于国庆节基本都在参加婚礼的路上所以现在才把成绩统计汇总了一下,份子钱太吓人已经把不多的工资吃掉了,这个月要靠泡面度日了.你们可是要好好学习,好好赚钱,好出的起同学的份子钱啊.扯远了,第二次结对编程成绩统计如下: 学号 第二次结对编程 031402233 9.5 031402224 9.5 031402330 9.5 031402516 9 031402524 9 031402304 9 031402509 9 031402341 9 031402508 9 03140232

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.html Exercise 2:Logistic Regression---实现一个逻辑回归 问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学. 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y=1表示录

结对编程作业——电梯调度

作业要求: 现有一新建办公大厦,共有21层,共有四部电梯,所有电梯基本参数如下表所示: 电梯编号 可服务楼层 最大乘客数量 最大载重量 1 全部楼层 10 800 kg 2 单层 10 800 kg 3 双层 20 1600 kg 4 全部楼层 20 2000 kg 其使用规定如下: 1.楼层号为0~20,其中0号为地下一层: 2.有楼层限制的电梯不在响应楼层停靠,如单双层: 3.所有电梯采用统一按钮控制 请根据上述要求设计并实现一个电梯控制程序,如果有图形显示就更好了. 需求分析: 1.共有4