Assumes a NXT robot configuration alike the TriBot.
Text
property to "COM".txtComPort, set
its Text property to "40" (or whatever COM port your NXT connects through)
and resize it to fit the text.txtComPort, change its name to btnConnect
and change its Text property to "Connect".btnConnect, change its name to
btnDisconnect and change its Text property to "Disconnect".btnDisconnect, change its name to
btnIdle and change its Text property to "Idle".Anchor property to "Bottom, Left".remoteControlPanel, change
its BorderStyle property to FixedSingle, and resize it
to fill the upper part of the form (see image above).Anchor property to "Top, Bottom, Left, Right".vbarPower.
This scrollbar determines the power of the NXT robot. Change its Minimum
property to -100 (while keeping the Maximum property at 100). Change
its Dock property to Right. hbarTurnRatio.
This scrollbar determines the turnRatio of the NXT robot.
Change its Maximum property to 10 and its Minimum property
to -10. You could set them to 100 and -100, but that would only result in a difficult
steering. You may also consider changing the LargeChange property to
1. Lastly change its Dock property to Bottom.Click eventhandlers.ValueChanged eventhandler on each.Then add the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NKH.MindSqualls;
namespace NxtRemote2
{
public partial class Form1: Form
{
private NxtBrick brick;
private NxtMotorSync motorPair;
public Form1()
{
InitializeComponent();
this.Text = "Disconnected";
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
byte comPort = byte.Parse(this.txtComPort.Text);
brick = new NxtBrick(comPort);
brick.MotorB = new NxtMotor();
brick.MotorC = new NxtMotor();
motorPair = new NxtMotorSync(brick.MotorB, brick.MotorC);
brick.Connect();
this.Text = "Connected: " + brick.Name;
}
catch
{
Disconnect();
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
Disconnect();
}
private void btnIdle_Click(object sender, EventArgs e)
{
Idle();
}
private void Idle()
{
if (brick != null && brick.IsConnected)
motorPair.Idle();
this.vbarPower.Value = 0;
this.hbarTurnRatio.Value = 0;
}
private void Disconnect()
{
Idle();
if (brick != null && brick.IsConnected)
brick.Disconnect();
brick = null;
motorPair = null;
this.Text = "Disconnected";
}
private void hbarTurnRatio_ValueChanged(object sender, EventArgs e)
{
ValueChanged();
}
private void vbarPower_ValueChanged(object sender, EventArgs e)
{
ValueChanged();
}
private void ValueChanged()
{
if (brick != null && brick.IsConnected)
{
sbyte power = (sbyte) (-this.vbarPower.Value);
sbyte turnRatio = (sbyte) this.hbarTurnRatio.Value;
if (power < 0) turnRatio = (sbyte) (-turnRatio);
motorPair.Run(power, 0, turnRatio);
}
}
}
}