internal static void TestMotor() { BBBPinManager.AddMappingPWM(BBBPin.P9_14); BBBPinManager.ApplyPinSettings(RoverMain.ApplyDevTree); IPWMOutput MotorOut = PWMBBB.PWMDevice1.OutputA; IFilter <float> MotorFilter = new Average <float>(5); TalonMC Motor = new TalonMC(MotorOut, 1F, MotorFilter); Log.SetSingleOutputLevel(Log.Source.MOTORS, Log.Severity.DEBUG); Motor.SetSpeed(0.2f); /*while (true) * { * Log.Output(Log.Severity.DEBUG, Log.Source.MOTORS, "Outputs: " + Motor.TargetSpeed + ", " + ((PWMOutputBBB)MotorOut).GetOutput() + ", " + ((PWMOutputBBB)MotorOut).GetFrequency()); * //Motor.UpdateState(); * Thread.Sleep(100); * }*/ int Cycle = 0; while (true) { Motor.SetSpeed(((Cycle / 10) % 2 == 0) ? 1 : -1); Thread.Sleep(25); Cycle += 1; } }
internal static void TestPWM() { BBBPinManager.AddMappingPWM(BBBPin.P9_14); BBBPinManager.AddMappingPWM(BBBPin.P9_16); BBBPinManager.ApplyPinSettings(RoverMain.ApplyDevTree); IPWMOutput OutA = PWMBBB.PWMDevice1.OutputA; IPWMOutput OutB = PWMBBB.PWMDevice1.OutputB; PWMBBB.PWMDevice1.SetFrequency(5000); OutA.SetEnabled(true); OutB.SetEnabled(true); int Cycle = 0; while (true) { float A = (float)((Math.Sin(Cycle * Math.PI / 180.000D) + 1) / 2); // Sine waves! Fun! float B = (float)((Math.Sin(Cycle * Math.PI / 360.000D) + 1) / 2); OutA.SetOutput(A); OutB.SetOutput(B); Thread.Sleep(50); Cycle += 20; } }
static void Main(String[] args) { StateStore.Start("Start code"); BeagleBone.Initialize(SystemMode.DEFAULT, true); BBBPinManager.AddMappingPWM(BBBPin.P9_14); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_IF_NONE); IPWMOutput OutA = PWMBBB.PWMDevice1.OutputA; OutA.SetFrequency(50); OutA.SetOutput(0f); OutA.SetEnabled(true); float t = 0.1f; while (t < .9f) { OutA.SetOutput(t); t += 0.0001f; } while (t > .1f) { OutA.SetOutput(t); t -= 0.0001f; } OutA.Dispose(); }
public TalonMC(IPWMOutput PWMOut, float MaxSpeed, IFilter <float> SpeedFilter = null) { this.PWMOut = PWMOut; this.MaxSpeed = MaxSpeed; this.Filter = SpeedFilter; this.PWMOut.SetFrequency(333); this.PWMOut.SetEnabled(true); }
public void Initialize() { IPWMOutput MotorPWM = PWMBBB.PWMDevice2.OutputA; IPWMOutput ServoPWM = PWMBBB.PWMDevice0.OutputB; this.MotorCtrl = new TalonMC(MotorPWM, MOTOR_MAX_SPEED); this.DoorServo = new Servo(ServoPWM); }
/// <summary> Initializes a Talon Motor controller </summary> /// <param name="PWMOut"> PWM output to control the motor controller </param> /// <param name="MaxSpeed"> Limiting factor for speed (should never exceed + or - this val) </param> /// <param name="SpeedFilter"> Filter to use with MC. Good for ramp-up protection and other applications </param> public TalonMC(IPWMOutput PWMOut, float MaxSpeed, IFilter <float> SpeedFilter = null) { this.PWMOut = PWMOut; this.MaxSpeed = Math.Abs(MaxSpeed); this.Filter = SpeedFilter; this.SetSpeedDirectly(0.0f); this.PWMOut.SetFrequency(333); this.PWMOut.SetEnabled(true); }
/// <summary> Drives a basic PWM-controlled servo motor. </summary> /// <remarks> Servos usually don't care about the PWM frequency, but about the on-time of the pulses. Therefore, the range is configured with on-times instead of PWM percentage. </remarks> /// <param name="PWMOut"> The PWM output to use to control the servo. </param> /// <param name="AngleRange"> The range of the servo (check the datasheet). Usually between 180 and 300. </param> /// <param name="MaxTime_ms"> The on-time (in milliseconds) that corresponds with the maximum servo displacement. Usually between 1 and 3. </param> /// <param name="MinTime_ms"> The on-time (in milliseconds) that corresponds with the minimum servo displacement. Less than <see cref="MaxTime_ms"/>, usually between 0.5 and 2. </param> /// <param name="PWMFrequency"> The frequency to set the PWM output to. Usually between 10 and 200. </param> public Servo(IPWMOutput PWMOut, int AngleRange = 180, float MaxTime_ms = 2, float MinTime_ms = 1, float PWMFrequency = 50) { this.PWMOut = PWMOut; this.MaxTime = MaxTime_ms; this.MinTime = MinTime_ms; this.PWMFreq = PWMFrequency; this.AngleRange = AngleRange; this.PWMOut.SetFrequency(this.PWMFreq); }
public CytronMD30C(IPWMOutput PWMOut, IDigitalOut GPIOOut, float MaxSpeed, IFilter <float> SpeedFilter = null) { this.PWMOut = PWMOut; this.MaxSpeed = MaxSpeed; this.Filter = SpeedFilter; this.PWMOut.SetFrequency(5000); this.PWMOut.SetEnabled(true); this.GPIOOut = GPIOOut; this.GPIOOut.SetOutput(false); }
/// <summary> Provides easy output controls for an RGB LED. </summary> /// <param name="Red"> PWM output for the red LED channel. </param> /// <param name="Green"> PWM output for the green LED channel. </param> /// <param name="Blue"> PWM channel for the blue LED channel. </param> public RGBLED(IPWMOutput Red, IPWMOutput Green, IPWMOutput Blue) { this.Red = Red; this.Green = Green; this.Blue = Blue; this.RedScale = 1; this.GreenScale = 1; this.BlueScale = 1; SetOutput(0x811426); }
public void Initialize() { IPWMOutput MotorOut = PWMBBB.PWMDevice1.OutputB; this.MotorCtrl = new TalonMC(MotorOut, MOTOR_MAX_SPEED); //this.Pot = new Potentiometer(null, 300); //this.Pot.Turned += this.EventTriggered; this.GotoDrill(); }
public static void IPWMOutputConfig() { OutA = PWMBBB.PWMDevice1.OutputA; OutA.SetFrequency(50); OutA.SetOutput(0.0f); OutA.SetEnabled(true); OutB = PWMBBB.PWMDevice1.OutputB; OutB.SetFrequency(50); OutB.SetOutput(0.0f); OutB.SetEnabled(true); ServoSpinner = 0.05f; }
public Drill(IPWMOutput MotorPWM, IPWMOutput ServoPWM) { this.Out = MotorPWM; ((Scarlet.Components.Outputs.PCA9685.PWMOutputPCA9685)MotorPWM).Reset(); ((Scarlet.Components.Outputs.PCA9685.PWMOutputPCA9685)MotorPWM).SetPolarity(true); this.MotorCtrl = new TalonMC(MotorPWM, MOTOR_MAX_SPEED); this.DoorServo = new Servo(ServoPWM) { TraceLogging = true }; this.DoorServo.SetEnabled(true); }
public ArmMotorController() { Scarlet.Utilities.StateStore.Start("ARM"); BBBPinManager.AddMappingPWM(ShoulderPin); BBBPinManager.AddMappingPWM(ElbowPin); BBBPinManager.AddMappingPWM(WristPin1); BBBPinManager.AddMappingPWM(WristPin2); BBBPinManager.AddMappingGPIO(WristDirectionPin1, true, ResistorState.PULL_UP); BBBPinManager.AddMappingGPIO(WristDirectionPin2, true, ResistorState.PULL_UP); BBBPinManager.AddMappingADC(ShoulderPotPin); BBBPinManager.AddMappingADC(ElbowPotPin); BBBPinManager.AddMappingADC(WristPotPin); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_IF_NONE); BeagleBone.Initialize(SystemMode.DEFAULT, true); const double DegToRad = Math.PI / 180.0; a = new Arm((0, 0, Math.PI / 2, Math.PI / 2, 0, 0, 6.8), (0, 0, -76 * DegToRad, 100 * DegToRad, 0, 0, 28.0), (0, 0, -168.51 * DegToRad, -10 * DegToRad, 0, 0, 28.0), (-2 * Math.PI, 2 * Math.PI, -Math.PI / 2, Math.PI / 2, 0, 0, 12.75)); //(0, 12.75, -Math.PI / 2, Math.PI / 2)); LowPass <float> ShoulderFilter = new LowPass <float>(); LowPass <float> ElbowFilter = new LowPass <float>(); ShoulderPWM = PWMBBB.PWMDevice1.OutputB; ElbowPWM = PWMBBB.PWMDevice1.OutputA; IPWMOutput WristPWM1 = PWMBBB.PWMDevice2.OutputB; IPWMOutput WristPWM2 = PWMBBB.PWMDevice2.OutputA; IDigitalOut WristDirectionGPIO1 = new DigitalOutBBB(WristDirectionPin1); IDigitalOut WristDirectionGPIO2 = new DigitalOutBBB(WristDirectionPin2); IAnalogueIn ShoulderADC = new AnalogueInBBB(ShoulderPotPin); IAnalogueIn ElbowADC = new AnalogueInBBB(ElbowPotPin); IAnalogueIn WristADC = new AnalogueInBBB(WristPotPin); Shoulder = new TalonMC(ShoulderPWM, 0.2f, ShoulderFilter); Elbow = new TalonMC(ElbowPWM, 0.2f, ElbowFilter); Wrist1 = new CytronMD30C(WristPWM1, WristDirectionGPIO1, 0.3f); Wrist2 = new CytronMD30C(WristPWM2, WristDirectionGPIO2, 0.3f); ShoulderPot = new Potentiometer(ShoulderADC, 300); ElbowPot = new Potentiometer(ElbowADC, 300); WristPot = new Potentiometer(WristADC, 300); }
/// <summary> /// Prepares the rail for use by moving the motor all the way up to the top to find the zero position. /// </summary> public void Initialize() { // TODO: What happens when it is already at the top? This likely won't toggle the switch... this.Initializing = true; IPWMOutput MotorPWM = PWMBBB.PWMDevice2.OutputB; IDigitalIn LimitSw = new DigitalInBBB(BBBPin.P8_08); this.MotorCtrl = new TalonMC(MotorPWM, MOTOR_MAX_SPEED); this.Limit = new LimitSwitch(LimitSw, false); //this.Encoder = new Encoder(2, 3, 80); this.Limit.SwitchToggle += this.EventTriggered; //this.Encoder.Turned += this.EventTriggered; Timer TimeoutTrigger = new Timer() { Interval = INIT_TIMEOUT, AutoReset = false }; TimeoutTrigger.Elapsed += this.EventTriggered; TimeoutTrigger.Enabled = true; this.GotoTop(); }
static void Main(string[] args) { // Begin setup of Beaglebone pins StateStore.Start("SmartFlatbot"); BeagleBone.Initialize(SystemMode.DEFAULT, true); // Add Beaglebone mapings with Scarlet BBBPinManager.AddMappingPWM(BBBPin.P9_14); BBBPinManager.AddMappingPWM(BBBPin.P9_16); BBBPinManager.AddMappingGPIO(BBBPin.P9_15, true, ResistorState.NONE, true); BBBPinManager.AddMappingGPIO(BBBPin.P9_27, true, ResistorState.NONE, true); // Apply mappings to Beaglebone BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_IF_NONE); IDigitalOut Motor1Output = new DigitalOutBBB(BBBPin.P9_15); IDigitalOut Motor2Output = new DigitalOutBBB(BBBPin.P9_27); IPWMOutput OutA = PWMBBB.PWMDevice1.OutputA; IPWMOutput OutB = PWMBBB.PWMDevice1.OutputB; Motor1Output.SetOutput(false); Motor2Output.SetOutput(false); PWMBBB.PWMDevice1.SetFrequency(10000);; OutA.SetEnabled(true); OutB.SetEnabled(true); // Setup Motor Controls CytronMD30C[] Motor = new CytronMD30C[2]; Motor[0] = new CytronMD30C(OutA, Motor1Output, (float).5); Motor[1] = new CytronMD30C(OutB, Motor2Output, (float).5); Motor[0].SetSpeed(0); Motor[1].SetSpeed(0); }
public void Initialize() { this.Initializing = true; IPWMOutput MotorOut = PWMBBB.PWMDevice1.OutputA; IDigitalIn LimitSw = new DigitalInBBB(BBBPin.P8_12); this.MotorCtrl = new TalonMC(MotorOut, MOTOR_MAX_SPEED); this.Limit = new LimitSwitch(LimitSw, false); //this.Encoder = new Encoder(6, 7, 420); this.Limit.SwitchToggle += this.EventTriggered; //this.Encoder.Turned += this.EventTriggered; Timer TimeoutTrigger = new Timer() { Interval = INIT_TIMEOUT, AutoReset = false }; TimeoutTrigger.Elapsed += this.EventTriggered; TimeoutTrigger.Enabled = true; // Do init stuff. this.TargetAngle = 360; }
public Sample(IPWMOutput ServoPWM) { this.Servo = new Servo(ServoPWM); }
public Servo(IPWMOutput PWMOut) { this.PWMOut = PWMOut; this.PWMOut.SetFrequency(50); // TODO: Set this to an actual value, and check if this overrides others. }
public static void Main(string[] args) { Boolean turnMode = false; Boolean lineMode = false; double SetTurn = 0; double PTurn = 0; double ITurn = 0; double DTurn = 0; double SetDis = 0; double PDis = 0; double IDis = 0; double DDis = 0; // Begin setup of Beaglebone pins StateStore.Start("SmartFlatbot"); BeagleBone.Initialize(SystemMode.DEFAULT, true); // Add Beaglebone mapings with Scarlet BBBPinManager.AddMappingPWM(BBBPin.P9_14); BBBPinManager.AddMappingPWM(BBBPin.P9_16); BBBPinManager.AddMappingGPIO(BBBPin.P9_15, true, ResistorState.NONE, true); BBBPinManager.AddMappingGPIO(BBBPin.P9_27, true, ResistorState.NONE, true); // Apply mappings to Beaglebone BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_IF_NONE); IDigitalOut Motor1Output = new DigitalOutBBB(BBBPin.P9_15); IDigitalOut Motor2Output = new DigitalOutBBB(BBBPin.P9_27); IPWMOutput OutA = PWMBBB.PWMDevice1.OutputA; IPWMOutput OutB = PWMBBB.PWMDevice1.OutputB; Motor1Output.SetOutput(false); Motor2Output.SetOutput(false); PWMBBB.PWMDevice1.SetFrequency(10000);; OutA.SetEnabled(true); OutB.SetEnabled(true); // Setup Motor Controls CytronMD30C[] Motor = new CytronMD30C[2]; Motor[0] = new CytronMD30C(OutA, Motor1Output, (float).5); Motor[1] = new CytronMD30C(OutB, Motor2Output, (float).5); // Make rover (hopefully) stay still in begining. Motor[0].SetSpeed(0); Motor[1].SetSpeed(0); // Set the rover in turn only mode for testing if (args.Length > 0) { if (args[0].Equals("turn")) { turnMode = true; } else if (args[0].Equals("line")) { lineMode = true; } } /* Get the PID values from file PIDValues.txt * All values will be in double format * Data must be in this format below: * ------------------------------------------ * Line 1 Ignored * Line 2 Ignored * Line 3 Direction target value * Line 4 Direction P value * Line 5 Direction I value * Line 6 Direction D value * Line 7 Ignored * Line 8 Distance target value * Line 9 Distance P value * Line 10 Distance I value * Line 11 Distance D value * All lines after are ignored * ----------------------------------------- */ try { // Open the text file using a stream reader. using (StreamReader sr = new StreamReader("PIDValues.txt")) { // Read the stream to a string, and write the string to the console. sr.ReadLine(); sr.ReadLine(); // Ignore first two lines SetTurn = Convert.ToDouble(sr.ReadLine()); PTurn = Convert.ToDouble(sr.ReadLine()); ITurn = Convert.ToDouble(sr.ReadLine()); DTurn = Convert.ToDouble(sr.ReadLine()); sr.ReadLine(); SetDis = Convert.ToDouble(sr.ReadLine()); PDis = Convert.ToDouble(sr.ReadLine()); IDis = Convert.ToDouble(sr.ReadLine()); DDis = Convert.ToDouble(sr.ReadLine()); } } catch (Exception e) { Console.WriteLine("PIDValues.txt is either missing or in improper format:"); Console.WriteLine(e.Message); } Console.WriteLine("Direction Target, P, I, D: " + SetTurn + " " + PTurn + " " + ITurn + " " + DTurn); Console.WriteLine("Distance Target, P, I, D: " + SetDis + " " + PDis + " " + IDis + " " + DDis); // Set the PIDs PID directionPID = new PID(PTurn, ITurn, DTurn); directionPID.SetTarget(SetTurn); PID distancePID = new PID(PDis, IDis, DDis); directionPID.SetTarget(SetDis); // Start UDP communication // Listen for any IP with port 9000 UdpClient udpServer = new UdpClient(9000); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 9000); // Main Loop. Keep running until program is stopped do { // Get data from UDP. Convert it to a String // Note this is a blocking call. var data = udpServer.Receive(ref remoteEP); String dataString = Encoding.ASCII.GetString(data); Console.WriteLine(dataString); // Set the wheel values to be zero for now Double leftWheel = 0; Double rightWheel = 0; // Make sure the data received is not an empty string if (!String.IsNullOrEmpty(dataString)) { // Split the String into distance and direction // Is in format "Distance,Direction" // Distance is from 1 to 150 ish // Direction is from -10 to 10 degrees String[] speedDis = dataString.Split(','); Double distance = Convert.ToDouble(speedDis[0]); Double direction = (Convert.ToDouble(speedDis[1])); // If mode is set to not turn only the run if (!turnMode) { distancePID.Feed(distance); if (!Double.IsNaN(distancePID.Output)) { rightWheel += distancePID.Output; leftWheel += distancePID.Output; } } if (!lineMode) { directionPID.Feed(direction); if (!Double.IsNaN(directionPID.Output)) { rightWheel += directionPID.Output; leftWheel -= directionPID.Output; } } } // If data received is not a full string, stop motors. else { leftWheel = 0; rightWheel = 0; } // Set the speed for the motor controllers // Max speed is one Motor[0].SetSpeed((float)(rightWheel)); Motor[1].SetSpeed((float)(leftWheel)); } while (true); }
public static void Start(string[] args) { if (args.Length < 3) { TestMain.ErrorExit("io bbb command requires functionality to test."); } BeagleBone.Initialize(SystemMode.DEFAULT, true); switch (args[2].ToLower()) { case "digin": { if (args.Length < 4) { TestMain.ErrorExit("io bbb digin command requires pin to test."); } BBBPin InputPin = StringToPin(args[3]); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing digital input on BBB pin " + InputPin.ToString()); BBBPinManager.AddMappingGPIO(InputPin, false, ResistorState.PULL_DOWN); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_REGARDLESS); IDigitalIn Input = new DigitalInBBB(InputPin); while (true) { Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Current pin state: " + (Input.GetInput() ? "HIGH" : "LOW")); Thread.Sleep(250); } } case "digout": { if (args.Length < 4) { TestMain.ErrorExit("io bbb digout command requires pin to test."); } BBBPin OutputPin = StringToPin(args[3]); if (args.Length < 5) { TestMain.ErrorExit("io bbb digout command requires output mode (high/low/blink)."); } if (args[4] != "high" && args[4] != "low" && args[4] != "blink") { TestMain.ErrorExit("Invalid digout test mode supplied."); } Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing digital output on BBB pin " + OutputPin.ToString()); BBBPinManager.AddMappingGPIO(OutputPin, true, ResistorState.PULL_DOWN); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_REGARDLESS); IDigitalOut Output = new DigitalOutBBB(OutputPin); if (args[4] == "high") { Output.SetOutput(true); } else if (args[4] == "low") { Output.SetOutput(false); } else { bool Out = false; while (true) { Output.SetOutput(Out); Out = !Out; Thread.Sleep(250); } } break; } case "pwm": { if (args.Length < 4) { TestMain.ErrorExit("io bbb pwm command requires pin to test."); } BBBPin OutputPin = StringToPin(args[3]); if (args.Length < 5) { TestMain.ErrorExit("io bbb pwm command requires frequency."); } int Frequency = int.Parse(args[4]); if (args.Length < 6) { TestMain.ErrorExit("io bbb pwm command requires output mode."); } if (args[5] != "per" && args[5] != "sine") { TestMain.ErrorExit("io bbb pwm command invalid (per/sine)."); } if (args[5] == "per" && args.Length < 7) { TestMain.ErrorExit("io bbb pwm per must be provided duty cycle."); } BBBPinManager.AddMappingPWM(OutputPin); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_REGARDLESS); IPWMOutput Output = PWMBBB.GetFromPin(OutputPin); Output.SetFrequency(Frequency); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing PWM output on BBB pin " + OutputPin.ToString() + " at " + Frequency + "Hz."); if (args[5] == "per") { Output.SetOutput(int.Parse(args[6]) / 100F); Output.SetEnabled(true); Thread.Sleep(15000); // Not sure if it stops outputting when the program exits. } else { int Cycle = 0; while (true) { float Val = (float)((Math.Sin(Cycle * Math.PI / 180.000D) + 1) / 2); Output.SetOutput(Val); Thread.Sleep(50); Cycle += 20; } } break; } case "adc": { if (args.Length < 4) { TestMain.ErrorExit("io bbb adc command requires pin to test."); } BBBPin InputPin = StringToPin(args[3]); BBBPinManager.AddMappingADC(InputPin); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_REGARDLESS); IAnalogueIn Input = new AnalogueInBBB(InputPin); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing analogue input on BBB pin " + InputPin.ToString()); while (true) { Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "ADC Input: " + Input.GetInput() + " (Raw: " + Input.GetRawInput() + ")"); Thread.Sleep(250); } } case "int": { if (args.Length < 4) { TestMain.ErrorExit("io bbb int command requires pin to test."); } BBBPin InputPin = StringToPin(args[3]); if (args.Length < 5) { TestMain.ErrorExit("io bbb int command requires interrupt mode (rise/fall/both)."); } if (args[4] != "rise" && args[4] != "fall" && args[4] != "both") { TestMain.ErrorExit("Invalid interrupt mode supplied."); } BBBPinManager.AddMappingGPIO(InputPin, true, ResistorState.PULL_DOWN); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_REGARDLESS); IDigitalIn Input = new DigitalInBBB(InputPin); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing interrupts on BBB pin " + InputPin.ToString()); switch (args[4]) { case "rise": ((IInterruptSource)Input).RegisterInterruptHandler(GetInterrupt, InterruptType.RISING_EDGE); break; case "fall": ((IInterruptSource)Input).RegisterInterruptHandler(GetInterrupt, InterruptType.FALLING_EDGE); break; case "both": ((IInterruptSource)Input).RegisterInterruptHandler(GetInterrupt, InterruptType.ANY_EDGE); break; } while (true) { Thread.Sleep(50); } // Program needs to be running to receive. } case "mtk3339": { BBBPinManager.AddMappingUART(BBBPin.P9_24); BBBPinManager.AddMappingUART(BBBPin.P9_26); BBBPinManager.ApplyPinSettings(BBBPinManager.ApplicationMode.APPLY_REGARDLESS); IUARTBus UART = UARTBBB.UARTBus1; Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Press any key to stop."); while (Console.KeyAvailable) { Console.ReadKey(); } byte[] Buffer = new byte[32]; while (true) { Thread.Sleep(10); if (UART.BytesAvailable() > 0) { int Count = UART.Read(32, Buffer); Console.Write(System.Text.Encoding.ASCII.GetString(UtilMain.SubArray(Buffer, 0, Count))); } if (Console.KeyAvailable) { break; } } break; } } }
public static void Start(string[] args) { if (args.Length < 3) { TestMain.ErrorExit("io pi command requires functionality to test."); } RaspberryPi.Initialize(); switch (args[2].ToLower()) { case "digin": { if (args.Length < 4) { TestMain.ErrorExit("io pi digin command requires pin to test."); } int PinNum = int.Parse(args[3]); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing digital input on RPi pin " + PinNum); IDigitalIn Input = new DigitalInPi(PinNum); while (true) { Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Current pin state: " + (Input.GetInput() ? "HIGH" : "LOW")); Thread.Sleep(250); } } case "digout": { if (args.Length < 4) { TestMain.ErrorExit("io pi digout command requires pin to test."); } int PinNum = int.Parse(args[3]); if (args.Length < 5) { TestMain.ErrorExit("io pi digout command requires output mode (high/low/blink)."); } if (args[4] != "high" && args[4] != "low" && args[4] != "blink") { TestMain.ErrorExit("Invalid digout test mode supplied."); } Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing digital output on RPi pin " + PinNum); IDigitalOut Output = new DigitalOutPi(PinNum); if (args[4] == "high") { Output.SetOutput(true); } else if (args[4] == "low") { Output.SetOutput(false); } else { bool Out = false; while (true) { Output.SetOutput(Out); Out = !Out; Thread.Sleep(250); } } break; } case "pwm": { TestMain.ErrorExit("io pi pwm command not yet implemented."); // TODO: Remove when implementing. if (args.Length < 4) { TestMain.ErrorExit("io pi pwm command requires pin to test."); } int PinNum = int.Parse(args[3]); if (args.Length < 5) { TestMain.ErrorExit("io pi pwm command requires frequency."); } int Frequency = int.Parse(args[4]); if (args.Length < 6) { TestMain.ErrorExit("io pi pwm command requires output mode."); } if (args[5] != "per" && args[5] != "sine") { TestMain.ErrorExit("io pi pwm command invalid (per/sine)."); } if (args[5] == "per" && args.Length < 7) { TestMain.ErrorExit("io pi pwm per must be provided duty cycle."); } IPWMOutput Output = null; // TODO: Implement RPi PWM output. Output.SetFrequency(Frequency); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing PWM output on RPi pin " + PinNum + " at " + Frequency + "Hz."); if (args[5] == "per") { Output.SetOutput(int.Parse(args[6]) / 100F); Output.SetEnabled(true); Thread.Sleep(15000); // Not sure if it stops outputting when the program exits. } else { int Cycle = 0; while (true) { float Val = (float)((Math.Sin(Cycle * Math.PI / 180.000D) + 1) / 2); Output.SetOutput(Val); Thread.Sleep(50); Cycle += 20; } } break; } case "adc": { TestMain.ErrorExit("RPI does not have an ADC."); break; } case "int": { if (args.Length < 4) { TestMain.ErrorExit("io pi int command requires pin to test."); } int PinNum = int.Parse(args[3]); if (args.Length < 5) { TestMain.ErrorExit("io pi int command requires interrupt mode (rise/fall/both)."); } if (args[4] != "rise" && args[4] != "fall" && args[4] != "both") { TestMain.ErrorExit("Invalid interrupt mode supplied."); } IDigitalIn Input = new DigitalInPi(PinNum); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing interrupts on RPi pin " + PinNum); switch (args[4]) { case "rise": ((IInterruptSource)Input).RegisterInterruptHandler(GetInterrupt, InterruptType.RISING_EDGE); break; case "fall": ((IInterruptSource)Input).RegisterInterruptHandler(GetInterrupt, InterruptType.FALLING_EDGE); break; case "both": ((IInterruptSource)Input).RegisterInterruptHandler(GetInterrupt, InterruptType.ANY_EDGE); break; } while (true) { Thread.Sleep(50); } // Program needs to be running to receive. } case "outperf": { if (args.Length < 4) { TestMain.ErrorExit("io pi outperf command requires pin to test."); } int PinNum = int.Parse(args[3]); Log.Output(Log.Severity.INFO, Log.Source.HARDWAREIO, "Testing digital output speed on RPi pin " + PinNum); IDigitalOut Output = new DigitalOutPi(PinNum); bool Out = false; while (!Console.KeyAvailable) { Output.SetOutput(Out); Out = !Out; } Output.SetOutput(false); break; } } }
} // -100.0 to 100.0 public AdafruitMotor(IPWMOutput Output) { Output.SetEnabled(true); this.Output = Output; }