|
颜色分类机器人 NXC程序,自己写的,带有颜色自动分辨功能
如果不想要开头的汉字显示,可注销掉center();这个函数
/*
NXT机器人 分拣程序 版本2.0 boylong制作 2013.10.23 15:55
*/
#include "center.h" //汉字显示
int numb_1 = 0;
int numb_2 = 0;
int numb_3 = 0;
int biaozhi = 1;
#define Null 28 //空积木判断
void Set_all(void); // 启动外围端口设置
void display(int i,int j,int k); //样本数据显示
int fangzhi(int data); //积木放置程序
void good(void); // 样本颜色读取程序
task main()
{
int test;
Set_all();
center(); // 信息显示
good();
display(numb_1,numb_2,numb_3);
while(1){
test = Sensor(IN_3);
if( ( test > (numb_1 - 2) ) && ( test < (numb_1 + 2) )){
PlayToneEx(3000,500,100,0);
fangzhi(1);
}
if( ( test > (numb_2 - 2) ) && ( test < (numb_2 + 2) )){
PlayToneEx(3000,500,100,0);
fangzhi(2);
}
if( ( test > (numb_3 - 2) ) && ( test < (numb_3 + 2) )){
PlayToneEx(3000,500,100,0);
fangzhi(3);
}
}
}
void Set_all(void)
{
SetSensorLight(IN_3,1);
SetSensorTouch(IN_1);
}
void display(int i,int j,int k)
{
TextOut(8,55,"A:");
NumOut(20,55,i);
TextOut(8,35,"B:");
NumOut(20,35,j);
TextOut(8,20,"C:");
NumOut(20,20,k);
}
int fangzhi(int data)
{
if(data == 1){
RotateMotor(OUT_A,70,-360);
RotateMotor(OUT_B,70,320);
RotateMotor(OUT_C,100,-1600);
RotateMotor(OUT_B,70,-320);
return 0;
}
if(data == 2){
RotateMotor(OUT_A,70,-360);
RotateMotor(OUT_B,70,-320);
RotateMotor(OUT_C,100,-1600);
RotateMotor(OUT_B,70,320);
return 0;
}
if(data == 3){
RotateMotor(OUT_A,70,-360);
RotateMotor(OUT_C,100,-1600);
return 0;
}
}
void good(void)
{
int k = 1;
int m = 1;
while(k){
numb_1 = Sensor(IN_3);
if(numb_1 < Null){
TextOut(8,32,"NULL 1");
}
else {
k = 0;
TextOut(8,32,"Detect..1") ;
}
}
fangzhi(1);
PlayToneEx(3000,500,100,0);
k = 1;
m = 1;
while(k){
numb_2 = Sensor(IN_3);
while(m){
if(numb_2 < Null){
TextOut(8,32,"NULL 2");
}
else
m = 0;
}
if((numb_2 > numb_1 - 2) && (numb_2 < numb_1 + 2))
{
PlayToneEx(3000,500,100,0);
fangzhi(1);
TextOut(8,32,"Detect..2") ;
;
}
else{
PlayToneEx(3000,500,100,0);
numb_2 = Sensor(IN_3);
fangzhi(2);
k = 0;
}
}
k = 1;
m = 1;
while(k){
numb_3 = Sensor(IN_3);
while(m){
if(numb_3 < Null){
TextOut(8,32,"NULL 3");
}
else
m = 0;
}
if((numb_3 > numb_1 - 2) && (numb_3 < numb_1 + 2))
{
PlayToneEx(3000,500,100,0);
TextOut(8,32,"Detect..3") ;
fangzhi(1);
}
else{
if((numb_3 > numb_2 - 2) && (numb_3 < numb_2 + 2))
{
PlayToneEx(3000,500,100,0);
TextOut(8,32,"Detect..3") ;
fangzhi(2);
}
else{
PlayToneEx(3000,500,100,0);
numb_3 = Sensor(IN_3);
fangzhi(3);
k = 0;
}
}
}
}
|
|