Exemplo n.º 1
0
        public Measurement GetWeight(bool single = false)
        {
            try
            {
                if (clockPin == null || dataPin == null)
                {
                    return(null);
                }

                // TODO: sample sensors and report weight -- mocked for now
                //return new Measurement(165.0f, Measurement.UnitsOfMeasure.Pounds);
                device = new HX711(clockPin, dataPin);

                var w = _GetOutputData();
                Debug.WriteLine($"Single:{w}");
                var c = Calibrated(w);

                if (!CheckAnomaly(c))
                {
                    PriorMeasurements.Enqueue(new Measurement(c, Measurement.UnitsOfMeasure.Ounces));
                }

                Debug.WriteLine($"Current Avg:{PriorMeasurements.Average(i => i.Amount)}");
#if DEBUG
                foreach (var item in PriorMeasurements)
                {
                    Debug.WriteLine($"   {item.Amount}");
                }
#endif

                if (single)
                {
                    return(new Measurement(w, Measurement.UnitsOfMeasure.Ounces));
                }
                else
                {
                    return(new Measurement(PriorMeasurements.Average(i => i.Amount), Measurement.UnitsOfMeasure.Ounces));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error,{ex.Message}");
                KegLogger.KegLogException(ex, "Weight:GetWeight", SeverityLevel.Critical);

                KegLogger.KegLogTrace(ex.Message, "Weight:GetWeight", SeverityLevel.Warning,
                                      new Dictionary <string, string>()
                {
                    { "ClockPin", CalibrationSettings[WeightClockGpioPinNumberSetting].ToString() },
                    { "DataPin", CalibrationSettings[WeightDataGpioPinNumberSetting].ToString() }
                });
            }

            return(null);
        }
Exemplo n.º 2
0
        private static void Loop(HX711 scale)
        {
            var result = scale.GetUnits(10);

            //Blink the on board led according to the weight
            for (int i = 0; i < Math.Abs(result / 10); i++)
            {
                Led.Write(true);
                Thread.Sleep(100);
                Led.Write(false);
                Thread.Sleep(100);
            }

            scale.PowerDown(); // put the ADC in sleep mode for two seconds
            Thread.Sleep(2000);
            scale.PowerUp();
        }
Exemplo n.º 3
0
        public static void Main()
        {
            // parameter "gain" is ommited; the default value 128 is used by the library
            var scale = new HX711(Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D0)
            {
                Scale = 2280.0
            };


            // this value is obtained by calibrating the scale with known weights; see the https://github.com/bogde/HX711 for details
            scale.Tare();

            while (true)
            {
                Loop(scale);
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var settings = new HX711Settings(25, 24);
            var hx711    = new HX711(settings);

            hx711.SetGain(Gain.x64);

            Console.WriteLine($"TARE: {hx711.Tare(50)}");

            Console.ReadLine();

            Console.WriteLine($"Calibration: {hx711.Calibrate(Iot.Units.Weight.WeightSystem.Metric, 100)}");

            while (true)
            {
                Console.WriteLine($"{hx711.MetricWeight.Grams.ToString("0.00")}g");
                Thread.Sleep(500);
            }
        }
Exemplo n.º 5
0
        public void Initialize()
        {
            if (device == null)
            {
                GpioController controller = GpioController.GetDefault();
                GpioOpenStatus status;

                if (controller != null &&
                    controller.TryOpenPin(Int32.Parse(CalibrationSettings[WeightClockGpioPinNumberSetting].ToString()), GpioSharingMode.Exclusive, out clockPin, out status) &&
                    controller.TryOpenPin(Int32.Parse(CalibrationSettings[WeightDataGpioPinNumberSetting].ToString()), GpioSharingMode.Exclusive, out dataPin, out status))
                {
                    device = new HX711(clockPin, dataPin);
                }
                else
                {
                    device = null;
                }
            }
            if (device != null)
            {
                device.PowerOn();
            }
        }
Exemplo n.º 6
0
        public static void Start(string[] args)
        {
            if (args.Length < 2)
            {
                TestMain.ErrorExit("Device testing needs device to test.");
            }
            switch (args[1].ToLower())
            {
            case "hx711":
            {
                if (args.Length < 5)
                {
                    TestMain.ErrorExit("Insufficient info to run HX711 test. See help.");
                }
                IDigitalIn  DataPin  = null;
                IDigitalOut ClockPin = null;
                if (args[2].Equals("pi", StringComparison.InvariantCultureIgnoreCase))
                {
                    RaspberryPi.Initialize();
                    DataPin  = new DigitalInPi(int.Parse(args[3]));
                    ClockPin = new DigitalOutPi(int.Parse(args[4]));
                }
                else if (args[2].Equals("bbb", StringComparison.InvariantCultureIgnoreCase))
                {
                    BeagleBone.Initialize(SystemMode.DEFAULT, true);
                    BBBPin DataBBBPin  = IOBBB.StringToPin(args[3]);
                    BBBPin ClockBBBPin = IOBBB.StringToPin(args[4]);
                    BBBPinManager.AddMappingGPIO(DataBBBPin, false, ResistorState.NONE);
                    BBBPinManager.AddMappingGPIO(ClockBBBPin, true, ResistorState.NONE);
                    BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_IF_NONE);
                    DataPin  = new DigitalInBBB(DataBBBPin);
                    ClockPin = new DigitalOutBBB(ClockBBBPin);
                }
                else
                {
                    TestMain.ErrorExit("HX711 test: Unknown platform. See help.");
                }
                HX711 DUT = new HX711(ClockPin, DataPin);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey();
                }
                Log.Output(Log.Severity.INFO, Log.Source.GUI, "[w] to increase gain, [s] to decrease. [z] to zero.");
                Log.Output(Log.Severity.INFO, Log.Source.GUI, "Press any other key to exit.");

                HX711.Gain Gain     = HX711.Gain.GAIN_128x;
                bool       Continue = true;
                while (Continue)
                {
                    if (Console.KeyAvailable)
                    {
                        char Key = Console.ReadKey().KeyChar;
                        switch (Key)
                        {
                        case 'w':
                        {
                            if (Gain == HX711.Gain.GAIN_32x)
                            {
                                Gain = HX711.Gain.GAIN_64x;
                            }
                            else if (Gain == HX711.Gain.GAIN_64x)
                            {
                                Gain = HX711.Gain.GAIN_128x;
                            }
                            else
                            {
                                Log.Output(Log.Severity.ERROR, Log.Source.SENSORS, "Gain at maximum already.");
                            }
                            DUT.SetGain(Gain);
                            Log.Output(Log.Severity.INFO, Log.Source.SENSORS, "Gain now at " + Gain);
                            break;
                        }

                        case 's':
                        {
                            if (Gain == HX711.Gain.GAIN_128x)
                            {
                                Gain = HX711.Gain.GAIN_64x;
                            }
                            else if (Gain == HX711.Gain.GAIN_64x)
                            {
                                Gain = HX711.Gain.GAIN_32x;
                            }
                            else
                            {
                                Log.Output(Log.Severity.ERROR, Log.Source.SENSORS, "Gain at minimum already.");
                            }
                            DUT.SetGain(Gain);
                            Log.Output(Log.Severity.INFO, Log.Source.SENSORS, "Gain now at " + Gain);
                            break;
                        }

                        case 'z':
                        {
                            DUT.Tare();
                            Log.Output(Log.Severity.INFO, Log.Source.SENSORS, "Tared.");
                            break;
                        }

                        default:
                        {
                            Continue = false;
                            break;
                        }
                        }
                    }
                    DUT.UpdateState();
                    Log.Output(Log.Severity.INFO, Log.Source.SENSORS, "HX711 readings: Raw: " + DUT.GetRawReading() + ", Adjusted: " + DUT.GetAdjustedReading());
                    Thread.Sleep(250);
                }
                break;
            }

            default:
            {
                TestMain.ErrorExit("Unknown device.");
                break;
            }
            }
        }