Main Site Documentation

Won't run...don't understand


#1

I’m porting and making some “improvements” to Fabien Royer’s AnalogJoystick class (http://netduinohelpers.codeplex.com/). However, after making some changes I found that I could no longer run the program. It sits in the “Preparing to deploy assemblies to the device…” state forever until canceled or times out. Not really anything in the output. I’ve narrowed it down to one change that seems to be the hold up but I can’t make sense of why it shouldn’t work. All I basically did was add a reference of one property inside of another. See the X property below. If you want entire project, let me know where to put it.


using System;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;

namespace Joystick_Tutorial
{
    /// <summary>
    /// AnalogJoystick class from the netduino Helpers project (http://netduinohelpers.codeplex.com/) by Fabien Royer.
    /// This class interfaces with an analog x, y joystick such as a the ones found in game controllers (PS/2, Xbox 360, etc.)
    /// 
    /// Tested with a thumb joystick by Sparkfun: http://www.sparkfun.com/products/9032
    /// Datasheet: http://www.p3america.com/pp/pdfs/802.PDF
    /// </summary>
    public class AnalogJoystick : IDisposable
    {
        protected AnalogIn Xinput;
        protected AnalogIn Yinput;

        /// <summary>
        /// Returns the current raw x position
        /// </summary>
/* WORKS */        
        public int X
        {
            get
            {
                return Xinput.Read();
            }
        }

/* DOESN'T WORK 
        public int X
        {
            get
            {
                var direction = XDirection;
                switch (direction)
                {
                    case Direction.Negative:
                        return Xinput.Read(); // +XTrim.Negative;
                    case Direction.Positive:
                        return Xinput.Read(); // +XTrim.Positive;
                }
                return Xinput.Read();
            }
        }
*/

        /// <summary>
        /// Returns the current raw y position
        /// </summary>
        public int Y { get { return Yinput.Read(); } }

        // Upper end of of the user-defined value range for the X and Y axis
        protected int MaxRange;

        // Defines a relative area around the center of the joystick where the values fluctuate constantly and should be treated as 'center'.
        protected int CenterDeadZoneRadius;

        /// <summary>
        /// Auto-calibration min and max values defining the logical center of the X axis
        /// </summary>
        protected int XMinCenter { get; set; }
        protected int XMaxCenter { get; set; }

        /// <summary>
        /// Auto-calibration min and max values defining the logical center of the Y axis
        /// </summary>
        protected int YMinCenter { get; set; }
        protected int YMaxCenter { get; set; }

        /// <summary>
        /// Relative direction definitions
        /// </summary>
        public enum Direction
        {
            Center = 0,
            Negative = -1,
            Positive = 1
        }

        /// <summary>
        /// Returns the relative direction in which the joystick is moving on the X axis
        /// </summary>
        public Direction XDirection
        {
            get
            {
                int tempX = X;

                if (tempX >= XMinCenter && tempX <= XMaxCenter)
                {
                    return Direction.Center;
                }

                if (tempX < XMinCenter)
                {
                    return Direction.Negative;
                }

                if (tempX > XMaxCenter)
                {
                    return Direction.Positive;
                }

                return Direction.Center;
            }
        }

        /// <summary>
        /// Returns the relative direction in which the joystick is moving on the Y axis
        /// </summary>
        public Direction YDirection
        {
            get
            {
                int tempY = Y;

                if (tempY >= YMinCenter && tempY <= YMaxCenter)
                {
                    return Direction.Center;
                }

                if (tempY < YMinCenter)
                {
                    return Direction.Negative;
                }

                if (tempY > YMaxCenter)
                {
                    return Direction.Positive;
                }

                return Direction.Center;
            }
        }

        public double Angle
        {
            get
            {
                return Trigo.Atan2(Y - 512, X - 512);
            }
        }

        public double Amplitude
        {
            get
            {
                var x = X - 512;
                var y = Y - 512;
                return Trigo.Sqrt(x * x + y * y);
            }
        }

        /// <summary>
        /// Automatically determines the range of values defining the center for the X and Y axis.
        /// Assumes that the joystick is at the center position on the X & Y axis.
        /// Do not touch the joystick during auto-calibration :)
        /// </summary>
        /// <param name="centerDeadZoneRadius">A user-defined radius used to eliminate spurious readings around the center</param>
        public void AutoCalibrateCenter(int centerDeadZoneRadius)
        {
            XMinCenter = X;
            XMaxCenter = XMinCenter;
            YMinCenter = Y;
            YMaxCenter = YMinCenter;

            for (var I = 0; I < 100; I++)
            {
                var tempX = X;
                var tempY = Y;

                if (tempX < XMinCenter)
                {
                    XMinCenter = tempX;
                }

                if (tempX > XMaxCenter)
                {
                    XMaxCenter = tempX;
                }

                if (tempY < YMinCenter)
                {
                    YMinCenter = tempY;
                }

                if (tempY > YMaxCenter)
                {
                    YMaxCenter = tempY;
                }
            }

            XMinCenter -= centerDeadZoneRadius;
            YMaxCenter += centerDeadZoneRadius;
        }

        /// <summary>
        /// Expects two analog pins connected to the x & y axis of the joystick.
        /// </summary>
        /// <param name="xAxisPin">Analog pin for the x axis</param>
        /// <param name="yAxisPin">Analog pin for the y axis</param>
        /// <param name="minRange"></param>
        /// <param name="maxRange"></param>
        /// <param name="centerDeadZoneRadius"></param>
        public AnalogJoystick(Cpu.Pin xAxisPin, Cpu.Pin yAxisPin, int minRange = 0, int maxRange = 1023, int centerDeadZoneRadius = 10)
        {
            MaxRange = maxRange;

            Xinput = new AnalogIn((AnalogIn.Pin)xAxisPin);
            Xinput.SetLinearScale(minRange, maxRange);

            Yinput = new AnalogIn((AnalogIn.Pin)yAxisPin);
            Yinput.SetLinearScale(minRange, maxRange);

            AutoCalibrateCenter(centerDeadZoneRadius);
        }

        /// <summary>
        /// Frees the resources allocated for reading values from the analog joystick
        /// </summary>
        public void Dispose()
        {
            Xinput.Dispose();
            Xinput = null;

            Yinput.Dispose();
            Yinput = null;
        }
    }
}



#2

It’s probably never resting. Try hitting the rest button on the device while pushing; should error out and then work next time through.

Or you could always use MFDeploy to erase the app.


#3

Doesn’t seem to work. However, it did show me a link error when I hit reset. Which gave me an idea… So, I enabled both the Works version and the Don’t Work version and called the working version X and the non-working version X2. Compile and ran fine. I then reversed their names and now it doesn’t work. So, it seems the linker/compiler is getting confused. I’ve tried removing and re-adding all references.

Here is my very basic main()


        public static void Main()
        {
            var leftJoystick = new AnalogJoystick((Cpu.Pin)FEZ_Pin.AnalogIn.An0, (Cpu.Pin)FEZ_Pin.AnalogIn.An1, -100, 100);

            while (true)
            {
                Debug.Print("X=" + leftJoystick.X.ToString() + " (" + leftJoystick.XDirection.ToString() + ")" + ", Y=" + leftJoystick.Y.ToString() + " (" + leftJoystick.YDirection.ToString() + ")"
                    + "  Angle=" + leftJoystick.Angle + "  Amplitude=" + leftJoystick.Amplitude);
                Debug.Print(DateTime.Now.ToString());
                Thread.Sleep(80);
            }
        }


#4

Does no one have any ideas about what is going on with this? I’m at a total loss and can’t move forward. I’ve even gone as far as trying the same code on a netduino and got the same results. So, I’m certain it’s a problem with NETMF or the way I’m using NETMF. Please help!


#5

In the getter of X you call the getter of XDirection…
In the getter of XDirection, you call the getter of X…
In the getter of X you call the getter of XDirection…
In the getter of XDirection, you call the getter of X…
In the getter of X you call the getter of XDirection…
In the getter of XDirection, you call the getter of X…
In the getter of X you call the getter of XDirection…

And so on :wink:


#6

Well…@ #$#! That was my initial thought and I re-wrote XDirection as XDirection(int x) and I was getting the same problem. After reading your reply I thought of course that just has to be it and tried rewriting it again. This time it worked. Apparently, I did something wrong first time around.

Thanks!!!