psp-nx sample

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

psp-nx sample

Postby Vurdilla » Mon Feb 20, 2012 4:37 pm

Hi everyone!
Could someone tell me how to use mindsensors psp-nx on lejos?
I don't know how to use classes on Java, the way (or syntax) looks too different from C++, i could not get it. So if someone has working example that i could modify and play with, it would be the best thing!

Thank you in advance!
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm

Re: psp-nx sample

Postby Rickz2020 » Mon Feb 20, 2012 5:22 pm

Hi,

I've never used this controller but looking at the API, it seems pretty easy.

It looks like you just need to initialize your controller:

PSPNXController controller = new PSPNXController(SensorPort.X); where X is the port you have connected it.

Then just just check when buttons are pressed by usung getButtons().
getButtons() returns an array of bytes and each bit represents a boolean which indicates if a button was pressed or not.

So, you could just have a loop to check if a button was pressed and which one...
You might want to have a look at the API if you haven't already done so. http://lejos.sourceforge.net/nxt/nxj/api/index.html

Hope this helps a bit, but as I've never used it, I'm afraid I can't promise what I have said is accurate... I'm sure someone will help you out further if they can :D

Rich.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: psp-nx sample

Postby Vurdilla » Mon Feb 20, 2012 6:34 pm

Hi,
thank you for rapid answer. I have tried something like you written:
Code: Select all
import java.lang.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.util.*;
import lejos.robotics.TachoMotor;
import java.io.*;

public class psptest
{
  public static void main (String[] args)
  {
    PSPNXController controller = new PSPNXController(SensorPort.S1);
    Sound.twoBeeps();

  }
}

I have copied import block from previous projects etc, i don't understand the exact meaning of it. As I understand it is something like #include in C/C++, but working in a very strange way (kind of DB required, as i don't write file names but names of some objects)... I am not sure about it.
Then it's pretty small code to test th program works in general:
1) controller initialization in a way you've recommended
2) two beeps to show it works not just stops somewhere
The result is compilation error (i am compiling in command line):
Code: Select all
psptest.java:12: cannot access PSPNXController
bad class file: .\PSPNXController.java
file does not contain class PSPController
Please remove or make sure it appears in the correct subdirectory of the classpath.
PSPNXController controller = new PSPNXController(SensorPort.S1);
^
1 error


I have copied file "PSPNXController.java" (from mindsensors website) to the same directory where psptest.java is, and nothing changed.

So it looks something else needed to include "PSPNXController.java" into the project, not just a file. Something like #include "PSPController.java" in C/C++.
How to do it?
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm

Re: psp-nx sample

Postby Rickz2020 » Mon Feb 20, 2012 8:35 pm

Hi,

Yes, the imports are similar to the c++ headers. It just basically allows you to use other libraries in your projects

The controller class is included in the API and so you don't need to copy anything. What you need to do is import the PSPNXController class.
Add this to the top of your class file
Code: Select all
import lejos.nxt.addon.PSPNXController;


Also, what version of leJOS are you using?

I'm not sure about earlier versions, but I'm using 0.9.0 and this version is fine to use with the controller.

Then after

Code: Select all
PSPNXController controller = new PSPNXController(SensorPort.S1);


add:
Code: Select all
//while you have not pressed the escape button...
while(!Button.Escape.isPressed()){
//beep
Sound.twoBeeps();
//wait 2 seconds
Delay.msDelay(2000);
}


The reason I have added this extra bit of code is because even if you got your code to compile, the program would start up and finish so fast it would look like the nxt just reboot itself as the program would finish and return you to the NXT menu.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: psp-nx sample

Postby Vurdilla » Tue Feb 21, 2012 6:07 am

Hi, Rickz2020!
Thank you for your response!

I've added import line you've mentioned and modified code with respect to PSP controller button:
Code: Select all
import java.lang.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
import lejos.util.*;
import lejos.robotics.TachoMotor;
import java.io.*;

import lejos.nxt.addon.PSPNXController;

public class psptest
{
  public static void main (String[] args)
  {
    PSPNXController controller = new PSPNXController(SensorPort.S1);

    //while you have not pressed the escape button...
    while(!Button.Escape.isPressed())
    {
        Sound.twoBeeps();
        Delay.msDelay(2000);
    }
  }
}

The old error vanishes (seems import works :), but the new one is:
Code: Select all
pstptest.java:17: cannot find symbol
symbol: variable Escape
location: class lejos.nxt.Button


Have PSP replaced brick buttons with PSP buttons?

I have leJOS 0.85 on brick, don't know what on PC (don't know how to check version here).
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm

Re: psp-nx sample

Postby Vurdilla » Tue Feb 21, 2012 7:46 am

One more question is how to get button state from PSP controller.
The following function was found in local copy of PSPNXController.java:
Code: Select all
public int[] getButtons(){...}

So I have thought it's ok to get state like this:
Code: Select all
int pspbuttons[] = new int[16];
pspbuttons=controller.getButtons();

However, compiler writes an error with:
Code: Select all
incompatible types
found: short
required: int[]


There is clearly some syntax problem i don't realize, as in cpp function shouldl be a little bit different to work with arrays, pointers to that are copied to function).

Sorry for dumb questions ;)
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm

Re: psp-nx sample

Postby Vurdilla » Tue Feb 21, 2012 9:44 am

One more short question.
The following code causes error:
Code: Select all
int lx=controller.getLeftX();
int ly=controller.getLeftY();
int rx=controller.getRightX();
int ry=controller.getRightY();


The error is:
Code: Select all
cannot find symbol
symbol: method getLeftY()
location: class lejos.nxt.addon.PSPNXController


What the ?..
What's wrong with getLeftY()???

I'd wanted to try to replace "possibly old" PSPNXController.java with the latest from mindsensors website, but have not found it anywhere.
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm

