Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Generic line detector class


  • Please log in to reply
No replies to this topic

#1 Asymptot

Asymptot

    Member

  • Members
  • PipPip
  • 19 posts

Posted 25 July 2012 - 04:58 PM

Hello,

while working on my line follower robot project, I had to make a class for reading the sensors of my line detector.
The idea is to read all sensors and send a grouped message to the main program in the form of an int.
I thought I'd make it as generic as possible for any sensor array that would use one digital input per sensor.


Here is the code :

/// <summary>
    /// This class represents the reflectance array - or line detector.
    /// I made it with the Parallax 8-reflectance sensors array in mind, but I'm trying to make it
    /// as generic as possible.
    /// </summary>
    class CLineDetector
    {
        private InputPort[] sensorArray;
        /// <summary>
        /// Represents the arrays of inputs used in the Netduino for the sensor array
        /// </summary>
        public InputPort[] SensorArray
        {
            get 
            {
                return sensorArray;
            }
            set 
            {
                value = sensorArray;
            }
        }

        private int arrayLength;
        /// <summary>
        /// Represents the numbers of sensors in the Line detector.
        /// </summary>
        public int ArrayLength
        {
            get 
            {
                return arrayLength;
            }
            set 
            {
                value = arrayLength;
            }
        }


        public CLineDetector()
        {

        }

        public CLineDetector(InputPort[] sensorarray)
        {
            sensorArray = sensorarray;
        }

        /// <summary>
        /// Reads all sensors and returns their grouped readings.
        /// </summary>
        /// <returns>A reading of the whole array</returns>
        public int ReadAll()
        {
            int iRes = 0;
            int iCount, iReading;
            

            //Reads all the sensors and put them all in a single int

            for (iCount = 0; iCount < sensorArray.Length; iCount++)
            {
                iReading = sensorArray[iCount].Read() ? 1 : 0;
                iRes += iReading << iCount;
            }
            // Put them in form

            return iRes;
        }
    }

I'm posting it for critics as to the generic-ness of the class, and also for two specific questions :

- It seems to me the SensorArray member really doesn't need to be public, since the rest of the program will only need to know the end result and the number of sensors. What do you think?
- The time factor, I didn't use any "sleep" in my "ReadAll" method, thinking it's more the main program's job. Am I right?

Thanks you for comments and critics :-).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.