(1)內插
(2)動作內插
(3)T-R-T小考
(4)總複習
T-R-T小考
小考內容:
popMatrix();
glTranslatef(x,y,z);
glRotatef(angle,x,y,z);
glTranslatef(x,y,z);
pushMatrix();
課堂作業一:用excel做內插
Alpha值:0,0.1,0.2.....1
帶入公式:a*new+(1-a)*old
結果如下圖
課堂作業二:用程式將剛剛學的內插法帶到機器人上運用
建立一個貝殼最好設定並輸入程式碼
程式碼:
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
跑出畫面如下

控制機器人手臂6個關節,按鍵盤S鍵保留移動畫面的位置值,
紀錄好所以路徑後再重啟程式,按鍵盤R 機器人就會隨著上次的路徑順序自己動起來

程式碼:
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<7;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
跑出畫面如下

控制機器人手臂6個關節,按鍵盤S鍵保留移動畫面的位置值,
紀錄好所以路徑後再重啟程式,按鍵盤R 機器人就會隨著上次的路徑順序自己動起來



沒有留言:
張貼留言