public override void Set(double frequency, double dutyCycle)
        {
            if (frequency < 0)
            {
                throw new ArgumentException("frequency");
            }

            if (dutyCycle < 0 || dutyCycle > 1)
            {
                throw new ArgumentException("dutyCycle");
            }

            if (_port == null)
            {
                _port = new Hardware.PWM(_channel, frequency, dutyCycle, _invert);

                _port.Start();
                _started = true;
            }
            else
            {
                if (_started)
                {
                    _port.Stop();
                }

                _port.Frequency = frequency;
                _port.DutyCycle = dutyCycle;

                _port.Start();
                _started = true;
            }
        }
 /// <summary>
 /// Piezo speaker driver and notes and playback manager
 /// </summary>
 /// <param name="pin">From the SecretLabs.NETMF.Hardware.NetduinoPlus.PWMChannels namespace</param>
 /// <param name="name">Unique identifying name for command and control</param>
 public Piezo(Cpu.PWMChannel pin, string name)
     : base(name, "piezo")
 {
     //_piezo = new PWM(pin, 2048, 0, PWM.ScaleFactor.Milliseconds, false);
     _piezo = new PWM(pin, 2048, 0, false);
     _piezo.Start();
 }
        static double fadeAmount = 0.01; // brightness fade rate

        #endregion Fields

        #region Methods

        public static void Main()
        {
            // create a new outpot port using Pulse Width Modulation (PWM) and the onboard LED
            // PWM is a digital method of delivering a varying amount of power...
            // can be used to control the brightness of an LED or speed of a DC motor...
            PWM led = new PWM(SecretLabs.NETMF.Hardware.Netduino.PWMChannels.PWM_ONBOARD_LED, 100, 1.0, false);

            led.Start(); // enable the PWM LED

            // create a loop for the program to run in...
            while (true)
            {
                // PWM duty cycle value is a percentage of ON
                // 0.0 = 0%, while 1.0 = 100%
                led.DutyCycle = brightness;

                // scroll through the brightness at the rate of fadeAmount
                brightness = brightness + fadeAmount;

                // if we reach our limits...
                if (brightness <= 0.0 || brightness >= 0.99)
                {
                    // invert the fade rate to go in reverse direction
                    fadeAmount = -fadeAmount;
                }

                // sleep for 30ms, long enough to see the transistions
                Thread.Sleep(30);
            }
        }
        private void Move(Direction direction)
        {
            //Debug.Print((!CANifier.GetGeneralInput(forwardLimitSwitch)).ToString() + (!CANifier.GetGeneralInput(reverseLimitSwitch)).ToString());


            if (direction != lastDirection)
            {
                Thread.Sleep(250);
            }

            if (direction == Direction.FORWARDS && IsForwardLimitSwitchPressed())
            {
                Stop();
                //  Debug.Print("Forward Limit Switch Tripped");
                return;
            }
            else if (direction == Direction.BACKWARDS && IsReverseLimitSwitchPressed())
            {
                Stop();
                //  Debug.Print("Reverse Limit Switch Tripped");
                return;
            }

            lastDirection = direction;

            if (direction == Direction.STOPPED)
            {
                movePort.Stop();
                return;
            }

            CANifier.SetGeneralOutput(directionPort, direction == Direction.FORWARDS, true);

            movePort.Start();
        }
 public PwmSpeaker(PWM pwm)
 {
     _pwm = pwm;
     _pwm.Frequency = 50;
     _pwm.DutyCycle = 0;
     _pwm.Start();
 }
        public static void Main()
        {
            var pwm0 = new PWM(Cpu.PWMChannel.PWM_0, 300, 0, false);
            pwm0.Start();

            var pwm1 = new PWM(Cpu.PWMChannel.PWM_1, 300, 0, false);
            pwm1.Start();

            var pwm2 = new PWM(Cpu.PWMChannel.PWM_2, 300, 0, false);
            pwm2.Start();

            const int minBright = 0;
            const int maxBright = 100;
            int bright = minBright;
            int step = 1;

            while (true)
            {
                pwm0.DutyCycle = ToDutyCycle(bright);
                pwm1.DutyCycle = ToDutyCycleExp(bright);
                pwm2.DutyCycle = ToDutyCycle10(bright);

                bright += step;
                if (bright > maxBright || bright < minBright)
                {
                    bright = bright > maxBright ? maxBright : minBright;
                    step = -step;

                    Thread.Sleep(1000);
                }

                Thread.Sleep(40);
            }
        }
        public override void Set(double frequency, double dutyCycle)
        {
            if (frequency < 0)
                throw new ArgumentException("frequency");

            if (dutyCycle < 0 || dutyCycle > 1)
                throw new ArgumentException("dutyCycle");

            if (_port == null)
            {
                _port = new Hardware.PWM(_channel, frequency, dutyCycle, _invert);

                _port.Start();
                _started = true;
            }
            else
            {
                if (_started)
                    _port.Stop();

                _port.Frequency = frequency;
                _port.DutyCycle = dutyCycle;

                _port.Start();
                _started = true;
            }
        }
        public static void Main()
        {
            // write your code here

            int anaIn = 250; // ADC is 12-bit Resolution
            //SecretLabs.NETMF.Hardware.AnalogInput pinA4 = new Microsoft.NETMF.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_4);
            SecretLabs.NETMF.Hardware.AnalogInput pinA4 = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A4);

            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

            //PWM led2 = new PWM(Cpu.PWMChannel.PWM_0, 100, 100, 0);
            PWM servo = new PWM(Cpu.PWMChannel.PWM_0,20000,1500,PWM.ScaleFactor.Microseconds, false);
            servo.Start();

            servo.DutyCycle = 0;
            servo.Duration = (uint)1500;

            while (true)
            {

                double anaRead = pinA4.Read();
                anaIn = (int)anaRead;

                //LED stuff

                //led.Write(true); // turn on the LED
                //Thread.Sleep(anaIn); // sleep for 250ms
                //led.Write(false); // turn off the LED
                //Thread.Sleep(anaIn); // sleep for 250ms

                servo.Duration = (uint)((1000+anaRead));

            }
        }
        private void PlaySound(uint period, uint duration, int length)
        {
            var pwm = new PWM(_channel, period, duration, PWM.ScaleFactor.Microseconds, false);

            pwm.Start();
            Thread.Sleep(length);
            pwm.Stop();
        }
