private static string UID = "XYZ"; // Change XYZ to the UID of your Distance IR Bricklet

    #endregion Fields

    #region Methods

    static void Main()
    {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        BrickletDistanceIR dir = new BrickletDistanceIR(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT); // Connect to brickd
        // Don't use device before ipcon is connected

        // Get current distance (unit is mm)
        int distance = dir.GetDistance();
        Console.WriteLine("Distance: " + distance/10.0 + " cm");

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
Exemplo n.º 2
0
        private void InitializeDevices()
        {
            Connection = new IPConnection();
            Connection.Connect("localhost", 4223);

            ServoBrick = new BrickServo("ap6zRki6edN", Connection);
            SetupServoBrick();
            DistanceBricklet = new BrickletDistanceIR("8XU", Connection);
            var distanceCollection = new ClosestDistanceSensorCollection();
            distanceCollection.AddSensor(new ImmediateDistanceIRSensor(DistanceBricklet));
            Distances = distanceCollection;

            var drivingStrategy = new SimpleDistanceDrivingStrategy(Engine, Distances);
            drivingStrategy.MinimumDrivingDistance = 30.Centimeters();
            drivingStrategy.ReversalDistance = 11.Centimeters();
            drivingStrategy.RefreshInterval = 10;
            this.DrivingStrategy = drivingStrategy;
        }
    static void Main()
    {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        BrickletDistanceIR dir = new BrickletDistanceIR(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT); // Connect to brickd
        // Don't use device before ipcon is connected

        // Register distance callback to function DistanceCB
        dir.Distance += DistanceCB;

        // Set period for distance callback to 0.2s (200ms)
        // Note: The distance callback is only called every 0.2 seconds
        //       if the distance has changed since the last call!
        dir.SetDistanceCallbackPeriod(200);

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
    static void Main()
    {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        BrickletDistanceIR dir = new BrickletDistanceIR(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT); // Connect to brickd
        // Don't use device before ipcon is connected

        // Get threshold callbacks with a debounce time of 10 seconds (10000ms)
        dir.SetDebouncePeriod(10000);

        // Register distance reached callback to function DistanceReachedCB
        dir.DistanceReached += DistanceReachedCB;

        // Configure threshold for distance "smaller than 30 cm" (unit is mm)
        dir.SetDistanceCallbackThreshold('<', 30*10, 0);

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
    private static string UID = "XYZ"; // Change XYZ to the UID of your Distance IR Bricklet

    #endregion Fields

    #region Methods

    // Callback function for distance reached callback (parameter has unit mm)
    static void DistanceReachedCB(BrickletDistanceIR sender, int distance)
    {
        Console.WriteLine("Distance: " + distance/10.0 + " cm");
    }