create a own class

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

Moderators: 99jonathan, roger, imaqine

create a own class

Postby airglide » Mon Apr 09, 2012 3:48 pm

hello everyone

I wrote my own class for navigating my robot. but i don't
know how i have to import the class or to save it. I'm using eclipse.. do i have to create a file with the ending .class? and where do i have to save it?

thank you for your help

airglide
airglide
New User
 
Posts: 19
Joined: Sun Mar 04, 2012 2:09 pm

Re: create a own class

Postby Xfel » Tue Apr 10, 2012 12:19 pm

eclipse manages that for you. Within the same project, you can use the class the same way as a class from the library.
Xfel
New User
 
Posts: 11
Joined: Mon Jan 30, 2012 3:29 pm

Re: create a own class

Postby airglide » Sat Apr 14, 2012 7:47 pm

thank you for your reply, then there will be a problem in the code I've written

airglide
airglide
New User
 
Posts: 19
Joined: Sun Mar 04, 2012 2:09 pm

Re: create a own class

Postby RoboDesigners » Sun Apr 15, 2012 11:40 pm

Could you post your custom class and how you call it?
(use code tags to keep indentation...)

To save a class/code library, you save it as a *.java file. The IDE should compile it for you.
To reference methods in the code library, you use: <ClassName>.<methodName>

For example, let's say you have a class Navigation, and in that class a method driveForward(). To call this function, use:
Code: Select all
Navigation.driveForward();


This is going off of my knowledge of Java, so someone more familiar with leJOS may need to correct me if there's a leJOS-specific change...

//Andrew
User avatar
RoboDesigners
New User
 
Posts: 2
Joined: Wed Apr 11, 2012 7:56 pm

Re: create a own class

Postby matthias » Mon Apr 16, 2012 2:02 pm

Thats only true, when the method driveForward is declared as static.

for non static Methods you first need to create a new object of your class with the "new" operator.
The same applies for variables that are inside the class.

Like:
Code: Select all
MyGreatClass mgc = new MyGreatClass()
matthias
New User
 
Posts: 20
Joined: Thu Mar 22, 2012 11:44 am

Re: create a own class

Postby RoboDesigners » Mon Apr 16, 2012 4:24 pm

matthias wrote:Thats only true, when the method driveForward is declared as static.

for non static Methods you first need to create a new object of your class with the "new" operator.
The same applies for variables that are inside the class.

Like:
Code: Select all
MyGreatClass mgc = new MyGreatClass()

This is true. So to reference a method/variable in MyGreatClass, you'd use:
Code: Select all
MyGreatClass mgc = new MyGreatClass();
mgc.methodName(); //Runs methodName()
mgc.variableName; //Accesses variableName
User avatar
RoboDesigners
New User
 
Posts: 2
Joined: Wed Apr 11, 2012 7:56 pm

Re: create a own class

Postby airglide » Tue Apr 17, 2012 2:17 pm

Hello everyone and thank you for replying

Code: Select all
package com.mydomain;

public class TestDrive {
   static Drive drv;
   
   public static void main(String[] args) {
   drv = new Drive(20.734, 29.0);
   drv.turn(90);
   }
}


That's the example code which uses the class Drive

Code: Select all
package com.mydomain;

import lejos.nxt.Motor;
import lejos.util.Delay;

public class Drive {
   
   private double wheel;
   private double axle;
   
   public Drive(double wheelCircum, double Dist_wheel) {
      wheel = wheelCircum;
      axle = Dist_wheel;
   }
   
   void turn(int degree)
   {
      double to_go = axle*Math.PI*(degree/360);
      double numberOfRot = to_go/wheel;
      long time = (long) (numberOfRot*1000);
      time = Math.abs(time); // has to be positive
      
      Motor.A.setSpeed(360);
      Motor.B.setSpeed(360);
      
      if (degree > 0)
      {
      Motor.A.forward();
      Motor.B.backward();
      }else{
            Motor.A.backward();
            Motor.B.forward();
          }   
      
      Delay.msDelay(time);   
      
      Motor.A.stop();
      Motor.B.stop();
   }
   
   void go(double dist, int speed)
   {
      // dist [cm], speed [degree/sec]
      
      Motor.A.setSpeed(speed);
      Motor.B.setSpeed(speed);
      
      double numberOfRot = dist/wheel;
      double time = (numberOfRot*360*1000)/speed;
      
      Delay.msDelay((long) time);
                Motor.A.forward();
                Motor.B.forward();
   }
}


and that's the Drive class. The constructor takes to double values: the wheelcircumstance and the distance between the wheels (I've a robot which has two wheels so it can turn without moving from place).
I'm using eclipse and they're in the same package. I managed to use the go method but the turn method doesn't work...
can someone help me?

thank you very much

airglide
airglide
New User
 
Posts: 19
Joined: Sun Mar 04, 2012 2:09 pm

Re: create a own class

Postby airglide » Wed Apr 18, 2012 12:35 pm

I've found my fault :wink:

I inizialized the variable to_go with
Code: Select all
double to_go = axle*Math.PI*(degree/360);

and I thought degree/360 is anyway a fraction, the problem was that 360 is an integer and he wanted a double value..

thank you for your help ;)

airglide
airglide
New User
 
Posts: 19
Joined: Sun Mar 04, 2012 2:09 pm

Re: create a own class

Postby roger » Fri Apr 20, 2012 6:56 am

Hi airglide,
There are simpler ways to code your class. Take a look at the rotate(angle) and rotateTo(angle) methods in the NXTRegulatedMotor class.
If you really want to use forward() and backward(), consider stopping the motors based on the tacho count , which is more accurate than time * speed since the motor is not constant while the motor is accelerating.
Also, you might want to take a look at the DifferentialPilot class.

Roger
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 0 guests

more stuff