public static void Main(string[] args) { InfraredSensor s = new InfraredSensor(Inputs.INPUT_4); var driveService1 = new DriveService(Outputs.OUTPUT_A, Outputs.OUTPUT_D); var driveService2 = new DriveService(Outputs.OUTPUT_B, Outputs.OUTPUT_C); s.SetIrRemote(); while (true) { Console.WriteLine("Sensor value: " + s.GetInt()); Thread.Sleep(100); if (Console.KeyAvailable) { var key = Console.ReadKey(); if (key.Key == ConsoleKey.Escape) { break; } } int value0 = s.GetInt(0); DriveState driveState1 = new DriveState(value0); driveService1.Drive(driveState1); int value1 = s.GetInt(1); DriveState driveState2 = new DriveState(value1); driveService2.Drive(driveState2); } }
/// <summary> /// Stop后有问题,暂时不可用关闭,要关闭要重启站点 /// </summary> /// <returns></returns> public async Task <IActionResult> Stop() { ishw = "stop"; sensor?.Dispose(); sensor = null; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "71", "红外遥控器关闭"); return(Content("红外遥控器关闭")); }
public IRIN(int pin) { Pi.Init <BootstrapWiringPi>(); mPin = pin; gpio = Pi.Gpio[mPin]; gpio.PinMode = GpioPinDriveMode.Input; mSignalVal = gpio.Read(); IRSensor = new InfraredSensor(gpio, true); }
/// <summary> /// Tests the infrared sensor HX1838. /// </summary> public static void TestInfraredSensor() { Console.Clear(); "Send a signal...".Info("IR"); var inputPin = Pi.Gpio[BcmPin.Gpio25]; // BCM Pin 25 or Physical pin 22 on the right side of the header. var sensor = new InfraredSensor(inputPin, true); sensor.DataAvailable += (s, e) => { Console.Clear(); var necData = InfraredSensor.NecDecoder.DecodePulses(e.Pulses); if (necData != null) { $"NEC Data: {BitConverter.ToString(necData).Replace("-", " "),12} Pulses: {e.Pulses.Length,4} Duration(us): {e.TrainDurationUsecs,6} Reason: {e.FlushReason}".Warn("IR"); if (InfraredSensor.NecDecoder.IsRepeatCode(e.Pulses)) { return; } } else { if (e.Pulses.Length >= 4) { var debugData = InfraredSensor.DebugPulses(e.Pulses); $"RX Length: {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Warn("IR"); debugData.Info("IR"); } else { $"RX (Garbage): {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Error("IR"); } } ExitMessage.WriteLine(); }; while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } sensor.Dispose(); }
/// <summary> /// Tests the infrared sensor HX1838. /// </summary> public static void TestInfraredSensor() { var inputPin = Pi.Gpio[P1.Gpio23]; // BCM Pin 23 or Physical pin 16 on the right side of the header. var sensor = new InfraredSensor(inputPin, true); var emitter = new InfraredEmitter(Pi.Gpio[P1.Gpio18]); sensor.DataAvailable += (s, e) => { var necData = InfraredSensor.NecDecoder.DecodePulses(e.Pulses); if (necData != null) { $"NEC Data: {BitConverter.ToString(necData).Replace("-", " "),12} Pulses: {e.Pulses.Length,4} Duration(us): {e.TrainDurationUsecs,6} Reason: {e.FlushReason}".Warn("IR"); if (InfraredSensor.NecDecoder.IsRepeatCode(e.Pulses)) { return; } // Test repeater signal var outputPulses = InfraredEmitter.NecEncoder.Encode(necData); emitter.Send(outputPulses); var debugData = InfraredSensor.DebugPulses(outputPulses); $"TX Length: {outputPulses.Length,5}".Warn("IR"); debugData.Info("IR"); } else { if (e.Pulses.Length >= 4) { var debugData = InfraredSensor.DebugPulses(e.Pulses); $"RX Length: {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Warn("IR"); debugData.Info("IR"); } else { $"RX (Garbage): {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Error("IR"); } } }; Console.ReadLine(); sensor.Dispose(); }
public async Task <IActionResult> Start(double speed = 0.8) { if (ishw == "stop") { var inputPin = Pi.Gpio[BcmPin.Gpio25]; // BCM Pin 25 or Physical pin 22 on the right side of the header. sensor = new InfraredSensor(inputPin, true); sensor.DataAvailable += async(s, e) => { Console.Clear(); var necData = InfraredSensor.NecDecoder.DecodePulses(e.Pulses); if (necData != null) { var decdata = BitConverter.ToString(necData); Console.WriteLine($"NEC Data: {decdata.Replace("-", " "),12} Pulses: {e.Pulses.Length,4} Duration(us): {e.TrainDurationUsecs,6} Reason: {e.FlushReason}"); #region 遥控器控制小车 switch (decdata) { case "DD-22-0B-F4": _l298n.Move(1, speed); L298NController.InitFX(); L298NController.isup = "start"; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "42", "前进."); break; case "DD-22-0D-F2": _l298n.Move(2, speed); L298NController.InitFX(); L298NController.isdown = "start"; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "43", "后退."); break; case "DD-22-0E-F1": _l298n.Move(3, speed); L298NController.InitFX(); L298NController.isleft = "start"; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "44", "左转."); break; case "DD-22-0C-F3": _l298n.Move(4, speed); L298NController.InitFX(); L298NController.isright = "start"; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "45", "右转."); break; case "DD-22-0A-F5": _l298n.Move(5, 0); L298NController.InitFX(); L298NController.ispause = "start"; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "46", "暂停."); break; } await _chatHub.Clients.All.SendAsync("ReceiveMessage", "3", $"NEC Data: {BitConverter.ToString(necData).Replace("-", " "),12}"); #endregion if (InfraredSensor.NecDecoder.IsRepeatCode(e.Pulses)) { return; } } else { if (e.Pulses.Length >= 4) { var debugData = InfraredSensor.DebugPulses(e.Pulses); //Console.WriteLine($"RX Length: {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}"); //Console.WriteLine($"Debug data: {debugData}"); await _chatHub.Clients.All.SendAsync("ReceiveMessage", "3", $"Debug data: {debugData}"); } else { //Console.WriteLine($"RX (Garbage): {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}"); } } }; } ishw = "start"; await _chatHub.Clients.All.SendAsync("ReceiveMessage", "70", "红外遥控器开启"); return(Content("红外遥控器开启")); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here InitalizeSerialCommunication(); lowerLeftBumper = new Bumper(); upperLeftBumper = new Bumper(); upperRightBumper = new Bumper(); lowerRightBumper = new Bumper(); wheels = new CoupledWheels(); wheels.ClicksPerTurn = 3900; wheels.WheelDistance = 0.345f; wheels.WheelRadius = 0.0467f; wheels.Updated += new EventHandler(wheels_Updated); sonar0 = new MaxbotixSonar(); sonar1 = new MaxbotixSonar(); sonar2 = new MaxbotixSonar(); sonar3 = new MaxbotixSonar(); sonar4 = new MaxbotixSonar(); groundSensor0 = new InfraredSensor(); groundSensor1 = new InfraredSensor(); groundSensor2 = new InfraredSensor(); redLed = new LedPwm(); greenLed = new LedPwm(); blueLed = new LedPwm(); drive = new DifferentialDrive(wheels); odometry = new DifferentialOdometry(wheels); proximity = new ProximityArray { { new Transform(0.15f, 0.14f, MathHelper.ToRadians(45)), sonar0 }, { new Transform(0.165f, 0.07f, MathHelper.ToRadians(20)), sonar1 }, { new Transform(0.18f, 0.0f, MathHelper.ToRadians(0)), sonar2 }, { new Transform(0.165f, -0.07f, MathHelper.ToRadians(-20)), sonar3 }, { new Transform(0.15f, -0.14f, MathHelper.ToRadians(-45)), sonar4 }, }; protocol = new ProtocolManager(); protocol.SubscribeComponent(lowerLeftBumper); protocol.SubscribeComponent(upperLeftBumper); protocol.SubscribeComponent(upperRightBumper); protocol.SubscribeComponent(lowerRightBumper); protocol.SubscribeComponent(wheels); protocol.SubscribeComponent(sonar0); protocol.SubscribeComponent(sonar1); protocol.SubscribeComponent(sonar2); protocol.SubscribeComponent(sonar3); protocol.SubscribeComponent(sonar4); protocol.SubscribeComponent(groundSensor0); protocol.SubscribeComponent(groundSensor1); protocol.SubscribeComponent(groundSensor2); protocol.SubscribeComponent(greenLed); protocol.SubscribeComponent(redLed); protocol.SubscribeComponent(blueLed); transport.Protocol = protocol; renderer = new SpriteRenderer(this); renderer.PixelsPerMeter = 100; Components.Add(renderer); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Services.AddService(typeof(SpriteBatch), spriteBatch); base.Initialize(); }