/// <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) { InfoDialog dialog = new InfoDialog ("Attach a motor to port A", true); dialog.Show ();//Wait for enter to be pressed Motor motor = new Motor (MotorPort.OutA); motor.SetSpeed (50); Thread.Sleep (3000); motor.Off (); Lcd.Instance.Clear (); }
public static void Main(string[] args) { Console.WriteLine ("Resetting the motors"); TouchSensor ts = new TouchSensor(SensorPort.In2); Motor motorFwd = new Motor(MotorPort.OutB); Motor motorTurn = new Motor(MotorPort.OutC); Motor motorSwitch = new Motor(MotorPort.OutA); Motor motorArm = new Motor(MotorPort.OutD); motorArm.On (-60); ButtonEvents buts = new ButtonEvents(); bool keepGoing = true; while (keepGoing) { buts.EscapePressed += () => { keepGoing = false; }; if (ts.IsPressed ()) { keepGoing = false; } } motorArm.Off (); switchMode (); keepGoing = true; TouchSensor ts2 = new TouchSensor(SensorPort.In4); motorArm.On (-60); while (keepGoing) { buts.EscapePressed += () => { keepGoing = false; }; if (ts2.IsPressed ()) { keepGoing = false; } } motorArm.Off (); switchMode (); motorArm.ResetTacho (); motorFwd.ResetTacho (); motorSwitch.ResetTacho (); motorTurn.ResetTacho (); }
public static void switchMode() { Motor motorSwitch = new Motor(MotorPort.OutA); if (extensionMode) { LcdConsole.WriteLine ("Switching to height adjustment"); motorSwitch.On (30, 1550, true); extensionMode = false; } else { LcdConsole.WriteLine ("Switching to extension mode"); motorSwitch.On (-30, 1500, true); extensionMode = true; } motorSwitch.Off (); }
public static void Main(string[] args) { ManualResetEvent terminateProgram = new ManualResetEvent(false); Motor motor = new Motor (MotorPort.OutA); // pull the arm back slightly to the starting position ResetArmLocation (motor); // a loop needs to run to watch the arm and reset the switch if someone is rude enough to push it Task.Factory.StartNew(() => MainLoop(motor)); // Setup the down button to terminate the app ButtonEvents buts = new ButtonEvents (); buts.DownPressed += () => { motor.Off(); terminateProgram.Set(); }; terminateProgram.WaitOne(); }
/// <summary> /// Resets the useless arm to its initial location (down) /// </summary> private static void ResetArmLocation(Motor motor) { LcdConsole.WriteLine ("Resetting the arm"); motor.On (20); // there is a ramp up time for the motor. if the speed is checked // to fast then it will still be zero System.Threading.Thread.Sleep (200); // we'll be using the tachometer to track rotations so lets reset it motor.ResetTacho (); System.Threading.Thread.Sleep (100); while (motor.GetSpeed() > 0) { // just keeps us from moving on until the arm is fully reset // hacky I know } LcdConsole.WriteLine ("Arm reset"); motor.Off(); }
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(); }