这个是我该写的原来hans先生的程序,现在利用了新的颜色传感器,实际上程序比原来的还要简单
代码
#define LEFTFACE 0
#define FRONTFACE 1
#define RIGHTFACE 2
#define BACKFACE 3
#define UPPERFACE 4
#define DOWNFACE 5
#define COLOR_SENSOR IN_2
#define OFFSETC 35
int tachoCenter; //转动中心
int color[9]={0,0,0,0,0,0,0,0,0};
int raw[3]={0,0,0};
int norm[3]={0,0,0};
int scaled[3]={0,0,0};
void Tilt()
{
RotateMotorEx(OUT_B,50,40,0,false,false);
RotateMotorEx(OUT_B,100,15,0,false,false);
Wait(800);
RotateMotor(OUT_B,40,10);
RotateMotor(OUT_B,30,-65);
}
void TurnQuarter(bool coast)
{
RotateMotorExPID(OUT_A,100,315,false,false,!coast,40,40,90);
}
int ReadColor(int face)
{
int colorval;
ReadSensorColorEx(COLOR_SENSOR, colorval, raw, norm, scaled);
return colorval;
}
void ScanFace(int face, string title, int preTurn, int postTurn)
{
TextOut(20,LCD_LINE2,title,true);
//Center
RotateMotor(OUT_C,20,tachoCenter - OFFSETC);
color[4]=ReadColor(face);
//Edges
RotateMotor(OUT_C,20,-20);
RotateMotorExPID(OUT_A,100,60+preTurn*315,false,false,true,40,40,90);
color[1]=ReadColor(face);
//TurnQuarter(!colorSensor);
TurnQuarter(false);
color[3]=ReadColor(face);
//TurnQuarter(!colorSensor);
TurnQuarter(false);
color[7]=ReadColor(face);
//TurnQuarter(false);
TurnQuarter(false);
color[5]=ReadColor(face);
//Corners
RotateMotor(OUT_C,20,-10);
RotateMotorExPID(OUT_A,100,160,false,false,true,40,40,90);
color[0]=ReadColor(face);
TurnQuarter(false);
color[2]=ReadColor(face);
TurnQuarter(false);
color[8]=ReadColor(face);
TurnQuarter(false);
color[6]=ReadColor(face);
RotateMotorPID(OUT_A,100,95+postTurn*315,40,40,90);
RotateMotor(OUT_C,20,-1 * tachoCenter + 30 + OFFSETC);
}
void CalibrateSensorPosition()
{
TextOut(0,LCD_LINE1,"Position sensor",true);
TextOut(0,LCD_LINE2,"above center");
TextOut(0,LCD_LINE3,"of cube");
TextOut(0,LCD_LINE5,"Press orange btn");
TextOut(0,LCD_LINE6,"to continue");
while(ButtonPressed(BTNCENTER,false)==0);
PlayTone(500,1);
ClearScreen();
Wait(2000);
ResetTachoCount(OUT_C);
OnRev(OUT_C,10);
until(SENSOR_1 == 1);
Off(OUT_C);
tachoCenter = abs(MotorTachoCount(OUT_C));
NumOut(30,LCD_LINE3,tachoCenter);
RotateMotor(OUT_C,10,OFFSETC);
Wait(2000);
}
void Initialize()
{
SetSensorColorFull(IN_2);
TextOut(0,LCD_LINE1,"color sensor");
SetSensorLowspeed(IN_3);
SetSensorTouch(IN_1);
}
task main()
{
Initialize();
CalibrateSensorPosition();
ScanFace(LEFTFACE,"LEFT FACE",0,0);
NumOut(10,LCD_LINE3,color[0]);
NumOut(20,LCD_LINE3,color[1]);
NumOut(30,LCD_LINE3,color[2]);
NumOut(10,LCD_LINE4,color[3]);
NumOut(20,LCD_LINE4,color[4]);
NumOut(30,LCD_LINE4,color[5]);
NumOut(10,LCD_LINE5,color[6]);
NumOut(20,LCD_LINE5,color[7]);
NumOut(30,LCD_LINE5,color[8]);
Wait(3000);
Tilt();
ScanFace(BACKFACE,"BACK FACE",1,0);
NumOut(10,LCD_LINE3,color[0]);
NumOut(20,LCD_LINE3,color[1]);
NumOut(30,LCD_LINE3,color[2]);
NumOut(10,LCD_LINE4,color[3]);
NumOut(20,LCD_LINE4,color[4]);
NumOut(30,LCD_LINE4,color[5]);
NumOut(10,LCD_LINE5,color[6]);
NumOut(20,LCD_LINE5,color[7]);
NumOut(30,LCD_LINE5,color[8]);
Wait(3000);
Tilt();
ScanFace(RIGHTFACE,"RIGHT FACE",1,3);
NumOut(10,LCD_LINE3,color[0]);
NumOut(20,LCD_LINE3,color[1]);
NumOut(30,LCD_LINE3,color[2]);
NumOut(10,LCD_LINE4,color[3]);
NumOut(20,LCD_LINE4,color[4]);
NumOut(30,LCD_LINE4,color[5]);
NumOut(10,LCD_LINE5,color[6]);
NumOut(20,LCD_LINE5,color[7]);
NumOut(30,LCD_LINE5,color[8]);
Wait(3000);
Tilt();
ScanFace(DOWNFACE,"DOWN FACE",1,1);
NumOut(10,LCD_LINE3,color[0]);
NumOut(20,LCD_LINE3,color[1]);
NumOut(30,LCD_LINE3,color[2]);
NumOut(10,LCD_LINE4,color[3]);
NumOut(20,LCD_LINE4,color[4]);
NumOut(30,LCD_LINE4,color[5]);
NumOut(10,LCD_LINE5,color[6]);
NumOut(20,LCD_LINE5,color[7]);
NumOut(30,LCD_LINE5,color[8]);
Wait(3000);
Tilt();
ScanFace(FRONTFACE,"FRONT FACE",0,1);
NumOut(10,LCD_LINE3,color[0]);
NumOut(20,LCD_LINE3,color[1]);
NumOut(30,LCD_LINE3,color[2]);
NumOut(10,LCD_LINE4,color[3]);
NumOut(20,LCD_LINE4,color[4]);
NumOut(30,LCD_LINE4,color[5]);
NumOut(10,LCD_LINE5,color[6]);
NumOut(20,LCD_LINE5,color[7]);
NumOut(30,LCD_LINE5,color[8]);
Wait(3000);
Tilt();
ScanFace(UPPERFACE,"UPPER FACE",0,0);
NumOut(10,LCD_LINE3,color[0]);
NumOut(20,LCD_LINE3,color[1]);
NumOut(30,LCD_LINE3,color[2]);
NumOut(10,LCD_LINE4,color[3]);
NumOut(20,LCD_LINE4,color[4]);
NumOut(30,LCD_LINE4,color[5]);
NumOut(10,LCD_LINE5,color[6]);
NumOut(20,LCD_LINE5,color[7]);
NumOut(30,LCD_LINE5,color[8]);
Wait(3000);
} |