Exemple #10
0
 internal static void SetUpServo()
 {
     uint period = 20000;
     uint duration = SERVO_NEUTRAL;
     _servo = new PWM(PWMChannels.PWM_PIN_D5, period, duration, PWM.ScaleFactor.Microseconds, false);
     _servo.Start();
     _servoReady = true;
 }
        public static void Start()
        {
            _port = new PWM(PWMChannels.PWM_PIN_D9, 100, 0.5, false);
            _port.Frequency = 100;
            _port.DutyCycle = 0.5;

            _worker.Start();
            _port.Start();
        }
        public static void Main()
        {
            PWM speaker = new PWM(PWMChannels.PWM_PIN_D5, 2000, 0.95, true);

            // Store the notes on the music scale and their associated pulse lengths
            System.Collections.Hashtable scale = new System.Collections.Hashtable();

            // low octave
            scale.Add("c", 1915u);
            scale.Add("d", 1700u);
            scale.Add("e", 1519u);
            scale.Add("f", 1432u);
            scale.Add("g", 1275u);
            scale.Add("a", 1136u);
            scale.Add("b", 1014u);

            // upper octave
            scale.Add("C", 956u);
            scale.Add("D", 851u);
            scale.Add("E", 758u);
            scale.Add("F", 671u);
            scale.Add("G", 594u);

            // hold note
            scale.Add("H", 0u);

            int beatsPerMinute = 90;
            int beatTimeInMilliseconds = 60000 / beatsPerMinute;
            int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1);

            // Define the song (letter of note followed by length of note)
            string song = "C1C1C1g1a1a1g2E1E1D1D1C2";

            // interpret and play the song
            for (int i = 0; i < song.Length; i += 2)
            {
                //Extract each note and the length in beats
                string note = song.Substring(i, 1);
                int beatCount = int.Parse(song.Substring(i + 1, 1));

                // look up the note duration in milliseconds
                uint noteFrequency = (uint)scale[note];
                int duration = beatCount * beatTimeInMilliseconds;

                // play the note for the desired duration
                speaker.Frequency = noteFrequency;
                Debug.Print(beatCount + "\t" + beatTimeInMilliseconds + "\t" + pauseTimeInMilliseconds + "\t" + duration + "\t"  + speaker.Duration + "\t" + speaker.DutyCycle + "\t" + speaker.Frequency + "\t" + speaker.Period);
                speaker.Start();
                Thread.Sleep(duration);
                speaker.Stop();

                // Pause for 1/10th of one beat
                Thread.Sleep(pauseTimeInMilliseconds);
            }
            Thread.Sleep(Timeout.Infinite);
        }
