#ifndef _XUNXIAN2_
#define _XUNXIAN2_
#include "HardwareInfo.c"
#include <GetSysTime.h>
#include <GetLightSensor.h>
#include <SetMotor.h>
#include <SetDisplayVar.h>
#include <SetWaitForTime.h>
void xunxian2(int speed, int speed1, int gray1, int gray2, int speed2, double time)
{
int P1 = 0;
int P2 = 0;
double time_x_start = 0;
double time_x = 0;
int flag = 0;
double T1 = 0;
double T2 = 0;
double T3 = 0;
double x1 = 0;
double xt = 0;
double x2 = 0;
time_x_start = GetSysTime();
while ( GetSysTime()-time_x_start<time )
{
P1 = GetLightSensor(_P1_);
P2 = GetLightSensor(_P2_);
if ( P1<=gray1&&P2<=gray2 )
{
break;
}
else
{
if ( P1<=gray1&&P2>gray2 )
{
SetMotor(_M1_, speed1);
SetMotor(_M2_, speed);
flag=1;
time_x = GetSysTime();
T1=time_x-time_x_start;
}
else
{
if ( P1>gray1&&P2<=gray2 )
{
SetMotor(_M1_, speed);
SetMotor(_M2_, speed1);
flag=2;
time_x = GetSysTime();
T2=time_x-time_x_start;
}
else
{
if ( P1>gray1&&P2>gray2&&flag==1 )
{
SetMotor(_M1_, speed1);
SetMotor(_M2_, speed2);
time_x = GetSysTime();
T1=time_x-time_x_start;
}
else
{
if ( P1>gray1&&P2>gray2&&flag==2 )
{
SetMotor(_M1_, speed2);
SetMotor(_M2_, speed1);
time_x = GetSysTime();
T2=time_x-time_x_start;
}
else
{
SetMotor(_M1_, speed);
SetMotor(_M2_, speed);
}
}
}
}
}
SetDisplayVar(1, flag, YELLOW, BLACK);
}
time_x = GetSysTime();
T3=time_x-time_x_start;
x1=abs((T1-T2)/2);
if ( flag==1 )
{
x2=T3-T1;
if ( x2-x1>0 )
{
xt=(x2-x1)*1000;
SetMotor(_M1_, speed1);
SetMotor(_M2_, 0);
SetWaitForTime(xt);
}
else
{
if ( x2-x1<0 )
{
xt=(x2-x1)*1000;
SetMotor(_M1_, 0);
SetMotor(_M2_, speed1);
SetWaitForTime(xt);
}
else
{
SetMotor(_M1_, 0);
SetMotor(_M2_, 0);
}
}
}
else
{
if ( flag==2 )
{
x2=T3-T2;
if ( x2-x1>0 )
{
xt=(x2-x1)*1000;
SetMotor(_M1_, 0);
SetMotor(_M2_, speed1);
SetWaitForTime(xt);
}
else
{
if ( x2-x1<0 )
{
xt=(x2-x1)*1000;
SetMotor(_M1_, speed1);
SetMotor(_M2_, 0);
SetWaitForTime(xt);
}
else
{
SetMotor(_M1_, 0);
SetMotor(_M2_, 0);
}
}
}
else
{
SetMotor(_M1_, 0);
SetMotor(_M2_, 0);
}
}
SetMotor(_M1_, 0);
SetMotor(_M2_, 0);
}
#endif
评论0
最新资源