19,468
社区成员
发帖
与我相关
我的任务
分享
/*REM Design of disk cam profile */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include <conio.h>
#define PI 3.1415926
void sds();
void sdjdj();
void scos();
void ssin();
void zbz(int);
void hds();
void hdjdj();
void hcos();
void hsin();
void dral();
void htl();
float s[360], v[360], a[360], x[360], z[360], x1[360], z1[360];
float r0, e, rr, h, d, d0, ds, d02, ds2, d03, ds3, d01, ds1, pi, p, s0, d1, d2, sn, hn, i, d3, d4, d5;
float maxs, maxv, maxa, yy, ip, d0p, sd, cd, dss, dx, dy, q, ss;
void ingraph();
void ingraph()
{
int gd = 0, gm, ErrorCode;
initgraph(&gd, &gm, "");
ErrorCode = 0;
if (ErrorCode != 0)
{
printf("Crahpics System Error:%s\n", "ErrorCode");
exit(1);
}
setbkcolor(0);
}
int SNG(float va)
{
if (va > 0)return 1;
if (va == 0)return 0;
if (va < 0)return -1;
}
main()
{
maxs = 0;maxv = 0;maxa = 0;
p = PI / 180;
printf("Design of disk cam profile\n");
printf("---------------------\n");
printf("input r0=");scanf("%f", &r0);
printf("input rr=");scanf("%f", &rr);
printf("input e= ");scanf("%f", &e);
printf("input h= ");scanf("%f", &h);
printf("rise motion law sn=");scanf("%f", &sn);
printf("input d0=");scanf("%f", &d0);
printf("input ds=");scanf("%f", &ds);
printf("input d01=");scanf("%f", &d01);
printf("input ds1=");scanf("%f", &ds1);
printf("input d02=");scanf("%f", &d02);
printf("input ds2=");scanf("%f", &ds2);
printf("return motion law hn=");scanf("%f", &hn);
printf("input d03=");scanf("%f", &d03);
printf("input ds3=");scanf("%f", &ds3);
s0 = sqrt(r0*r0 - e*e);
for (i = 0;i <= d0;i++)
{
int sn = 0;
switch (sn)
{
case 1:
sds();break;
case 2:
sdjdj();break;
case 3:
scos();break;
default:
ssin();break;
}
zbz(i);
}
d1 = d0 + ds;
for (i = d0 + 1;i <= d1;i++)
{
int i=0;
s[i] = h;v[i] = 0;a[i] = 0;
zbz(i);
}
d2 = d1 + d01 + ds1;
for (i = d1 + 1;i <= d2;i++)
{
int i=0;
s[i] = h+2;v[i]= 0;a[i] = 0;
zbz(i);
}
d3 = d2 + d02 + ds2;
for (i = d2 + 1;i <= d3;i++)
{
int i=0;
s[i] = h+4;v[i] = 0;a[i] = 0;
zbz(i);
}
d4 = d3 + d03;
for (i = d3 + 1;i <= d4;i++)
{
int hn=0;
switch (hn)
{
case 1:
hds();break;
case 2:
hdjdj();break;
case 3:
hcos();break;
case 4:
hsin();break;
}
zbz(i);
}
d5 = d4 + ds3;
for (i = d4 + 1;i <= d5;i++)
{
int i=0;
s[i] = 0;v[i] = 0;a[i] = 0;
zbz(i);
}
printf("Coodinate of disk cam profile and motion law\n");
printf("-------------------------------------\n");
printf(" i\t x\t z\t\t x1\t z1\t s\t v\t a\n");
for(i=0;i<=360;i=i+30)
{
int i=0;
printf("%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
i, x[i], z[i], x1[i], z1[i], s[i], v[i], a[i]);
if (fabs(s[i]) > maxs) maxs = fabs(s[i]);
if (fabs(v[i]) > maxv) maxv = fabs(v[i]);
if (fabs(a[i]) > maxa) maxa = fabs(a[i]);
}
_getch();
ingraph();
dral();
_getch();
closegraph();
ingraph();
htl();
_getch();
closegraph();
}
void dral()
{
int i;
printf(" s");
printf("\t\tmaxs(mm) =%f\n", maxs);
printf(" v");
printf("\t\tmaxv(m) =%f\n,maxv/1000");
printf(" a");
printf("\t\tmaxa(m) =%f\n,maxa/1000");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\tdlt\n\n");
printf(" 0 90 180 ");
printf(" 270 360\n");
line(0, 240, 380, 240);
line(380, 240, 373, 235);
line(380, 240, 373, 245);
line(10, 480, 10, 0);
line(10, 0, 5, 7);line(10, 0, 15, 7);
putpixel(10, 240 - s[1], 3);
moveto(10, 240 - s[1]);
setcolor(YELLOW);
line(35, 10, 15, 10);
for (i = 0;i <= 360;i++)
lineto(i + 10, 240 - s[i] * maxs*0.4);
putpixel(10, 240 - v[1], 4);
moveto(10, 240 - v[1]);
setcolor(RED);
line(35, 25, 45, 25);
for (i = 0;i <= 360;i++)
lineto(i + 10, 240 - v[i] * maxv*0.4);
putpixel(10, 240 - a[1], 14);
moveto(10, 240 - a[1]);
setcolor(BLUE);
line(35, 40, 45, 40);
for (i = 0;i <= 360;i++)
lineto(i + 10, 240 - a[i] * maxa*0.2);
}
void hcos()
{
int i=0;
s[i] = 0.5*(h+4)*(1 + cos(PI*(i - d3) / d03));
v[i] = -0.5*(h+4)*PI*sin(PI*(i - d3) / d03) / (d03*p);
a[i] = -0.5*(h+4)*PI*PI*cos(PI*(i - d3) / d03) / ((d03*p)*(d03*p));
}
void hdjdj()
{
int i = 0;
if (i <= (d3 + d03 / 2))
{
s[i] = (h + 4)*(1 - 2 * ((i - d3) / d03)*((i - d3) / d03));
v[i] = -4 * (h + 4)*(i - d3)*p / ((d03*p)*(d03*p));
a[i] = -4 * (h + 4) / ((d03*p)*(d03*p));
}
else
{
s[i] = 2 * (h + 4)*(d03 + d3 - i)*(d03 + d3 - i) / (d03*d03);
v[i] = -4 * (h + 4)*(d03 + d3 - i)*p / ((d03*p)*(d03*p));
a[i] = 4 * (h + 4) / ((d03*p)*(d03*p));
}
}
void hds()
{
int i=0;
s[i] = (h + 4)*(1 - (i - d3) / d03);
v[i] = -(h + 4) / (d03*p);
a[i] = 0;
}
void hsin()
{
int i=0;
s[i] = (h + 4)*(1 - (i - d3) / d03 + sin(2 * PI*(i - d3) / d03) / (2 * PI));
v[i] = -(h + 4)*(1 - cos(2 * PI*(i - d3) / d03)) / (d03*p);
a[i] = -2 * PI*(h + 4)*sin(2 * PI*(i - d3) / d03) / ((d03*p)*(d03*p));
}
void htl()
{
line(0, 240, 640, 240);
line(640, 240, 633, 245);
line(640, 240, 633, 235);
line(320, 20, 320, 460);
line(320, 20, 325, 27);
line(320, 20, 315, 27);
circle(320, 240, r0);
circle(320, 240, 5);
line(305, 255, 316, 244);
line(335, 255, 324, 244);
line(303, 255, 337, 255);
line(305, 255, 298, 263);
line(315, 255, 308, 263);
line(325, 255, 318, 263);
line(335, 255, 328, 263);
circle(320, 240, e);
putpixel(320 + e, 240, 3);
int yy = 0;
moveto(320 + e, 240);
while (yy<240)
{
line(320 + e, -yy + 240, 320 + e, -yy + 225);
moveto(320 + e, -yy + 225);
yy = yy + 20;
if (yy>r0) lineto(320 + e, -yy + 240);
}
line(e + 315, 40, e + 315, 90);
line(e + 325, 40, e + 325, 90);
line(e + 315, 50, e + 307, 60);
line(e + 315, 60, e + 307, 70);
line(e + 315, 70, e + 307, 80);
line(e + 315, 80, e + 307, 90);
line(e + 333, 40, e + 325, 50);
line(e + 333, 50, e + 325, 60);
line(e + 333, 60, e + 325, 70);
line(e + 333, 70, e + 325, 80);
putpixel(320 + x[1], 240 - z[1], 3);
moveto(x[1] + 320, -z[1] + 240);
for (i = 0;i<360;i++)
{
int i=0;
lineto(x[i] + 320, -z[i] + 240);
circle(x[i] + 320, -z[i] + 240, rr);
moveto(x[i] + 320, 240 - z[i]);
}int i;
putpixel(x1[1] + 320, -z1[1] + 240, 3);
moveto(x1[1] + 320, -z1[1] + 240);
for (i = 0;i <= 360; i++)
lineto(x1[i] + 320, -z1[i] + 240);
}
void scos()
{
int i=0;
s[i] = 0.5*h*(1 - cos(PI*i / d0));
v[i] = 0.5*h*PI*sin(PI*i / d0) / (d0*p);
a[i] = 0.5*h*PI*PI*cos(PI*i / d0) / ((d0*p)*(d0*p));
}
void sdjdj()
{
int ip, d0p;
ip = i*p;d0p = d0*p;
if (i <= d0 / 2)
{
int i=0;
s[i] = 2 * h*(i / d0)*(i / d0);
v[i] = 4 * h*ip / (d0p*d0p);
a[i] = 4 * h / (d0p*d0p);
}
else
{
int i=0;
s[i] = h - 2 * h*((d0 - i) / d0)*((d0 - i) / d0);
v[i] = 4 * h*(d0 - i)*p / (d0p*d0p);
a[i] = -4 * h / (d0p*d0p);
}
}
void sds()
{
int i=0;
s[i] = h*i / d0;
v[i] = h / (d0*p);
a[i] = 0;
}
void ssin()
{
int i=0;
s[i] = h*(i / d0 - sin(2 * PI*i / d0) / (2 * PI));
v[i] = h*(1 - cos(2 * PI*i / d0)) / (d0*p);
a[i] = 2 * PI*h*sin(2 * PI*i / d0) / ((d0*p)*(d0*p));
}
void zbz(int j)
{
ss = s0 + s[j];d = j*p;
sd = sin(d);cd = cos(d);
x[j] = ss*sd + e*cd;
z[j] = ss*cd - e*sd;
dss = v[j] - e;
dx = dss*sd + ss*cd;
dy = dss*cd - ss*sd;
if (fabs(dy)>1E-10)
{
q = atan(dx / (-dy));
q = q - (SNG(-dy) - 1)*PI / 2;
}
else
{
q = PI / 2;
q = q - (SNG(-dy) - 1)*PI / 2;
}
x1[j] = x[j] - rr*cos(q);
z1[j] = z[j] - rr*sin(q);
}
////////////////////////////////////////////////////////
/*REM kinematics analysis for crank-slider mechanism*/
#define PI 3.1415926f
float Q1[360], Q2[360], w2[360], e2[360], s[360], v[360], a[360];
float maxs=0, maxv=0, maxa=0;
void drawl(float maxx, float maxy, float maxz)
{
int i;
#if 0
printf(" s");
printf("\t\tsmax(m)=%f\n", maxx / 1000);
printf(" v");
printf("\t\tvmax(m)=%f\n", maxy / 1000);
printf(" a");
printf("\t\tamax(m)=%f\n", maxz / 1000);
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\tQ1\n");
printf("%9.4f ", f10 / p);
printf("%9.4f ", 90 + f10 / p);
printf("%9.4f ", 180 + f10 / p);
printf("%9.4f ", f10 / p - 90);
printf("%9.4f ", f10 / p);
#endif
maxx = (float)(480 * 0.4 / maxx);
maxy = (float)(480 * 0.4 / maxy);
maxz = (float)(480 * 0.4 / maxz);
line(0, 240, 420, 240);
line(420, 240, 413, 245);line(420, 240, 413, 235);
line(10, 0, 10, 480);
line(10, 0, 5, 7);line(10, 0, 15, 7);
// red
putpixel(10, 240 -s[1] * maxx , 4);// useless ?
setcolor(4);
line(35, 10, 45, 10);
text(50,0,"s");
moveto(10, 240 - s[1] * maxx);
for (i = 2;i < 360;i++) // a[360] 0->359 !
lineto(i + 10, 240 - s[i] * maxx);
// green
putpixel(10, 240 - v[1] * maxy, 2);
setcolor(2);
line(35, 25, 45, 25);
text(50,16,"v");
moveto(10, 240 - v[1] * maxy);
for (i = 2;i < 360;i++)
lineto(i + 10, 240 - v[i] * maxy);
// blue
putpixel(10, 240 - a[1] * maxz, 1);
setcolor(1);// yellow-> blue
line(35, 40, 45, 40);
text(50,30,"a");
moveto(10, 240 - a[1] * maxz);
for (i = 2;i < 360;i++)
lineto(i + 10, 240 - a[i] * maxz);
}
//
int main()
{
int i;
// printf("input l1=28.5");scanf("%f", &l1);
float l1=28.5;
// printf("input l2=64");scanf("%f", &l2);
float l2=64;
// printf("input e=20");scanf("%f", &e);
float e=20;
// printf("input n=44");scanf("%f", &n);
float n=44;
// printf("\t kinetics analysis for crank - slider mechanism\n");
// printf("\t - - - - - - - - - - - - - - - - - - -\n");
// printf(" Q1\t\tQ2\t w2\t e2\t s\t v\t a\n");
float p = PI / 180.0f;
float w1 = PI*n / 30.0f;
float f = (float)atan(e / sqrt((l2 - l1)*(l2 - l1) - e*e));
float f10 = PI - f;
float xc0 = (float)sqrt((l2 - l1)*(l2 - l1) - e*e);
for (i = 0;i < 360;i++)
{
float f1 = f10 + i*p;
if (f1 > 2 * PI) f1 -= 2 * PI;
Q1[i] = f1;
float sf2 =(float) (-(e + l1*sin(Q1[i])) / l2);
float f2 = (float)atan(sf2 / sqrt(1 - sf2*sf2));
if (f2 > 0) Q2[i] = f2;
else Q2[i] = (float)(2 * PI - fabs(f2));
w2[i] = (float)(-(l1*w1*cos(Q1[i] - Q2[i])) / l2);
e2[i] = (float)(l1*w2[i] * w2[i] * sin(Q1[i] - Q2[i])) / l2;
s[i] = (float)(l1*cos(Q1[i]) + l2*cos(Q2[i]) - xc0);
v[i] = (float)(-l1*w1*sin(Q1[i]) - l2*w2[i] * sin(Q2[i]));
a[i] = (float)(-l1*w1*w1*cos(Q1[i]) - l2*w2[i] * w2[i] * cos(Q2[i]) - l2*e2[i] * sin(Q2[i]));
if((i % 10) == 0)
printf("%9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f\n",
Q1[i]/p, Q2[i]/p, w2[i], e2[i], s[i], v[i]/1000, a[i]/1000);
if (fabs(s[i]) > maxs) maxs = (float)fabs(s[i]);
if (fabs(v[i]) > maxv) maxv = (float)fabs(v[i]);
if (fabs(a[i]) > maxa) maxa = (float)fabs(a[i]);
}
printf("Press return to continue!");
getchar();
cls();
// draw curves
initgraph();
drawl(maxs, maxv, maxa);
getchar();
return 0;
}
// Crank.cpp : Defines the entry point for the console application.
//
//#define _WIN32_WINNT 0x0500 // for ::SetDCPenColor(g_hdc, Colors[color]);
//#include "stdafx.h"
#include <Windows.h>
#include <Wincon.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();
PROCGETCONSOLEWINDOW GetConsoleWindow;
HWND g_hWnd=0;
HDC g_hdc=0;
void line(int x0,int y0,int x1,int y1)
{
MoveToEx(g_hdc,x0,y0,NULL);
LineTo(g_hdc,x1,y1);
}
void moveto(float x0,float y0)
{
MoveToEx(g_hdc,(int)x0,(int)y0,NULL);
}
void lineto(float x1,float y1)
{
LineTo(g_hdc,(int)x1,(int)y1);
}
void text(int x0,int y0,char txt[])
{
TextOut(g_hdc,(int)x0,(int)y0,txt,strlen(txt));
}
/* 0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色
B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色
*/
COLORREF Colors[]={
RGB(0,0,0), // BLACK , //0
RGB(0,0,255), // BLUE , //1
RGB(0,255,0), // GREEN , //2
RGB(0,255,255), // CYAN , //3
RGB(255,0,0), // RED , //4
RGB(255,0,255), // MAGENTA , //5
RGB(0,128,128), // BROWN , //6
RGB(192,192,192),// LIGHTGRAY ,//7
RGB(128,128,128),// DARKGRAY ,//8
RGB(0,0,128), // LIGHTBLUE ,//9
RGB(0,128,0), // LIGHTGREEN ,//10
RGB(0,128,128), // LIGHTCYAN ,//11
RGB(128,0,0), // LIGHTRED ,//12
RGB(255,128,155),// LIGHTMAGENTA,//13
RGB(255,255,0), // YELLOW ,//14
RGB(255,255,255),// WHITE ,//15
};
void setcolor(int color)
{
HPEN hPen,hOldPen;
hPen = CreatePen(PS_SOLID,2,Colors[color]);
hOldPen=(HPEN)SelectObject(g_hdc,HGDIOBJ(hPen));
DeleteObject(hOldPen);
}
void putpixel(float x0,float y0, int color)
{
SetPixel(g_hdc,(int)x0,(int)y0,Colors[color]);
}
void circle(int xc,int yc,int rd)
{
Ellipse(g_hdc,xc-rd,yc-rd,xc+rd,yc+rd);
}
void initgraph()
{// no vscroll bar
HANDLE hConsoleOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwSize = {80, 26};
::SetConsoleScreenBufferSize(hConsoleOutput, dwSize);
SMALL_RECT rc1 = {0, 0, 79, 25};
::SetConsoleWindowInfo(hConsoleOutput, TRUE, &rc1);
// hide caret
CONSOLE_CURSOR_INFO cursor_info = {1, FALSE};
SetConsoleCursorInfo(hConsoleOutput, &cursor_info);
//
SetConsoleTitle("Drawing");
HMODULE hKernel32 = GetModuleHandle("kernel32");
GetConsoleWindow = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
// get handles
g_hWnd=GetConsoleWindow();
g_hdc=GetDC(g_hWnd);
system("color F0");
}
//
void cls()
{
// here's where we'll home the cursor
COORD coordScreen = { 0, 0 };
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;// to get buffer info
DWORD dwConSize;// number of character cells in the current buffer
HANDLE hConsoleOutput;
// get the handle
hConsoleOutput=GetStdHandle(STD_OUTPUT_HANDLE);
// get the number of character cells in the current buffer
bSuccess=GetConsoleScreenBufferInfo(hConsoleOutput,&csbi );
dwConSize=csbi.dwSize.X * csbi.dwSize.Y;
// fill the entire screen with blanks
bSuccess=FillConsoleOutputCharacter(hConsoleOutput,
(TCHAR)' ',dwConSize,
coordScreen,&cCharsWritten);
// get the current text attribute
bSuccess=GetConsoleScreenBufferInfo(hConsoleOutput,&csbi);
// now set the buffer's attributes accordingly
bSuccess=FillConsoleOutputAttribute(hConsoleOutput,
csbi.wAttributes,dwConSize,
coordScreen,&cCharsWritten);
// put the cursor at (0, 0)
bSuccess=SetConsoleCursorPosition(hConsoleOutput,
coordScreen);
return;
}
还有