Exemple #13
0
 public static void Main()
 {
     InterruptPort przycisk =
         new InterruptPort(Pins.GPIO_PIN_A_0, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLevelHigh);//przycisk
     var green = new PWM(Cpu.PWMChannel.PWM_0, 300, 0, false);//zielona
     var orange = new PWM(Cpu.PWMChannel.PWM_1, 300, 0, false);//pomaranczowa
     var red = new PWM(Cpu.PWMChannel.PWM_2, 300, 0, false); //czerwona
     var blue = new PWM(Cpu.PWMChannel.PWM_3, 300, 0, false);//niebieska
     green.Start();
     orange.Start();
     red.Start();
     blue.Start();
     int x = 0, y = 0, z = 0; 
     var timer = new System.Threading.Timer(TimerCallback, null, 0, 1000);
     SPI.Configuration MyConfig =
         new SPI.Configuration(Pins.GPIO_PIN_E_3, false, 0, 0, true, true, 1000, SPI.SPI_module.SPI1);
     MySPI = new SPI(MyConfig);
     Config();
     while (true)
     {
         red.DutyCycle = 0;
         green.DutyCycle = 0;
         blue.DutyCycle = 0;
         orange.DutyCycle = 0;
         if((ReadRegister(0x2D)>150)&&(ReadRegister(0x2D)<230))
         {
             red.DutyCycle = 1;
             green.DutyCycle = 1;
             blue.DutyCycle = 1;
             orange.DutyCycle = 1;
         }
         if ((ReadRegister(0x29) > 160) && (ReadRegister(0x29) < 230))
             blue.DutyCycle = 1;
         if ((ReadRegister(0x29) < 140) && (ReadRegister(0x29) > 20))
             orange.DutyCycle = 1;
         if ((ReadRegister(0x2B) > 160) && (ReadRegister(0x2B) < 230))
             red.DutyCycle = 1;
         if ((ReadRegister(0x2B) < 140) && (ReadRegister(0x2B) > 20))
             green.DutyCycle = 1;
         x = DateTime.Now.Millisecond;
         if (x + 200 >= 999) x -= 999;
         Debug.Print(ReadRegister(0x29).ToString() + " " + ReadRegister(0x2B).ToString() + " " + ReadRegister(0x2D).ToString() + " " + x);
         while (DateTime.Now.Millisecond<= x + 200) { }
     }
     
 }
        public PwmOutputPin (Microsoft.SPOT.Hardware.Cpu.PWMChannel channel, double frequencyHz = 1000, double dutyCycle = 0)
        {
            pwm = new PWM (channel, frequencyHz, dutyCycle, false);

            DutyCycleInput = AddInput ("DutyCycleInput", Units.Ratio, dutyCycle);
            FrequencyInput = AddInput ("FrequencyInput", Units.Frequency, frequencyHz);

            DutyCycleInput.ValueChanged += (s, e) => {
                pwm.DutyCycle = DutyCycleInput.Value;
            };

            FrequencyInput.ValueChanged += (s, e) => {
                pwm.Frequency = FrequencyInput.Value;
            };

            pwm.Start ();
        }
Exemple #15
0
        public static void Main()
        {
            // write your code here
            var scale = new System.Collections.Hashtable
            {
                { "c", 1915u },
                { "d", 1700u },
                { "e", 1519u },
                { "f", 1432u },
                { "g", 1275u },
                { "a", 1136u },
                { "b", 1014u },
                { "C", 956u },
                { "D", 851u },
                { "E", 758u },
                { "h", 0u }
            };

            int beatsPerMinute = 90;
            int beatTimeInMilliseconds = 6000 / beatsPerMinute;
            int pauseTimeInMillisenconds = (int)(beatTimeInMilliseconds * 0.1);

            string song = "C1C1C1g1a1a1g2E1E1D1D1C2";

            PWM speaker = new PWM(PWMChannels.PWM_PIN_D5, 100, .5, false);
            speaker.Start();

            for (int i = 0; i < song.Length; i += 2)
            {
                string note = song.Substring(i, 1);
                int beatCount = int.Parse(song.Substring(i + 1, 1));

                uint noteDuration = (uint)scale[note];
                speaker.Duration = noteDuration;
                speaker.Period = (noteDuration * 2);

                Thread.Sleep(beatTimeInMilliseconds * beatCount - pauseTimeInMillisenconds);
                speaker.DutyCycle = 0;
                Thread.Sleep(pauseTimeInMillisenconds);
            }

            speaker.Stop();

            Thread.Sleep(Timeout.Infinite);
        }
