Example #1
0
        private void RangerDistanceChangedEvent(object sender, RangerSensorEventArgs e)
        {
            //Debug.WriteLine("Ranger: " + e.Name + "         RangeMeters=" + e.RangeMeters);

            lock (currentSensorsDataLock)
            {
                ISensorsData sensorsData = new SensorsDataPlucky(this.currentSensorsData);

                switch (e.Name)
                {
                case "ParkingSonar":
                    sensorsData.RangerFrontRightMeters = e.RangeMeters[0];
                    sensorsData.RangerFrontLeftMeters  = e.RangeMeters[1];
                    sensorsData.RangerRearRightMeters  = e.RangeMeters[2];
                    sensorsData.RangerRearLeftMeters   = e.RangeMeters[3];

                    sensorsData.RangerFrontRightMetersTimestamp    = sensorsData.RangerFrontLeftMetersTimestamp =
                        sensorsData.RangerRearRightMetersTimestamp = sensorsData.RangerRearLeftMetersTimestamp = e.TimeTicks;
                    break;

                default:
                    throw new NotImplementedException("Error: RangerDistanceChangedEvent: unknown name " + e.Name);
                }

                //Debug.WriteLine(sensorsData.ToString());

                this.currentSensorsData = sensorsData;
            }
        }
Example #2
0
        private void RPiCameraSensor_TargetsChanged(object sender, TargetingCameraEventArgs args)
        {
            // Raspberry Pi based camera sensor works under Wheezy and uses OpenCV and Python to process
            // 240x320 frames and select areas with yellow color. Bearing, inclination and size of blobs is then
            // reported over HTTP to RPiCamera (derived from HttpServerBase). Frequency is around 10 FPS.

            //Debug.WriteLine("RPi Camera Event: " + args);

            // On Raspberry Pi:
            //      pixy.blocks[i].signature    The signature number of the detected object (1-7)
            //      pixy.blocks[i].x       The x location of the center of the detected object (0 to 319)
            //      pixy.blocks[i].y       The y location of the center of the detected object (0 to 199)
            //      pixy.blocks[i].width   The width of the detected object (1 to 320)
            //      pixy.blocks[i].height  The height of the detected object (1 to 200)

            // Field of view:
            //     goal 45 degrees  left  x=10
            //                    middle  x=160
            //     goal 45 degrees right  x=310
            //
            //     goal 30 degrees  up    y=10
            //                    middle  y=90
            //     goal 30 degrees down   y=190
            //

            if (args.width * args.height > 500) // only large objects count
            {
                int bearing     = GeneralMath.map(args.x, 0, 320, 45, -45);
                int inclination = GeneralMath.map(args.y, 0, 200, 30, -30);

                //Debug.WriteLine("RPi: bearing=" + bearing + "  inclination: " + inclination);

                lock (currentSensorsDataLock)
                {
                    ISensorsData sensorsData = new SensorsDataPlucky(this.currentSensorsData);

                    sensorsData.TargetingCameraBearingDegrees     = bearing;
                    sensorsData.TargetingCameraInclinationDegrees = inclination;
                    sensorsData.TargetingCameraTimestamp          = args.timestamp;

                    //Debug.WriteLine(sensorsData.ToString());

                    this.currentSensorsData = sensorsData;
                }
            }
        }
Example #3
0
        private void Ahrs_ValuesChanged(IHardwareComponent sender)
        {
            // AHRS is based on Arduino Motion Plug sketch, a pro-mini is connected to Plucky Wheels using I2C.

            IAhrs  ahrs    = (IAhrs)sender;
            double heading = Direction.to360(ahrs.Yaw);

            //Debug.WriteLine("Compass: heading=" + heading);

            lock (currentSensorsDataLock)
            {
                ISensorsData sensorsData = new SensorsDataPlucky(this.currentSensorsData);
                sensorsData.CompassHeadingDegrees = heading;
                //Debug.WriteLine(sensorsData.ToString());

                this.currentSensorsData = sensorsData;
            }
        }
