Ejemplo n.º 1
0
        /// <summary>
        /// Read value from the specified ADC channel on the device.
        /// </summary>
        /// <param name="pinNumber">Number of the pin to use. These are typically the pins on the device that are connected to the ADC. For example on an Arduino they are pin A0-A6 but are used as pins 0-6.</param>
        /// <param name="message">A value representing what was read in terms of the ADC resolution. A 10-bit ADC will have a range from 0-1024</param>
        public static IAsyncOperation <int> AnalogReadAsync(int pinNumber)
        {
            return(InitAsync().ContinueWith((result) =>
            {
                int adcResult = 0;
                try
                {
                    // We might try to use the pin before connection is finished
                    if (adcController != null)
                    {
                        if (_ADCPin[pinNumber] == null)
                        {
                            _ADCPin[pinNumber] = adcController.OpenChannel(pinNumber);
                        }

                        adcResult = _ADCPin[pinNumber].ReadValue();
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }

                return adcResult;
            }).AsAsyncOperation());
        }
Ejemplo n.º 2
0
        public MainPage()
        {
            this.InitializeComponent();

            // Create a new SpeechSynthesizer instance for later use.
            synthesizer = new SpeechSynthesizer();

            // Initialize the ADC chip for use
            adcController     = (await AdcController.GetControllersAsync(AdcMcp3008Provider.GetAdcProvider()))[0];
            LowPotAdcChannel  = adcController.OpenChannel(LowPotentiometerADCChannel);
            HighPotAdcChannel = adcController.OpenChannel(HighPotentiometerADCChannel);
            CdsAdcChannel     = adcController.OpenChannel(CDSADCChannel);
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            Debug.WriteLine("devMobile.Longboard.PwmTest starting");
            Debug.WriteLine(PwmController.GetDeviceSelector());

            try
            {
                PwmController pwm        = PwmController.FromId("TIM5");
                AdcController adc        = AdcController.GetDefault();
                AdcChannel    adcChannel = adc.OpenChannel(0);

                PwmPin pwmPin = pwm.OpenPin(PinNumber('A', 0));
                pwmPin.Controller.SetDesiredFrequency(1000);
                pwmPin.Start();

                while (true)
                {
                    double value = adcChannel.ReadRatio();

                    Debug.WriteLine(value.ToString("F2"));

                    pwmPin.SetActiveDutyCyclePercentage(value);

                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            string devs = AdcController.GetDeviceSelector();

            Console.WriteLine("devs=" + devs);

            AdcController adc1 = AdcController.GetDefault();

            int max1 = adc1.MaxValue;
            int min1 = adc1.MinValue;

            Console.WriteLine("min1=" + min1.ToString() + " max1=" + max1.ToString());

            AdcChannel ac0 = adc1.OpenChannel(0);

            // the following indexes are valid for STM32F769I-DISCO board
            AdcChannel vref = adc1.OpenChannel(0);
            AdcChannel vbat = adc1.OpenChannel(8);

            // VP
            //AdcChannel ac3 = adc1.OpenChannel(3);
            // VN
            while (true)
            {
                int value     = ac0.ReadValue();
                int valueVref = vref.ReadValue();
                int valueVbat = vbat.ReadValue();

                double percent = ac0.ReadRatio();

                Console.WriteLine("value0=" + value.ToString() + " ratio=" + percent.ToString());
                Console.WriteLine("verf" + valueVref.ToString() + " ratio=" + percent.ToString());
                Console.WriteLine("vbat" + valueVbat.ToString() + " ratio=" + percent.ToString());

                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 5
0
        static async Task adc(int channelint)
        {
            try
            {
                UpBridge.Up   upb        = new UpBridge.Up();
                AdcController controller = await AdcController.GetDefaultAsync();

                AdcChannel channel = controller.OpenChannel(channelint);
                Console.WriteLine(channel.ReadValue());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 6
0
        public static void Main()
        {
            try
            {
                // See if any analog stuff is still out there
                Console.WriteLine(AdcController.GetDeviceSelector());

                // Assign first ADC
                AdcController _ctl1 = AdcController.GetDefault();

                // Some stats to see we are alive
                Console.WriteLine("Channels    : " + _ctl1.ChannelCount.ToString());
                Console.WriteLine("Active mode : " + _ctl1.ChannelMode.ToString()); // 0=SingleEnded, 1=Differential
                Console.WriteLine("Resolution  : " + _ctl1.ResolutionInBits.ToString() + " bits");
                Console.WriteLine("Min value   : " + _ctl1.MinValue);
                Console.WriteLine("Max value   : " + _ctl1.MaxValue);

                // Now open a channel.
                // We don't need additional HW to test ADC.
                // if we use the internal temp sensor
                AdcChannel _ac0 = _ctl1.OpenChannel(AdcChannels.Channel_0);

                int _val1 = -1;

                // Loopie
                for (; ;)
                {
                    _val1 = _ac0.ReadValue();
                    Console.WriteLine("Value read from ADC = " + _val1);

                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                // Do whatever please you with the exception caught
                Console.WriteLine(ex.ToString());
                // Loopie
                for (; ;)
                {
                    Thread.Sleep(1000);
                }
            }
        }
Ejemplo n.º 7
0
        public float GetMcuTemperature()
        {
            AdcController adc1 = AdcController.GetDefault();

            adcTemp = adc1.OpenChannel(Pinout.AdcChannel.ADC_CHANNEL_SENSOR);
            return(adcTemp.ReadValue() / 100.00f);

            //https://www.st.com/resource/en/datasheet/stm32f769ni.pdf
            //https://electronics.stackexchange.com/questions/324321/reading-internal-temperature-sensor-stm32
            //const int ADC_TEMP_3V3_30C = 0x1FF0F44C //0x1FFF7A2C;
            //const int ADC_TEMP_3V3_110C = 0x1FF0 F44E //0x1FFF7A2E;
            //const float CALIBRATION_REFERENCE_VOLTAGE = 3.3F;
            //const float REFERENCE_VOLTAGE = 3.0F; // supplied with Vref+ or VDDA

            // scale constants to current reference voltage
            //float adcCalTemp30C = getRegisterValue(ADC_TEMP_3V3_30C) * (REFERENCE_VOLTAGE / CALIBRATION_REFERENCE_VOLTAGE);
            //float adcCalTemp110C = getRegisterValue(ADC_TEMP_3V3_110C) * (REFERENCE_VOLTAGE / CALIBRATION_REFERENCE_VOLTAGE);

            // return (adcTemp.ReadValue() - adcCalTemp30C)/(adcCalTemp110C - adcCalTemp30C) *(110.0F - 30.0F) + 30.0F);
        }
Ejemplo n.º 8
0
        public double GetTemperatureOnBoard()
        {
            AdcController adc1 = AdcController.GetDefault();

            adcTemp = adc1.OpenChannel(Pinout.AdcChannel.ADC1_IN13_TEMP);

            double tempInCent = 0;

            try
            {
                var    maximumValue     = 4095;
                var    analogReference  = 3300;
                double adcTempCalcValue = (analogReference * adcTemp.ReadValue()) / maximumValue;
                tempInCent = ((13.582f - Math.Sqrt(184.470724f + (0.01732f * (2230.8f - adcTempCalcValue)))) / (-0.00866f)) + 30;
                // double tempInF = ((9f / 5f) * tempInCent) + 32f;
            }
            catch { }

            return(tempInCent);
        }
Ejemplo n.º 9
0
 private static AdcChannel GetChannel(int channelNumber)
 {
     if (!Valid())
     {
         return(null);
     }
     if (!channels.ContainsKey(channelNumber))
     {
         try
         {
             var channel = controller.OpenChannel(channelNumber);
             channels[channelNumber] = channel;
         }
         catch
         {
             channels[channelNumber] = null;
         }
     }
     return(channels[channelNumber]);
 }
Ejemplo n.º 10
0
        public static void Main()
        {
            Debug.WriteLine("devMobile.Longboard.ServoTest starting");

            try
            {
                AdcController adc        = AdcController.GetDefault();
                AdcChannel    adcChannel = adc.OpenChannel(0);

#if POSITIONAL
                ServoMotor servo = new ServoMotor("TIM5", ServoMotor.ServoType.Positional, PinNumber('A', 0));
                servo.ConfigurePulseParameters(0.1, 2.3);
#endif
#if CONTINUOUS
                ServoMotor servo = new ServoMotor("TIM5", ServoMotor.ServoType.Continuous, PinNumber('A', 0));
                servo.ConfigurePulseParameters(0.1, 2.3);
#endif

                while (true)
                {
                    double value = adcChannel.ReadRatio();

#if POSITIONAL
                    double position = Map(value, 0.0, 1.0, 0.0, 180);
#endif
#if CONTINUOUS
                    double position = Map(value, 0.0, 1.0, -100.0, 100.0);
#endif
                    Debug.WriteLine($"Value: {value:F2} Position: {position:F1}");

                    servo.Set(position);

                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 11
0
        private void ReadVals(CancellationToken token)
        {
            Task.Run(() =>
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                for (var c = 0; c <= 3; c++)
                {
                    var comm = _controller.OpenChannel(c);

                    int maxT         = 0;
                    int minT         = 10000;
                    int runningTotal = 0;
                    for (var j = 0; j < 5; j++)
                    {
                        for (var i = 0; i < 500; i++)
                        {
                            var t = comm.ReadValue();
                            if (t > maxT)
                            {
                                maxT = t;
                            }
                            if (t < minT)
                            {
                                minT = t;
                            }
                        }

                        runningTotal += maxT - minT;
                    }

                    _vals[c] = runningTotal / 5;
                }
            });
        }
Ejemplo n.º 12
0
        //private AdcChannel adc420mA;

        /// <summary>
        /// Returns the value of the 12V battery voltage coming in the system
        /// </summary>
        /// <returns></returns>
        public double GetBatteryUnregulatedVoltage()
        {
            float voltage = 0;

            if (adcVBAT == null)
            {
                AdcController adc1 = AdcController.GetDefault();
                adcVBAT = adc1.OpenChannel(Pinout.AdcChannel.ADC1_IN8_VBAT);
            }

            var average = 0;

            for (byte i = 0; i < 5; i++)
            {
                average += adcVBAT.ReadValue();

                Thread.Sleep(50);//pause to stabilize
            }

            try
            {
                average /= 5;

                //maximumValue = 4095;
                //analogReference = 3300;
                //VBat = 0.25 x VIN adc count
                //float voltage = ((3300 * average) / 4096)* 4;

                voltage = ((3300 * average) / 4096) * 0.004f;

                voltage += 0.25f;//small offset calibration factor for board to even drop on measure
            }
            catch
            {
            }

            return(voltage);
        }
Ejemplo n.º 13
0
        public static void Main()
        {
            Debug.WriteLine("devMobile.Longboard.AdcTest starting");
            Debug.WriteLine(AdcController.GetDeviceSelector());

            try
            {
                AdcController adc        = AdcController.GetDefault();
                AdcChannel    adcChannel = adc.OpenChannel(0);

                while (true)
                {
                    double value = adcChannel.ReadRatio();

                    Debug.WriteLine($"Value: {value:F2}");

                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }