Skip to content

Commit edea0e2

Browse files
author
21HS
committed
Create myprogram.cpp
第二次编程作业 学号:U201210554
1 parent 865344d commit edea0e2

File tree

1 file changed

+341
-0
lines changed

1 file changed

+341
-0
lines changed

U201210554/myprogram.cpp

+341
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
#include "class_dec.h"
2+
#include "auxiliary.h"
3+
4+
myRobot robot;
5+
6+
int main()
7+
{
8+
while(1)
9+
{
10+
robot.Operation();
11+
}
12+
}
13+
14+
/*************************************************************************************************/
15+
/*@note
16+
這個文件里定義了所有的類的成員函數
17+
*/
18+
19+
/**includes***************************************************************************************/
20+
#include "auxiliary.h"
21+
#include "class_dec.h"
22+
23+
/**CS::Fuctions***********************************************************************************/
24+
25+
/*@note
26+
CS的構造函數
27+
*/
28+
CS::CS()
29+
{
30+
strcpy(name,"TCS");
31+
x=0;
32+
y=0;
33+
angle=0;
34+
//mov(3,3);
35+
}
36+
37+
/*@note
38+
用來設定新建坐標系相對於世界坐標系的平移、旋轉量
39+
*/
40+
void CS::Set(char n[11],float a,float b,float c)
41+
{
42+
strcpy(name,n);
43+
x=a;
44+
y=b;
45+
angle=c;
46+
/*mov(0,0)=cos(angle);
47+
mov(0,1)=sin(angle);
48+
mov(0,2)=x;
49+
mov(1,0)=-sin(angle);
50+
mov(1,1)=cos(angle);
51+
mov(1,2)=y;
52+
mov(2,0)=0;
53+
mov(2,1)=0;
54+
mov(2,2)=1;*/
55+
}
56+
57+
/*@note
58+
獲取坐標系名稱
59+
*/
60+
char* CS::GetName()
61+
{
62+
return name;
63+
}
64+
65+
/*@note
66+
插入該坐標系下用戶要求機器人到達的坐標
67+
*/
68+
void CS::Insert(float a,float b)
69+
{
70+
para1=a;
71+
para2=b;
72+
}
73+
74+
/*@note
75+
將該坐標系下的坐標變換至世界坐標系并更新關節坐標系中的坐標值
76+
*/
77+
void CS::Transform()
78+
{
79+
MatrixXd mov(3,3);
80+
mov(0,0)=cos(angle);
81+
mov(0,1)=sin(angle);
82+
mov(0,2)=x;
83+
mov(1,0)=-sin(angle);
84+
mov(1,1)=cos(angle);
85+
mov(1,2)=y;
86+
mov(2,0)=0;
87+
mov(2,1)=0;
88+
mov(2,2)=1;
89+
MatrixXd coor_TCS(3,1);
90+
coor_TCS(0,0)=para1;
91+
coor_TCS(1,0)=para2;
92+
coor_TCS(2,0)=1;
93+
MatrixXd coor_WCS(3,1);
94+
coor_WCS=mov*coor_TCS;
95+
if((coor_WCS(0,0)*coor_WCS(0,0)+coor_WCS(1,0)*coor_WCS(1,0))>=400) //這裡假定機器人兩個手臂的長度都是10,所以半徑20的圓以外的地方是到不了的
96+
{
97+
cout<<"Robot can't reach that point!"<<endl;
98+
}
99+
else
100+
{
101+
cout<<"The coordinates of the Robot in WCS are shown as below:"<<endl<<"("<<coor_WCS(0,0)<<","<<coor_WCS(1,0)<<")"<<endl;
102+
robot.Set(acos(sqrt(coor_WCS(0,0)*coor_WCS(0,0)+coor_WCS(1,0)*coor_WCS(1,0))/20)+atan(coor_WCS(1,0)/coor_WCS(0,0)),\
103+
2*(90-acos(sqrt(coor_WCS(0,0)*coor_WCS(0,0)+coor_WCS(1,0)*coor_WCS(1,0))/20)));
104+
}
105+
}
106+
107+
/**myRobot::Functions*****************************************************************************/
108+
109+
/*@note
110+
構造函數
111+
*/
112+
myRobot::myRobot()
113+
{
114+
angle1=90;
115+
angle2=180;
116+
CS* p;
117+
p=new CS;
118+
char str[3]={'W','C','S'};
119+
p->Set(str,0,0,0);
120+
cs_vector.push_back(*p);
121+
}
122+
123+
/*@note
124+
更新關節坐標系下的坐標值
125+
*/
126+
void myRobot::Set(float a,float b)
127+
{
128+
angle1=a;
129+
angle2=b;
130+
}
131+
132+
/*@note
133+
判斷用戶是新建坐標系還是移動機器人
134+
*/
135+
void myRobot::Operation()
136+
{
137+
input=Input();
138+
if(strcmp(input.OperationType,"S")==0)
139+
{
140+
CS* p;
141+
p=new CS;
142+
p->Set(input.CSName,input.para1,input.para2,input.para3);
143+
cs_vector.push_back(*p);
144+
}
145+
else
146+
{
147+
PTPMove();
148+
}
149+
}
150+
151+
/*@note
152+
實現坐標變換并更新關節坐標系下坐標
153+
*/
154+
void myRobot::PTPMove()
155+
{
156+
for(it=cs_vector.begin();it!=cs_vector.end();it++)
157+
{
158+
if(strcmp(it->GetName(),input.CSName)==0)
159+
{
160+
it->Insert(input.para1,input.para2);
161+
it->Transform();
162+
Show();
163+
break;
164+
}
165+
}
166+
}
167+
168+
/*@note
169+
顯示關節坐標系下坐標
170+
*/
171+
void myRobot::Show()
172+
{
173+
cout<<"The coordinates of the Robot in Joint_CS are shown as below:"<<endl<<"("<<angle1<<","<<angle2<<")"<<endl;
174+
}
175+
176+
#ifndef __CLASS_DEC_H
177+
#define __CLASS_DEC_H
178+
179+
#include <iostream>
180+
#include <vector>
181+
#include <Eigen/Dense>
182+
#include <math.h>
183+
184+
using namespace std;
185+
using Eigen::MatrixXd;
186+
187+
class CS
188+
{
189+
public:
190+
CS();
191+
void Set(char n[11],float a,float b,float c);
192+
char* GetName();
193+
void Insert(float a,float b);
194+
void Transform();
195+
protected:
196+
char name[11];
197+
float x;
198+
float y;
199+
float angle;
200+
//MatrixXd mov;
201+
float para1;
202+
float para2;
203+
};
204+
205+
class myRobot
206+
{
207+
public:
208+
myRobot();
209+
void Set(float a,float b);
210+
void Operation();
211+
void PTPMove();
212+
void Show();
213+
protected:
214+
float angle1;
215+
float angle2;
216+
vector<CS> cs_vector;
217+
vector<CS>::iterator it;
218+
};
219+
220+
extern myRobot robot;
221+
222+
#endif
223+
224+
#include "auxiliary.h"
225+
226+
InputCom input;
227+
228+
InputCom Input()
229+
{
230+
char str[11];
231+
InputCom input;
232+
cout<<"Input OperationType 'S'or'M':"<<endl;
233+
cin>>str;
234+
strcpy(input.OperationType,str);
235+
cout<<"Input CSName:"<<endl;
236+
cin>>str;
237+
strcpy(input.CSName,str);
238+
cout<<"Input para1:"<<endl;
239+
cin>>str;
240+
input.para1=Extraction(str);
241+
cout<<"Input para2:"<<endl;
242+
cin>>str;
243+
input.para2=Extraction(str);
244+
if(strcmp(input.OperationType,"S")==0)
245+
{
246+
cout<<"Input para3:"<<endl;
247+
cin>>str;
248+
input.para3=Extraction(str);
249+
return input;
250+
}
251+
else
252+
{
253+
input.para3=0;
254+
return input;
255+
}
256+
}
257+
258+
float Extraction(char str[11])
259+
{
260+
int i,k;
261+
float a=0;
262+
for(i=0;str[i]!='\0';i++)
263+
{
264+
if(str[i]=='.')
265+
{
266+
k=i;
267+
}
268+
}
269+
if(str[0]=='-')
270+
{
271+
for(i=0;i<k-1;i++)
272+
{
273+
a+=((float)str[k-i-1]-48)*Index1(i);
274+
}
275+
for(i=1;str[k+i]!='\0';i++)
276+
{
277+
a+=((float)str[k+i]-48)*Index2(i);
278+
}
279+
}
280+
else
281+
{
282+
for(i=0;i<k;i++)
283+
{
284+
a+=((float)str[k-i-1]-48)*Index1(i);
285+
}
286+
for(i=1;str[k+i]!='\0';i++)
287+
{
288+
a+=((float)str[k+i]-48)*Index2(i);
289+
}
290+
}
291+
return a;
292+
}
293+
294+
float Index1(int n)
295+
{
296+
float a=1,i;
297+
for(i=n;i!=0;i--)
298+
{
299+
a*=10;
300+
}
301+
return a;
302+
}
303+
304+
float Index2(int n)
305+
{
306+
float a=1,i;
307+
for(i=n;i!=0;i--)
308+
{
309+
a*=0.1;
310+
}
311+
return a;
312+
}
313+
314+
#ifndef __AUXILIARY_H
315+
#define __AUXILIARY_H
316+
317+
#include <iostream>
318+
#include <string.h>
319+
320+
using namespace std;
321+
322+
/*@note
323+
用於存儲用戶輸入的內容
324+
*/
325+
typedef struct
326+
{
327+
char OperationType[11];
328+
char CSName[11];
329+
float para1;
330+
float para2;
331+
float para3;
332+
}InputCom;
333+
334+
extern InputCom input;
335+
336+
InputCom Input();
337+
float Extraction(char str[40]);
338+
float Index1(int n);
339+
float Index2(int n);
340+
341+
#endif

0 commit comments

Comments
 (0)