Exemple #16
0
        protected void InitializePeripherals()
        {
            _rtc = new DS3231(0x68, 100);
            // Run once to adjust the time on the RTC
            // _rtc.CurrentDateTime = new DateTime(2018, 12, 17, 22, 41, 0);

            _lcd = new Lcd2004
                   (
                RS: N.Pins.GPIO_PIN_D8,
                E: N.Pins.GPIO_PIN_D9,
                D4: N.Pins.GPIO_PIN_D10,
                D5: N.Pins.GPIO_PIN_D11,
                D6: N.Pins.GPIO_PIN_D12,
                D7: N.Pins.GPIO_PIN_D13
                   );

            H.PWM _contrast = new H.PWM(H.Cpu.PWMChannel.PWM_0, 1000, 0.6, false);
            _contrast.Start();
        }
        public static void Main()
        {
            // write your code here
            AnalogInput pot = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);
            PWM pwm = new PWM(PWMChannels.PWM_PIN_D5, 1000.0, 0.1, false);
            pot.Offset = 0;
            pot.Scale = 100;

            double potValue = 0.0;

            while (true)
            {
                potValue = pot.Read();
                pwm.DutyCycle = potValue;
                pwm.Start();

                Thread.Sleep(10);
            }
        }
        public static void Main()
        {
            var pwm0 = new PWM(Cpu.PWMChannel.PWM_0, 300, 0, false);
            pwm0.Start();

            var pwm1 = new PWM(Cpu.PWMChannel.PWM_1, 300, 0, false);
            pwm1.Start();

            var pwm2 = new PWM(Cpu.PWMChannel.PWM_2, 300, 0, false);
            pwm2.Start();

            var pwm3 = new PWM(Cpu.PWMChannel.PWM_3, 300, 0, false);
            pwm3.Start();

            using (var analogInput = new AnalogInput(Cpu.AnalogChannel.ANALOG_0))
            {
                analogInput.Scale = 100;
                analogInput.Offset = 0;

                double prevVal = Double.MinValue;
                for (;;)
                {
                    double currentVal = analogInput.Read();
                    //int raw = analogInput.ReadRaw();
                    //Debug.Print("Sample: " + val + " (" + raw + ")");

                    if (Math.Abs(currentVal - prevVal) >= 1)
                    {
                        pwm0.DutyCycle =
                            pwm1.DutyCycle =
                            pwm2.DutyCycle =
                            pwm3.DutyCycle = ToDutyCycle(currentVal);

                        //int volume = ToVolume(currentVal);
                        //SendXbmcVolume(volume);

                        prevVal = currentVal;
                    }
                }
            }
        }
        public static void Main()
        {
            var sensor = new HCSR04(Stm32F4Discovery.FreePins.PC2, Stm32F4Discovery.FreePins.PC1);

            //crash detect: red led on or off
            var crashLed = new OutputPort(Stm32F4Discovery.LedPins.Red, false);
            var scanPeriod = new TimeSpan(0, 0, 0, 0, 100);//100ms
            var detector = new CollisionDetector(sensor, scanPeriod) { Barier = 10 }; //10cm
            detector.StateChanged += crashLed.Write;

            //pwm frequency display (green led linear blink): far->lo freq, near->hi freq
            var distancePwm = new PWM(Cpu.PWMChannel.PWM_0, 1, 0, false);
            distancePwm.Start();
            
            //print distance every 1s and change pwm freq
            DateTime nextPrint = DateTime.Now;
            for(;;)
            {
                TimeSpan pulse = sensor.Ping();

                if (DateTime.Now > nextPrint)
                {
                    float cm = HCSR04.ToCentimeters(pulse);
                    string cmStr = cm.Equals(Single.MaxValue) ? "?" : cm.ToString("F1");

                    float inch = HCSR04.ToInches(pulse);
                    string inStr = inch.Equals(Single.MaxValue) ? "?" : inch.ToString("F1");

                    Debug.Print("Distance: " + cmStr + " cm = " + inStr + " in");
                    nextPrint = DateTime.Now.AddSeconds(1);
                }

                distancePwm.Frequency = ToFrequency(pulse);
                distancePwm.DutyCycle = 0.5;

                Thread.Sleep(200);
            }
        }
        public static void Main()
        {
            const uint period = 20000; //20ms in us
            const uint minDuration = 530; //0.53ms in us
            const uint maxDuration = 2350; //2.35ms in us

            var servo = new PWM(Cpu.PWMChannel.PWM_0, period, minDuration,
                                PWM.ScaleFactor.Microseconds, false);
            servo.Start();

            uint step = 30;
            for (uint angle = 0;; angle += step)
            {
                if (angle > 180)
                {
                    step = (uint) -step;
                    continue;
                }

                servo.Duration = Map(angle, 0, 180, minDuration, maxDuration);
                Thread.Sleep(2000);
            }
        }
