## Posts Tagged ‘机器人’

### 解魔方的机器人攻略28 – 总结

NXT端的程序下载(源代码)

PC端的程序下载(exe)

http://singularityhub.com/2010/02/17/lego-robot-solves-any-rubiks-cube-in-less-than-12-seconds-video/

http://blog.izxg.com/?tag=%E4%B9%90%E9%AB%98

### 解魔方的机器人攻略25 – 解魔方

1，蓝牙连接成功
2，NXT扫描魔方，发送6个面，每个面9块共54组颜色数据到电脑
3，NXT发送一个字节（0xFF）到电脑，表示颜色读取完毕
4，电脑开始计算解法，得到解魔方的步骤，一共N步
5，电脑发送一个字节N到NXT
6，NXT进行从1到N的循环，每次发送一个字节n到电脑，请求第n步操作
7，电脑发送第n步操作给NXT
8，NXT执行完全部N个操作，发送一个字节(0xFE)到电脑，通知解魔方完成
9，电脑清空步骤和颜色数组，准备迎接下一次任务
10，按下Escape按钮，NXT发送三个(0XFF)给电脑，关闭蓝牙连接并退出

```public class CubeCenter
{
public string[] CenterColor = new string[6] { "U", "R", "D", "L", "F", "B" };

public void RotateBottom(bool colockwise)
{
if (colockwise)
{
string n = CenterColor[5];
CenterColor[5] = CenterColor[1];
CenterColor[1] = CenterColor[4];
CenterColor[4] = CenterColor[3];
CenterColor[3] = n;
}
else
{
string n = CenterColor[5];
CenterColor[5] = CenterColor[3];
CenterColor[3] = CenterColor[4];
CenterColor[4] = CenterColor[1];
CenterColor[1] = n;
}
}

public void RotatePaw()
{
//Only can move forward
string n = CenterColor[0];
CenterColor[0] = CenterColor[3];
CenterColor[3] = CenterColor[2];
CenterColor[2] = CenterColor[1];
CenterColor[1] = n;
}

public int FindCenter(string position)
{
int center = -1;
for (int i = 0; i < 6; i++)
{
if (CenterColor[i] == position) center = i;
}
return center;
}
}
```

P: Paw 爪子翻动一次
B：RotateBottom 从底面旋转魔方，后面需要接一个1~3的数字
S：RotateBottomSide 旋转魔方的底面，跟B的区别是这时候爪子抓住上两层，然后旋转底面

```int findSidePosition = CenterStatus.FindCenter(targetSide);

//Rotate to corrent bottom
switch (findSidePosition)
{
case 2:
//Do Nothing
break;
case 1:
CenterStatus.RotatePaw();
break;
case 0:
CenterStatus.RotatePaw();
CenterStatus.RotatePaw();
break;
case 3:
CenterStatus.RotateBottom(true);
CenterStatus.RotateBottom(true);
CenterStatus.RotatePaw();
break;
case 4:
CenterStatus.RotateBottom(true);
CenterStatus.RotatePaw();
break;
case 5:
CenterStatus.RotateBottom(false);
CenterStatus.RotatePaw();
break;
}
Steps[Steps.Count - 1].OrginStep = currentStep;
```

P B3 P S2 B1 P S1

```switch (MoveType)
{
case MoveType.RotatePaw:
return (byte)10;
case MoveType.RotateBottom:
return (byte)(20 + Count);
case MoveType.RotateBottomSide:
return (byte)(30 + Count);
default:
return (byte)0;
}
```

```//Get result
if(step==10)
{
//Rotate paw
Robot.RotatePaw();
}
else if(step>=20 && step<30)
{
//Rotate Bottom
int count = step - 20;
if(count == 3) count = -1;
Robot.RotateBottom(count);
}
else if(step>=30 && step<40)
{
//Rotate Bottom Side
int count = step - 30;
if(count == 3) count = -1;
Robot.RotateBottomSide(count);
}
```

1，用超声波测距传感器（就是那对眼睛）制作“开关”；
2，读色错误，卡住等情况的异常处理
3，语音提示，让萝卜头开口说话
4，暂停功能，帮助我们进行调试

### 解魔方的机器人攻略23 – 识别颜色（上）

1，从NXT发送颜色数据到电脑

