新手上路
积分 21
金钱 21
注册时间 2016-6-30
在线时间 1 小时
1 金钱
本帖最后由 ShoneX 于 2016-6-30 16:52 编辑
两个项目,一个是从项目,一个是主项目。其中就是某一个子程序的区别。我也仔细看了的,以前没什么问题,但这一次有问题。从项目可以正常使用,但是主项目不行,按复位按钮没反应,而且flash里面不能存数据,重新烧写程序,原来的数据也丢失了。请问这可能是什么原因,首先不是硬件问题,换一个程序就可以用的。下面是程序,后生先谢谢各位前辈解答!
从项目:
#include <stdio.h>
#include <sim908.h>
#include <lcd.h>
#include "stm32f2xx.h"
#include "math.h"
#include "config.h"
#include "stdlib.h"
#include "string.h"
#define PI 3.1415926
#define security_angle (3.1415926/12)
int security_value=3;
typedef struct point
{
float x;
float y;
}Point;
//判断直线是否相交
int lineIntersectSide(Point A, Point B, Point C, Point D)
{
float fC = (C.y - A.y) * (A.x - B.x) - (C.x - A.x) * (A.y - B.y);
float fD = (D.y - A.y) * (A.x - B.x) - (D.x - A.x) * (A.y - B.y);
if(fC * fD <= 0)
return 1;
else
return 0;
}
int sideIntersectSide(Point A, Point B, Point C, Point D)
{
if(lineIntersectSide(A, B, C, D)&&lineIntersectSide(C, D, A, B))
return 1;
else
return 0;
}
int check_intersection(float x1,float y1, float l1,float angle1, float x2,float y2,float l2,float angle2)
{
Point P1,P2,P3,P4,P5,P6;
P1.x=x1; P1.y=y1;
P2.x=x1+l1/cos(security_angle)*cos(angle1-security_angle); P2.y=y1+l1/cos(security_angle)*sin(angle1-security_angle);
P3.x=x1+l1/cos(security_angle)*cos(angle1+security_angle); P3.y=y1+l1/cos(security_angle)*sin(angle1+security_angle);
P4.x=x2; P4.y=y2;
P5.x=x2+l2/cos(security_angle)*cos(angle2-security_angle); P5.y=y2+l2/cos(security_angle)+sin(angle2-security_angle);
P6.x=x2+l2/cos(security_angle)*cos(angle2+security_angle); P6.y=y2+l2/cos(security_angle)*sin(angle2+security_angle);
//判断是否相交
if(
sideIntersectSide(P1, P2, P4, P5)||sideIntersectSide(P1, P2, P4, P6)||sideIntersectSide(P1, P2, P5, P6)
||sideIntersectSide(P2, P3, P4, P5)||sideIntersectSide(P2, P3, P4, P6)||sideIntersectSide(P2, P3, P5, P6)
||sideIntersectSide(P1, P3, P4, P5)||sideIntersectSide(P1, P3, P4, P6)||sideIntersectSide(P1, P3, P5, P6)
)
return 1;
else
return 0;
}
int equal_collision(float x0,float y0,float l0,float angle_0,float x1,float y1,float l1,float angle_1)
{
float D1=0,D2=0;
D1=(fabs(y1+l1*sin(angle_1)-y0-tan(angle_0)*(x1+l1*cos(angle_1)-x0)))/sqrt(1+pow(tan(angle_0),2));
D2=(fabs(y0+l0*sin(angle_0)-y1-tan(angle_1)*(x0+l0*cos(angle_0)-x1)))/sqrt(1+pow(tan(angle_1),2));
printf("D1=%f, D2=%f\r\n",D1,D2);
if( (D1<=security_value) || (D2<=security_value)){
return 1;}
return 0;
}
int unequal_collision(float x0,float y0,float l0,float angle_0,float x1,float y1,float l1,float angle_1)
{
float D1=0;
D1=(fabs(y1+l1*sin(angle_1)-y0-tan(angle_0)*(x1+l1*cos(angle_1)-x0)))/sqrt(1+pow(tan(angle_0),2));
printf("D1=%f\r\n",D1);
if( D1<=security_value )
{return 1;}
return 0;
}
int angle_check(float angle1,float angle2,float actual_angle)
{
if(angle2-angle1>=180)
{
if( (actual_angle*180/3.1415-angle1)*(actual_angle*180/3.1415-angle2) >=0)
return 1;
}
else
{
if( (actual_angle*180/3.1415-angle1)*(actual_angle*180/3.1415-angle2) <=0)
return 1;
}
return 0;
}
float zigbee_angle,zigbee_high,zigbee_amplitude,zigbee_hook=0;
int security_high=5;
void check_collision()
{
if(zigbeemark)
{
int tower_crane_number=0 ;
zigbee_angle=(zigbeemessage[9]*16*16 + zigbeemessage[10])/(float)1000;
zigbee_high=zigbeemessage[12]*16*16+zigbeemessage[13];
zigbee_amplitude=zigbeemessage[14]*16*16+zigbeemessage[15];
zigbee_hook=zigbeemessage[16]*16*16+zigbeemessage[17];
if(zigbeemessage[11]==0x00)
{ float angle0=0;
tower_crane_number=0;
zigbee_angle=fmod(zigbee_angle,6.28)*18/3.1415926;
float_byte_change(zigbee_angle);
show_machine_angle0((16*float_byte[0]+float_byte[1]),(float_byte[2]*16+float_byte[3]));
}
else if(zigbeemessage[11]==0x02)
{ float angle2=0;
tower_crane_number=2;
angle2=fmod(zigbee_angle,6.28)*18/3.1415926;
float_byte_change(angle2);
show_machine_angle2((16*float_byte[0]+float_byte[1]),(float_byte[2]*16+float_byte[3]));
}
else {printf("no such tower!\r\n");}
if(angle_check(intersect_angle1,intersect_angle2,machine_angle)&&angle_check(intersect_angle3,intersect_angle4,zigbee_angle))
{
switch (tower_crane_number)
{
case 0:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
{
if(equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[18],flashresult[19],flashresult[20]/100,zigbee_angle))
collision_mark=1;
}
else
{
if(height_tower_crane>zigbee_high)
{
if(height_tower_crane-machine_hook <= zigbee_high+security_value)
{
printf("original distance = %f\r\n",sqrt(pow(flashresult[15]+machine_amplitude*cos(machine_angle)-flashresult[18],2)+pow(flashresult[16]+machine_amplitude*sin(machine_angle)-flashresult[19],2)));
if(sqrt(pow(flashresult[15]+machine_amplitude*cos(machine_angle)-flashresult[18],2)+pow(flashresult[16]+machine_amplitude*sin(machine_angle)-flashresult[19],2))<= flashresult[20]/100+security_value )
{
if(unequal_collision(flashresult[18],flashresult[19],flashresult[20]/100,zigbee_angle,flashresult[15],flashresult[16],machine_amplitude,machine_angle))
collision_mark=1;
}
}
}
else if(zigbee_high>=height_tower_crane)
{
if(zigbee_high-zigbee_hook<=height_tower_crane+security_value)
{
printf("original distance = %f\r\n",sqrt(pow(flashresult[18]+zigbee_amplitude*cos(zigbee_angle)-flashresult[15],2)+pow(flashresult[19]+zigbee_amplitude*sin(zigbee_angle)-flashresult[16],2)));
if(sqrt(pow(flashresult[18]+zigbee_amplitude*cos(zigbee_angle)-flashresult[15],2)+pow(flashresult[19]+zigbee_amplitude*sin(zigbee_angle)-flashresult[16],2))<= flashresult[17]/100+security_value)
{
if(unequal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[18],flashresult[19],zigbee_amplitude,zigbee_angle))
collision_mark=1;
}
}
}
}
}
break;
case 2:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
{if(equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[21],flashresult[22],flashresult[23]/100,zigbee_angle))
collision_mark=1;}
}
break;
default:
printf("no match tower_crane!\r\n");
break;
}
}
else
{
printf("no intersection!\r\n");
}
}
zigbeemark=0;
zigbee_number=0;
memset(zigbeemessage,0,sizeof(zigbeemessage));
float_byte_change(10*machine_angle);
zigbeemessage[0]=16*float_byte[0]+float_byte[1];
zigbeemessage[1]=16*float_byte[2]+float_byte[3];
zigbeemessage[2]=(char)((int)flashresult[242]);
float_byte_change(machine_high/100);
zigbeemessage[3]=16*float_byte[0]+float_byte[1];
zigbeemessage[4]=16*float_byte[2]+float_byte[3];
float_byte_change(machine_amplitude);
zigbeemessage[5]=16*float_byte[0]+float_byte[1];
zigbeemessage[6]=16*float_byte[2]+float_byte[3];
float_byte_change(machine_hook);
zigbeemessage[7]=16*float_byte[0]+float_byte[1];
zigbeemessage[8]=16*float_byte[2]+float_byte[3];
send_lcd(USART1,zigbeemessage,sizeof(zigbeemessage)/2+1);
memset(zigbeemessage,0,sizeof(zigbeemessage));
}
主项目(问题子程序):
#include <stdio.h>
#include <sim908.h>
#include <lcd.h>
#include "stm32f2xx.h"
#include "math.h"
#include "config.h"
#include "stdlib.h"
#include "string.h"
#include <math.h>
#define PI 3.1415926
#define security_angle (3.1415926/12)
int security_value=3;
typedef struct point
{
float x;
float y;
}Point;
int lineIntersectSide(Point A, Point B, Point C, Point D)
{
float fC = (C.y - A.y) * (A.x - B.x) - (C.x - A.x) * (A.y - B.y);
float fD = (D.y - A.y) * (A.x - B.x) - (D.x - A.x) * (A.y - B.y);
if(fC * fD <= 0)
return 1;
else
return 0;
}
int sideIntersectSide(Point A, Point B, Point C, Point D)
{
if(lineIntersectSide(A, B, C, D)&&lineIntersectSide(C, D, A, B))
return 1;
else
return 0;
}
int check_intersection(float x1,float y1, float l1,float angle1, float x2,float y2,float l2,float angle2)
{
Point P1,P2,P3,P4,P5,P6;
P1.x=x1; P1.y=y1;
P2.x=x1+l1/cos(security_angle)*cos(angle1-security_angle); P2.y=y1+l1/cos(security_angle)*sin(angle1-security_angle);
P3.x=x1+l1/cos(security_angle)*cos(angle1+security_angle); P3.y=y1+l1/cos(security_angle)*sin(angle1+security_angle);
P4.x=x2; P4.y=y2;
P5.x=x2+l2/cos(security_angle)*cos(angle2-security_angle); P5.y=y2+l2/cos(security_angle)+sin(angle2-security_angle);
P6.x=x2+l2/cos(security_angle)*cos(angle2+security_angle); P6.y=y2+l2/cos(security_angle)*sin(angle2+security_angle);
if(
sideIntersectSide(P1, P2, P4, P5)||sideIntersectSide(P1, P2, P4, P6)||sideIntersectSide(P1, P2, P5, P6)
||sideIntersectSide(P2, P3, P4, P5)||sideIntersectSide(P2, P3, P4, P6)||sideIntersectSide(P2, P3, P5, P6)
||sideIntersectSide(P1, P3, P4, P5)||sideIntersectSide(P1, P3, P4, P6)||sideIntersectSide(P1, P3, P5, P6)
)
return 1;
else
return 0;
}
int equal_collision(float x0,float y0,float l0,float angle_0,float x1,float y1,float l1,float angle_1)
{
float D1=0,D2=0;
D1=(fabs(y1+l1*sin(angle_1)-y0-tan(angle_0)*(x1+l1*cos(angle_1)-x0)))/sqrt(1+pow(tan(angle_0),2));
D2=(fabs(y0+l0*sin(angle_0)-y1-tan(angle_1)*(x0+l0*cos(angle_0)-x1)))/sqrt(1+pow(tan(angle_1),2));
printf("euqal_D1=%f, euqal_D1D2=%f\r\n",D1,D2);
if( (D1<=security_value) || (D2<=security_value)){
return 1;}
return 0;
}
int unequal_collision(float x0,float y0,float l0,float angle_0,float x1,float y1,float l1,float angle_1)
{
float D1=0;
D1=(fabs(y1+l1*sin(angle_1)-y0-tan(angle_0)*(x1+l1*cos(angle_1)-x0)))/sqrt(1+pow(tan(angle_0),2));
printf("car to arm =%f\r\n",D1);
if( D1<=security_value )
{return 1;}
return 0;
}
int angle_check(float angle_1,float angle_2,float actual_angle)
{
if(angle_2-angle_1>=180)
{
if( (fmod(actual_angle,2*3.1415)*180/3.1415-angle_1)*( fmod(actual_angle,2*3.1415)*180/3.1415-angle_2) >=0)
return 1;
}
else
{
if( (fmod(actual_angle,2*3.1415)*180/3.1415-angle_1)*( fmod(actual_angle,2*3.1415)*180/3.1415-angle_2) <=0)
return 1;
}
return 0;
}
float zigbee_angle,zigbee_high,zigbee_amplitude,zigbee_hook=0;
int security_high=5;
void check_collision()
{
int tower_crane_number=0;
if(zigbeemark)
{
zigbee_angle=(zigbeemessage[0]*16*16 + zigbeemessage[1])/1000.0;
zigbee_high=zigbeemessage[3]*16*16+zigbeemessage[4];
zigbee_amplitude=zigbeemessage[5]*16*16+zigbeemessage[6]/100.0;
zigbee_hook=(zigbeemessage[7]*16*16+zigbeemessage[8])/100.0;
float_byte_change(10*machine_angle);
zigbeemessage[9]=16*float_byte[0]+float_byte[1];
zigbeemessage[10]=16*float_byte[2]+float_byte[3];
zigbeemessage[11]=(char)((int)flashresult[242]);
float_byte_change(machine_high/100);
zigbeemessage[12]=16*float_byte[0]+float_byte[1];
zigbeemessage[13]=16*float_byte[2]+float_byte[3];
float_byte_change(machine_amplitude);
zigbeemessage[14]=16*float_byte[0]+float_byte[1];
zigbeemessage[15]=16*float_byte[2]+float_byte[3];
float_byte_change(machine_hook);
zigbeemessage[16]=16*float_byte[0]+float_byte[1];
zigbeemessage[17]=16*float_byte[2]+float_byte[3];
send_lcd(USART1,zigbeemessage,sizeof(zigbeemessage));//êÕμ½¼′·¢3ö
if(zigbeemessage[2]==0x01)
{ float angle1=0;
tower_crane_number=1;
//ÆáÄ»1ÏÔ꾽Ƕè
angle1=fmod(zigbee_angle,6.28)*18/3.1415926;
float_byte_change(angle1);
show_machine_angle1((16*float_byte[0]+float_byte[1]),(float_byte[2]*16+float_byte[3]));
}
else if(zigbeemessage[2]==0x02)
{ float angle2=0;
tower_crane_number=2;
angle2=fmod(zigbee_angle,6.28)*18/3.1415926;
float_byte_change(angle2);
show_machine_angle2((16*float_byte[0]+float_byte[1]),(float_byte[2]*16+float_byte[3]));
}
else if(zigbeemessage[2]==0x03)
{tower_crane_number=3;}
else if(zigbeemessage[2]==0x04)
{tower_crane_number=4;}
else if(zigbeemessage[2]==0x05)
{tower_crane_number=5;}
else {printf("no such tower crane!\r\n");}
if (angle_check(intersect_angle1,intersect_angle2,machine_angle) && angle_check(intersect_angle3,intersect_angle4,zigbee_angle) )
{
switch (tower_crane_number)
{
case 1:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
{
if( equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[18],flashresult[19],flashresult[20]/100,zigbee_angle))
collision_mark=1;
}
else
{
if(height_tower_crane>zigbee_high)
{
if(height_tower_crane-machine_hook <= zigbee_high+security_value)
{
printf("car0 to rotate center = %f\r\n",sqrt(pow(flashresult[15]+machine_amplitude*cos(machine_angle)-flashresult[18],2)+pow(flashresult[16]+machine_amplitude*sin(machine_angle)-flashresult[19],2)));
if(sqrt(pow(flashresult[15]+machine_amplitude*cos(machine_angle)-flashresult[18],2)+pow(flashresult[16]+machine_amplitude*sin(machine_angle)-flashresult[19],2))<= flashresult[20]/100+security_value )
{
if(unequal_collision(flashresult[18],flashresult[19],flashresult[20]/100,zigbee_angle,flashresult[15],flashresult[16],machine_amplitude,machine_angle))
collision_mark=1;
}
}
}
else if(zigbee_high>=height_tower_crane)
{
if(zigbee_high-zigbee_hook<=height_tower_crane+security_value)
{
printf("car1 to rotate center = %f\r\n",sqrt(pow(flashresult[18]+zigbee_amplitude*cos(zigbee_angle)-flashresult[15],2)+pow(flashresult[19]+zigbee_amplitude*sin(zigbee_angle)-flashresult[16],2)));
if(sqrt(pow(flashresult[18]+zigbee_amplitude*cos(zigbee_angle)-flashresult[15],2)+pow(flashresult[19]+zigbee_amplitude*sin(zigbee_angle)-flashresult[16],2))<= flashresult[17]/100+security_value)t
{
if(unequal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[18],flashresult[19],zigbee_amplitude,zigbee_angle))
collision_mark=1;
}
}
}
}
}
break;
case 2:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
{if(equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[21],flashresult[22],flashresult[23]/100,zigbee_angle))
collision_mark=1;}
} break;
case 3:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[24],flashresult[25],flashresult[26]/100,zigbee_angle);
} break;
case 4:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[27],flashresult[28],flashresult[29]/100,zigbee_angle);
} break;
case 5:
{
if( fabs( height_tower_crane- zigbee_high)<=security_high )
equal_collision(flashresult[15],flashresult[16],flashresult[17]/100,machine_angle,flashresult[30],flashresult[31],flashresult[32]/100,zigbee_angle/1000);
} break;
default:
printf("no match tower_crane!\r\n");
break;
}
}
else
{
printf("not in commnon place!\r\n");
}
zigbeemark=0;
zigbee_number=0;
}
memset(zigbeemessage,0,sizeof(zigbeemessage));
}
我来回答