Exemple #21
0
        public static void Main()
        {
            // write your code here

            PWM LED1 = new PWM(PWMChannels.PWM_ONBOARD_LED, 10000, 0.5, false);
            bool upside = true;
            LED1.Frequency = 10000;
            LED1.DutyCycle = 1;
            LED1.Start();
            while (true)
            {

                if (upside == true)
                {
                    Debug.Print("Plus");
                    LED1.DutyCycle+=0.01;
                    Debug.Print(LED1.DutyCycle.ToString());
                    if (LED1.DutyCycle >= 0.90)
                    {
                        upside = false;
                    }
                }
                else
                {
                    Debug.Print("Minus");
                    LED1.DutyCycle-=0.01;
                    Debug.Print(LED1.DutyCycle.ToString());
                    if (LED1.DutyCycle <= 0.10)
                    {
                        upside = true;
                    }
                }

                Thread.Sleep(10);
            }
        }
        public override void Set(uint period, uint highTime, SocketInterfaces.PwmScaleFactor factor)
        {
            if (_port == null)
            {
                _port = new Hardware.PWM(_channel, period, highTime, (Hardware.PWM.ScaleFactor)factor, _invert);

                _port.Start();
                _started = true;
            }
            else
            {
                if (_started)
                {
                    _port.Stop();
                }

                _port.Scale    = (Hardware.PWM.ScaleFactor)factor;
                _port.Period   = period;
                _port.Duration = highTime;

                _port.Start();
                _started = true;
            }
        }
Exemple #23
0
 public static void SendBit(PWM infraredOut, OutputPort led, char c)
 {
     if (c == '1')
     {
         var startTime = DateTime.Now;
         infraredOut.Start();
         while (startTime.AddMilliseconds(sleep) > DateTime.Now)
         {
             led.Write(true);
             //noop
         }
         infraredOut.Stop();
     }
     else
     {
         var startTime = DateTime.Now;
         while (startTime.AddMilliseconds(sleep) > DateTime.Now)
         {
             led.Write(false);
             //noop
         }
         startTime = DateTime.Now;
     }
 }
Exemple #24
0
        public static void Main()
        {
            _lcdShield = new DfRobotLcdShield(20, 4);

            // Create a custom degrees symbol (°), store it on the LCD for later use with the lat/long display
            _lcdShield.CreateChar(0, new byte[] { 0x0, 0x4, 0xa, 0x4, 0x0, 0x0, 0x0, 0x0 });

            // 0 bars are not required as that would just require sending " "
            _lcdShield.CreateChar(1, new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1F });
            _lcdShield.CreateChar(2, new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1F, 0x1F });
            _lcdShield.CreateChar(3, new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x1F, 0x1F, 0x1F });
            _lcdShield.CreateChar(4, new byte[] { 0x0, 0x0, 0x0, 0x0, 0x1F, 0x1F, 0x1F, 0x1F });
            _lcdShield.CreateChar(5, new byte[] { 0x0, 0x0, 0x0, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F });
            _lcdShield.CreateChar(6, new byte[] { 0x0, 0x0, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F });
            _lcdShield.CreateChar(7, new byte[] { 0x0, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F });
            // 8 bars are not required as that would just require sending byte 255.