```//Send colors to PC
public static void SendColorToPC(int center, int n) throws Exception
{
//get the x,y of n
int y = n % 3;
int x = (n - y) / 3;

//send to PC by bluetooth
byte[] data = new byte[9];
data[0] = (byte)center;    //center表示是魔方的某一面
data[1] = (byte)x;         //x 表示魔方这一面3*3的色块中，第x行的色块
data[2] = (byte)y;         //y 表示魔方这一面3*3的色块中，第y列的色块
data[3] = (byte)color.getRed();
data[4] = (byte)color.getGreen();
data[5] = (byte)color.getBlue();
data[6] = (byte)(color.getRawRed() / 3);
data[7] = (byte)(color.getRawGreen() / 3);
data[8] = (byte)(color.getRawBlue() / 3);
BlueTooth.WriteBytes(data);
}```

2，在PC端接受颜色数据

```else if (length == 9)
{
int i = data[0];
int j = data[1];
int k = data[2];
int r = data[3];
int g = data[4];
int b = data[5];
int rawR = data[6];
int rawG = data[7];
int rawB = data[8];
ColorItem newItem = new ColorItem(i, j, k, r, g, b, rawR, rawG, rawB);
DisplayMessage += newItem.ToString() + "\r\n";
Status = "成功获取数据：" + i + "," + j + "," + k;
}```

3，解析颜色的方案

```Red
[0,1,2]=>RGB=(23,0,0),RawRGB={45,1,8}
[0,2,2]=>RGB=(30,0,0),RawRGB={60,1,5}
[0,2,1]=>RGB=(25,0,0),RawRGB={49,3,12}
[0,2,0]=>RGB=(32,0,0),RawRGB={63,2,6}
[0,1,0]=>RGB=(22,0,0),RawRGB={43,2,11}
[0,0,0]=>RGB=(25,0,0),RawRGB={59,3,3}
[0,0,1]=>RGB=(30,0,0),RawRGB={58,5,17}
[0,0,2]=>RGB=(31,0,0),RawRGB={61,8,17}
[0,1,1]=>RGB=(31,0,0),RawRGB={62,15,22}

Orange
[2,1,2]=>RGB=(28,0,0),RawRGB={55,12,8}
[2,2,1]=>RGB=(30,0,0),RawRGB={57,14,14}
[2,0,1]=>RGB=(32,0,0),RawRGB={62,15,13}
[2,1,0]=>RGB=(32,0,0),RawRGB={63,16,12}
[2,2,2]=>RGB=(42,0,0),RawRGB={83,24,10}
[2,2,0]=>RGB=(41,0,0),RawRGB={82,24,13}
[2,0,0]=>RGB=(41,0,0),RawRGB={80,23,10}
[2,0,2]=>RGB=(39,0,0),RawRGB={76,22,13}
[2,1,1]=>RGB=(41,5,0),RawRGB={81,30,21}```

1，假设RGB三个值的最小值为Min，按Min从大到小排序，前9个是白色
2，剩下的颜色，按照G分量从大到小排序，前9个是黄色（有意思吧，绿色分量最大的是黄色）
3，剩下的颜色，按照B分量从大到小排序，前9个是蓝色（这个还算靠谱）
4，剩下的颜色，按照R分量从小到大排序，前9个是绿色
5，剩下的颜色，按照R+2*RawG-2*RawB从大到小排序，前9个是橙色
6，剩下的颜色全是红色

### 解魔方的机器人攻略21 – 读取魔方颜色

1，依次扫描魔方的6*9=54个色块
2，用蓝牙连接把数据发送到电脑
3，通过颜色分组函数，从读数分辨出不同的颜色

```[0,1,2]=>RGB=(23,0,0),RawRGB={45,1,8}  //红色
[0,2,2]=>RGB=(30,0,0),RawRGB={60,1,5}  //红色
[2,1,2]=>RGB=(28,0,0),RawRGB={55,12,8}  //橙色
[2,2,1]=>RGB=(29,0,0),RawRGB={57,14,14}  //橙色
```

```int[][] idx={
{4,6,7,8,5,2,1,0,3},
{4,0,3,6,7,8,5,2,1},
{4,2,1,0,3,6,7,8,5},
{4,8,5,2,1,0,3,6,7},
{4,2,1,0,3,6,7,8,5},
{4,2,1,0,3,6,7,8,5}};
int[] idx2={5,1,4,3,2,0};```

```	//add offset positions for color sensor motor
static int ColorMotorOffset1 = 33;
static int ColorMotorOffset2 = 9;
static int ColorMotorOffset3 = 18;

//Read each side colors of the cube
{ }

//Read one side by the index
{ }```

### 机械结构 – 一次不成功的尝试

1，木头+山寨工具=误差，精度实在很差，强度也不够
2，污染很大，满屋子一股奇怪的木头味，另外还有噪声污染
3，粗大的件可以做，复杂的小件做不了