/// <summary> /// Repeatedly checks the state of the switch and resets it necessary /// </summary> private static void MainLoop(Motor motor) { var touchSensor = new TouchSensor(SensorPort.In1); while (true) { // there are a few states that the switch and the arm can be in // our default is the arm down, and the switch off // if someone flips the switch, we'll move our arm up until it completes its motion // at that point, the switch will be off again and we'll move the arm back // Our "switch" is off when the touch sensor is pressed (inverse) var switchStatus = !touchSensor.IsPressed (); // When our switch is on, we need to turn it back off if (switchStatus) { // if the motor hasn't moved backwards to push the switch off, we'll do that if (motor.GetTachoCount () > -120) { // LcdConsole.WriteLine ("(On) Extending arm"); motor.On (-20); System.Threading.Thread.Sleep (50); } else { // if it's moved all the way back, stop pushing it // LcdConsole.WriteLine ("(On) Arm fully extended"); motor.Off (); } } else { // When it's off, we're happy // if the arm isn't fully retracted, it's time to put it back to zero if (motor.GetTachoCount () < -20) { // LcdConsole.WriteLine ("(Off) Reset the arm"); motor.On (20); System.Threading.Thread.Sleep (200); } else { // if it is, we're done // LcdConsole.WriteLine ("(Off) Arm fully reset"); motor.Off (); } } if (switchStatus != PreviouslySwitched && !PreviouslySwitched) { Action<string> foo = delegate(string x) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create ("http://crappychatws.azurewebsites.net/api/lego"); request.Proxy = null; request.GetResponse (); LcdConsole.WriteLine(x); }; foo.BeginInvoke ("Someone flipped my switch >_<", null, null); } PreviouslySwitched = switchStatus; } }
public static void Main(string[] args) { Motor motor = new Motor (MotorPort.OutA); motor.ResetTacho(); motor.MoveTo(25,1000,true); System.Threading.Thread.Sleep(3000); Console.WriteLine(motor.GetTachoCount().ToString()); /*motor.MoveTo(75,0,false,true); LcdConsole.WriteLine(motor.GetTachoCount().ToString()); System.Threading.Thread.Sleep(3000); LcdConsole.WriteLine ("Done executing motor test");*/ }
public static void Main (string[] args) { Motor motor = new Motor(MotorPort.OutA); motor.ResetTacho(); PositionPID PID = new PositionPID(motor, 4000, true, 50, P, I, D, 5000); var waitHandle = PID.Run(); Console.WriteLine("Moving motor A to position 4000"); Console.WriteLine("Waiting for controller to finish"); //Wait for controller to finish - you can do other stuff here waitHandle.WaitOne(); Console.WriteLine("Done"); Console.WriteLine("Motor position: " + motor.GetTachoCount()); }
protected override float CalculateError() { float currentPosition = ((float)motor.GetTachoCount()); return((float)(target - currentPosition)); }
/* * This method moves a motor in absolute positions. * Although it can be used with any motor, only the * sensor arm and the cube arm are in fact using it. */ public static void Move(Motor a, int abspos, sbyte speed = 30) { int relpos; WaitHandle handle; relpos = a.GetTachoCount () - abspos; try { if (relpos < 0) handle = a.SpeedProfile (speed, 4, (uint)-relpos-8, 4, true); else handle = a.SpeedProfile ((sbyte)-speed, 4, (uint) relpos-8, 4, true); handle.WaitOne (); } catch (WaitHandleCannotBeOpenedException e) { Console.WriteLine (e.ToString ()); } }
public static RGBColor[] CountFacelets(Motor a, EV3ColorSensor sensor) { RGBColor[] colors = new RGBColor[8]; a.ResetTacho(); for (int count = 0; count < 8;) if (count == a.GetTachoCount () / 45 % 8) colors [count++] = sensor.ReadRGB (); return colors; }
public static void Main (string[] args) { Motor motorA = new Motor (MotorPort.OutA); Motor motorD = new Motor (MotorPort.OutD); WaitHandle motorWaitHandle; motorA.Off(); motorD.Off(); //Power control LcdConsole.WriteLine("Set power to 50"); motorA.SetPower(50); Thread.Sleep(3000); LcdConsole.WriteLine("Break"); motorA.Brake(); //Speed control LcdConsole.WriteLine("Set speed to 50"); motorA.SetSpeed(50); Thread.Sleep(1000); LcdConsole.WriteLine("Speed: " + motorA.GetSpeed()); Thread.Sleep(2000); LcdConsole.WriteLine("Break"); motorA.Brake(); //Position control of single motor Thread.Sleep(3000); motorA.ResetTacho(); LcdConsole.WriteLine("Moving motor A to 2000 "); motorWaitHandle = motorA.SpeedProfile(50, 200, 1600, 200,true); //you could do something else here LcdConsole.WriteLine("Waiting for motor A to stop"); motorWaitHandle.WaitOne(); LcdConsole.WriteLine("Done moving motor"); LcdConsole.WriteLine("Position A: " + motorA.GetTachoCount()); //Individual position control of both motors Thread.Sleep(3000); motorA.ResetTacho(); motorD.ResetTacho(); LcdConsole.WriteLine("Moving motors A to 2000"); LcdConsole.WriteLine("Moving motor B to 1000"); WaitHandle[] handles = new WaitHandle[2]; handles[0] = motorA.SpeedProfile(50, 200, 1600, 200,true); handles[1] = motorD.SpeedProfile(50,200,600,200,true); //you could do something else here LcdConsole.WriteLine("Waiting for both motors to stop"); WaitHandle.WaitAll(handles); LcdConsole.WriteLine("Done moving both motors"); LcdConsole.WriteLine("Position A: " + motorA.GetTachoCount()); LcdConsole.WriteLine("Position D: " + motorD.GetTachoCount()); motorA.Off(); motorD.Off(); //Motor synchronisation position control Thread.Sleep(3000); motorA.ResetTacho(); motorD.ResetTacho(); MotorSync sync = new MotorSync(MotorPort.OutA, MotorPort.OutD); LcdConsole.WriteLine("Sync motors to move 3000 steps forward"); motorWaitHandle = sync.StepSync(40,0, 3000, true); //you could do something else here LcdConsole.WriteLine("Waiting for sync to stop"); motorWaitHandle.WaitOne(); LcdConsole.WriteLine("Done sync moving both motors"); LcdConsole.WriteLine("Position A: " + motorA.GetTachoCount()); LcdConsole.WriteLine("Position D: " + motorD.GetTachoCount()); sync.Off(); }