#if (NETDUINO)
            _thunderbolt = new ThunderBolt("COM1", AngleUnits.Degrees, AltitudeUnits.Meters, new OutputPort(Pins.GPIO_PIN_D13, false));
#endif
#if (FEZLEMUR)
            _thunderbolt = new ThunderBolt("COM1", AngleUnits.Degrees, AltitudeUnits.Meters, new OutputPort(GHI.Pins.FEZLemur.Gpio.D13, false));
#endif

#if (NTP)
            _ntpServer = new NtpServer();
#endif

            _lcdShield.OnButtonPressed += LcdshieldOnOnButtonPressed;

            DisplaySplash();

#if (NETDUINO)
            var backlight = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D10, 10000, LcdBrightness / 100d, false);
            backlight.Start();
#endif

            TestLeds();

            _thunderbolt.Open();
            _thunderbolt.TimingMode = TimingModes.UTC;
            _thunderbolt.RequestManufacturingParameters();
            _thunderbolt.RequestFirmwareVersion();
            _thunderbolt.SetupUnitForDisciplining();
            _thunderbolt.RequestTrackedSatelliteStatus();

            _thunderbolt.TimeChanged += ThunderboltOnTimeChanged;

            Thread.Sleep(3000);

            DisplayVersion();

#if (NTP)
            _ntpServer.Start();
#endif

            while (true)
            {
                if (_thunderbolt.IsSerialDataBeingReceived)
                {
                    if (!_isSurveyInProgress && _thunderbolt.IsSurveyInProgress())
                    {
                        // Survey has just started.  Jump to the page displaying survey progress.
                        _previousPageNumber = _pageNumber; // Take note of the page we were on so we can switch back later.
                        _isSurveyInProgress = true;
                        _pageNumber         = 2;           // Set the new page to jump to.
                    }
                    else if (_isSurveyInProgress && !_thunderbolt.IsSurveyInProgress())
                    {
                        // Survey has just finished.  Jump to the previous page we were displaying.
                        _pageNumber         = _previousPageNumber;
                        _isSurveyInProgress = false;
                    }

                    switch (_pageNumber)
                    {
                    case 1:
                        DisplayScreenOne();
                        break;

                    case 2:
                        DisplayScreenTwo();
                        break;

                    case 3:
                        DisplayScreenThree();
                        break;

                    case 4:
                        DisplayScreenFour();
                        break;

                    case 5:
                        DisplaySatelliteSignalScreen();
                        break;

                    case 6:
                        DisplayPRNScreen();
                        break;

                    case 7:
                        DisplayDOPScreen();
                        break;

#if (NTP)
                    case 8:
                        DisplayScreenNTP();
                        break;
#endif
                    default:
                        DisplayScreenOne();
                        break;
                    }

                    UpdateAlarmIndicators();
                }
                else
                {
                    DisplayNoSerialDataScreen();
                }
            }
        }
        private void Initialise()
        {
            _range = _maxPosition - _minPosition;
            degreesRatio = (float)_range / (float)_maxDegrees;
            _servoMotor = new PWM(_pin, 100, 0.5, false);
            _servoMotor.DutyCycle = 0;
            _servoMotor.Duration = _minPosition;
            _servoMotor.Start();

            //give the servo enough time to swing to _minPosition
            Util.Delay(250);
        }
Exemple #26
0
 public ContServo(Cpu.PWMChannel servo_pin)
 {
     servo = new PWM(servo_pin, 20, 0, false);
     servo.Start();
 }
Exemple #27
0
        public static void SendBit(PWM infraredOut, char c)
        {
            if (c == '1')
            {
                var startTime = DateTime.Now;
                infraredOut.Start();
                while (startTime.AddMilliseconds(sleep) > DateTime.Now)
                {

                }
                infraredOut.Stop();
            }
            else
            {
                var startTime = DateTime.Now;
                while (startTime.AddMilliseconds(sleep) > DateTime.Now)
                {

                }
                startTime = DateTime.Now;
            }
        }
