using System;
using System.Collections.Generic;
using System.Text;
using Bram.NxtSharp;
namespace LegoNxtConsoleSample
{
class Program
{
const string c_BluetoothComPort = "COM4";
static void MotorDemo()
{
NxtBrick brick;
brick = new NxtBrick();
brick.COMPortName = c_BluetoothComPort;
brick.Connect();
//select MotorB and turn 180 degrees
NxtMotor motor1;
motor1 = new NxtMotor();
//unfortunately this is necessary, should be wrapped by the API
//e.g. by "AttachMotor"
motor1.Brick = brick;
//that will actually reattach motor to different port
//if port was already set
brick.MotorB = motor1;
//brick.MotorB=motor1 wraps
//brick.AttachMotor(NxtMotorPort.PortB, motor1);
Console.WriteLine("turn motor B 180 degrees backwards");
motor1.Turn(-50, 180);
System.Threading.Thread.Sleep(2000);
//will not stop the motor apruptly
Console.WriteLine("coast motor B");
motor1.Coast();
System.Threading.Thread.Sleep(2000);
Console.WriteLine("turn motor B 180 degrees");
//turns the other direction and let the other motor start after that
motor1.Turn(75, 180);
//attach MotorC in a different way and turn
NxtMotor motor2 = new NxtMotor();
motor2.Brick = brick;
//will also use "AttachMotor" internally
motor2.Port = NxtMotorPort.PortC;
Console.WriteLine("turn motor C until break or coast is sent");
motor2.Turn(60, 0);
System.Threading.Thread.Sleep(2000);
Console.WriteLine("brake motor C");
motor2.Brake();
//break MotorB also if still running
Console.WriteLine("brake motor B if still running");
motor1.Brake();
//use SetOutputState for motor C
Console.WriteLine("turn motor C with granular control, " +
"flip first (should change direction)");
motor2.Flip = !motor2.Flip;
motor2.SetOutputState(50, NxtMotorMode.MotorOn | NxtMotorMode.Regulated,
NxtMotorRegulationMode.MotorSpeed, 0, NxtMotorRunState.Running, 1000);
System.Threading.Thread.Sleep(2000);
Console.WriteLine("brake motor C");
motor2.Brake();
//iterate through Motorports, not depending on connection state
foreach (NxtMotorPort motorPort in brick.MotorPorts())
{
motor1 = brick.GetMotor(motorPort);//returns null when motor not set
if (motor1 != null)
{
Console.WriteLine(motorPort.ToString() + " is connected");
}
else
{
Console.WriteLine(motorPort.ToString() + " is not connected");
}
}
//only motors where the brick and port were set are shown here.
foreach (NxtMotor motor in brick.ConnectedMotors())
{
Console.WriteLine("reset motorposition for motor with port " + motor.Port.ToString());
motor.ResetPosition(false);
}
brick.Disconnect();
}
static void Main(string[] args)
{
MotorDemo();
}
}
}