Assumes a NXT robot configuration alike the TriBot.
remotecontrolPanel
and set the BorderStyle
to FixedSingle
.Text
property to "COM".txtComPort
and set the Text
property to your NXT port e.g. 40.btnConnect
and change the Text
property to "Connect".btnDisconnect
and change the
Text
property to "Disconnect".btnIdle
and change the Text
property to "Idle".Click
eventhandlers.MouseMove
eventhandler.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 NxtRemote1 { 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(); } const double dblMaxTurnRatio = 10; private void panel1_MouseMove(object sender, MouseEventArgs e) { if (brick != null && brick.IsConnected) { double dblPower = (-200D / this.remotecontrolPanel.Height) * e.Y + 100; sbyte power = (sbyte) dblPower; double dblTurnRatio = (2 * dblMaxTurnRatio / this.remotecontrolPanel.Width) * e.X - dblMaxTurnRatio; if (dblTurnRatio < -dblMaxTurnRatio) dblTurnRatio = -dblMaxTurnRatio; if (dblTurnRatio > dblMaxTurnRatio) dblTurnRatio = dblMaxTurnRatio; if (power < 0) dblTurnRatio = -dblTurnRatio; sbyte turnRatio = (sbyte) dblTurnRatio; motorPair.Run(power, 0, turnRatio); } } private void btnIdle_Click(object sender, EventArgs e) { Idle(); } private void Idle() { if (brick != null && brick.IsConnected) motorPair.Idle(); } private void Disconnect() { Idle(); if (brick != null && brick.IsConnected) brick.Disconnect(); brick = null; motorPair = null; this.Text = "Disconnected"; } } }