Exemple #28
0
 protected void SendCommandPulse(double pulseDuration)
 {
     //Debug.Print("Sending Command Pulse");
     _pwm.DutyCycle = CalculateDutyCycle(pulseDuration);
     _pwm.Start(); // servo expects to run continuously
 }
        public override void Set(uint period, uint highTime, SocketInterfaces.PwmScaleFactor factor)
        {
            if (_port == null)
            {
                _port = new Hardware.PWM(_channel, period, highTime, (Hardware.PWM.ScaleFactor)factor, _invert);

                _port.Start();
                _started = true;
            }
            else
            {
                if (_started)
                    _port.Stop();

                _port.Scale = (Hardware.PWM.ScaleFactor)factor;
                _port.Period = period;
                _port.Duration = highTime;

                _port.Start();
                _started = true;
            }
        }
        private void Initialize(UIOMode mode, int select, int hwres, int intParam, bool isDigital, double scale, double offset)
        {
            _uioMode = mode;
            SelPort = (UIOSelector)select;

            switch (mode)
            {
                case UIOMode.UIOModeDigitalInput:
                    break;
                case UIOMode.UIOModeDigitalOutput:
                    DigitalOut = new DigitalOutput((Output)hwres);
                    break;
                case UIOMode.UIOModeAnalogInput:
                    _avg = new MovingAverageCalculator(intParam);
                    AnalogIn = new AnalogIn((ADC)hwres, scale, offset, intParam) {Scale = scale, Offset = offset};
                    break;
                case UIOMode.UIOModeSoftPwm:
                    if (intParam < 1 || intParam > 1000)
                        throw new ArgumentOutOfRangeException("Il valore deve essere compreso nel range 1...1000 Hz", "freq");
                    //creo ed avvio il PWM software
                    _softPwm = new SoftPWM(intParam, (Cpu.Pin)hwres);
                    SoftPWM.Start();
                    break;
                case UIOMode.UIOModeRealPwm:
                    if (intParam < 1 || intParam > 1000000)
                        throw new ArgumentOutOfRangeException("Il valore deve essere compreso nel range 1...1 MHz", "freq");
                    //creo ed avvio il PWM hardware
                    _realPwm = new PWM((Cpu.PWMChannel)hwres, intParam, 0, false);
                    _realPwm.Start();
                    break;
                case UIOMode.UIOModeSoftDac:
                    if (intParam < 1 || intParam > 1000)
                        throw new ArgumentOutOfRangeException("Il valore deve essere compreso nel range 1...1000 Hz", "freq");
                    //creo ed avvio il PWM software
                    _softPwm = new SoftPWM(intParam, (Cpu.Pin)hwres);
                    SoftPWM.Start();
                    break;
                case UIOMode.UIOModeRealDac:
                    if (intParam < 1 || intParam > 1000000)
                        throw new ArgumentOutOfRangeException("Il valore deve essere compreso nel range 1...1 MHz", "freq");
                    //creo ed avvio il PWM hardware
                    _realPwm = new PWM((Cpu.PWMChannel)hwres, intParam, 0, false);
                    _realPwm.Start();
                    break;
                default:
                    break;
            }

            //seleziono sul modulo il tipo di uscita DAC/PWM
            _pwmSel = new DigitalOutput((Output)SelPort);
            _pwmSel.Write(isDigital);

        }