Example #4
0
        /// <summary>
        /// we can create sensors here, but cannot send commands before bridge_CommunicationStarted is called in PluckyTheRobot
        /// for example, encoder.Clear() will hang.
        /// </summary>
        public async Task InitSensors(CancellationTokenSource cts)
        {
            // see C:\Projects\Serializer_3_0\ExampleApps\AnalogSensorExample\AnalogSensorExample\Form1.cs

            // Note: the Element board communicates at 19200 Baud, which is roughly 1.5 kbytes/sec
            //       Comm link is busy with motor commands and has to be responsive to encoders, for odometry to work.
            //       Sensors must carefully adjust their demands by setting UpdateFrequency and Enabled properties.

            // *********************** Parking Sonar:
            SensorPose spParkingSonar = new SensorPose()
            {
                XMeters = 0.0d, YMeters = 0.0d, ThetaRadians = 0.0d
            };

            ParkingSonar = RangerSensorFactory.produceRangerSensor(RangerSensorFactoryProducts.RangerSensorParkingSonar, "ParkingSonar", spParkingSonar,
                                                                   hardwareBrick, rangersSamplingIntervalMs);
            ParkingSonar.distanceChangedEvent += new EventHandler <RangerSensorEventArgs>(RangerDistanceChangedEvent);
            RangerSensors.Add(ParkingSonar.Name, ParkingSonar);

            // *********************** wheel encoders - roughly XXX ticks per wheel revolution, XXX ticks per meter.
            encoderLeft  = CreateWheelEncoder(hardwareBrick, WheelEncoderId.Encoder2, (int)mainLoopCycleMs, encodersSensitivityThresholdTicks);
            encoderRight = CreateWheelEncoder(hardwareBrick, WheelEncoderId.Encoder1, (int)mainLoopCycleMs, encodersSensitivityThresholdTicks);

            Ahrs = hardwareBrick.produceAhrs(0x00, AhrsSamplingIntervalMs, AhrsSensitivityThreshold);
            Ahrs.ValuesChanged += new HardwareComponentEventHandler(Ahrs_ValuesChanged);

            await RPiCameraSensor.Open(cts);

            RPiCameraSensor.TargetingCameraTargetsChanged += RPiCameraSensor_TargetsChanged;

            batteryVoltage = CreateBatteryVoltageMeter(hardwareBrick, batterySamplingIntervalMs, batterySensitivityThresholdVolts);

            batteryVoltage.Enabled = true;  // slow update rate, leave it turned on

            SonarsEnabled    = true;
            EncodersEnabled  = true;
            CompassEnabled   = true;
            RPiCameraEnabled = true;

            currentSensorsData = new SensorsDataPlucky()
            {
                RangerSensors = this.RangerSensors
            };
        }
Example #5
0
        internal void ArduinoBrickOdometryChanged(RobotAbstraction.IHardwareComponent sender)
        {
            IOdometry odom = (IOdometry)sender;

            //odom.XMeters;
            //odom.YMeters;
            //odom.ThetaRadians;

            lock (currentSensorsDataLock)
            {
                SensorsDataPlucky sensorsData = new SensorsDataPlucky(this.currentSensorsData);

                sensorsData.WheelEncoderLeftTicks  = odom.LDistanceTicks;
                sensorsData.WheelEncoderRightTicks = odom.RDistanceTicks;

                //Debug.WriteLine(sensorsData.ToString());

                this.currentSensorsData = sensorsData;
            }
        }
Example #6
0
        internal void ArduinoBrickGpsChanged(RobotAbstraction.IHardwareComponent sender)
        {
            IGps gps = (IGps)sender;

            lock (currentSensorsDataLock)
            {
                SensorsDataPlucky sensorsData = new SensorsDataPlucky(this.currentSensorsData);

                sensorsData.GpsLatitude  = gps.Latitude;
                sensorsData.GpsLongitude = gps.Longitude;
                sensorsData.GpsAltitude  = gps.Altitude;
                sensorsData.GpsNsat      = gps.GpsNsat;
                sensorsData.GpsHdop      = gps.GpsHdop;
                sensorsData.FixAgeMs     = gps.FixAgeMs;
                sensorsData.GpsTimeUTC   = gps.TimeUTC;
                sensorsData.GpsFixType   = gps.FixType;

                //Debug.WriteLine(sensorsData.ToString());

                this.currentSensorsData = sensorsData;
            }
        }
Example #7
0
        private void encoder_CountChanged(IHardwareComponent sender)
        {
            lock (currentSensorsDataLock)
            {
                SensorsDataPlucky sensorsData = new SensorsDataPlucky(this.currentSensorsData);

                IWheelEncoder encoder = sender as IWheelEncoder;

                Debug.Assert(encoder != null, "SensorsControllerPlucky: encoder_CountChanged(): Encoder must be non-null");

                if (encoder.WheelEncoderId == WheelEncoderId.Encoder2)
                {
                    sensorsData.WheelEncoderLeftTicks = encoder.Count;
                }
                else if (encoder.WheelEncoderId == WheelEncoderId.Encoder1)
                {
                    sensorsData.WheelEncoderRightTicks = encoder.Count;
                }

                //Debug.WriteLine(sensorsData.ToString());

                this.currentSensorsData = sensorsData;
            }
        }
Example #8
0
        private void batteryVoltage_ValueChanged(IHardwareComponent sender)
        {
            lock (currentSensorsDataLock)
            {
                SensorsDataPlucky sensorsData = new SensorsDataPlucky(this.currentSensorsData);

                IAnalogSensor bv = sender as IAnalogSensor;

                Debug.Assert(bv != null, "SensorsControllerPlucky: batteryVoltage_ValueChanged(): AnalogSensor must be non-null");

                // analog pin 5 in Element board is internally tied to 1/3 of the supply voltage level.
                // The 5.0d is 5V, microcontroller's ADC reference and 1024 is range.
                // on Plucky the battery voltage goes through 1/3 divider and comes to Arduino Pin 3, which is reported as Pin 5 to here.
                // see PluckyWheels.ino sketch

                double voltage = 3.0d * (bv.AnalogValue * 5.0d) / 1024.0d;

                sensorsData.BatteryVoltage = voltage;   // also sets sensorsData.BatteryVoltageTimestamp

                this.currentSensorsData = sensorsData;
            }

            Debug.WriteLine(this.currentSensorsData.ToString());
        }