## Posts Tagged ‘C#’

### 蛋疼的小球 – 重力感应器小试牛刀

x2的坐标则可以用x2=x1+(t2-t1)*(vx1+vx2)/2 来计算，即位移等于平均速度乘以时间。

```// http://www.diy-robots.com
// apply physics to a ball
Ball ApplyDevicePhysics(GVector gVector, Ball ball, int millisecondsElapsed)
{
int maxX = ClientSize.Width - ball.Radius;
int maxY = ClientSize.Height - ball.Radius;
double t = millisecondsElapsed / 1000.0;

#region calculate x values
gVector.X *= frictionForce;
double v1 = ball.Velocity.X;
double x1 = ball.Position.X;
ball.Velocity.X = v1 + gVector.X * t;
ball.Position.X = x1 + v1 * t + 0.5 * gVector.X * t * t;
{
if (ball.Velocity.X < 0)
{
ball.Velocity.X *= (-elasticRation);
if (gVector.X < 0 && (-ball.Velocity.X / gVector.X) < (t * 2 * elasticRation))
{
ball.Velocity.X = 0;
}
}
}
if (ball.Position.X > maxX)
{
ball.Position.X = maxX;
if (ball.Velocity.X > 0)
{
ball.Velocity.X *= (-elasticRation);
if (gVector.X > 0 && (-ball.Velocity.X / gVector.X) < (t * 2 * elasticRation))
{
ball.Velocity.X = 0;
}
}
}
#endregion

#region calculate y values
gVector.Y *= frictionForce;
v1 = ball.Velocity.Y;
double y1 = ball.Position.Y;
ball.Velocity.Y = v1 + gVector.Y * t;
ball.Position.Y = y1 + v1 * t + 0.5 * gVector.Y * t * t;
{
if (ball.Velocity.Y < 0)
{
ball.Velocity.Y *= (-elasticRation);
if (gVector.Y < 0 && (-ball.Velocity.Y / gVector.Y) < (t * 2 * elasticRation))
{
ball.Velocity.Y = 0;
}
}
}
else if (ball.Position.Y > maxY)
{
ball.Position.Y = maxY;
if (ball.Velocity.Y > 0)
{
ball.Velocity.Y *= (-elasticRation);
if (gVector.Y > 0 && (-ball.Velocity.Y / gVector.Y) < (t * 2 * elasticRation))
{
ball.Velocity.Y = 0;
}
}
}
#endregion
return ball;
}
```

### 解魔方的机器人攻略22 – 蓝牙通讯

1. 蓝牙配对

2. C#中使用蓝牙通讯

```BluetoothConnection = new SerialPort();
ConnectButton.Enabled = false;
BluetoothConnection.PortName = PortList.SelectedItem.ToString();
BluetoothConnection.Open();
```

```private void BlueToothDataSend(byte[] data)
{
int length = data.Length;
byte[] readData = new byte[length + 2];
for (int i = 0; i < length; i++)
{
}
Status = "发送数据字节数：" + length;
}
```

```private void BlueToothDataReceived(object o, SerialDataReceivedEventArgs e)
{

byte[] data = new byte[length];
for (int i = 0; i < length; i++)
{
BlueToothReceivedData += string.Format("data[{0}] = {1}\r\n", i, data[i]);
}
}```

```BluetoothConnection.Close();
BluetoothConnection.Dispose();
BluetoothConnection = null;
```

3. Lejos中使用蓝牙通讯

```public static void Connect() throws Exception
{
LCD.clear();
LCD.drawString("Waiting BTC...",0,0);
btc = Bluetooth.waitForConnection();
LCD.drawString("Connected",0,2);
LCD.refresh();
dis = btc.openDataInputStream();
dos = btc.openDataOutputStream();
}```

```public static byte[] ReadBytes() throws Exception
{
byte[] buffer = new byte[255];
if(length==-2)
{
//lost data, re-sync
return new byte[0];
}
else
{
byte[] data = new byte[length];
for(int i=0;i<length;i++)
{
data[i] = buffer[i];
}
return data;
}
}```

```public static void WriteBytes(byte[] data) throws Exception
{
for(int i=0;i<data.length;i++)
{
dos.writeByte(data[i]);
}
dos.flush();
}```

```public static void Disconnect() throws Exception
{
if(btc!=null)
{
WriteBytes(new byte[]{(byte)255,(byte)255,(byte)255});
dos.close();
dis.close();
btc.close();
}
}```

4. 蓝牙通讯小实验

```BlueTooth.Connect();
byte[] colorData = new byte[6];

while(true)
{
{
switch(action)
{
case 1:
Robot.FixBasePosition();
break;
case 2:
Robot.FixColorSensorPosition();
break;
case 3:
colorData[0] = (byte) color.getRed();
colorData[1] = (byte) color.getGreen();
colorData[2] = (byte) color.getBlue();
colorData[3] = (byte) (color.getRawRed() / 3);
colorData[4] = (byte) (color.getRawGreen() / 3);
colorData[5] = (byte) (color.getRawBlue() / 3);
BlueTooth.WriteBytes(colorData);
break;
default:
BlueTooth.Disconnect();
return;
}
}