Exemple #31
0
        public static void Main()
        {
            AnalogInput potentiometer = new AnalogInput(Cpu.AnalogChannel.ANALOG_0);

              //  AnalogInput potentiometer = new AnalogInput(Pins.GPIO_PIN_A0);

            InputPort button1 = new InputPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.Disabled);

            PWM redLED = new PWM(PWMChannels.PWM_PIN_D6, 100, .5, false);
            PWM greenLED = new PWM(PWMChannels.PWM_PIN_D5, 100, .5, false);
            PWM blueLED = new PWM(PWMChannels.PWM_PIN_D9, 100, .5, false);
            /*
            uint R = 0;
            uint G = 0;
            uint B = 0;
            */
            double Rdty = .5;
            double Gdty = .5;
            double Bdty = .5;

            uint Frequency_hz = 400;

            redLED.Frequency = Frequency_hz;
            redLED.DutyCycle = .5;
            redLED.Start();

            greenLED.Frequency = Frequency_hz;
            greenLED.DutyCycle = .5;
            greenLED.Start();

            blueLED.Frequency = Frequency_hz;
            blueLED.DutyCycle = .5;
            blueLED.Start();

            bool LEDState = false;

            while (true)
            {
                if (button1.Read())
                {
                    if (LEDState == true)
                    {
                        LEDState = false;
                    }
                    else
                    {
                        LEDState = true;
                    }
                }

                double sensorValue = 0;

                sensorValue = potentiometer.Read();

                double brightness = sensorValue;

              //  string sSensorValue = sensorValue.ToString;

              //  Debug.Print(sensorValue.ToString("F1"));
                if (LEDState == true)
                {
                    redLED.DutyCycle = sensorValue;
                    greenLED.DutyCycle = sensorValue;
                    blueLED.DutyCycle = sensorValue;
                    redLED.Frequency = Frequency_hz;
                    greenLED.Frequency = Frequency_hz;
                    blueLED.Frequency = Frequency_hz;

                }
                else
                {
                    redLED.DutyCycle = 0;
                    greenLED.DutyCycle = 0;
                    blueLED.DutyCycle = 0;
                    redLED.Frequency = 0;
                    greenLED.Frequency = 0;
                    blueLED.Frequency = 0;
                }

                Thread.Sleep(10);
            }
        }
        private static void ActivateServo()
        {
            uint period = 20;
            uint duration = 1;

            PWM servo = new PWM(PWMChannels.PWM_PIN_D5, period, duration, PWM.ScaleFactor.Milliseconds, false);
            servo.Start();

            Thread.Sleep(Timeout.Infinite);
        }
        private static void TestServoFullRange()
        {
            uint period = 20000;
            uint duration = 1500;

            PWM servo = new PWM(PWMChannels.PWM_PIN_D5, period, duration, PWM.ScaleFactor.Microseconds, false);
            servo.Start();

            Thread.Sleep(2000);
            servo.Duration = 2500;
            Thread.Sleep(3000);
            servo.Duration = 500;
            Thread.Sleep(3000);
            servo.Duration = 1500;
        }
Exemple #34
0
        public PWM_AIO_Demo_Main()
        {
            //AIO pin connected to arbitrary range potentiometer.
            AnalogInput pot = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);
            pot.Scale = 1; //sets range value that is returned by aio read()

            /*
             * Spawn task to poll DHT11 temp/humid sensor.
             * This polling takes time and the quality of the hardware/driver mean the
             * polling takes inconsistent amounts of time. Also, the read operation is
             * subject to timeouts which should not be allowed to suspend other tasks.
             */
            Thread analogReadTask = new Thread(new ThreadStart(new AnalogReadClass().PollTempHumidity));
            analogReadTask.Start();

            //Spin untill task spawn completes
            while (!analogReadTask.IsAlive) ;

            //initialize PWM properties before handoff to forever loop
            double freq = 1000;
            double duty = .5;

            OutputPort onboardLed = new OutputPort(Pins.ONBOARD_LED, false);
            PWM led = new PWM(PWMChannels.PWM_PIN_D5, freq, duty, false);

            //Another output port to drive transistor, which drives coil of relay.
            OutputPort relay = new OutputPort(Pins.GPIO_PIN_D0, false);

            //Starts pwm channel at initialized rate/duty
            //Will be modified by pollwrite loop
            led.Start();

            LCD_Display Display = new LCD_Display(0x27, 20, 4);
            Display.writeValue("Good");

            //loop forever to get AIO value and set led pwm
            PollAndWriteLoop(pot, led, relay);
        }
Exemple #35
0
 public void Start()
 {
     pwm = new PWM(pwmChannel, frequency, Duty, false);
     pwm.Start();
 }
 /// <summary>
 /// To be called after setting the properties of the object.
 /// If not called explicitly, it is automatically called when
 /// the actuator is used for the first time.
 /// Preconditions
 ///     Actuator is not open
 ///     Channel is set
 ///     Period is set
 ///     if MinValue or MaxValue is set: MinValue lessThan MaxValue
 /// Postconditions
 ///     Actuator is open
 /// </summary>
 public void Open()
 {
     Contract.Requires(port == null);        // actuator is not open
     Contract.Requires(Channel >= 0);
     Contract.Requires(Period > 0);
     Contract.Requires(MinValue < MaxValue);
     minValue = MinValue;
     maxValue = MaxValue;
     port = new PWM((Cpu.PWMChannel)Channel, (uint)Period, 0, PWM.ScaleFactor.Microseconds, false);
     port.Start();
 }