Re: psp-nx sample

Postby Rickz2020 » Tue Feb 21, 2012 4:01 pm

Oops. You need the button import too.

Code: Select all
import lejos.nxt.Button;


Are you not using an IDE? I recommend Eclipse if you aren't - it's great and there is a leJOS plugin available, too.
If you use the Eclipse IDE with the plugin, you can rest the mouse pointer over the error (it will have a red line under any errors), it will give you the option to add the import as well as give recommendations for other errors.

Here is the tutorial on installing and setting up the Eclipse plugin for leJOS:Eclipse + leJOS plugin

As for:
Code: Select all
int pspbuttons[] = new int[16];
pspbuttons=controller.getButtons();


I don't see what is wrong with that... No idea why it's telling you that it found a "short" and requires an "int".

Something like this should wait for you to press a button on the controller and i you do, it should exit the app:
Code: Select all
public static void main(String[] args) {
   PSPNXController controller = new PSPNXController(SensorPort.S1);
        int pspbuttons[] = new int[16];
        pspbuttons=controller.getButtons();
        while(true){
                for(int i = 0; i < pspbuttons.length; i++){
                 if(pspbuttons[i] == 1){
                         System.exit(0);
                         }
                }
         }
   }


But you don't need to declare another int array (pspbuttons). You could just say eg: controller.getButtons().length; and controller.getButtons()[i];

I've got no idea why you are getting errors for:

Code: Select all
int lx=controller.getLeftX();
int ly=controller.getLeftY();
int rx=controller.getRightX();
int ry=controller.getRightY();


They work fine for me.

Let me know how you get on.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: psp-nx sample

Postby Rickz2020 » Tue Feb 21, 2012 4:09 pm

Vurdilla wrote:
I have leJOS 0.85 on brick, don't know what on PC (don't know how to check version here).


I'd install version 0.9.0 or 0.9.1 if I were you - I'm not sure 0.85 has the PSPNXController class.
Download a new version here
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: psp-nx sample

Postby Vurdilla » Tue Feb 21, 2012 8:39 pm

Rickz2020 wrote:Oops. You need the button import too.

Code: Select all
import lejos.nxt.Button;

Oh, thank you! I've forgot too. Will try it tomorrow.

Rickz2020 wrote:Are you not using an IDE? I recommend Eclipse if you aren't - it's great and there is a leJOS plugin available, too.
If you use the Eclipse IDE with the plugin, you can rest the mouse pointer over the error (it will have a red line under any errors), it will give you the option to add the import as well as give recommendations for other errors.

I have tried Netbeans, but i don't find it useful. I have failed to proceed with setting it up to compile programs for nxt brick, and it also failed to detect all leJOS libraries, so it's as usefull as notepad currently, and it's assistant is just annoying dumb because of no link with leJOS. After trying Netbeans on few different PCs I don't believe it could installed smoothly and in standard way. Is Eclipse more accurate?

Rickz2020 wrote:
Code: Select all
int pspbuttons[] = new int[16];
pspbuttons=controller.getButtons();


I don't see what is wrong with that... No idea why it's telling you that it found a "short" and requires an "int".

Also have no idea. The good side of this way (against using
Code: Select all
controller.getButtons()[i]
) is that i don't need to get state N times to analyze N buttons. Getting state requires lots of time as far as i know (compared with simple math), and if I want to do smoothening or other analysis, i couldn't act in a way of "controller.getButtons()[i]".

Will write if i get the point.

Rickz2020 wrote:I've got no idea why you are getting errors for:

Code: Select all
int lx=controller.getLeftX();
int ly=controller.getLeftY();
int rx=controller.getRightX();
int ry=controller.getRightY();


They work fine for me.

Let me know how you get on.


I will. Possibly, the problem is old version of leJOS on PC. I have downloaded the newest one. Could I just run it and install new one on the top of previous? Or should I uninstall the previous leJOS before installing new one?
A little bit nervous about it, because currently JDK and leJOS runs on laptop on which i do my work (stupid, i know :). So i could not reinstall OS if JDK or leJOS behaves wrong.
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm

Re: psp-nx sample

Postby Rickz2020 » Tue Feb 21, 2012 9:20 pm

I have tried Netbeans, but i don't find it useful. I have failed to proceed with setting it up to compile programs for nxt brick, and it also failed to detect all leJOS libraries, so it's as usefull as notepad currently, and it's assistant is just annoying dumb because of no link with leJOS. After trying Netbeans on few different PCs I don't believe it could installed smoothly and in standard way. Is Eclipse more accurate?


Yeah, definitely use Eclipse. I first tried with NetBeans and it gave me nothing but trouble. After installing Eclipse coding leJOS has been a dream.

Could I just run it and install new one on the top of previous? Or should I uninstall the previous leJOS before installing new one?


As far as I know that is fine. I have never uninstalled leJOS prior to installing a new version and have never had problems.

A little bit nervous about it, because currently JDK and leJOS runs on laptop on which i do my work (stupid, i know . So i could not reinstall OS if JDK or leJOS behaves wrong.


Don't worry, you'll be fine :D Besides, even if something does go wrong, you wouldn't need to re-install the OS. If you have Java SDK 6 you should be fine but if you are feeling brave then install SDK 7 :D

Follow the tutorial on setting up eclipse. It's long, but it's worth it!
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: psp-nx sample

Postby Vurdilla » Mon Feb 27, 2012 12:53 pm

Sorry for the silence. I work these days on the desing of the car device and it's logics, seems will work on software a little bit later. Will write how it is going then :)
Vurdilla
New User
 
Posts: 22
Joined: Sun Apr 10, 2011 4:03 pm


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider], Yahoo [Bot] and 2 guests

more stuff