/// <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 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) { 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 (); }
/// <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(); }