public CharacterDisplay( IPwmPort portV0, IDigitalOutputPort portRS, IDigitalOutputPort portE, IDigitalOutputPort portD4, IDigitalOutputPort portD5, IDigitalOutputPort portD6, IDigitalOutputPort portD7, ushort rows = 4, ushort columns = 20) { Console.WriteLine("Constructor with Contrast pin"); DisplayConfig = new TextDisplayConfig { Height = rows, Width = columns }; LCD_V0 = portV0; LCD_V0.Start(); LCD_RS = portRS; LCD_E = portE; LCD_D4 = portD4; LCD_D5 = portD5; LCD_D6 = portD6; LCD_D7 = portD7; Initialize(); }
public void RampUpDown(IPwmPort pwmPort, int totalDurationInMs) { var iterations = this.BreathingBrightnessProgressionHalf.Length; var delay = (totalDurationInMs / iterations) - this.delayInMilliseconds; if (delay <= 0) { delay = 0; } var stopwatch = Stopwatch.StartNew(); for (int index = 0; index < iterations; index++) { pwmPort.DutyCycle = 1 - this.BreathingBrightnessProgressionHalf[index]; pwmPort.Start(); Thread.Sleep(delay); } stopwatch.Stop(); var totalTime = ( int )stopwatch.ElapsedMilliseconds; var diff = totalTime - totalDurationInMs; var factor = diff / iterations; this.delayInMilliseconds += factor; pwmPort.Stop(); }
/// <summary> /// /// </summary> /// <param name="gearPwm">PWM port for controlling servo</param> public GearBox(IPwmPort gearPwm) { pwm = gearPwm; ServoConfig config = new ServoConfig(new Angle(0), new Angle(180), 500, 2500, 50); servo = new Servo(pwm, config); }
public PiezoSpeakerApp() { pwm = Device.CreatePwmPort(Device.Pins.D05); piezoSpeaker = new PiezoSpeaker(pwm); TestPiezoSpeaker(); }
public LedLamp(IPwmPort p, Fan f, string name) { port = p; p.DutyCycle = 0; Name = name; fan = f; }
public void RampUpDown(IPwmPort port, int totalDurationInMs, int count) { for (int iteration = 0; iteration < count; iteration++) { this.RampUpDown(port, totalDurationInMs); } }
public Tb67h420ftg( IPwmPort inA1, IPwmPort inA2, IDigitalOutputPort pwmA, IPwmPort?inB1, IPwmPort?inB2, IDigitalOutputPort?pwmB, IDigitalInputPort?fault1, IDigitalInputPort?fault2, IDigitalOutputPort?hbMode = null, IDigitalOutputPort?tblkab = null ) { this.inA1 = inA1; this.inA2 = inA2; this.pwmA = pwmA; this.inB1 = inB1; this.inB2 = inB2; this.pwmB = pwmB; this.fault1 = fault1; this.fault2 = fault2; this.hbMode = hbMode; this.tblkab = tblkab; ValidateConfiguration(); if (hbMode is null) { hbridgeMode = HBridgeMode.Dual; } //Motor1 = new Motor(this, inA1, inA2, pwmA, fault1); Motor1 = new HBridgeMotor(inA1, inA2, pwmA); if (inB1 != null && inB2 != null && pwmB != null) { //Motor2 = new Motor(this, inB1, inB2, pwmB, fault2); Motor2 = new HBridgeMotor(inB1, inB2, pwmB); } }
private void TimeScaleChecks(IPwmPort pwm) { var delta = 100; pwm.Frequency = 50f; pwm.Start(); while (true) { pwm.TimeScale = TimeScale.Seconds; pwm.Period = 0.02f; Console.WriteLine($"Freq: {(int)pwm.Frequency} Period: {(int)pwm.Period} {pwm.TimeScale}"); Thread.Sleep(2000); pwm.TimeScale = TimeScale.Milliseconds; Console.WriteLine($"Freq: {(int)pwm.Frequency} Period: {(int)pwm.Period} {pwm.TimeScale}"); Thread.Sleep(2000); pwm.Period = 50f; Console.WriteLine($"Freq: {(int)pwm.Frequency} Period: {(int)pwm.Period} {pwm.TimeScale}"); Thread.Sleep(2000); pwm.TimeScale = TimeScale.Microseconds; Console.WriteLine($"Freq: {(int)pwm.Frequency} Period: {(int)pwm.Period} {pwm.TimeScale}"); pwm.Period = 80f; Console.WriteLine($"Freq: {(int)pwm.Frequency} Period: {(int)pwm.Period} {pwm.TimeScale}"); Thread.Sleep(2000); } }
public ServoApp() { pwm = Device.CreatePwmPort(Device.Pins.D05); servo = new Servo(pwm, NamedServoConfigs.Ideal180Servo); TestServo(); }
/// <summary> /// Instantiates a new Servo on the specified PWM Pin with the specified config. /// </summary> /// <param name="pwm"></param> /// <param name="config"></param> public ServoBase(IPwmPort pwm, ServoConfig config) { _config = config; _pwm = pwm; _pwm.Frequency = config.Frequency; _pwm.DutyCycle = 0; _pwm.Start(); }
internal Motor( Tb67h420ftg driver, IPwmPort in1, IPwmPort in2, IDigitalOutputPort enable, IDigitalInputPort?fault) : base(in1, in2, enable) { this.driver = driver; this.fault = fault; Init(); }
/// <summary> /// Initializes a new instance of the SoundGenerator class /// </summary> /// <param name="inputPort1">First sound-level input port</param> /// <param name="inputPort2">Second sound-level input port</param> /// <param name="pwmPort">PWM port used to drive the speaker</param> public SoundGenerator(IDigitalInputPort inputPort1, IDigitalInputPort inputPort2, IPwmPort pwmPort) { this.volumeIn1 = inputPort1; this.volumeIn2 = inputPort2; this.speakerPWM = pwmPort; this.speaker = new PiezoSpeaker(this.speakerPWM); this.PlayInitialSound(); }
/// <summary> /// Instantiates a new Servo on the specified PWM Pin with the specified config. /// </summary> /// <param name="pwm"></param> /// <param name="config"></param> public ServoBase(IPwmPort pwm, ServoConfig config) { _config = config; // OLD //_pwm = new PWM(pin, config.Frequency, 0, false); // NEW _pwm = pwm; _pwm.Frequency = config.Frequency; _pwm.DutyCycle = 0; }
/// <summary> /// Instantiates a new continuous rotation servo on the specified pin, with the specified configuration. /// </summary> /// <param name="pin"></param> /// <param name="config"></param> public ContinuousRotationServoBase(IPwmPort pwm, ServoConfig config) { _config = config; // OLD //_pwm = new PWM(pin, config.Frequency, 0, false); // NEW _pwm = pwm; _pwm.Frequency = (config.Frequency); _pwm.DutyCycle = (0); }
/// <summary> /// Main constructor /// </summary> /// <param name="leftForwardPwm">PWM port responsible for left motor moving forward</param> /// <param name="leftBackPwm">PWM port responsible for left motor moving backward</param> /// <param name="rightForwardPwm">PWM port responsible for right motor moving forward</param> /// <param name="rightBackPwm">PWM port responsible for right motor moving backward</param> /// <param name="gearPwm">PWM port responsible for controling gear changing servo</param> public MotorController(IPwmPort rightForwardPwm, IPwmPort rightBackPwm, IPwmPort leftForwardPwm, IPwmPort leftBackPwm, IPwmPort gearPwm, IDigitalInputPort leftCounterPort, IDigitalInputPort righCounterPort, BNO055 posSens, CameraGimbal gimb, Action clQueue) { quarterCircle = fullCircle / 4; halfCircle = fullCircle / 2; positionSensor = posSens; gimbal = gimb; clearQueue = clQueue; circumference = teethCount * chainPitch / magnetsCount; leftMotor = new Motor(leftForwardPwm, leftBackPwm); rightMotor = new Motor(rightForwardPwm, rightBackPwm); gearbox = new GearBox(gearPwm); rightCounter = new HallEffectCounter(righCounterPort); leftCounter = new HallEffectCounter(leftCounterPort); rightCounter.Name = Side.Right; leftCounter.Name = Side.Left; #if DEBUG rightCounter.RegisterForCount(CountChanged); leftCounter.RegisterForCount(CountChanged); #endif rightCounter.RegisterForLimitReached(MoveForwardStop); leftCounter.RegisterForLimitReached(MoveForwardStop); positionSensor.RegisterForHeadingChanged(HeadingChanged); moveForwardResetEventLeft = new AutoResetEvent(false); moveForwardResetEventRight = new AutoResetEvent(false); turnResetEvent = new AutoResetEvent(false); turnTokenSource = new CancellationTokenSource(); turnPid = new IdealPidController(); turnPid.ProportionalComponent = 1.8f; turnPid.IntegralComponent = 0.05f; turnPid.DerivativeComponent = 0.01f; turnPid.OutputMax = 50; turnPid.OutputMin = -50; stabilizePid = new IdealPidController(); stabilizePid.ProportionalComponent = 1.8f; stabilizePid.IntegralComponent = 0.05f; stabilizePid.DerivativeComponent = 0.01f; stabilizePid.OutputMax = stabilizeTurnRate; stabilizePid.OutputMin = -stabilizeTurnRate; }
public CharacterDisplay( IPwmPort portV0, IDigitalOutputPort portRS, IDigitalOutputPort portE, IDigitalOutputPort portD4, IDigitalOutputPort portD5, IDigitalOutputPort portD6, IDigitalOutputPort portD7, byte rows = 4, byte columns = 20) { characterDisplay = new GpioCharacterDisplay(portV0, portRS, portE, portD4, portD5, portD6, portD7, rows, columns); }
public IContinuousRotationServo GetContinuousRotatioServo(byte num, ServoConfig servoConfig) { if ((num < 0) || (num > _ports)) { throw new ArgumentException($"Continuous Rotatio Servo num must be between 1 and {_ports}", "num"); } IPwmPort pwm = _pca9685.CreatePwmPort(num); ContinuousRotationServo servo = new ContinuousRotationServo(pwm, servoConfig); return(servo); }
public void ConfigurePorts() { Console.WriteLine("Creating Outputs..."); this.redPwm = Device.CreatePwmPort(Device.Pins.OnboardLedRed, pwmFrequency, 0f); this.bluPwm = Device.CreatePwmPort(Device.Pins.OnboardLedBlue, pwmFrequency, 0f); this.grePwm = Device.CreatePwmPort(Device.Pins.OnboardLedGreen, pwmFrequency, 0f); var busForDisplay = Device.CreateI2cBus( ) as I2cBus; busForDisplay.Frequency = i2cFrequency; Console.WriteLine("I2C bus frequency: {0}", busForDisplay.Frequency); this.display = new Ssd1306(busForDisplay, 0x3C, Ssd1306.DisplayType.OLED128x32); this.display.SendUGdash2832HSWEG02Startup(); this.display.InvertDisplay = true; this.display.Clear(true); this.chicagoFont = new ShikaakwaProportionalFont(); this.cornerFont = new CornerProportionalFont(); var yPosition = 3; this.line1 = this.chicagoFont.GetBitmapFromString("Programmed in C#").ShiftLsbToMsb(yPosition); this.line1Page = 0; yPosition += this.chicagoFont.Spacing; var shiftLineTwo = yPosition % 8; this.line2 = this.chicagoFont.GetBitmapFromString("by SupraBitKid").ShiftLsbToMsb(shiftLineTwo); this.line2Page = ( byte )(yPosition / 8); this.topLeftCorner = this.cornerFont.GetCharacter('a'); this.topRightCorner = this.cornerFont.GetCharacter('b'); this.bottomLeftCorner = this.cornerFont.GetCharacter('c'); this.bottomRightCorner = this.cornerFont.GetCharacter('d'); this.redColorText = this.chicagoFont.GetBitmapFromString("red").ShiftLsbToMsb(shiftLineTwo); this.greenColorText = this.chicagoFont.GetBitmapFromString("green").ShiftLsbToMsb(shiftLineTwo); this.blueColorText = this.chicagoFont.GetBitmapFromString("blue").ShiftLsbToMsb(shiftLineTwo); this.display.Clear(false); this.display.DrawBitmap(0, 0, this.topLeftCorner, Ssd1306Extensions.BitmapOp.Or); this.display.DrawBitmap(( byte )(this.display.Width - this.topRightCorner.Width), 0, this.topRightCorner, Ssd1306Extensions.BitmapOp.Or); this.display.DrawBitmap(0, ( byte )(this.display.GetPageCount() - 1), this.bottomLeftCorner, Ssd1306Extensions.BitmapOp.Or); this.display.DrawBitmap(( byte )(this.display.Width - this.bottomRightCorner.Width), ( byte )(this.display.GetPageCount() - 1), this.bottomRightCorner, Ssd1306Extensions.BitmapOp.Or); this.display.DrawBitmap(6, this.line1Page, this.line1, Ssd1306Extensions.BitmapOp.Or); this.display.DrawBitmap(6, this.line2Page, this.line2, Ssd1306Extensions.BitmapOp.Or); this.display.Show(); }
private void ChangeDirection() { if (currentPort == rPwm) { rPwm.DutyCycle = 0; Thread.Sleep(50); //Tihs is most important, as I've read IBT_2 can be easily fried with double PWM signal currentPort = lPwm; //...and 50ms is small enough to be invisible with such powerfull motors, but allows me to be sure } //...that previous line defiinetelu executed and electronics had enough time to execute it else { lPwm.DutyCycle = 0; Thread.Sleep(50); currentPort = rPwm; } }
/// <summary> /// Calculates new speed, determines if it needs to use forward or backward PWM port, /// Selectr correct one and sets correct duty cycle /// </summary> private void ChangeSpeed() { int newSpeed = baseSpeed + turnModifier; if (newSpeed >= 0 && CurrentPort != forwardPwm) { CurrentPort = forwardPwm; } else if (newSpeed < 0 && CurrentPort != backPwm) { CurrentPort = backPwm; } currentSpeed = newSpeed; newSpeed = Math.Abs(newSpeed); newSpeed = newSpeed > 100 ? 100 : newSpeed; CurrentPort.DutyCycle = newSpeed / 100.01f; }
private void FrequencyChecks(IPwmPort pwm) { var delta = 100; pwm.Start(); while (true) { Console.WriteLine($"Freq: {pwm.Frequency} Period: {pwm.Period} {pwm.TimeScale}"); Thread.Sleep(5000); pwm.Frequency += delta; if (pwm.Frequency <= 100 || pwm.Frequency >= 1000) { delta *= -1; } } }
/// <summary> /// Basic constructor /// </summary> /// <param name="ver">Vertical movement servo PWM port</param> /// <param name="hor">Horizontal movement servo PWM port</param> /// <param name="s">Inertial Measurement Unit to check current device position</param> public CameraGimbal(IPwmPort ver, IPwmPort hor, IPositionSensor s) { horizontalPort = hor; verticalPort = ver; horizontalCoinfig = new ServoConfig(minServoAngle, maxServoAngle, 370, 2700, 50); verticalConfig = new ServoConfig(minServoAngle, maxServoAngle, 370, 2700, 50); horizontal = new Servo(horizontalPort, horizontalCoinfig); vertical = new Servo(verticalPort, verticalConfig); sensor = s; source = new CancellationTokenSource(); angleDelta = maxServoAngle - maxAngle; quarterCircle = fullCircle / 4; locker = new object(); Test(); #if DEBUG stopwatch = new Stopwatch(); #endif }
private void DutyCycleChecks(IPwmPort pwm) { var delta = 0.10000f; pwm.Start(); while (true) { Console.WriteLine($"Duty: {pwm.DutyCycle} Duration: {pwm.Duration} {pwm.TimeScale}"); Thread.Sleep(2000); var temp = Math.Round(pwm.DutyCycle + delta, 1); pwm.DutyCycle = (float)temp; if (pwm.DutyCycle <= .00 || pwm.DutyCycle >= 1.0) { delta *= -1; } } }
private void DurationChecks(IPwmPort pwm) { var delta = 1f; pwm.TimeScale = TimeScale.Milliseconds; pwm.Start(); while (true) { Console.WriteLine($"Duty: {pwm.DutyCycle} Duration: {pwm.Duration} {pwm.TimeScale}"); Thread.Sleep(2000); var temp = Math.Round(pwm.Duration + delta, 0); pwm.Duration = (float)temp; if (pwm.Duration <= 000 || pwm.Duration >= 10.0) { delta *= -1; } } }
public GpioCharacterDisplay( IPwmPort portV0, IDigitalOutputPort portRS, IDigitalOutputPort portE, IDigitalOutputPort portD4, IDigitalOutputPort portD5, IDigitalOutputPort portD6, IDigitalOutputPort portD7, byte rows = 4, byte columns = 20) { DisplayConfig = new TextDisplayConfig { Height = rows, Width = columns }; LCD_V0 = portV0; LCD_V0.Start(); LCD_RS = portRS; LCD_E = portE; LCD_D4 = portD4; LCD_D5 = portD5; LCD_D6 = portD6; LCD_D7 = portD7; Initialize(); }
void Initialize() { Console.WriteLine("Initialize hardware..."); onboardLed = new RgbPwmLed(device: Device, redPwmPin: Device.Pins.OnboardLedRed, greenPwmPin: Device.Pins.OnboardLedGreen, bluePwmPin: Device.Pins.OnboardLedBlue, 3.3f, 3.3f, 3.3f, Meadow.Peripherals.Leds.IRgbLed.CommonType.CommonAnode); bus = Device.CreateI2cBus(400000); pca = new Pca9685(bus, 64, 500); pca.Initialize(); lPwm = pca.CreatePwmPort(14, 0); rPwm = pca.CreatePwmPort(15, 0); currentPort = rPwm; encoder = new RotaryEncoder(Device, Device.Pins.D15, Device.Pins.D11); encoder.Rotated += Encoder_Rotated; }
/// <summary> /// Main constructor /// </summary> /// <param name="forward">PWM port for forward signal</param> /// <param name="back">PWM port for backward signal</param> public Motor(IPwmPort forward, IPwmPort back) { forwardPwm = forward; backPwm = back; currentPwmPort = forward; }
/// <summary> /// Create a new PiezoSpeaker instance /// </summary> /// <param name="port"></param> public PiezoSpeaker(IPwmPort port) { Port = port; }
public Servo(IPwmPort pwm, ServoConfig config) : base(pwm, config) { }
/// <summary> /// Instantiates a new continuous rotation servo on the specified pin, with the specified configuration. /// </summary> /// <param name="pin"></param> /// <param name="config"></param> public ContinuousRotationServo(IPwmPort pwm, ServoConfig config) : base(pwm, config) { }