USBHost - Joystick

I hooked up a mouse to be sure… and finally with success!
This is your way for the joystick button, but it works for the mouse. I have tried this with the joystick too, but it did not work. Maybe I have done something wrong since I got really confused, but I am glad that this works.

else if ((args.ChangedButton == 1) && (args.ButtonState == USBH_MouseButtonState.Pressed))
            {
                Position[0] = (X * 4);
                sendSerial();
            }

thanks :slight_smile:

Ahem… Now I am confused, too :confused: What is working ?

You just said that the code was working for the mouse but not for the joystick ?! Also, did you use the thread method or your previous code ?

Anyway, I’m glad too that you’re glad this works :wink:

I will try more tomorrow and I will post the code tomorrow.
I hope that will make it more clear.

Just a quick thought : I see you use “else if” statements. Be careful with them as it can be tricky sometimes, depending on what you test and the order of the tests…

When debugging a code, I prefer using many single “if” statements one under another, even if it’s not optimized or doesn’t seem so. This way, I’m sure that all the tests are done at least once. With “else if” it may not always be the case.

I have tried, and it’s not working with the joypad/joystick

Also, joypads do not transfer X-Y completely, they only send +512 or -512 (not 0-512 for example)
I guess the mouse is fine then. Has a lot less buttons, but I will figure something out.

Could it be a “problem” with analog vs digital joystick ? :confused:

Might be… I have absolutely no idea what the problem is… I am totally confused… :stuck_out_tongue:

Robert,

Can you post your entire SRC? I have a USB joystick I could test. IT has been confirmed working with other projects.

I have already posted it. :wink:

I mean your entire source tree.

Now, with the new joystick it’s still not working. Button pressed is a single event, not a event that keeps being fired while having the button pressed. With the mouse it works.

source:
http://rapidshare.com/files/405908946/FEZ.rar.html

OK. I had an idea (checking the RobotArmMP3 code) and with that I have got to work what I wanted (FINALLY :confused: )

Thanks guys! :wink:

Glad it worked fro you finally

Sent you a email, Gus, there are some unclear parts in the RobotArmMP3 code, so I have sent you some questions. :wink:

thanks

The code is 3 years old…god know who made it and how it works. Sorry, you will have to try to understand it on your own.

Why do you send an email??

Maybe somebody other can explain it :stuck_out_tongue:

Yea, sorry, tried to get a quick and detailed answer.

Anyway, here is the code: (had to remove some junk to make it fit)

class Program
    {
        static Servo[] servos = new Servo[18]
        {
            new Servo(X_SERVOA),
            new Servo(Y_SERVOA),
            new Servo(Z_SERVOA),

            new Servo(X_SERVOB),
            new Servo(Y_SERVOB),
            new Servo(Z_SERVOB),

            new Servo(X_SERVOC),
            new Servo(Y_SERVOC),
            new Servo(Z_SERVOC),

            new Servo(X_SERVOD),
            new Servo(Y_SERVOD),
            new Servo(Z_SERVOD),

            new Servo(X_SERVOE),
            new Servo(Y_SERVOE),
            new Servo(Z_SERVOE),

            new Servo(X_SERVOF),
            new Servo(Y_SERVOF),
            new Servo(Z_SERVOF),
        };

        static void ButtonDown(USBH_Joystick sender, USBH_JoystickEventArgs args)
        {
            switch (args.ChangedButton)
            {
                case 2: 
                    Debug.Print("down");
                    servos[Y_SERVOA].Increase();
                    servos[Y_SERVOB].Increase();
                    servos[Y_SERVOC].Increase();
                    servos[Y_SERVOD].Increase();
==== Error lines
                    servos[Y_SERVOE].Increase();
                    servos[Y_SERVOF].Increase();
==== 
                    break;

            }
        }

And the error:

[quote]#### Exception System.IndexOutOfRangeException - 0xa9000000 (4) ####
#### FEZ_USB_SERIAL_new_cable.Program::ButtonDown [IP: 0061] ####
#### GHIElectronics.NETMF.USBHost.USBH_JoystickEventHandler::Invoke [IP: 5e9dc] ####
#### GHIElectronics.NETMF.USBHost.USBH_Joystick::DeviceThreadMethod [IP: 00e8] ####
A first chance exception of type ‘System.IndexOutOfRangeException’ occurred in FEZ_USB_SERIAL_new_cable.exe
An unhandled exception of type ‘System.IndexOutOfRangeException’ occurred in FEZ_USB_SERIAL_new_cable.exe[/quote]

As said, I believe there is something wrong with the array.

Thanks!

Robert,

You also need to post the servo class code.

class Servo
        {
            public int id;
            public int position = 1500;
            public int state = 0;      // 0: none
                                // 1: increasing
                                // -1: decreasing

            public Servo(int id)
            {
                this.id = id;
            }

            public void Increase() { state = 1; Debug.Print("state1: " + state); }
            public void Decrease() { state = -1; Debug.Print("state1: " + state); }

            public void Stop()  { state = 0;}


            public void Process()
            {
                switch (state)
                {
                    case 1: // increasing
                        if (position  1115)
                            position -= 10;
                        SetArmPosition(id, position);
                        break;

              case -1: // decreasing
                        if (position > 1115)
                            position -= 10;
                        SetArmPosition(id, position);
                        break;
                }
            }
        }