public static void Main() { var serial = new SerialPort("COM1", 9800, Parity.None, 8, StopBits.One); var power = new PWM(Pins.GPIO_PIN_D9); var motorIn8 = new OutputPort(Pins.GPIO_PIN_D8, false); serial.DataReceived += Serial_DataReceived; motorIn8.Write(true); while (true) { try { var value = MotorPower; if (!serial.IsOpen) { serial.Open(); } power.SetPulse(20000, value); Thread.Sleep(1000); } catch (Exception ex) { var mes = ex.Message; } } }
public void Buzz(int milliseconds, int frequency) { var period = (uint)(1000000 / frequency); pwm.SetPulse(period, period / 2); timer = new Timer(TimerTick, null, milliseconds, 0); }
public static void Main() { var axisX = new AnalogInput(Pins.GPIO_PIN_A0); var axisY = new AnalogInput(Pins.GPIO_PIN_A1); var axisZ = new AnalogInput(Pins.GPIO_PIN_A2); var IrFloorSensor = new AnalogInput(Pins.GPIO_PIN_A3); var out1 = new OutputPort(Pins.GPIO_PIN_D1, false); var out2 = new OutputPort(Pins.GPIO_PIN_D12, false); var in1 = new InputPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.Disabled); var in2 = new InputPort(Pins.GPIO_PIN_D4, false, Port.ResistorMode.PullUp); var servo1 = new PWM(Pins.GPIO_PIN_D9); servo1.SetDutyCycle(0); var servo2 = new PWM(Pins.GPIO_PIN_D10); servo2.SetDutyCycle(0); var stopWatch = Stopwatch.StartNew(); stopWatch.Start(); int i = 0; bool digState = false; while (i < 5000) { axisX.Read(); axisY.Read(); axisZ.Read(); IrFloorSensor.Read(); in1.Read(); in2.Read(); digState = !digState; out1.Write(digState); out2.Write(digState); servo1.SetPulse(20000, 1500); servo2.SetPulse(20000, 1500); i++; } stopWatch.Stop(); Debug.Print("Elapsed: " + stopWatch.ElapsedMilliseconds.ToString()); }
public static void Main() { Hashtable scale = new 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); // high octave scale.Add("C", 956u); scale.Add("D", 851u); scale.Add("E", 758u); // silence ("hold note") scale.Add("h", 0u); int beatsPerMinute = 90; int beatTimeInMilliseconds = 60000 / beatsPerMinute; // 60,000 ms per min int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1); // define the song(letter of note followed by length of note) string song = "C1C1C1g1a1a1g2E1E1D1D1C2"; // define the speaker PWM speaker = new PWM(Pins.GPIO_PIN_D5); while (true) { for (int i = 0; i < song.Length; i += 2) { // song loop // extract each note and its length in beats string note = song.Substring(i, 1); int beatCount = int.Parse(song.Substring(i + 1, 1)); // look up the note duration(in microseconds) uint noteDuration = (uint)scale[note]; // play the note for the desired number of beats speaker.SetPulse(noteDuration * 2, noteDuration); Thread.Sleep(beatTimeInMilliseconds * beatCount - pauseTimeInMilliseconds); // pause for 1/10th of a beat in between every note. speaker.SetDutyCycle(0); Thread.Sleep(pauseTimeInMilliseconds); } //Thread.Sleep(Timeout.Infinite); Thread.Sleep(1000); } }
public void Rotate(int percent) { if (percent < 0 || percent > 100) { throw new ArgumentException("percent"); } var duration = (uint)(_minDuration + (_range / 100) * percent); _servo.SetPulse(_period, duration); }
static void sw_OnInterrupt(uint data1, uint data2, DateTime time) { led.Write(true); // write your code here // move through the full range of positions for (uint currentPosition = firstPosition; currentPosition <= lastPosition; currentPosition += 10) { // move the servo to the new position. servo.SetPulse(20000, currentPosition); Thread.Sleep(100); } // return to first position and wait a half second. servo.SetPulse(20000, firstPosition); led.Write(false); }
public void Rotate(int degrees) { if (degrees < 0 || degrees > 180) { throw new ArgumentException("percent"); } _position = degrees; var duration = (uint)(_minDuration + (_range / 100d) * (degrees / 180d * 100d)); _servo.SetPulse(_period, duration); }
public static void Main() { PWM servo = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5); InputPort Button; bool button_state; Button = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, false, Port.ResistorMode.PullUp); while (true) { button_state = Button.Read(); if (button_state == false) { servo.SetPulse(20 * 1000 * 1000, 1450 * 1000); } if (button_state == true) { servo.SetPulse(20 * 1000 * 1000, 1600 * 1000); } /* * // 0 degrees. 20ms period and 1.00ms high pulse * Debug.Print("1.00"); * servo.SetPulse(20 * 1000 * 1000, 1000 * 1000); * Thread.Sleep(5000); * * // 90 degrees. 20ms period and 1.50ms high pulse * Debug.Print("1.50"); * servo.SetPulse(20 * 1000 * 1000, 1500 * 1000); * Thread.Sleep(5000); * * // 180 degrees. 20ms period and 2.00ms high pulse * Debug.Print("2.10"); * servo.SetPulse(20 * 1000 * 1000, 2100 * 1000); * Thread.Sleep(5000); */ } }
public static void Main() { try { tftBacklight.SetPulse(0, 0); //ShowGcStats(); InitializeResources(); ShowKloutSplashScreen(); while (true) { SetInternalClock(); ShowTime(); KloutGet(_host, _port, @"/1/users/show.json", _key, _user, @"SD\Cache\show.txt"); KloutGet(_host, _port, @"/1/users/topics.json", _key, _user, @"SD\Cache\topics.txt"); KloutGet(_host, _port, @"/1/soi/influenced_by.json", _key, _user, @"SD\Cache\influenced.txt"); KloutGet(_host, _port, @"/1/soi/influencer_of.json", _key, _user, @"SD\Cache\influencer.txt"); ProcessCachedResults(@"SD\Cache\show.txt", KloutShowHandler); ProcessCachedResults(@"SD\Cache\topics.txt", KloutTopicsHandler); ProcessCachedResults(@"SD\Cache\influenced.txt", KloutInfluencedByHandler); ProcessCachedResults(@"SD\Cache\influencer.txt", KloutInfluencerOfHandler); ShowKlout(); ShowTime(); //ShowGcStats(); Thread.Sleep(30000); } } catch (OutOfMemoryException e) { PowerState.RebootDevice(false); } }
/// <summary> /// Play an single tone on a given channel /// </summary> /// <param name="tone">A RttlTone object</param> /// <param name="channel">Any PWN pin</param> public void PlayTone(RttlTone tone, PWM channel) { if (tone.Note != 0) { channel.SetPulse(tone.Period, tone.Period / 2); Thread.Sleep(tone.GetDelay(Tempo)); channel.SetDutyCycle(0); } else { channel.SetDutyCycle(0); Thread.Sleep(tone.GetDelay(Tempo)); } }
public static void Main() { PWM servo = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5); LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true); // the pin will generate interrupt on high and low edges InterruptPort IntEncoderA = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.An0, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); InterruptPort IntEncoderB = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.An1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); // add an interrupt handler to the pin IntEncoderA.OnInterrupt += new NativeEventHandler(IntEncoderA_OnInterrupt); IntEncoderB.OnInterrupt += new NativeEventHandler(IntEncoderB_OnInterrupt); InputPort Button; bool button_state; Button = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, false, Port.ResistorMode.PullUp); while (true) { button_state = Button.Read(); if (button_state == false) { servo.SetPulse(20 * 1000 * 1000, 1300 * 1000); } if (button_state == true) { servo.SetPulse(20 * 1000 * 1000, 1500 * 1000); } Debug.Print("I: " + i); } }
/// <summary> /// Play a note /// </summary> public void PlayNote(uint period, uint duration, int beatCount) { if (disposed) { throw new ObjectDisposedException("Piezzo buzzer"); } // Play the note for the desired number of beats speaker.SetPulse(period, duration); Thread.Sleep(BeatTimeInMilliseconds * beatCount - PauseTimeInMilliseconds); // Pause for 1/10th of a beat in between every note. speaker.SetDutyCycle(0); Thread.Sleep(PauseTimeInMilliseconds); }
public static void Main() { uint watchdogTimer = 1000; PWM umbrella = new PWM(Pins.GPIO_PIN_D10); //Right controller Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); receiveSocket.Bind(new IPEndPoint(IPAddress.Any, 4444)); byte[] rxData = new byte[10]; // Incoming data buffer double raw_speed = 0; while (true) /* Main program loop */ { /* Try to receive new data - spend 100uS waiting */ if (receiveSocket.Poll(100, SelectMode.SelectRead)) { int rxCount = receiveSocket.Receive(rxData); watchdogTimer = 0; } if (watchdogTimer < 200) // Only enable the robot if data was received recently { // 900 (full rev) to 2100 (full fwd), 1500 is neutral raw_speed += (rxData[0] - 127.5) * .001; // Add the value of the stick to the current speed // Mediate added speed to negative if it's below center line(on ipgamepad). Make the added speed very little because the mount of UDP packets is large. // map function only accept input between 0-255 if (raw_speed < 0) { raw_speed = 0; } else if (raw_speed > 255) { raw_speed = 255; } // Stick maintains speed unless calibrate changes. umbrella.SetPulse(20000, map((uint)raw_speed, 0, 255, 1500, 2100)); // Right controller 1500-2100 -- only positive watchdogTimer++; } else { // Disable the robot umbrella.SetDutyCycle(0); } } }
/// <summary> /// Play a particular frequency for a defined /// time period /// </summary> /// <param name="frequency">The frequency (in hertz) of the note to be played</param> /// <param name="duration">How long (in milliseconds: 1000 = 1 second) the note is to play for</param> public void Play(float frequency, int duration) { if (!_busy) { _busy = true; // calculate the actual period and turn the // speaker on for the defined period of time uint period = (uint)(1000000 / frequency); _pin.SetPulse(period, period / 2); Thread.Sleep(duration); // turn the speaker off _pin.SetDutyCycle(0); _busy = false; } }
private static void PlaySong() { for (int i = 0; i < song.Length; i += 2) { // extract each note and its length in beats string note = song.Substring(i, 1); int beatCount = int.Parse(song.Substring(i + 1, 1)); // look up the note duration (in microseconds) uint noteDuration = (uint)scale[note]; // play the note for the desired number of beats speaker.SetPulse(noteDuration * 2, noteDuration); Thread.Sleep( beatTimeInMilliseconds * beatCount - pauseTimeInMilliseconds); // pause for 1/10th of a beat in between every note. speaker.SetDutyCycle(0); Thread.Sleep(pauseTimeInMilliseconds); } }
/// <summary> /// Constructor /// </summary> /// <param name="stepsPerRev">The number of steps per complete revolution of the output shaft</param> /// <param name="port">The port to which the stepper is connected</param> public Stepper(uint stepsPerRev, StepperPorts port) { StepsPerRevolution = stepsPerRev; stepperPort = port; currentStep = 0; microsteps = (uint)microstepCurve.Length; int latchState = 0; switch (stepperPort) { /* * case StepperPorts.M1_M2: * // Turn off all motor pins * latchState &= ~(1 << (int)MotorBits.Motor1_A) & * ~(1 << (int)MotorBits.Motor1_B) & * ~(1 << (int)MotorBits.Motor2_A) & * ~(1 << (int)MotorBits.Motor2_B); * * coilA = new PWM(PwmPins.pwm2A); * coilB = new PWM(PwmPins.pwm2B); * break; */ case StepperPorts.M3_M4: // turn off all motor pins latchState &= ~(1 << (int)MotorBits.Motor3_A) & ~(1 << (int)MotorBits.Motor3_B) & ~(1 << (int)MotorBits.Motor4_A) & ~(1 << (int)MotorBits.Motor4_B); coilA = new PWM(PwmPins.pwm0B); coilB = new PWM(PwmPins.pwm0A); break; default: throw new InvalidOperationException("Invalid motor header specified"); } latch_tx((byte)latchState); // Enable channels coilA.SetPulse(1000000 / 64000, 0); // 64KHz microstep pwm coilB.SetPulse(1000000 / 64000, 0); // 64KHz microstep pwm }
public static void Main() { var ledR = new PWM(Pins.GPIO_PIN_D9); var ledG = new PWM(Pins.GPIO_PIN_D6); var ledB = new PWM(Pins.GPIO_PIN_D5); while (true) { for (double i = 0; i < 1; i += 0.003) { var c = ColorRGB.Hsl2Rgb(i, 1.0, 0.5); ledR.SetPulse(255, c.R); ledG.SetPulse(255, c.G); ledB.SetPulse(255, c.B); Thread.Sleep(25); } } }
public static void Main() { var ledR = new PWM(Pins.GPIO_PIN_D9); var ledG = new PWM(Pins.GPIO_PIN_D6); var ledB = new PWM(Pins.GPIO_PIN_D5); while (true) { for (double i = 0; i < 1; i += 0.003) { var c = ColorRGB.Hsl2Rgb(i, 1.0, 0.5); ledR.SetPulse(255, c.R); ledG.SetPulse(255, c.G); ledB.SetPulse(255, c.B); Thread.Sleep(25); } } }
public static void Main() { int dashLength = 3; int pauseBetweenElements = 1; int pauseBetweenCharacters = 2; int pauseBetweenWords = 7; int duration = 90; PWM speaker = new PWM(Pins.GPIO_PIN_D5); while (true) { foreach (char curChar in "HELLO PO") { for (int index = ",ETINAMSDRGUKWOHBL,F,PJVXCQZYAA54A3AAA2AAAAAAA16AAAAAAA7AAA8A90".IndexOf(curChar); index > 0; index /= 2) { speaker.SetPulse(851u * 2, 851u); if ("-."[index-- % 2] == '.') { Thread.Sleep(duration); Debug.Print("."); } else { Thread.Sleep(duration * dashLength); Debug.Print("-"); } speaker.SetDutyCycle(0); Thread.Sleep(duration * pauseBetweenElements); } Thread.Sleep(duration * pauseBetweenCharacters); Debug.Print(" "); if (curChar.Equals(' ')) { Thread.Sleep(duration * pauseBetweenWords); } } Thread.Sleep(1400); } }
private void SetPwmValue(double value) { // Range checks if (value > 180) { value = 180; } if (value < 0) { value = 0; } // Are we inverted? if (_inverted) { value = 180 - value; } // Set the pulse _servo.SetPulse(20000, (uint)Map((long)value, 0, 180, range[0], range[1])); }
/// <summary> /// A DC Motor controller /// </summary> /// <param name="header">The header to which the motor is connected</param> /// <param name="frequency">The PWM frequency (in Hz) at which to drive the motor. Defaults to 10kHz.</param> public DCMotor(MotorHeaders header, uint frequency = 10000) { switch (header) { /* * case MotorHeaders.M1: * motorBitA = (int)MotorBits.Motor1_A; * motorBitB = (int)MotorBits.Motor1_B; * pwm = new PWM(PwmPins.pwm2A); * break; * case MotorHeaders.M2: * motorBitA = (int)MotorBits.Motor2_A; * motorBitB = (int)MotorBits.Motor2_B; * pwm = new PWM(PwmPins.pwm2B); * break; */ case MotorHeaders.M3: motorBitA = (int)MotorBits.Motor3_A; motorBitB = (int)MotorBits.Motor3_B; pwm = new PWM(PwmPins.pwm0B); break; case MotorHeaders.M4: motorBitA = (int)MotorBits.Motor4_A; motorBitB = (int)MotorBits.Motor4_B; pwm = new PWM(PwmPins.pwm0A); break; default: throw new InvalidOperationException("Invalid motor header specified"); } latchState &= (byte)(~(1 << motorBitA) & ~(1 << motorBitB)); latch_tx(latchState); // Set both motor pins low pwm.SetPulse(1000000 / frequency, 0); // Set PWM frequency, but 0% duty cycle }
public void Drive(int speed) { if (speed > 100) { speed = 100; } if (speed < -100) { speed = -100; } uint realSpeed = 0; if (speed > 0) { realSpeed = (uint)(1500 * (100 - speed) / 100); } if (speed < 0) { realSpeed = (uint)(1500 + (1500 * (-100 - speed) * -1 / 100)); } servo.SetPulse(10000, realSpeed); }
public static void Main() { Thread.Sleep(1000); led.Write(false); if (isCharging.Read()) { PWM printerLight = new PWM(Pins.GPIO_PIN_D5); printerLight.SetPulse(100, 0); printerLight.SetDutyCycle(100); while (true) { for (int pulse = 0; pulse < 15; pulse++) { for (uint i = 0; i < 100; i++) { printerLight.SetDutyCycle(i); Thread.Sleep(15); } for (uint i = 0; i < 100; i++) { printerLight.SetDutyCycle(100 - i); Thread.Sleep(10); } Thread.Sleep(2000); } } } try { System.Text.Encoding enc = System.Text.Encoding.UTF8; int fileNum = randy.Next(19999); var fileStream = File.Open(@"SD\rand.txt", FileMode.Open); byte[] randNum = new byte[30]; fileStream.Read(randNum, 0, 30); int seed = int.Parse(new string(enc.GetChars(randNum)).Trim()); randy = new Random(seed); fileStream.Close(); fileStream = File.OpenWrite(@"SD\rand.txt"); byte[] tempToWriteInt = enc.GetBytes((seed + 1).ToString() + " "); fileStream.Write(tempToWriteInt, 0, tempToWriteInt.Length); fileStream.Close(); var stream = File.Open(@"SD\chain\" + fileNum + ".txt", FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; stream.Read(buffer, 0, buffer.Length); stream.Close(); PrintText = new string(enc.GetChars(buffer)); } catch (Exception e) { PrinterTest("Could not access SD Card. Or other issues related to generating text"); } while (true) { iRobotTest(); //there is text to be printed! // todo: stop robot // todo: start lights flashing (probably around the printer) PWM printerLight = new PWM(Pins.GPIO_PIN_D5); printerLight.SetPulse(100, 0); printerLight.SetDutyCycle(100); for (int pulse = 0; pulse < 10; pulse++) { for (uint i = 0; i < 100; i++) { printerLight.SetDutyCycle(i); Thread.Sleep(3); } for (uint i = 0; i < 100; i++) { printerLight.SetDutyCycle(100 - i); Thread.Sleep(3); } } if (PRINT) { Thread.Sleep(1500); PrintPoem(PrintText); } for (int pulse = 0; pulse < 15; pulse++) { for (uint i = 0; i < 100; i++) { printerLight.SetDutyCycle(i); Thread.Sleep(10); } for (uint i = 0; i < 100; i++) { printerLight.SetDutyCycle(100 - i); Thread.Sleep(10); } } printerLight.SetDutyCycle(0); PowerState.RebootDevice(false); } }
public static void Main() { Radio = new SerialPort("COM1", 9600); Radio.Open(); // Radio.DataReceived += new SerialDataReceivedEventHandler(UART_DataReceived); uint i = 0; while (true) { if (leftPwr != 0) { leftMotor.SetPulse(100000, (uint)(((double)leftPwr / 100) * 90000.0)); } else { leftMotor.SetPulse(100000, 0); } if (rightPwr != 0) { rightMotor.SetPulse(100000, (uint)(((double)rightPwr / 100) * 90000.0)); } else { rightMotor.SetPulse(100000, 0); } //leftMotor.SetPulse(100000, 90000); Thread.Sleep(1); string input = ""; // read the data try { read_count = Radio.Read(rx_data, 0, Radio.BytesToRead); if (read_count > 0) { for (int k = 0; k < read_count; k++) { if (rx_data[k] == '$') { i = 0; } else if (rx_data[k] == '*') { Radio.Flush(); for (int j = 0; j < i; j++) { input += (char)str[j]; } ParseCommand(input); i = 0; read_count = 0; } else { str[i] = rx_data[k]; i++; if (i > str.Length) { i = 0; } } } } } catch { } } }
/// <summary> /// Play an single tone on a given channel /// </summary> /// <param name="tone">A RttlTone object</param> /// <param name="channel">Any PWN pin</param> public void PlayTone(RttlTone tone, PWM channel) { if (tone.Note != 0) { channel.SetPulse(tone.Period, tone.Period / 2); Thread.Sleep(tone.GetDelay(Tempo)); channel.SetDutyCycle(0); } else { channel.SetDutyCycle(0); Thread.Sleep(tone.GetDelay(Tempo)); } }
public override void Update(float throttle) { throttle = Logic.Constrain(throttle, _settings.MinimumOutput, _settings.MaximumOutput); throttle = _settings.MotorScale.Calculate(throttle); _pwmOutput.SetPulse((uint)throttle); }
public void TurnOn() { uint period = 1654; _pin.SetPulse(period, period / 2); }
public void MoveTo(uint position) { servo.SetPulse(FiftyHertz, position); }